changes: themes support, scale for direction text

This commit is contained in:
serso
2011-10-15 21:11:11 +04:00
parent d7b423e953
commit 5ee51fc3f0
28 changed files with 276 additions and 103 deletions

View File

@@ -31,6 +31,10 @@ import org.solovyev.common.utils.history.HistoryAction;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
@@ -54,11 +58,26 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private volatile boolean initialized;
@NotNull
private String themeName;
// key: style name, value: id of style in R.class
private Map<String, Integer> styles = null;
// ids of drag buttons in R.class
private List<Integer> dragButtonIds = null;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(this.getClass().getName(), "org.solovyev.android.calculator.CalculatorActivity.onCreate()");
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(preferences);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@@ -68,28 +87,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
dpclRegister.clear();
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(this);
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this);
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences);
dpclRegister.addListener(onDragListener);
// todo serso: check if there is more convenient method for doing this
final R.id ids = new R.id();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
final View view = findViewById(field.getInt(ids));
if (view instanceof DragButton) {
((DragButton) view).setOnDragListener(onDragListener);
}
} catch (IllegalArgumentException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
setOnDragListeners(dragPreferences);
final SimpleOnDragListener historyOnDragListener = new SimpleOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(this.calculatorView), dragPreferences);
((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener);
@@ -100,10 +100,68 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
dpclRegister.addListener(toPositionOnDragListener);
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this);
preferences.registerOnSharedPreferenceChangeListener(this);
this.onSharedPreferenceChanged(defaultSharedPreferences, null);
this.onSharedPreferenceChanged(preferences, null);
}
private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences) {
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences);
dpclRegister.addListener(onDragListener);
if (dragButtonIds == null) {
dragButtonIds = new ArrayList<Integer>();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
int dragButtonId = field.getInt(R.id.class);
final View view = findViewById(dragButtonId);
if (view instanceof DragButton) {
dragButtonIds.add(dragButtonId);
}
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
}
for (Integer dragButtonId : dragButtonIds) {
((DragButton) findViewById(dragButtonId)).setOnDragListener(onDragListener);
}
}
private synchronized void setTheme(@NotNull SharedPreferences preferences) {
if (styles == null) {
styles = new HashMap<String, Integer>();
for (Field themeField : R.style.class.getDeclaredFields()) {
int modifiers = themeField.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
Log.d(this.getClass().getName(), "Style found: " + themeField.getName());
int styleId = themeField.getInt(R.style.class);
Log.d(this.getClass().getName(), "Style id: " + styleId);
styles.put(themeField.getName(), styleId);
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
}
themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
Integer styleId = styles.get(themeName);
if (styleId == null) {
Log.d(this.getClass().getName(), "No saved theme found => applying default theme: " + R.style.default_theme);
styleId = R.style.default_theme;
} else {
Log.d(this.getClass().getName(), "Saved them found: " + styleId);
}
setTheme(styleId);
}
private void init() {
@@ -130,6 +188,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override
public void doOperation(@NotNull EditText editor) {
editor.setText(s);
calculatorView.setCursorOnEnd();
}
});
}
@@ -146,13 +205,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
try {
CalculatorModel.instance.init(this);
} catch (EvalError evalError) {
// todo serso: create serso runtime exception
throw new RuntimeException("Could not initialize interpreter!");
}
initialized = true;
}
}
@Override
protected void onDestroy() {
unregisterReceiver(textReceiver);
@@ -332,15 +391,39 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
}
public void restart() {
final Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Log.d(this.getClass().getName(), "Finishing current activity!");
finish();
overridePendingTransition(0, 0);
Log.d(this.getClass().getName(), "Starting new activity!");
startActivity(intent);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(CalculatorActivity.this));
protected void onResume() {
super.onResume();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final String newThemeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
if (!newThemeName.equals(themeName)) {
restart();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
CalculatorModel.instance.reset(this);
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
assert colorExpressionsInBracketsDefault != null;
this.calculatorView.getEditor().setHighlightText(sharedPreferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault));
this.calculatorView.getEditor().setHighlightText(preferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault));
this.calculatorView.evaluate();
}

View File

@@ -13,6 +13,7 @@ import android.text.TextPaint;
import android.util.AttributeSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.common.utils.Point2d;
import org.solovyev.common.utils.StringUtils;
@@ -23,6 +24,9 @@ import org.solovyev.common.utils.StringUtils;
*/
public class DirectionDragButton extends DragButton {
@NotNull
private final static Float DEFAULT_DIRECTION_TEXT_SCALE = 0.33f;
@Nullable
private String textUp;
@@ -41,6 +45,9 @@ public class DirectionDragButton extends DragButton {
@NotNull
private TextPaint upDownTextPaint;
@Nullable
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
public DirectionDragButton(Context context, @NotNull AttributeSet attrs) {
super(context, attrs, false);
init(context, attrs);
@@ -76,12 +83,15 @@ public class DirectionDragButton extends DragButton {
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case org.solovyev.android.calculator.R.styleable.DragButton_textUp:
case R.styleable.DragButton_textUp:
this.textUp = a.getString(attr);
break;
case org.solovyev.android.calculator.R.styleable.DragButton_textDown:
case R.styleable.DragButton_textDown:
this.textDown = a.getString(attr);
break;
case R.styleable.DragButton_directionTextScale:
this.directionTextScale = Float.valueOf(a.getString(attr));
break;
}
}
@@ -99,15 +109,15 @@ public class DirectionDragButton extends DragButton {
initUpDownTextPaint(basePaint);
if (textUp != null) {
textUpPosition = getTextPosition(upDownTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight());
textUpPosition = getTextPosition(upDownTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight(), getDirectionTextScale());
}
if (textDown != null) {
textDownPosition = getTextPosition(upDownTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight());
textDownPosition = getTextPosition(upDownTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight(), getDirectionTextScale());
}
if ( textDownPosition != null && textUpPosition != null ) {
if ( textDownPosition.getX() > textUpPosition.getX() ) {
if (textDownPosition != null && textUpPosition != null) {
if (textDownPosition.getX() > textUpPosition.getX()) {
textDownPosition.setX(textUpPosition.getX());
} else {
textUpPosition.setX(textDownPosition.getX());
@@ -116,7 +126,7 @@ public class DirectionDragButton extends DragButton {
}
public static Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h) {
public static Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h, float scale) {
final Point2d result = new Point2d();
float width = paint.measureText(text.toString() + " ");
@@ -157,15 +167,7 @@ public class DirectionDragButton extends DragButton {
upDownTextPaint = new TextPaint(paint);
upDownTextPaint.setAlpha(150);
upDownTextPaint.setTextSize(paint.getTextSize() / 3);
}
private String getStyledUpDownText(@Nullable String text) {
return StringUtils.getNotEmpty(text, "&nbsp;");
}
public void setTextUp(@Nullable String textUp) {
this.textUp = textUp;
upDownTextPaint.setTextSize(paint.getTextSize() * getDirectionTextScale());
}
@Nullable
@@ -173,19 +175,11 @@ public class DirectionDragButton extends DragButton {
return textUp;
}
public void setTextDown(@Nullable String textDown) {
this.textDown = textDown;
}
@Nullable
public String getTextDown() {
return textDown;
}
public void setTextMiddle(@Nullable String textMiddle) {
this.textMiddle = textMiddle;
}
@Nullable
public String getTextMiddle() {
return textMiddle;
@@ -197,7 +191,7 @@ public class DirectionDragButton extends DragButton {
if (direction == DragDirection.up) {
result = getTextUp();
} else if ( direction == DragDirection.down ) {
} else if (direction == DragDirection.down) {
result = getTextDown();
} else {
result = null;
@@ -205,4 +199,10 @@ public class DirectionDragButton extends DragButton {
return result;
}
@NotNull
public Float getDirectionTextScale() {
return directionTextScale == null ? DEFAULT_DIRECTION_TEXT_SCALE : directionTextScale;
}
}

View File

@@ -7,7 +7,6 @@ package org.solovyev.android.view.widgets;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import org.jetbrains.annotations.NotNull;
@@ -146,8 +145,7 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
}
@NotNull
public static Preferences getPreferences(@NotNull Context context) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
public static Preferences getPreferences(@NotNull final SharedPreferences preferences, @NotNull Context context) {
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);