Add support for high contrast in DragViews

This commit is contained in:
serso 2016-05-11 17:43:37 +02:00
parent 2609cede5b
commit 06dfc074b1
10 changed files with 105 additions and 7 deletions

View File

@ -26,10 +26,9 @@ import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import dagger.Lazy;
import jscl.math.Expression;
import jscl.math.Generic;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.buttons.CppSpecialButton; import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.ga.Ga; import org.solovyev.android.calculator.ga.Ga;
@ -42,6 +41,10 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import dagger.Lazy;
import jscl.math.Expression;
import jscl.math.Generic;
@Singleton @Singleton
public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListener { public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -69,17 +72,23 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
@Inject @Inject
ActivityLauncher launcher; ActivityLauncher launcher;
private boolean vibrateOnKeypress; private boolean vibrateOnKeypress;
private boolean highContrast;
@Inject @Inject
public Keyboard(@Nonnull SharedPreferences preferences) { public Keyboard(@Nonnull SharedPreferences preferences) {
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
vibrateOnKeypress = Preferences.Gui.vibrateOnKeypress.getPreference(preferences); vibrateOnKeypress = Preferences.Gui.vibrateOnKeypress.getPreference(preferences);
highContrast = Preferences.Gui.highContrast.getPreference(preferences);
} }
public boolean isVibrateOnKeypress() { public boolean isVibrateOnKeypress() {
return vibrateOnKeypress; return vibrateOnKeypress;
} }
public boolean isHighContrast() {
return highContrast;
}
public boolean buttonPressed(@Nullable String text) { public boolean buttonPressed(@Nullable String text) {
if (TextUtils.isEmpty(text)) { if (TextUtils.isEmpty(text)) {
return false; return false;
@ -292,6 +301,8 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (Preferences.Gui.vibrateOnKeypress.isSameKey(key)) { if (Preferences.Gui.vibrateOnKeypress.isSameKey(key)) {
vibrateOnKeypress = Preferences.Gui.vibrateOnKeypress.getPreference(preferences); vibrateOnKeypress = Preferences.Gui.vibrateOnKeypress.getPreference(preferences);
} else if (Preferences.Gui.highContrast.isSameKey(key)) {
highContrast = Preferences.Gui.highContrast.getPreference(preferences);
} }
} }
} }

View File

@ -47,6 +47,7 @@ import org.solovyev.android.prefs.*;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
@ -54,7 +55,7 @@ import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
public final class Preferences { public final class Preferences {
private static final Preference<Integer> version = IntegerPreference.of("version", 2); private static final Preference<Integer> version = IntegerPreference.of("version", 3);
private Preferences() { private Preferences() {
throw new AssertionError(); throw new AssertionError();
@ -86,6 +87,11 @@ public final class Preferences {
} }
version.putDefault(editor); version.putDefault(editor);
editor.apply(); editor.apply();
} else if (currentVersion == 2) {
final SharedPreferences.Editor editor = preferences.edit();
Gui.highContrast.tryPutDefault(preferences, editor);
version.putDefault(editor);
editor.apply();
} }
} }
@ -112,6 +118,7 @@ public final class Preferences {
Gui.rotateScreen.tryPutDefault(preferences, editor); Gui.rotateScreen.tryPutDefault(preferences, editor);
Gui.keepScreenOn.tryPutDefault(preferences, editor); Gui.keepScreenOn.tryPutDefault(preferences, editor);
Gui.language.tryPutDefault(preferences, editor); Gui.language.tryPutDefault(preferences, editor);
Gui.highContrast.tryPutDefault(preferences, editor);
Calculations.calculateOnFly.tryPutDefault(preferences, editor); Calculations.calculateOnFly.tryPutDefault(preferences, editor);
@ -250,6 +257,7 @@ public final class Preferences {
public static final Preference<Boolean> useBackAsPrevious = BooleanPreference.of("gui.useBackAsPrevious", false); public static final Preference<Boolean> useBackAsPrevious = BooleanPreference.of("gui.useBackAsPrevious", false);
public static final Preference<Boolean> rotateScreen = BooleanPreference.of("gui.rotateScreen", true); public static final Preference<Boolean> rotateScreen = BooleanPreference.of("gui.rotateScreen", true);
public static final Preference<Boolean> keepScreenOn = BooleanPreference.of("gui.keepScreenOn", true); public static final Preference<Boolean> keepScreenOn = BooleanPreference.of("gui.keepScreenOn", true);
public static final Preference<Boolean> highContrast = BooleanPreference.of("gui.highContrast", false);
public static final Preference<Boolean> vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true); public static final Preference<Boolean> vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true);
@Nonnull @Nonnull

View File

@ -143,6 +143,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
} }
dragButtons.add(button); dragButtons.add(button);
button.setVibrateOnDrag(keyboard.isVibrateOnKeypress()); button.setVibrateOnDrag(keyboard.isVibrateOnKeypress());
button.setHighContrast(keyboard.isHighContrast());
prepareButton((View) button); prepareButton((View) button);
button.setOnDragListener(listener); button.setOnDragListener(listener);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); button.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
@ -187,6 +188,11 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
for (DragView dragButton : dragButtons) { for (DragView dragButton : dragButtons) {
dragButton.setVibrateOnDrag(vibrate); dragButton.setVibrateOnDrag(vibrate);
} }
} else if (Preferences.Gui.highContrast.isSameKey(key)) {
final boolean highContrast = Preferences.Gui.highContrast.getPreference(preferences);
for (DragView dragButton : dragButtons) {
dragButton.setHighContrast(highContrast);
}
} }
} }

View File

@ -10,10 +10,16 @@ import android.util.AttributeSet;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static android.graphics.Color.BLACK;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import static android.util.TypedValue.applyDimension;
import static org.solovyev.android.views.dragbutton.DirectionTextView.SHADOW_RADIUS_DPS;
public class DirectionDragButton extends DragButton implements DirectionDragView { public class DirectionDragButton extends DragButton implements DirectionDragView {
private final DirectionTextView textView = new DirectionTextView(); private final DirectionTextView textView = new DirectionTextView();
@NonNull @NonNull
private final TextPaint baseTextPaint = new TextPaint(); private final TextPaint baseTextPaint = new TextPaint();
private boolean highContrast;
public DirectionDragButton(Context context) { public DirectionDragButton(Context context) {
super(context); super(context);
@ -86,4 +92,18 @@ public class DirectionDragButton extends DragButton implements DirectionDragView
getText(direction).setAlpha(alpha); getText(direction).setAlpha(alpha);
} }
} }
@Override
public void setHighContrast(boolean highContrast) {
if(this.highContrast == highContrast) {
return;
}
this.highContrast = highContrast;
this.textView.setHighContrast(highContrast);
if (highContrast) {
setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, getResources().getDisplayMetrics()), 0, 0, BLACK);
} else {
setShadowLayer(0, 0, 0, BLACK);
}
}
} }

View File

@ -69,4 +69,8 @@ public class DirectionDragImageButton extends DragImageButton implements Directi
public float getTextSize() { public float getTextSize() {
return baseTextPaint.getTextSize(); return baseTextPaint.getTextSize();
} }
@Override
public void setHighContrast(boolean highContrast) {
}
} }

View File

@ -2,7 +2,11 @@ package org.solovyev.android.views.dragbutton;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.*; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.TextPaint; import android.text.TextPaint;
@ -10,17 +14,24 @@ import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import static android.graphics.Color.BLACK;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import static android.util.TypedValue.applyDimension;
public class DirectionTextView { public class DirectionTextView {
public static final float DEF_ALPHA = 0.4f; public static final float DEF_ALPHA = 0.4f;
public static final float DEF_SCALE = 0.4f; public static final float DEF_SCALE = 0.4f;
public static final float SHADOW_RADIUS_DPS = 2;
@NonNull @NonNull
private final Map<DragDirection, Text> texts = new EnumMap<>(DragDirection.class); private final Map<DragDirection, Text> texts = new EnumMap<>(DragDirection.class);
@ -78,6 +89,12 @@ public class DirectionTextView {
return texts.get(direction); return texts.get(direction);
} }
public void setHighContrast(boolean highContrast) {
for (Text text : texts.values()) {
text.setHighContrast(highContrast);
}
}
public static class Text { public static class Text {
public final Rect bounds = new Rect(); public final Rect bounds = new Rect();
@NonNull @NonNull
@ -96,6 +113,7 @@ public class DirectionTextView {
private int color; private int color;
private float alpha; private float alpha;
private boolean visible = true; private boolean visible = true;
private boolean highContrast;
private int padding; private int padding;
public Text(@NonNull DragDirection direction, @NonNull View view, float minTextSize) { public Text(@NonNull DragDirection direction, @NonNull View view, float minTextSize) {
@ -138,9 +156,18 @@ public class DirectionTextView {
// set real text size value // set real text size value
paint.setTextSize(Math.max(base.getTextSize() * scale, minTextSize)); paint.setTextSize(Math.max(base.getTextSize() * scale, minTextSize));
initPaintShadow();
invalidate(true); invalidate(true);
} }
private void initPaintShadow() {
if (highContrast) {
paint.setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, view.getResources().getDisplayMetrics()), 0, 0, BLACK);
} else {
paint.setShadowLayer(0, 0, 0, BLACK);
}
}
private int intAlpha() { private int intAlpha() {
return (int) (255 * alpha); return (int) (255 * alpha);
} }
@ -161,6 +188,15 @@ public class DirectionTextView {
setColor(color, alpha); setColor(color, alpha);
} }
public void setHighContrast(boolean highContrast) {
if (this.highContrast == highContrast) {
return;
}
this.highContrast = highContrast;
initPaintShadow();
invalidate(false);
}
public void setColor(int color, float alpha) { public void setColor(int color, float alpha) {
if (this.color == color && this.alpha == alpha) { if (this.color == color && this.alpha == alpha) {
return; return;
@ -187,6 +223,10 @@ public class DirectionTextView {
if (offset.x == 0 && offset.y == 0) { if (offset.x == 0 && offset.y == 0) {
calculatePosition(); calculatePosition();
} }
if (highContrast) {
paint.setColor(Color.WHITE);
paint.setAlpha(255);
}
final int width = view.getWidth(); final int width = view.getWidth();
final int height = view.getHeight(); final int height = view.getHeight();
switch (direction) { switch (direction) {
@ -203,6 +243,10 @@ public class DirectionTextView {
canvas.drawText(value, width + offset.x, height / 2 + offset.y, paint); canvas.drawText(value, width + offset.x, height / 2 + offset.y, paint);
break; break;
} }
if (highContrast) {
paint.setColor(color);
paint.setAlpha(intAlpha());
}
} }
private void calculatePosition() { private void calculatePosition() {

View File

@ -9,7 +9,7 @@ import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.Button; import android.widget.Button;
public class DragButton extends Button implements DragView { public abstract class DragButton extends Button implements DragView {
@NonNull @NonNull
private final DragGestureDetector dragDetector = new DragGestureDetector(this); private final DragGestureDetector dragDetector = new DragGestureDetector(this);

View File

@ -9,7 +9,7 @@ import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.ImageButton; import android.widget.ImageButton;
public class DragImageButton extends ImageButton implements DragView { public abstract class DragImageButton extends ImageButton implements DragView {
@NonNull @NonNull
private final DragGestureDetector dragDetector = new DragGestureDetector(this); private final DragGestureDetector dragDetector = new DragGestureDetector(this);

View File

@ -6,4 +6,5 @@ public interface DragView {
int getId(); int getId();
void setOnDragListener(@Nullable DragListener listener); void setOnDragListener(@Nullable DragListener listener);
void setVibrateOnDrag(boolean vibrateOnDrag); void setVibrateOnDrag(boolean vibrateOnDrag);
void setHighContrast(boolean highContrast);
} }

View File

@ -38,6 +38,10 @@
a:key="gui.theme" a:key="gui.theme"
a:title="@string/cpp_theme" /> a:title="@string/cpp_theme" />
<android.preference.CheckBoxPreference
a:key="gui.highContrast"
a:title="High contrast" />
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:defaultValue="true" a:defaultValue="true"
a:key="gui.rotateScreen" a:key="gui.rotateScreen"