From 631ea40c15de426c1f82f611638bf91ac60ecdea Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 25 Sep 2012 22:25:08 +0400 Subject: [PATCH] Editor/display changes --- .../res/layout-land/main_calculator.xml | 153 ++- calculatorpp/res/layout/main_calculator.xml | 90 +- calculatorpp/res/menu/main_menu.xml | 22 - calculatorpp/res/values/dimensions.xml | 4 + calculatorpp/res/values/styles-common.xml | 86 +- .../calculator/CalculatorActivity.java | 771 +++++++------- .../calculator/CalculatorApplication.java | 217 ++-- .../calculator/CalculatorDisplayFragment.java | 68 +- .../calculator/CalculatorEditorFragment.java | 64 +- .../CalculatorKeyboardFragment.java | 986 +++++++++--------- .../android/calculator/CalculatorMenu.java | 26 +- .../calculator/CalculatorPreferences.java | 5 + .../android/fragments/FragmentUtils.java | 38 + 13 files changed, 1264 insertions(+), 1266 deletions(-) delete mode 100644 calculatorpp/res/menu/main_menu.xml create mode 100644 calculatorpp/res/values/dimensions.xml create mode 100644 calculatorpp/src/main/java/org/solovyev/android/fragments/FragmentUtils.java diff --git a/calculatorpp/res/layout-land/main_calculator.xml b/calculatorpp/res/layout-land/main_calculator.xml index c445eb00..cadee4c2 100644 --- a/calculatorpp/res/layout-land/main_calculator.xml +++ b/calculatorpp/res/layout-land/main_calculator.xml @@ -1,89 +1,64 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/calculatorpp/res/layout/main_calculator.xml b/calculatorpp/res/layout/main_calculator.xml index 59de37ce..a33f6497 100644 --- a/calculatorpp/res/layout/main_calculator.xml +++ b/calculatorpp/res/layout/main_calculator.xml @@ -1,44 +1,46 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/calculatorpp/res/menu/main_menu.xml b/calculatorpp/res/menu/main_menu.xml deleted file mode 100644 index d289db7b..00000000 --- a/calculatorpp/res/menu/main_menu.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/calculatorpp/res/values/dimensions.xml b/calculatorpp/res/values/dimensions.xml new file mode 100644 index 00000000..428505aa --- /dev/null +++ b/calculatorpp/res/values/dimensions.xml @@ -0,0 +1,4 @@ + + 0.5dp + @dimen/button_margin + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-common.xml b/calculatorpp/res/values/styles-common.xml index a0561d73..d0690e9d 100644 --- a/calculatorpp/res/values/styles-common.xml +++ b/calculatorpp/res/values/styles-common.xml @@ -1,43 +1,43 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 3be1d79c..685ed6a7 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -1,392 +1,381 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator; - -import android.app.*; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.pm.ActivityInfo; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.text.Html; -import android.text.method.LinkMovementMethod; -import android.util.Log; -import android.util.TypedValue; -import android.view.*; -import android.widget.TextView; -import net.robotmedia.billing.BillingController; -import net.robotmedia.billing.IBillingObserver; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.FontSizeAdjuster; -import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; -import org.solovyev.android.calculator.view.CalculatorAdditionalTitle; -import org.solovyev.android.menu.ActivityMenu; -import org.solovyev.android.menu.LayoutActivityMenu; -import org.solovyev.android.prefs.Preference; -import org.solovyev.android.view.ColorButton; -import org.solovyev.common.equals.EqualsTool; -import org.solovyev.common.history.HistoryAction; -import org.solovyev.common.text.StringUtils; - -public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { - - @NotNull - public static final String TAG = "Calculator++"; - - private static final int HVGA_WIDTH_PIXELS = 320; - - @Nullable - private IBillingObserver billingObserver; - - @NotNull - private CalculatorPreferences.Gui.Theme theme; - - @NotNull - private CalculatorPreferences.Gui.Layout layout; - - private boolean useBackAsPrev; - - @NotNull - private ActivityMenu menu = LayoutActivityMenu.newInstance(R.menu.main_menu, CalculatorMenu.class); - - /** - * Called when the activity is first created. - */ - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - - CalculatorApplication.registerOnRemoteStackTrace(); - - final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - - setTheme(preferences); - super.onCreate(savedInstanceState); - setLayout(preferences); - - CalculatorKeyboardFragment.fixThemeParameters(true, theme, this.getWindow().getDecorView()); - - createFragment(CalculatorEditorFragment.class, R.id.editorContainer, "tag"); - createFragment(CalculatorDisplayFragment.class, R.id.displayContainer, "display"); - createFragment(CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard"); - - if (customTitleSupported) { - try { - getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); - final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); - additionalAdditionalTitleText.init(preferences); - preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText); - } catch (ClassCastException e) { - // super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports) - Log.e(CalculatorActivity.class.getName(), e.getMessage(), e); - } - } - - billingObserver = new CalculatorBillingObserver(this); - BillingController.registerObserver(billingObserver); - - this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); - firstTimeInit(preferences, this); - - // init billing controller - BillingController.checkBillingSupported(this); - - toggleOrientationChange(preferences); - - CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout)); - - preferences.registerOnSharedPreferenceChangeListener(this); - } - - private void createFragment(@NotNull Class fragmentClass, int parentViewId, @NotNull String tag) { - final FragmentManager fm = getFragmentManager(); - - Fragment messagesFragment = fm.findFragmentByTag(tag); - - final FragmentTransaction ft = fm.beginTransaction(); - try { - if (messagesFragment == null) { - messagesFragment = Fragment.instantiate(this, fragmentClass.getName(), null); - ft.add(parentViewId, messagesFragment, tag); - } else { - if (messagesFragment.isDetached()) { - ft.attach(messagesFragment); - } - } - } finally { - ft.commit(); - } - } - - @NotNull - private AndroidCalculatorEngine getEngine() { - return ((AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine()); - } - - @NotNull - private AndroidCalculator getCalculator() { - return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); - } - - private synchronized void setLayout(@NotNull SharedPreferences preferences) { - layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); - - setContentView(layout.getLayoutId()); - } - - private synchronized void setTheme(@NotNull SharedPreferences preferences) { - theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); - setTheme(theme.getThemeId()); - } - - private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) { - final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences); - if (appOpenedCounter != null) { - CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1); - } - - final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences); - - final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName()); - - CalculatorPreferences.appVersion.putPreference(preferences, appVersion); - - boolean dialogShown = false; - if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { - // new start - final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); - builder.setPositiveButton(android.R.string.ok, null); - builder.setTitle(R.string.c_first_start_text_title); - builder.create().show(); - dialogShown = true; - } else { - if (savedVersion < appVersion) { - final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); - if (showReleaseNotes) { - final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1); - if (!StringUtils.isEmpty(releaseNotes)) { - final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes)); - builder.setPositiveButton(android.R.string.ok, null); - builder.setTitle(R.string.c_release_notes); - builder.create().show(); - dialogShown = true; - } - } - } - } - - - //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); - if (!dialogShown) { - if (appOpenedCounter != null && appOpenedCounter > 10) { - dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); - } - } - - if (!dialogShown) { - dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context); - } - } - - private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) { - boolean result = false; - - final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); - if ( specialWindowShown != null && !specialWindowShown ) { - final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); - final View view = layoutInflater.inflate(layoutId, null); - - final TextView feedbackTextView = (TextView) view.findViewById(textViewId); - feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); - - final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view); - builder.setPositiveButton(android.R.string.ok, null); - builder.create().show(); - - result = true; - specialWindowShownPref.putPreference(preferences, true); - } - - return result; - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) { - if (useBackAsPrev) { - getCalculator().doHistoryAction(HistoryAction.undo); - return true; - } - } - return super.onKeyDown(keyCode, event); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void equalsButtonClickHandler(@NotNull View v) { - getCalculator().evaluate(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - return this.menu.onCreateOptionsMenu(this, menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return menu.onOptionsItemSelected(this, item); - } - - /** - * The font sizes in the layout files are specified for a HVGA display. - * Adjust the font sizes accordingly if we are running on a different - * display. - */ - @Override - public void adjustFontSize(@NotNull TextView view) { - float fontPixelSize = view.getTextSize(); - Display display = getWindowManager().getDefaultDisplay(); - int h = Math.min(display.getWidth(), display.getHeight()); - float ratio = (float) h / HVGA_WIDTH_PIXELS; - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); - } - - @Override - protected void onResume() { - super.onResume(); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - - final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); - final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences); - if (!theme.equals(newTheme) || !layout.equals(newLayout)) { - AndroidUtils.restartActivity(this); - } - } - - @Override - protected void onDestroy() { - if (billingObserver != null) { - BillingController.unregisterObserver(billingObserver); - } - - super.onDestroy(); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) { - useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); - } - - if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) { - toggleOrientationChange(preferences); - } - } - - private void toggleOrientationChange(@Nullable SharedPreferences preferences) { - preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; - if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); - } else { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - } - - /* - ********************************************************************** - * - * BUTTON HANDLERS - * - ********************************************************************** - */ - - @SuppressWarnings({"UnusedDeclaration"}) - public void elementaryButtonClickHandler(@NotNull View v) { - throw new UnsupportedOperationException("Not implemented yet!"); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void historyButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showHistory(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void eraseButtonClickHandler(@NotNull View v) { - CalculatorLocatorImpl.getInstance().getEditor().erase(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void simplifyButtonClickHandler(@NotNull View v) { - throw new UnsupportedOperationException("Not implemented yet!"); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void moveLeftButtonClickHandler(@NotNull View v) { - getKeyboard().moveCursorLeft(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void moveRightButtonClickHandler(@NotNull View v) { - getKeyboard().moveCursorRight(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void pasteButtonClickHandler(@NotNull View v) { - getKeyboard().pasteButtonPressed(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void copyButtonClickHandler(@NotNull View v) { - getKeyboard().copyButtonPressed(); - } - - @NotNull - private static CalculatorKeyboard getKeyboard() { - return CalculatorLocatorImpl.getInstance().getKeyboard(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void clearButtonClickHandler(@NotNull View v) { - getKeyboard().clearButtonPressed(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void digitButtonClickHandler(@NotNull View v) { - Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); - if (((ColorButton) v).isShowText()) { - getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString()); - } - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void functionsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showFunctions(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void operatorsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showOperators(this); - } - - public static void operatorsButtonClickHandler(@NotNull Activity activity, @NotNull View view) { - CalculatorActivityLauncher.showOperators(activity); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void varsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showVars(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void donateButtonClickHandler(@NotNull View v) { - CalculatorApplication.showDonationDialog(this); - } - +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + */ + +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.pm.ActivityInfo; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.FragmentActivity; +import android.text.Html; +import android.text.method.LinkMovementMethod; +import android.util.Log; +import android.util.TypedValue; +import android.view.*; +import android.widget.TextView; +import net.robotmedia.billing.BillingController; +import net.robotmedia.billing.IBillingObserver; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.FontSizeAdjuster; +import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; +import org.solovyev.android.calculator.view.CalculatorAdditionalTitle; +import org.solovyev.android.fragments.FragmentUtils; +import org.solovyev.android.menu.ActivityMenu; +import org.solovyev.android.menu.AndroidMenuHelper; +import org.solovyev.android.menu.ListActivityMenu; +import org.solovyev.android.prefs.Preference; +import org.solovyev.android.view.ColorButton; +import org.solovyev.common.equals.EqualsTool; +import org.solovyev.common.history.HistoryAction; +import org.solovyev.common.text.StringUtils; + +public class CalculatorActivity extends FragmentActivity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { + + @NotNull + public static final String TAG = "Calculator++"; + + private static final int HVGA_WIDTH_PIXELS = 320; + + @Nullable + private IBillingObserver billingObserver; + + @NotNull + private CalculatorPreferences.Gui.Theme theme; + + @NotNull + private CalculatorPreferences.Gui.Layout layout; + + private boolean useBackAsPrev; + + @NotNull + private ActivityMenu menu = ListActivityMenu.fromList(CalculatorMenu.class, AndroidMenuHelper.getInstance()); + + /** + * Called when the activity is first created. + */ + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + + CalculatorApplication.registerOnRemoteStackTrace(); + + final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + + this.theme = CalculatorPreferences.Gui.getTheme(preferences); + setTheme(this.theme.getThemeId()); + + super.onCreate(savedInstanceState); + setLayout(preferences); + + CalculatorKeyboardFragment.fixThemeParameters(true, theme, this.getWindow().getDecorView()); + + FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor"); + FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display"); + FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard"); + + if (customTitleSupported) { + try { + getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); + final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); + additionalAdditionalTitleText.init(preferences); + preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText); + } catch (ClassCastException e) { + // super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports) + Log.e(CalculatorActivity.class.getName(), e.getMessage(), e); + } + } + + billingObserver = new CalculatorBillingObserver(this); + BillingController.registerObserver(billingObserver); + + this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); + firstTimeInit(preferences, this); + + // init billing controller + BillingController.checkBillingSupported(this); + + toggleOrientationChange(preferences); + + CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout)); + + preferences.registerOnSharedPreferenceChangeListener(this); + } + + @NotNull + private AndroidCalculator getCalculator() { + return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); + } + + private synchronized void setLayout(@NotNull SharedPreferences preferences) { + layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); + + setContentView(layout.getLayoutId()); + } + + private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) { + final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences); + if (appOpenedCounter != null) { + CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1); + } + + final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences); + + final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName()); + + CalculatorPreferences.appVersion.putPreference(preferences, appVersion); + + boolean dialogShown = false; + if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { + // new start + final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); + builder.setPositiveButton(android.R.string.ok, null); + builder.setTitle(R.string.c_first_start_text_title); + builder.create().show(); + dialogShown = true; + } else { + if (savedVersion < appVersion) { + final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); + if (showReleaseNotes) { + final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1); + if (!StringUtils.isEmpty(releaseNotes)) { + final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes)); + builder.setPositiveButton(android.R.string.ok, null); + builder.setTitle(R.string.c_release_notes); + builder.create().show(); + dialogShown = true; + } + } + } + } + + + //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); + if (!dialogShown) { + if (appOpenedCounter != null && appOpenedCounter > 10) { + dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); + } + } + + if (!dialogShown) { + dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context); + } + } + + private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) { + boolean result = false; + + final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); + if ( specialWindowShown != null && !specialWindowShown ) { + final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); + final View view = layoutInflater.inflate(layoutId, null); + + final TextView feedbackTextView = (TextView) view.findViewById(textViewId); + feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); + + final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view); + builder.setPositiveButton(android.R.string.ok, null); + builder.create().show(); + + result = true; + specialWindowShownPref.putPreference(preferences, true); + } + + return result; + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + if (useBackAsPrev) { + getCalculator().doHistoryAction(HistoryAction.undo); + return true; + } + } + return super.onKeyDown(keyCode, event); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void equalsButtonClickHandler(@NotNull View v) { + getCalculator().evaluate(); + } + + /* + ********************************************************************** + * + * MENU + * + ********************************************************************** + */ + + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + return this.menu.onPrepareOptionsMenu(this, menu); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + return this.menu.onCreateOptionsMenu(this, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return menu.onOptionsItemSelected(this, item); + } + + /** + * The font sizes in the layout files are specified for a HVGA display. + * Adjust the font sizes accordingly if we are running on a different + * display. + */ + @Override + public void adjustFontSize(@NotNull TextView view) { + float fontPixelSize = view.getTextSize(); + Display display = getWindowManager().getDefaultDisplay(); + int h = Math.min(display.getWidth(), display.getHeight()); + float ratio = (float) h / HVGA_WIDTH_PIXELS; + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); + } + + @Override + protected void onResume() { + super.onResume(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + + final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); + final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences); + if (!theme.equals(newTheme) || !layout.equals(newLayout)) { + AndroidUtils.restartActivity(this); + } + } + + @Override + protected void onDestroy() { + if (billingObserver != null) { + BillingController.unregisterObserver(billingObserver); + } + + super.onDestroy(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { + if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) { + useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); + } + + if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) { + toggleOrientationChange(preferences); + } + } + + private void toggleOrientationChange(@Nullable SharedPreferences preferences) { + preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; + if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + } else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } + } + + /* + ********************************************************************** + * + * BUTTON HANDLERS + * + ********************************************************************** + */ + + @SuppressWarnings({"UnusedDeclaration"}) + public void elementaryButtonClickHandler(@NotNull View v) { + throw new UnsupportedOperationException("Not implemented yet!"); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void historyButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showHistory(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void eraseButtonClickHandler(@NotNull View v) { + CalculatorLocatorImpl.getInstance().getEditor().erase(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void simplifyButtonClickHandler(@NotNull View v) { + throw new UnsupportedOperationException("Not implemented yet!"); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void moveLeftButtonClickHandler(@NotNull View v) { + getKeyboard().moveCursorLeft(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void moveRightButtonClickHandler(@NotNull View v) { + getKeyboard().moveCursorRight(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void pasteButtonClickHandler(@NotNull View v) { + getKeyboard().pasteButtonPressed(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void copyButtonClickHandler(@NotNull View v) { + getKeyboard().copyButtonPressed(); + } + + @NotNull + private static CalculatorKeyboard getKeyboard() { + return CalculatorLocatorImpl.getInstance().getKeyboard(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void clearButtonClickHandler(@NotNull View v) { + getKeyboard().clearButtonPressed(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void digitButtonClickHandler(@NotNull View v) { + Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); + if (((ColorButton) v).isShowText()) { + getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString()); + } + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void functionsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showFunctions(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void operatorsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showOperators(this); + } + + public static void operatorsButtonClickHandler(@NotNull Activity activity) { + CalculatorActivityLauncher.showOperators(activity); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void varsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showVars(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void donateButtonClickHandler(@NotNull View v) { + CalculatorApplication.showDonationDialog(this); + } + } \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 037394a3..7f252ea4 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -1,105 +1,112 @@ -package org.solovyev.android.calculator; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.SharedPreferences; -import android.net.Uri; -import android.preference.PreferenceManager; -import android.text.method.LinkMovementMethod; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.TextView; -import net.robotmedia.billing.BillingController; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.ads.AdsController; -import org.solovyev.android.calculator.history.AndroidCalculatorHistory; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; - -/** - * User: serso - * Date: 12/1/11 - * Time: 1:21 PM - */ -public class CalculatorApplication extends android.app.Application { - - private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; - - public static final String AD_FREE_PRODUCT_ID = "ad_free"; - public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; - - public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; - public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php"; - - @NotNull - private static CalculatorApplication instance; - - public CalculatorApplication() { - instance = this; - } - - @NotNull - public static CalculatorApplication getInstance() { - return instance; - } - - @Override - public void onCreate() { - super.onCreate(); - - final AndroidCalculator calculator = new AndroidCalculator(); - - CalculatorLocatorImpl.getInstance().init(calculator, - new AndroidCalculatorEngine(this), - new AndroidCalculatorClipboard(this), - new AndroidCalculatorNotifier(this), - new AndroidCalculatorHistory(this, calculator)); - - CalculatorLocatorImpl.getInstance().getCalculator().init(); - - AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { - - @Override - public byte[] getObfuscationSalt() { - return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82}; - } - - @Override - public String getPublicKey() { - return CalculatorSecurity.getPK(); - } - }); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - - CalculatorPreferences.setDefaultValues(preferences); - } - - public static void showDonationDialog(@NotNull final Context context) { - final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); - final View view = layoutInflater.inflate(R.layout.donate, null); - - final TextView donate = (TextView) view.findViewById(R.id.donateText); - donate.setMovementMethod(LinkMovementMethod.getInstance()); - - final AlertDialog.Builder builder = new AlertDialog.Builder(context) - .setCancelable(true) - .setNegativeButton(R.string.c_cancel, null) - .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - final Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(paypalDonateUrl)); - context.startActivity(i); - } - }) - .setView(view); - - builder.create().show(); - } - - public static void registerOnRemoteStackTrace() { - //Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); - } -} +package org.solovyev.android.calculator; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.net.Uri; +import android.preference.PreferenceManager; +import android.text.method.LinkMovementMethod; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; +import net.robotmedia.billing.BillingController; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.ads.AdsController; +import org.solovyev.android.calculator.history.AndroidCalculatorHistory; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; + +/** + * User: serso + * Date: 12/1/11 + * Time: 1:21 PM + */ +public class CalculatorApplication extends android.app.Application { + + private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; + + public static final String AD_FREE_PRODUCT_ID = "ad_free"; + public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; + + public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; + public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php"; + + @NotNull + private static CalculatorApplication instance; + + public CalculatorApplication() { + instance = this; + } + + @NotNull + public static CalculatorApplication getInstance() { + return instance; + } + + @Override + public void onCreate() { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + + setTheme(preferences); + + super.onCreate(); + + final AndroidCalculator calculator = new AndroidCalculator(); + + CalculatorLocatorImpl.getInstance().init(calculator, + new AndroidCalculatorEngine(this), + new AndroidCalculatorClipboard(this), + new AndroidCalculatorNotifier(this), + new AndroidCalculatorHistory(this, calculator)); + + CalculatorLocatorImpl.getInstance().getCalculator().init(); + + AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { + + @Override + public byte[] getObfuscationSalt() { + return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82}; + } + + @Override + public String getPublicKey() { + return CalculatorSecurity.getPK(); + } + }); + + CalculatorPreferences.setDefaultValues(preferences); + } + + private void setTheme(@NotNull SharedPreferences preferences) { + final CalculatorPreferences.Gui.Theme theme = CalculatorPreferences.Gui.getTheme(preferences); + setTheme(theme.getThemeId()); + } + + public static void showDonationDialog(@NotNull final Context context) { + final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); + final View view = layoutInflater.inflate(R.layout.donate, null); + + final TextView donate = (TextView) view.findViewById(R.id.donateText); + donate.setMovementMethod(LinkMovementMethod.getInstance()); + + final AlertDialog.Builder builder = new AlertDialog.Builder(context) + .setCancelable(true) + .setNegativeButton(R.string.c_cancel, null) + .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + final Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(paypalDonateUrl)); + context.startActivity(i); + } + }) + .setView(view); + + builder.create().show(); + } + + public static void registerOnRemoteStackTrace() { + //Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java index 5f4040d8..91cc76ba 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java @@ -1,34 +1,34 @@ -package org.solovyev.android.calculator; - -import android.app.Fragment; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -/** - * User: Solovyev_S - * Date: 25.09.12 - * Time: 12:03 - */ -public class CalculatorDisplayFragment extends Fragment { - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.calc_display, null); - view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - return view; - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setDisplay(getActivity()); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - } -} +package org.solovyev.android.calculator; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 12:03 + */ +public class CalculatorDisplayFragment extends Fragment { + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.calc_display, null); + view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + return view; + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setDisplay(getActivity()); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java index 8aeff57f..68164601 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java @@ -1,32 +1,32 @@ -package org.solovyev.android.calculator; - -import android.app.Fragment; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -/** - * User: Solovyev_S - * Date: 25.09.12 - * Time: 10:49 - */ -public class CalculatorEditorFragment extends Fragment { - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.calc_editor, null); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity()); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - } -} +package org.solovyev.android.calculator; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 10:49 + */ +public class CalculatorEditorFragment extends Fragment { + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.calc_editor, null); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity()); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java index 02c96511..bfd671b8 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java @@ -1,493 +1,493 @@ -package org.solovyev.android.calculator; - -import android.app.Activity; -import android.app.Fragment; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.os.Bundle; -import android.os.Vibrator; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.*; -import android.widget.Button; -import android.widget.LinearLayout; -import android.widget.Toast; -import jscl.AngleUnit; -import jscl.NumeralBase; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.calculator.history.CalculatorHistoryState; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; -import org.solovyev.android.calculator.view.AngleUnitsButton; -import org.solovyev.android.calculator.view.NumeralBasesButton; -import org.solovyev.android.calculator.view.OnDragListenerVibrator; -import org.solovyev.android.history.HistoryDragProcessor; -import org.solovyev.android.view.ColorButton; -import org.solovyev.android.view.drag.*; -import org.solovyev.common.Announcer; -import org.solovyev.common.math.Point2d; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -/** - * User: Solovyev_S - * Date: 25.09.12 - * Time: 12:25 - */ -public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener { - - @Nullable - private Vibrator vibrator; - - @NotNull - private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); - - @NotNull - private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); - - @NotNull - private CalculatorPreferences.Gui.Theme theme; - - @NotNull - private CalculatorPreferences.Gui.Layout layout; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); - - preferences.registerOnSharedPreferenceChangeListener(this); - - layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); - theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); - - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.calc_keyboard, null); - view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - return view; - } - - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - dpclRegister.clear(); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); - final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this.getActivity()); - - setOnDragListeners(root, dragPreferences, preferences); - - final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor(getCalculator()), dragPreferences), vibrator, preferences); - final DragButton historyButton = getButton(root, R.id.historyButton); - if (historyButton != null) { - historyButton.setOnDragListener(historyOnDragListener); - } - - final DragButton subtractionButton = getButton(root, R.id.subtractionButton); - if (subtractionButton != null) { - subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { - if (dragDirection == DragDirection.down) { - CalculatorActivity.operatorsButtonClickHandler(getActivity(), dragButton); - return true; - } - return false; - } - }, dragPreferences), vibrator, preferences)); - } - - - final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); - - final DragButton rightButton = getButton(root, R.id.rightButton); - if (rightButton != null) { - rightButton.setOnDragListener(toPositionOnDragListener); - } - - final DragButton leftButton = getButton(root, R.id.leftButton); - if (leftButton != null) { - leftButton.setOnDragListener(toPositionOnDragListener); - } - - final DragButton equalsButton = getButton(root, R.id.equalsButton); - if (equalsButton != null) { - equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); - } - - final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton); - if (angleUnitsButton != null) { - angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences)); - } - - final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton); - if (clearButton != null) { - clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences)); - } - - final DragButton varsButton = getButton(root, R.id.varsButton); - if (varsButton != null) { - varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences)); - } - - final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton); - if (roundBracketsButton != null) { - roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); - } - - if (layout == CalculatorPreferences.Gui.Layout.simple) { - toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down); - toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down); - - toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left); - - toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left); - toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up); - } - - numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); - - fixThemeParameters(true, theme, this.getView()); - - toggleEqualsButton(preferences, this.getActivity(), theme, root); - - initMultiplicationButton(); - } - - @Nullable - private T getButton(@NotNull View root, int buttonId) { - return (T) root.findViewById(buttonId); - } - - @Override - public void onDestroy() { - super.onDestroy(); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); - - preferences.unregisterOnSharedPreferenceChangeListener(this); - } - - public static void fixThemeParameters(boolean fixMagicFlames, - @NotNull CalculatorPreferences.Gui.Theme theme, - @NotNull View root) { - if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { - - if (fixMagicFlames) { - // for metro themes we should turn off magic flames - AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor() { - @Override - public void process(@NotNull ColorButton colorButton) { - colorButton.setDrawMagicFlame(false); - } - }); - } - } - } - - private void initMultiplicationButton() { - final View multiplicationButton = getView().findViewById(R.id.multiplicationButton); - if ( multiplicationButton instanceof Button) { - ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); - } - } - -/* private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @NotNull Context context) { - // IMPORTANT: this is workaround for probably android bug - // currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually - - if (view != null) { - final DisplayMetrics dm = context.getResources().getDisplayMetrics(); - if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { - final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams(); - final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight); - newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom)); - view.setLayoutParams(newParams); - } - } - }*/ - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - } - - private synchronized void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { - final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences); - - final List dragButtonIds = new ArrayList(); - final List buttonIds = new ArrayList(); - - for (Field field : R.id.class.getDeclaredFields()) { - int modifiers = field.getModifiers(); - if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { - try { - int viewId = field.getInt(R.id.class); - final View view = root.findViewById(viewId); - if (view instanceof DragButton) { - dragButtonIds.add(viewId); - } - if (view instanceof Button) { - buttonIds.add(viewId); - } - } catch (IllegalAccessException e) { - Log.e(R.id.class.getName(), e.getMessage()); - } - } - } - - for (Integer dragButtonId : dragButtonIds) { - final DragButton button = getButton(root, dragButtonId); - if (button != null) { - button.setOnDragListener(onDragListener); - } - } - } - - - @NotNull - private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor, - @NotNull SimpleOnDragListener.Preferences dragPreferences) { - final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences); - dpclRegister.addListener(onDragListener); - return onDragListener; - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { - dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this.getActivity())); - } - - if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { - numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); - } - - if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { - toggleEqualsButton(preferences, this.getActivity(), theme, getView()); - } - - if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { - initMultiplicationButton(); - } - } - - private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { - final View v = getButton(root, id); - if (v instanceof DirectionDragButton ) { - final DirectionDragButton button = (DirectionDragButton)v; - for (DragDirection dragDirection : dragDirections) { - button.showDirectionText(showDirectionText, dragDirection); - } - } - } - - public static void toggleEqualsButton(@Nullable SharedPreferences preferences, - @NotNull Activity activity, - @NotNull CalculatorPreferences.Gui.Theme theme, - @NotNull View root) { - preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; - - if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { - final Display display = activity.getWindowManager().getDefaultDisplay(); - - final DragButton button = (DragButton)activity.findViewById(R.id.equalsButton); - if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { - button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f)); - if (display.getWidth() <= 480) { - // mobile phones - final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); - if (calculatorDisplayView != null) { - calculatorDisplayView.setBackgroundDrawable(null); - } - } - } else { - button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f)); - if (display.getWidth() <= 480) { - // mobile phones - final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); - if (calculatorDisplayView != null) { - calculatorDisplayView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.equals9)); - } - } - } - - fixThemeParameters(false, theme, root); - } - } - - - @Nullable - private static AndroidCalculatorDisplayView getCalculatorDisplayView() { - return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); - } - - @NotNull - private Calculator getCalculator() { - return CalculatorLocatorImpl.getInstance().getCalculator(); - } - - @NotNull - private static CalculatorKeyboard getKeyboard() { - return CalculatorLocatorImpl.getInstance().getKeyboard(); - } - - /* - ********************************************************************** - * - * STATIC CLASSES - * - ********************************************************************** - */ - - private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { - final boolean result; - - if (dragDirection == DragDirection.left) { - getKeyboard().roundBracketsButtonPressed(); - result = true; - } else { - result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } - - return result; - } - } - - private static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { - - @NotNull - private Context context; - - private VarsDragProcessor(Context context) { - this.context = context; - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragDirection == DragDirection.up) { - CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); - result = true; - } - - return result; - } - } - - private static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { - - @NotNull - private final DigitButtonDragProcessor processor; - - @NotNull - private final Context context; - - private AngleUnitsChanger(@NotNull Context context) { - this.context = context; - this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragButton instanceof AngleUnitsButton) { - if (dragDirection != DragDirection.left) { - final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); - if (directionText != null) { - try { - - final AngleUnit angleUnits = AngleUnit.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); - - Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); - } - } - } else if (dragDirection == DragDirection.left) { - result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } - } - - return result; - } - } - - private static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { - - @NotNull - private final Context context; - - private NumeralBasesChanger(@NotNull Context context) { - this.context = context; - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragButton instanceof NumeralBasesButton) { - final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); - if (directionText != null) { - try { - - final NumeralBase numeralBase = NumeralBase.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); - - Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); - } - } - } - - return result; - } - } -} - +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.os.Bundle; +import android.os.Vibrator; +import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import android.util.Log; +import android.view.*; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.Toast; +import jscl.AngleUnit; +import jscl.NumeralBase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; +import org.solovyev.android.calculator.view.AngleUnitsButton; +import org.solovyev.android.calculator.view.NumeralBasesButton; +import org.solovyev.android.calculator.view.OnDragListenerVibrator; +import org.solovyev.android.history.HistoryDragProcessor; +import org.solovyev.android.view.ColorButton; +import org.solovyev.android.view.drag.*; +import org.solovyev.common.Announcer; +import org.solovyev.common.math.Point2d; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 12:25 + */ +public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener { + + @Nullable + private Vibrator vibrator; + + @NotNull + private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); + + @NotNull + private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); + + @NotNull + private CalculatorPreferences.Gui.Theme theme; + + @NotNull + private CalculatorPreferences.Gui.Layout layout; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + + preferences.registerOnSharedPreferenceChangeListener(this); + + layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); + theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.calc_keyboard, null); + view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + return view; + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + dpclRegister.clear(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this.getActivity()); + + setOnDragListeners(root, dragPreferences, preferences); + + final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor(getCalculator()), dragPreferences), vibrator, preferences); + final DragButton historyButton = getButton(root, R.id.historyButton); + if (historyButton != null) { + historyButton.setOnDragListener(historyOnDragListener); + } + + final DragButton subtractionButton = getButton(root, R.id.subtractionButton); + if (subtractionButton != null) { + subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + if (dragDirection == DragDirection.down) { + CalculatorActivity.operatorsButtonClickHandler(getActivity()); + return true; + } + return false; + } + }, dragPreferences), vibrator, preferences)); + } + + + final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); + + final DragButton rightButton = getButton(root, R.id.rightButton); + if (rightButton != null) { + rightButton.setOnDragListener(toPositionOnDragListener); + } + + final DragButton leftButton = getButton(root, R.id.leftButton); + if (leftButton != null) { + leftButton.setOnDragListener(toPositionOnDragListener); + } + + final DragButton equalsButton = getButton(root, R.id.equalsButton); + if (equalsButton != null) { + equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); + } + + final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton); + if (angleUnitsButton != null) { + angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton); + if (clearButton != null) { + clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final DragButton varsButton = getButton(root, R.id.varsButton); + if (varsButton != null) { + varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton); + if (roundBracketsButton != null) { + roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); + } + + if (layout == CalculatorPreferences.Gui.Layout.simple) { + toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down); + toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down); + + toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left); + + toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left); + toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up); + } + + numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); + + fixThemeParameters(true, theme, this.getView()); + + toggleEqualsButton(preferences, this.getActivity(), theme, root); + + initMultiplicationButton(); + } + + @Nullable + private T getButton(@NotNull View root, int buttonId) { + return (T) root.findViewById(buttonId); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + + preferences.unregisterOnSharedPreferenceChangeListener(this); + } + + public static void fixThemeParameters(boolean fixMagicFlames, + @NotNull CalculatorPreferences.Gui.Theme theme, + @NotNull View root) { + if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { + + if (fixMagicFlames) { + // for metro themes we should turn off magic flames + AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor() { + @Override + public void process(@NotNull ColorButton colorButton) { + colorButton.setDrawMagicFlame(false); + } + }); + } + } + } + + private void initMultiplicationButton() { + final View multiplicationButton = getView().findViewById(R.id.multiplicationButton); + if ( multiplicationButton instanceof Button) { + ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); + } + } + +/* private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @NotNull Context context) { + // IMPORTANT: this is workaround for probably android bug + // currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually + + if (view != null) { + final DisplayMetrics dm = context.getResources().getDisplayMetrics(); + if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { + final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams(); + final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight); + newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom)); + view.setLayoutParams(newParams); + } + } + }*/ + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + } + + private synchronized void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { + final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences); + + final List dragButtonIds = new ArrayList(); + final List buttonIds = new ArrayList(); + + for (Field field : R.id.class.getDeclaredFields()) { + int modifiers = field.getModifiers(); + if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { + try { + int viewId = field.getInt(R.id.class); + final View view = root.findViewById(viewId); + if (view instanceof DragButton) { + dragButtonIds.add(viewId); + } + if (view instanceof Button) { + buttonIds.add(viewId); + } + } catch (IllegalAccessException e) { + Log.e(R.id.class.getName(), e.getMessage()); + } + } + } + + for (Integer dragButtonId : dragButtonIds) { + final DragButton button = getButton(root, dragButtonId); + if (button != null) { + button.setOnDragListener(onDragListener); + } + } + } + + + @NotNull + private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor, + @NotNull SimpleOnDragListener.Preferences dragPreferences) { + final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences); + dpclRegister.addListener(onDragListener); + return onDragListener; + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { + dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this.getActivity())); + } + + if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { + numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); + } + + if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { + toggleEqualsButton(preferences, this.getActivity(), theme, getView()); + } + + if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { + initMultiplicationButton(); + } + } + + private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { + final View v = getButton(root, id); + if (v instanceof DirectionDragButton ) { + final DirectionDragButton button = (DirectionDragButton)v; + for (DragDirection dragDirection : dragDirections) { + button.showDirectionText(showDirectionText, dragDirection); + } + } + } + + public static void toggleEqualsButton(@Nullable SharedPreferences preferences, + @NotNull Activity activity, + @NotNull CalculatorPreferences.Gui.Theme theme, + @NotNull View root) { + preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; + + if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { + final Display display = activity.getWindowManager().getDefaultDisplay(); + + final DragButton button = (DragButton)activity.findViewById(R.id.equalsButton); + if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { + button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f)); + if (display.getWidth() <= 480) { + // mobile phones + final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); + if (calculatorDisplayView != null) { + calculatorDisplayView.setBackgroundDrawable(null); + } + } + } else { + button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f)); + if (display.getWidth() <= 480) { + // mobile phones + final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); + if (calculatorDisplayView != null) { + calculatorDisplayView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.equals9)); + } + } + } + + fixThemeParameters(false, theme, root); + } + } + + + @Nullable + private static AndroidCalculatorDisplayView getCalculatorDisplayView() { + return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); + } + + @NotNull + private Calculator getCalculator() { + return CalculatorLocatorImpl.getInstance().getCalculator(); + } + + @NotNull + private static CalculatorKeyboard getKeyboard() { + return CalculatorLocatorImpl.getInstance().getKeyboard(); + } + + /* + ********************************************************************** + * + * STATIC CLASSES + * + ********************************************************************** + */ + + private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + final boolean result; + + if (dragDirection == DragDirection.left) { + getKeyboard().roundBracketsButtonPressed(); + result = true; + } else { + result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + + return result; + } + } + + private static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { + + @NotNull + private Context context; + + private VarsDragProcessor(Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragDirection == DragDirection.up) { + CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); + result = true; + } + + return result; + } + } + + private static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final DigitButtonDragProcessor processor; + + @NotNull + private final Context context; + + private AngleUnitsChanger(@NotNull Context context) { + this.context = context; + this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof AngleUnitsButton) { + if (dragDirection != DragDirection.left) { + final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final AngleUnit angleUnits = AngleUnit.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); + + Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); + } + } + } else if (dragDirection == DragDirection.left) { + result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + } + + return result; + } + } + + private static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final Context context; + + private NumeralBasesChanger(@NotNull Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof NumeralBasesButton) { + final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final NumeralBase numeralBase = NumeralBase.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); + + Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); + } + } + } + + return result; + } + } +} + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java index 39a96822..6302f96a 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java @@ -6,51 +6,51 @@ import android.util.Log; import android.view.MenuItem; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.view.NumeralBaseConverterDialog; -import org.solovyev.android.menu.IdentifiableMenuItem; +import org.solovyev.android.menu.LabeledMenuItem; /** * User: serso * Date: 4/23/12 * Time: 2:25 PM */ -enum CalculatorMenu implements IdentifiableMenuItem { +enum CalculatorMenu implements LabeledMenuItem { - settings(R.id.main_menu_item_settings){ + settings(R.string.c_settings) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { CalculatorActivityLauncher.showSettings(context); } }, - history(R.id.main_menu_item_history) { + history(R.string.c_history) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { CalculatorActivityLauncher.showHistory(context); } }, - about(R.id.main_menu_item_about) { + about(R.string.c_about) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { CalculatorActivityLauncher.showAbout(context); } }, - help(R.id.main_menu_item_help) { + help(R.string.c_help) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { CalculatorActivityLauncher.showHelp(context); } }, - conversion_tool( R.id.main_menu_conversion_tool) { + conversion_tool(R.string.c_conversion_tool) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { new NumeralBaseConverterDialog(null).show(context); } }, - exit(R.id.main_menu_item_exit) { + exit(R.string.c_exit) { @Override public void onClick(@NotNull MenuItem data, @NotNull Context context) { if (context instanceof Activity) { @@ -61,15 +61,15 @@ enum CalculatorMenu implements IdentifiableMenuItem { } }; - private final int menuItemId; + private final int captionResId; - private CalculatorMenu (int menuItemId) { - this.menuItemId = menuItemId; + private CalculatorMenu(int captionResId) { + this.captionResId = captionResId; } @NotNull @Override - public Integer getItemId() { - return menuItemId; + public String getCaption(@NotNull Context context) { + return context.getString(captionResId); } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java index ba48b4ed..0d2a418f 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java @@ -39,6 +39,11 @@ public final class CalculatorPreferences { public static final Preference autoOrientation = new BooleanPreference("autoOrientation", true); public static final Preference hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true); + @NotNull + public static Theme getTheme(@NotNull SharedPreferences preferences) { + return theme.getPreferenceNoError(preferences); + } + public static enum Theme { default_theme(ThemeType.other, R.style.default_theme), diff --git a/calculatorpp/src/main/java/org/solovyev/android/fragments/FragmentUtils.java b/calculatorpp/src/main/java/org/solovyev/android/fragments/FragmentUtils.java new file mode 100644 index 00000000..b7888a54 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/fragments/FragmentUtils.java @@ -0,0 +1,38 @@ +package org.solovyev.android.fragments; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 9/25/12 + * Time: 9:29 PM + */ +public class FragmentUtils { + + public static void createFragment(@NotNull FragmentActivity activity, + @NotNull Class fragmentClass, + int parentViewId, + @NotNull String tag) { + final FragmentManager fm = activity.getSupportFragmentManager(); + + Fragment messagesFragment = fm.findFragmentByTag(tag); + + final FragmentTransaction ft = fm.beginTransaction(); + try { + if (messagesFragment == null) { + messagesFragment = Fragment.instantiate(activity, fragmentClass.getName(), null); + ft.add(parentViewId, messagesFragment, tag); + } else { + if (messagesFragment.isDetached()) { + ft.attach(messagesFragment); + } + } + } finally { + ft.commit(); + } + } +}