changes: themes support, scale for direction text

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

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#0f2e47" android:endColor="#3a5366" android:angle="90"/>
<corners android:radius="0dp" />
<shape xmlns:a="http://schemas.android.com/apk/res/android">
<gradient a:startColor="#002761"
a:endColor="#005ce5"
a:angle="90"/>
<corners a:radius="0dp" />
</shape>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<shape xmlns:a="http://schemas.android.com/apk/res/android">
<gradient a:startColor="#520546"
a:endColor="#8a307c"
a:angle="90"/>
<corners a:radius="0dp"/>
</shape>

View File

@ -10,5 +10,6 @@
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/divisionButton" a:text="/"
calc:textUp="√"
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/eightDigitButton" a:text="8"
calc:textUp="ln"
calc:textDown=""
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:text="5"
calc:textUp="cosh"
calc:textDown="acosh"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:text="4"
calc:textUp="sinh"
calc:textDown="asinh"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,7 @@
a:text="×"
calc:textUp="^"
calc:textDown="^2"
style="@style/digit_button_style"
style="?digitButtonStyle"
calc:directionTextScale="0.5"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/nineDigitButton" a:text="9"
calc:textDown="e"
calc:textUp="π"
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="1"
calc:textUp="sin"
calc:textDown="asin"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -7,6 +7,9 @@
-->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/plusButton" a:text="+"
style="@style/digit_button_style"
a:id="@+id/plusButton"
calc:directionTextScale="0.5"
a:text="+"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/roundBracketsButton" a:text="()"
calc:textUp="("
calc:textDown=")"
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/sevenDigitButton" a:text="7"
calc:textUp="i"
calc:textDown=""
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:id="@+id/sixDigitButton" a:text="6"
calc:textUp="tanh"
calc:textDown="atanh"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/squareBracketsButton" a:text="[]"
calc:textUp="["
calc:textDown="]"
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -10,5 +10,7 @@
a:id="@+id/subtractionButton" a:text="-"
calc:textUp="{"
calc:textDown="}"
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="3"
calc:textUp="tan"
calc:textDown="atan"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="2"
calc:textUp="cos"
calc:textDown="acos"
style="@style/digit_button_style"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/zeroDigitButton" a:text="0"
calc:textUp="."
calc:textDown=","
style="@style/digit_button_style"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -27,7 +27,7 @@
calc:textDown="@string/c_down"
a:layout_width="80dp"
a:layout_height="80dp"
style="@style/digit_button_style"/>
style="?digitButtonStyle"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/calibrationButtonRight"
a:text=""
@ -35,7 +35,7 @@
calc:textDown="@string/c_down"
a:layout_width="80dp"
a:layout_height="80dp"
style="@style/digit_button_style"/>
style="?digitButtonStyle"/>
<ImageView a:id="@+id/calibrationArrowRight"
a:layout_gravity="right"

18
res/values/arrays.xml Normal file
View File

@ -0,0 +1,18 @@
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<resources>
<string-array name="p_theme_names">
<item>Default</item>
<item>Violet</item>
<item>Light blue</item>
</string-array>
<string-array name="p_theme_values">
<item>default_theme</item>
<item>violet_theme</item>
<item>light_blue_theme</item>
</string-array>
</resources>

View File

@ -3,6 +3,7 @@
<declare-styleable name="DragButton">
<attr name="textUp" format="string"/>
<attr name="textDown" format="string"/>
<attr name="directionTextScale" format="float"/>
</declare-styleable>
<declare-styleable name="NumberRangeSeekBar">
@ -10,4 +11,6 @@
<attr name="steps" format="integer"/>
</declare-styleable>
<attr name="digitButtonStyle" format="reference" />
</resources>

View File

@ -20,4 +20,8 @@
<string name="p_calc_color_display">true</string>
<string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string>
<string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
<string name="p_calc_theme">default_theme</string>
</resources>

View File

@ -75,4 +75,5 @@
<string name="c_history_is_empty">History is empty!</string>
<string name="c_app_history">History</string>
<string name="c_char_is_not_accepted">Character \'%s\' is not accepted in variable name!</string>
<string name="c_calc_theme">Theme</string>
</resources>

View File

@ -15,13 +15,23 @@
<item name="android:textSize">20dp</item>
<item name="android:background">@drawable/button</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginBottom">1dp</item>
</style>
<style name="digit_button_style" parent="button_style">
<style name="default_digit_button_style" parent="button_style">
<item name="android:background">@drawable/blue_button</item>
</style>
<style name="violet_digit_button_style" parent="button_style">
<item name="android:background">@drawable/violet_button</item>
</style>
<style name="light_blue_digit_button_style" parent="button_style">
<item name="android:background">@drawable/light_blue_button</item>
</style>
<style name="control_button_style" parent="button_style"/>
<style name="control_image_button_style" parent="control_button_style">
<item name="android:padding">6dp</item>
</style>
@ -56,5 +66,16 @@
<item name="android:layout_height">match_parent</item>
</style>
<style name="default_theme">
<item name="digitButtonStyle">@style/default_digit_button_style</item>
</style>
<style name="violet_theme">
<item name="digitButtonStyle">@style/violet_digit_button_style</item>
</style>
<style name="light_blue_theme">
<item name="digitButtonStyle">@style/light_blue_digit_button_style</item>
</style>
</resources>

View File

@ -15,6 +15,15 @@
a:title="@string/c_calc_color_display_title"
a:defaultValue="true"/>
<ListPreference a:key="@string/p_calc_theme_key"
a:title="@string/c_calc_theme"
a:entries="@array/p_theme_names"
a:entryValues="@array/p_theme_values"
a:persistent="true"
a:defaultValue="2"
a:shouldDisableView="false"/>
<!-- <org.solovyev.android.view.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_distance_key"
a:title="Distance of drag event"

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,11 +109,11 @@ 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) {
@ -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;
@ -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);