This commit is contained in:
serso
2015-02-08 17:04:16 +01:00
parent b7a05b66bc
commit 362189f6ad
57 changed files with 298 additions and 421 deletions

View File

@@ -66,6 +66,15 @@ public class ActivityUi extends BaseUi {
this.layoutId = layoutId;
}
public void onPreCreate(@Nonnull Activity activity) {
final SharedPreferences preferences = App.getPreferences();
theme = Preferences.Gui.getTheme(preferences);
activity.setTheme(theme.getThemeId(activity));
layout = Preferences.Gui.getLayout(preferences);
}
@Override
public void onCreate(@Nonnull Activity activity) {
super.onCreate(activity);
@@ -74,13 +83,6 @@ public class ActivityUi extends BaseUi {
Locator.getInstance().getCalculator().addCalculatorEventListener((CalculatorEventListener) activity);
}
final SharedPreferences preferences = App.getPreferences();
theme = Preferences.Gui.getTheme(preferences);
activity.setTheme(theme.getThemeId(activity));
this.layout = Preferences.Gui.getLayout(preferences);
activity.setContentView(layoutId);
final View root = activity.findViewById(R.id.main_layout);

View File

@@ -24,7 +24,6 @@ package org.solovyev.android.calculator;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
@@ -38,8 +37,6 @@ import org.solovyev.android.view.AutoResizeTextView;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* User: serso
@@ -57,7 +54,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
*/
@Nonnull
private final static TextProcessor<TextProcessorEditorResult, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
private final TextProcessor<TextProcessorEditorResult, String> textHighlighter;
/*
**********************************************************************
@@ -70,17 +67,12 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@Nonnull
private volatile CalculatorDisplayViewState state = CalculatorDisplayViewStateImpl.newDefaultInstance();
private volatile boolean viewStateChange = false;
@Nonnull
private final Object lock = new Object();
@Nonnull
private final Handler uiHandler = new Handler();
@Nonnull
private final ExecutorService bgExecutor = Executors.newSingleThreadExecutor();
private volatile boolean initialized = false;
/*
@@ -93,15 +85,17 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
public AndroidCalculatorDisplayView(Context context) {
super(context);
textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false);
}
public AndroidCalculatorDisplayView(Context context, AttributeSet attrs) {
super(context, attrs);
textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false);
}
public AndroidCalculatorDisplayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false);
}
/*
@@ -121,14 +115,12 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
public void run() {
synchronized (lock) {
try {
viewStateChange = true;
final CharSequence text = prepareText(state.getStringResult(), state.isValid());
AndroidCalculatorDisplayView.this.state = state;
if (state.isValid()) {
setTextColor(getResources().getColor(R.color.cpp_text));
setTextColor(getTextColor().normal);
setText(text);
adjustTextSize();
@@ -136,20 +128,21 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
} else {
// update text in order to get rid of HTML tags
setText(getText().toString());
setTextColor(getResources().getColor(R.color.cpp_text_error));
setTextColor(getTextColor().error);
// error messages are never shown -> just greyed out text (error message will be shown on click)
//setText(state.getErrorMessage());
//redraw();
}
} finally {
viewStateChange = false;
}
}
}
});
}
private Preferences.Gui.TextColor getTextColor() {
return App.getTheme().getTextColor(App.getApplication());
}
@Nonnull
@Override
public CalculatorDisplayViewState getState() {
@@ -159,13 +152,10 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
}
@Nullable
private static CharSequence prepareText(@Nullable String text, boolean valid) {
private CharSequence prepareText(@Nullable String text, boolean valid) {
CharSequence result;
if (valid && text != null) {
//Log.d(this.getClass().getName(), text);
try {
final TextProcessorEditorResult processedText = textHighlighter.process(text);
text = processedText.toString();

View File

@@ -225,4 +225,9 @@ public final class App {
public static SharedPreferences getPreferences() {
return preferences;
}
@Nonnull
public static Preferences.Gui.Theme getTheme() {
return Preferences.Gui.getTheme(getPreferences());
}
}

View File

@@ -31,6 +31,7 @@ public class BaseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ui.onPreCreate(this);
super.onCreate(savedInstanceState);
ui.onCreate(this);
}

View File

@@ -22,9 +22,13 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.StyleRes;
import android.util.SparseArray;
import android.view.ContextThemeWrapper;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.solovyev.android.calculator.math.MathType;
@@ -110,9 +114,11 @@ public final class Preferences {
metro_purple_theme(R.style.cpp_metro_purple_theme),
metro_green_theme(R.style.cpp_metro_green_theme),
material_theme(R.style.Cpp_Theme_Material),
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Settings_Light),
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Settings_Light),
;
private static final SparseArray<TextColor> textColors = new SparseArray<>();
private final int themeId;
private final int wizardThemeId;
private final int settingsThemeId;
@@ -120,6 +126,7 @@ public final class Preferences {
Theme(@StyleRes int themeId) {
this(themeId, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Settings);
}
Theme(@StyleRes int themeId, @StyleRes int wizardThemeId, @StyleRes int settingsThemeId) {
this.themeId = themeId;
this.wizardThemeId = wizardThemeId;
@@ -130,15 +137,41 @@ public final class Preferences {
return getThemeId(null);
}
public int getThemeId(@Nullable Activity activity) {
if (activity instanceof WizardActivity) {
public int getThemeId(@Nullable Context context) {
if (context instanceof WizardActivity) {
return wizardThemeId;
}
if (activity instanceof BasePreferencesActivity) {
if (context instanceof BasePreferencesActivity) {
return settingsThemeId;
}
return themeId;
}
@Nonnull
public TextColor getTextColor(@Nonnull Context context) {
final int themeId = getThemeId(context);
TextColor textColor = textColors.get(themeId);
if (textColor == null) {
final ContextThemeWrapper themeContext = new ContextThemeWrapper(context, themeId);
final TypedArray a = themeContext.obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary, android.R.attr.textColorPrimaryInverse});
final int normal = a.getColor(0, Color.BLACK);
final int error = a.getColor(1, Color.WHITE);
a.recycle();
textColor = new TextColor(normal, error);
textColors.append(themeId, textColor);
}
return textColor;
}
}
public static final class TextColor{
public final int normal;
public final int error;
TextColor(int normal, int error) {
this.normal = normal;
this.error = error;
}
}
public static enum Layout {
main_calculator(R.layout.main_calculator, R.string.p_layout_calculator, true),

View File

@@ -26,7 +26,7 @@ import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
@@ -69,7 +69,8 @@ public class CalculatorPlotFragment extends AbstractCalculatorPlotFragment {
graphView = new CalculatorGraph2dView(getActivity());
}
graphView.init(PlotViewDef.newInstance(Color.WHITE, Color.WHITE, Color.DKGRAY, getBgColor()));
final int color = App.getTheme().getTextColor(getActivity()).normal;
graphView.init(PlotViewDef.newInstance(color, color, Color.DKGRAY, getBgColor()));
final PlotBoundaries boundaries = plotData.getBoundaries();
graphView.setXRange(boundaries.getXMin(), boundaries.getXMax());

View File

@@ -1,18 +1,22 @@
package org.solovyev.android.calculator.view;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.Preferences.Gui.colorDisplay;
import static org.solovyev.android.calculator.view.TextHighlighter.WHITE;
import static org.solovyev.android.calculator.Preferences.Gui.theme;
/**
* User: serso
@@ -23,7 +27,8 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
private boolean highlightText = true;
private final TextProcessor<TextProcessorEditorResult, String> textHighlighter = new TextHighlighter(WHITE, true);
@Nullable
private TextProcessor<TextProcessorEditorResult, String> textHighlighter;
public EditorTextProcessor() {
}
@@ -42,7 +47,7 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
if (highlightText) {
try {
final TextProcessorEditorResult processesText = textHighlighter.process(text);
final TextProcessorEditorResult processesText = getTextHighlighter().process(text);
result = new TextProcessorEditorResult(Html.fromHtml(processesText.toString()), processesText.getOffset());
} catch (CalculatorParseException e) {
@@ -58,6 +63,14 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
return result;
}
@Nonnull
private TextProcessor<TextProcessorEditorResult, String> getTextHighlighter() {
if (textHighlighter == null) {
onSharedPreferenceChanged(App.getPreferences(), theme.getKey());
}
return textHighlighter;
}
public boolean isHighlightText() {
return highlightText;
}
@@ -68,8 +81,17 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (colorDisplay.getKey().equals(key)) {
this.setHighlightText(colorDisplay.getPreference(preferences));
if (colorDisplay.isSameKey(key)) {
setHighlightText(colorDisplay.getPreference(preferences));
} else if (theme.isSameKey(key)) {
final int color = getTextColor(preferences);
textHighlighter = new TextHighlighter(color, true);
}
}
private int getTextColor(@Nonnull SharedPreferences preferences) {
final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences);
final Application application = App.getApplication();
return theme.getTextColor(application).normal;
}
}

View File

@@ -48,21 +48,42 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
nbFontAttributes.put("color", "#008000");
}
private final int color;
private final int colorRed;
private final int colorGreen;
private final int colorBlue;
private final int red;
private final int green;
private final int blue;
private final boolean formatNumber;
private final boolean dark;
public TextHighlighter(int baseColor, boolean formatNumber) {
this.color = baseColor;
public TextHighlighter(int color, boolean formatNumber) {
this.formatNumber = formatNumber;
//this.colorRed = Color.red(baseColor);
this.colorRed = (baseColor >> 16) & 0xFF;
//this.colorGreen = Color.green(baseColor);
this.colorGreen = (color >> 8) & 0xFF;
//this.colorBlue = Color.blue(baseColor);
this.colorBlue = color & 0xFF;
//this.red = Color.red(baseColor);
red = red(color);
//this.green = Color.green(baseColor);
green = green(color);
//this.blue = Color.blue(baseColor);
blue = blue(color);
dark = isDark(red, green, blue);
}
private static int blue(int color) {
return color & 0xFF;
}
private static int green(int color) {
return (color >> 8) & 0xFF;
}
private static int red(int color) {
return (color >> 16) & 0xFF;
}
public static boolean isDark(int color) {
return isDark(red(color), green(color), color & 0xFF);
}
public static boolean isDark(int red, int green, int blue) {
final float y = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
return y < 128;
}
@Nonnull
@@ -201,13 +222,14 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
}
private String getColor(int totalNumberOfOpenings, int numberOfOpenings) {
double c = 0.8;
int offset = ((int) (255 * c)) * numberOfOpenings / (totalNumberOfOpenings + 1);
int offset = ((int) (255 * 0.8)) * numberOfOpenings / (totalNumberOfOpenings + 1);
if (!dark) {
offset = -offset;
}
// for tests:
// innt result = Color.rgb(BASE_COLOUR_RED_COMPONENT - offset, BASE_COLOUR_GREEN_COMPONENT - offset, BASE_COLOUR_BLUE_COMPONENT - offset);
int result = (0xFF << 24) | ((colorRed - offset) << 16) | ((colorGreen - offset) << 8) | (colorBlue - offset);
// int result = Color.rgb(BASE_COLOUR_RED_COMPONENT - offset, BASE_COLOUR_GREEN_COMPONENT - offset, BASE_COLOUR_BLUE_COMPONENT - offset);
int result = (0xFF << 24) | ((red + offset) << 16) | ((green + offset) << 8) | (blue + offset);
return "#" + Integer.toHexString(result).substring(2);
}