From abebb25e530a6e6ceb3ebdb721fe8c7b6a527bb6 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 28 Sep 2012 14:44:49 +0400 Subject: [PATCH] changes --- ...ane_background.xml => pane_background.xml} | 2 +- .../res/layout-land/main_calculator.xml | 5 +- .../res/layout-large-land/main_calculator.xml | 9 +- calculatorpp/res/layout/history_fragment.xml | 32 +- calculatorpp/res/layout/main_calculator.xml | 3 +- calculatorpp/res/layout/main_empty.xml | 5 +- .../res/layout/main_empty_multi_pane.xml | 14 + calculatorpp/res/layout/math_entities.xml | 2 +- .../res/layout/math_entities_fragment.xml | 17 + calculatorpp/res/layout/vars_fragment.xml | 61 +- calculatorpp/res/menu/var_menu.xml | 25 +- calculatorpp/res/values-large-land/dimens.xml | 4 +- calculatorpp/res/values-large/dimens.xml | 4 +- calculatorpp/res/values/attributes.xml | 4 +- calculatorpp/res/values/colors.xml | 34 +- calculatorpp/res/values/dimens.xml | 15 +- calculatorpp/res/values/styles-common-var.xml | 55 +- calculatorpp/res/values/styles-common.xml | 19 +- .../res/values/styles-default-theme.xml | 107 ++- .../res/values/styles-light-blue-theme.xml | 64 +- .../res/values/styles-metro-blue-theme.xml | 50 +- .../res/values/styles-metro-green-theme.xml | 50 +- .../res/values/styles-metro-purple-theme.xml | 50 +- .../res/values/styles-violet-theme.xml | 60 +- calculatorpp/res/values/styles.xml | 123 +-- .../AndroidCalculatorDisplayView.java | 1 + .../calculator/CalculatorActivity.java | 704 +++++++++--------- .../CalculatorActivityHelperImpl.java | 318 ++++---- .../calculator/CalculatorApplication.java | 244 +++--- .../calculator/CalculatorFragmentHelper.java | 53 +- .../CalculatorFragmentHelperImpl.java | 121 +-- .../CalculatorKeyboardFragment.java | 238 +++--- .../AbstractCalculatorHistoryFragment.java | 660 ++++++++-------- .../edit/AbstractMathEntityListFragment.java | 647 ++++++++-------- .../CalculatorFunctionsFragmentActivity.java | 137 ++-- .../math/edit/CalculatorVarsFragment.java | 630 ++++++++-------- .../edit/CalculatorVarsFragmentActivity.java | 171 +++-- 37 files changed, 2421 insertions(+), 2317 deletions(-) rename calculatorpp/res/drawable/{second_pane_background.xml => pane_background.xml} (90%) create mode 100644 calculatorpp/res/layout/main_empty_multi_pane.xml create mode 100644 calculatorpp/res/layout/math_entities_fragment.xml diff --git a/calculatorpp/res/drawable/second_pane_background.xml b/calculatorpp/res/drawable/pane_background.xml similarity index 90% rename from calculatorpp/res/drawable/second_pane_background.xml rename to calculatorpp/res/drawable/pane_background.xml index 1c8cafb5..832b331b 100644 --- a/calculatorpp/res/drawable/second_pane_background.xml +++ b/calculatorpp/res/drawable/pane_background.xml @@ -15,6 +15,6 @@ --> - + \ No newline at end of file diff --git a/calculatorpp/res/layout-land/main_calculator.xml b/calculatorpp/res/layout-land/main_calculator.xml index 75966ec4..14b24087 100644 --- a/calculatorpp/res/layout-land/main_calculator.xml +++ b/calculatorpp/res/layout-land/main_calculator.xml @@ -7,12 +7,9 @@ --> + style="?mainLayoutStyle"> diff --git a/calculatorpp/res/layout-large-land/main_calculator.xml b/calculatorpp/res/layout-large-land/main_calculator.xml index 4650dcff..9a148121 100644 --- a/calculatorpp/res/layout-large-land/main_calculator.xml +++ b/calculatorpp/res/layout-large-land/main_calculator.xml @@ -7,12 +7,9 @@ --> + style="?mainMultiPaneLayoutStyle"> + style="?paneStyle"/> @@ -55,6 +52,6 @@ a:layout_height="0dp" a:layout_width="match_parent" a:layout_weight="1" - style="?fragmentLayoutStyle"/> + style="?paneStyleTransparent"/> diff --git a/calculatorpp/res/layout/history_fragment.xml b/calculatorpp/res/layout/history_fragment.xml index b90e45e3..73cecfb6 100644 --- a/calculatorpp/res/layout/history_fragment.xml +++ b/calculatorpp/res/layout/history_fragment.xml @@ -1,17 +1,17 @@ - - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/main_calculator.xml b/calculatorpp/res/layout/main_calculator.xml index c976ad8e..eee78009 100644 --- a/calculatorpp/res/layout/main_calculator.xml +++ b/calculatorpp/res/layout/main_calculator.xml @@ -12,8 +12,7 @@ a:layout_height="match_parent" a:id="@+id/main_layout" a:orientation="vertical" - a:layout_gravity="center" - a:background="@color/default_background"> + style="?mainLayoutStyle"> + style="?mainLayoutStyle" + a:orientation="vertical"> \ No newline at end of file diff --git a/calculatorpp/res/layout/main_empty_multi_pane.xml b/calculatorpp/res/layout/main_empty_multi_pane.xml new file mode 100644 index 00000000..35bf6f40 --- /dev/null +++ b/calculatorpp/res/layout/main_empty_multi_pane.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/math_entities.xml b/calculatorpp/res/layout/math_entities.xml index 193d9b3c..73cecfb6 100644 --- a/calculatorpp/res/layout/math_entities.xml +++ b/calculatorpp/res/layout/math_entities.xml @@ -2,7 +2,7 @@ diff --git a/calculatorpp/res/layout/math_entities_fragment.xml b/calculatorpp/res/layout/math_entities_fragment.xml new file mode 100644 index 00000000..73cecfb6 --- /dev/null +++ b/calculatorpp/res/layout/math_entities_fragment.xml @@ -0,0 +1,17 @@ + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/vars_fragment.xml b/calculatorpp/res/layout/vars_fragment.xml index dd81b2ce..b3a35d48 100644 --- a/calculatorpp/res/layout/vars_fragment.xml +++ b/calculatorpp/res/layout/vars_fragment.xml @@ -1,32 +1,31 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/menu/var_menu.xml b/calculatorpp/res/menu/var_menu.xml index e7ed20bb..4a338007 100644 --- a/calculatorpp/res/menu/var_menu.xml +++ b/calculatorpp/res/menu/var_menu.xml @@ -1,13 +1,14 @@ - - - - - - - + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values-large-land/dimens.xml b/calculatorpp/res/values-large-land/dimens.xml index 345babb6..69ce6ea2 100644 --- a/calculatorpp/res/values-large-land/dimens.xml +++ b/calculatorpp/res/values-large-land/dimens.xml @@ -1,3 +1,3 @@ - - 20dp + + 20dp \ No newline at end of file diff --git a/calculatorpp/res/values-large/dimens.xml b/calculatorpp/res/values-large/dimens.xml index 4fb0425a..d3472662 100644 --- a/calculatorpp/res/values-large/dimens.xml +++ b/calculatorpp/res/values-large/dimens.xml @@ -1,3 +1,3 @@ - - 40dp + + 40dp \ No newline at end of file diff --git a/calculatorpp/res/values/attributes.xml b/calculatorpp/res/values/attributes.xml index 235e557d..eef6df9c 100644 --- a/calculatorpp/res/values/attributes.xml +++ b/calculatorpp/res/values/attributes.xml @@ -6,10 +6,12 @@ - + + + diff --git a/calculatorpp/res/values/colors.xml b/calculatorpp/res/values/colors.xml index 8499b8ac..5ffabbff 100644 --- a/calculatorpp/res/values/colors.xml +++ b/calculatorpp/res/values/colors.xml @@ -1,18 +1,18 @@ - - - - #00ffffff - #ffffffff - #ffffff99 - #ffffffff - #ffffffff - #ff393939 - #ffffff99 - #ff000000 - #ff1f1f1f - #ff1f1f1f + + + + #00ffffff + #ffffffff + #ffffff99 + #ffffffff + #ffffffff + #ff393939 + #ffffff99 + #ff000000 + #ff1f1f1f + #ff1f1f1f \ No newline at end of file diff --git a/calculatorpp/res/values/dimens.xml b/calculatorpp/res/values/dimens.xml index 744bbfd2..25446232 100644 --- a/calculatorpp/res/values/dimens.xml +++ b/calculatorpp/res/values/dimens.xml @@ -1,7 +1,10 @@ - - 0.5dp - 20sp - 30dp - 25sp - 25sp + + 0.5dp + 20sp + 30dp + 20dp + 25sp + 25sp + 5dp + 5dp \ No newline at end of file diff --git a/calculatorpp/res/values/styles-common-var.xml b/calculatorpp/res/values/styles-common-var.xml index f4046e80..c79e8964 100644 --- a/calculatorpp/res/values/styles-common-var.xml +++ b/calculatorpp/res/values/styles-common-var.xml @@ -1,30 +1,27 @@ - - - - - - - - - - - + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-common.xml b/calculatorpp/res/values/styles-common.xml index ffc99c8e..cf1def55 100644 --- a/calculatorpp/res/values/styles-common.xml +++ b/calculatorpp/res/values/styles-common.xml @@ -8,13 +8,20 @@ + @drawable/metro_button_blue + + + + - + - - - + + + + + + + + - + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-light-blue-theme.xml b/calculatorpp/res/values/styles-light-blue-theme.xml index 718ef36f..e449c1b1 100644 --- a/calculatorpp/res/values/styles-light-blue-theme.xml +++ b/calculatorpp/res/values/styles-light-blue-theme.xml @@ -1,33 +1,33 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-metro-blue-theme.xml b/calculatorpp/res/values/styles-metro-blue-theme.xml index 761fbe96..e0eaa444 100644 --- a/calculatorpp/res/values/styles-metro-blue-theme.xml +++ b/calculatorpp/res/values/styles-metro-blue-theme.xml @@ -1,26 +1,26 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-metro-green-theme.xml b/calculatorpp/res/values/styles-metro-green-theme.xml index a0e4f8f1..d0b03739 100644 --- a/calculatorpp/res/values/styles-metro-green-theme.xml +++ b/calculatorpp/res/values/styles-metro-green-theme.xml @@ -1,26 +1,26 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-metro-purple-theme.xml b/calculatorpp/res/values/styles-metro-purple-theme.xml index 73de7b5f..795f25a1 100644 --- a/calculatorpp/res/values/styles-metro-purple-theme.xml +++ b/calculatorpp/res/values/styles-metro-purple-theme.xml @@ -1,26 +1,26 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles-violet-theme.xml b/calculatorpp/res/values/styles-violet-theme.xml index ee521e07..e421baec 100644 --- a/calculatorpp/res/values/styles-violet-theme.xml +++ b/calculatorpp/res/values/styles-violet-theme.xml @@ -1,31 +1,31 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/values/styles.xml b/calculatorpp/res/values/styles.xml index bb62e4d1..604b5917 100644 --- a/calculatorpp/res/values/styles.xml +++ b/calculatorpp/res/values/styles.xml @@ -1,60 +1,65 @@ - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java index 68b8ec0b..9317c901 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java @@ -17,6 +17,7 @@ import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.view.TextHighlighter; import org.solovyev.android.view.AutoResizeTextView; +import org.solovyev.common.text.StringUtils; /** * User: serso 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 fe21b15e..8d8d14d9 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -1,354 +1,352 @@ -/* - * 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.text.Html; -import android.text.method.LinkMovementMethod; -import android.util.Log; -import android.view.KeyEvent; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.TextView; -import com.actionbarsherlock.app.SherlockFragmentActivity; -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.calculator.about.CalculatorReleaseNotesActivity; -import org.solovyev.android.calculator.history.CalculatorHistoryFragment; -import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; -import org.solovyev.android.fragments.FragmentUtils; -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 SherlockFragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener { - - @NotNull - public static final String TAG = CalculatorActivity.class.getSimpleName(); - - @Nullable - private IBillingObserver billingObserver; - - private boolean useBackAsPrev; - - @NotNull - private CalculatorActivityHelper activityHelper; - - /** - * 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); - - final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); - - activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG); - activityHelper.logDebug("onCreate"); - activityHelper.onCreate(this, savedInstanceState); - - super.onCreate(savedInstanceState); - activityHelper.logDebug("super.onCreate"); - - if (findViewById(R.id.main_second_pane) != null) { - activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_second_pane); - activityHelper.addTab(this, "saved_history", CalculatorSavedHistoryFragment.class, null, R.string.c_saved_history, R.id.main_second_pane); - activityHelper.addTab(this, "vars", CalculatorVarsFragment.class, null, R.string.c_vars, R.id.main_second_pane); - activityHelper.addTab(this, "functions", CalculatorFunctionsFragment.class, null, R.string.c_functions, R.id.main_second_pane); - activityHelper.addTab(this, "operators", CalculatorOperatorsFragment.class, null, R.string.c_operators, R.id.main_second_pane); - - activityHelper.restoreSavedTab(this); - } else { - getSupportActionBar().hide(); - } - - CalculatorButtons.processButtons(true, activityHelper.getTheme(), 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); - - CalculatorButtons.toggleEqualsButton(preferences, this, activityHelper.getTheme(), findViewById(R.id.main_layout)); - - preferences.registerOnSharedPreferenceChangeListener(this); - } - - @NotNull - private AndroidCalculator getCalculator() { - return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); - } - - 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 - protected void onResume() { - super.onResume(); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); - if ( newLayout.getLayoutId() != activityHelper.getLayoutId() ) { - AndroidUtils.restartActivity(this); - } - - this.activityHelper.onResume(this); - } - - @Override - protected void onDestroy() { - if (billingObserver != null) { - BillingController.unregisterObserver(billingObserver); - } - - activityHelper.onDestroy(this); - - 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); - } - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - activityHelper.onSaveInstanceState(this, outState); - } - - 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); - } - +/* + * 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.text.Html; +import android.text.method.LinkMovementMethod; +import android.util.Log; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; +import com.actionbarsherlock.app.SherlockFragmentActivity; +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.calculator.about.CalculatorReleaseNotesActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryFragment; +import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; +import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; +import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; +import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; +import org.solovyev.android.fragments.FragmentUtils; +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 SherlockFragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener { + + @NotNull + public static final String TAG = CalculatorActivity.class.getSimpleName(); + + @Nullable + private IBillingObserver billingObserver; + + private boolean useBackAsPrev; + + @NotNull + private CalculatorActivityHelper activityHelper; + + /** + * 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); + + final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); + + activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG); + activityHelper.logDebug("onCreate"); + activityHelper.onCreate(this, savedInstanceState); + + super.onCreate(savedInstanceState); + activityHelper.logDebug("super.onCreate"); + + if (findViewById(R.id.main_second_pane) != null) { + activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_second_pane); + activityHelper.addTab(this, "saved_history", CalculatorSavedHistoryFragment.class, null, R.string.c_saved_history, R.id.main_second_pane); + activityHelper.addTab(this, "vars", CalculatorVarsFragment.class, null, R.string.c_vars, R.id.main_second_pane); + activityHelper.addTab(this, "functions", CalculatorFunctionsFragment.class, null, R.string.c_functions, R.id.main_second_pane); + activityHelper.addTab(this, "operators", CalculatorOperatorsFragment.class, null, R.string.c_operators, R.id.main_second_pane); + + activityHelper.restoreSavedTab(this); + } else { + getSupportActionBar().hide(); + } + + 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); + + CalculatorButtons.toggleEqualsButton(preferences, this, activityHelper.getTheme(), findViewById(R.id.main_layout)); + + preferences.registerOnSharedPreferenceChangeListener(this); + } + + @NotNull + private AndroidCalculator getCalculator() { + return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); + } + + 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 + protected void onResume() { + super.onResume(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); + if ( newLayout.getLayoutId() != activityHelper.getLayoutId() ) { + AndroidUtils.restartActivity(this); + } + + this.activityHelper.onResume(this); + } + + @Override + protected void onDestroy() { + if (billingObserver != null) { + BillingController.unregisterObserver(billingObserver); + } + + activityHelper.onDestroy(this); + + 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); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + activityHelper.onSaveInstanceState(this, outState); + } + + 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/CalculatorActivityHelperImpl.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java index e1a48b79..ef78293d 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java @@ -1,158 +1,160 @@ -package org.solovyev.android.calculator; - -import android.app.Activity; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.support.v4.app.Fragment; -import com.actionbarsherlock.app.ActionBar; -import com.actionbarsherlock.app.SherlockFragmentActivity; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; - -import java.util.ArrayList; -import java.util.List; - -/** - * User: serso - * Date: 9/25/12 - * Time: 10:32 PM - */ -public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper implements CalculatorActivityHelper { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - private static final String SELECTED_NAV = "selected_nav"; - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - - private int layoutId; - - private boolean homeIcon = false; - - @NotNull - private final List fragmentTags = new ArrayList(); - - @NotNull - private CalculatorPreferences.Gui.Theme theme; - private int navPosition = 0; - - - public CalculatorActivityHelperImpl(int layoutId, @NotNull String logTag) { - super(logTag); - this.layoutId = layoutId; - } - - public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) { - this.layoutId = layoutId; - this.homeIcon = homeIcon; - } - - @Override - public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) { - super.onCreate(activity); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - - this.theme = CalculatorPreferences.Gui.getTheme(preferences); - activity.setTheme(this.theme.getThemeId()); - - activity.setContentView(layoutId); - } - - @Override - public void onCreate(@NotNull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) { - this.onCreate((Activity) activity, savedInstanceState); - - final ActionBar actionBar = activity.getSupportActionBar(); - actionBar.setDisplayUseLogoEnabled(false); - actionBar.setDisplayHomeAsUpEnabled(homeIcon); - actionBar.setDisplayShowHomeEnabled(true); - actionBar.setDisplayShowTitleEnabled(true); - - if (savedInstanceState != null) { - navPosition = savedInstanceState.getInt(SELECTED_NAV, 0); - } - } - - @Override - public void restoreSavedTab(@NotNull SherlockFragmentActivity activity) { - final ActionBar actionBar = activity.getSupportActionBar(); - if (navPosition >= 0 && navPosition < actionBar.getTabCount()) { - activity.getSupportActionBar().setSelectedNavigationItem(navPosition); - } - } - - @Override - public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) { - onSaveInstanceState((Activity) activity, outState); - outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex()); - } - - @Override - public void onSaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) { - } - - @Override - public void onResume(@NotNull Activity activity) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - - final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences); - if (!theme.equals(newTheme)) { - AndroidUtils.restartActivity(activity); - } - } - - @Override - public void onDestroy(@NotNull SherlockFragmentActivity activity) { - super.onDestroy(activity); - } - - @Override - public void addTab(@NotNull SherlockFragmentActivity activity, - @NotNull String tag, - @NotNull Class fragmentClass, - @Nullable Bundle fragmentArgs, - int captionResId, - int parentViewId) { - activity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); - - final ActionBar actionBar = activity.getSupportActionBar(); - final ActionBar.Tab tab = actionBar.newTab(); - tab.setTag(tag); - tab.setText(captionResId); - tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId)); - actionBar.addTab(tab); - - fragmentTags.add(tag); - } - - @Override - public int getLayoutId() { - return layoutId; - } - - @Override - @NotNull - public CalculatorPreferences.Gui.Theme getTheme() { - return theme; - } - - @Override - public void onResume(@NotNull SherlockFragmentActivity activity) { - onResume((Activity) activity); - } -} +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 9/25/12 + * Time: 10:32 PM + */ +public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper implements CalculatorActivityHelper { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + private static final String SELECTED_NAV = "selected_nav"; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + private int layoutId; + + private boolean homeIcon = false; + + @NotNull + private final List fragmentTags = new ArrayList(); + + @NotNull + private CalculatorPreferences.Gui.Theme theme; + private int navPosition = 0; + + + public CalculatorActivityHelperImpl(int layoutId, @NotNull String logTag) { + super(logTag); + this.layoutId = layoutId; + } + + public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) { + this.layoutId = layoutId; + this.homeIcon = homeIcon; + } + + @Override + public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) { + super.onCreate(activity); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); + + this.theme = CalculatorPreferences.Gui.getTheme(preferences); + activity.setTheme(this.theme.getThemeId()); + + activity.setContentView(layoutId); + + CalculatorButtons.processButtons(true, theme, activity.getWindow().getDecorView()); + } + + @Override + public void onCreate(@NotNull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) { + this.onCreate((Activity) activity, savedInstanceState); + + final ActionBar actionBar = activity.getSupportActionBar(); + actionBar.setDisplayUseLogoEnabled(false); + actionBar.setDisplayHomeAsUpEnabled(homeIcon); + actionBar.setDisplayShowHomeEnabled(true); + actionBar.setDisplayShowTitleEnabled(true); + + if (savedInstanceState != null) { + navPosition = savedInstanceState.getInt(SELECTED_NAV, 0); + } + } + + @Override + public void restoreSavedTab(@NotNull SherlockFragmentActivity activity) { + final ActionBar actionBar = activity.getSupportActionBar(); + if (navPosition >= 0 && navPosition < actionBar.getTabCount()) { + activity.getSupportActionBar().setSelectedNavigationItem(navPosition); + } + } + + @Override + public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) { + onSaveInstanceState((Activity) activity, outState); + outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex()); + } + + @Override + public void onSaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) { + } + + @Override + public void onResume(@NotNull Activity activity) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); + + final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences); + if (!theme.equals(newTheme)) { + AndroidUtils.restartActivity(activity); + } + } + + @Override + public void onDestroy(@NotNull SherlockFragmentActivity activity) { + super.onDestroy(activity); + } + + @Override + public void addTab(@NotNull SherlockFragmentActivity activity, + @NotNull String tag, + @NotNull Class fragmentClass, + @Nullable Bundle fragmentArgs, + int captionResId, + int parentViewId) { + activity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + + final ActionBar actionBar = activity.getSupportActionBar(); + final ActionBar.Tab tab = actionBar.newTab(); + tab.setTag(tag); + tab.setText(captionResId); + tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId)); + actionBar.addTab(tab); + + fragmentTags.add(tag); + } + + @Override + public int getLayoutId() { + return layoutId; + } + + @Override + @NotNull + public CalculatorPreferences.Gui.Theme getTheme() { + return theme; + } + + @Override + public void onResume(@NotNull SherlockFragmentActivity activity) { + onResume((Activity) activity); + } +} 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 b0673c7b..6d648482 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -1,122 +1,122 @@ -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)); - } - - @NotNull - public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) { - return new CalculatorActivityHelperImpl(layoutResId, logTag); - } - - @NotNull - public CalculatorFragmentHelper createFragmentHelper() { - return new CalculatorFragmentHelperImpl(); - } -} +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)); + } + + @NotNull + public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) { + return new CalculatorActivityHelperImpl(layoutResId, logTag); + } + + @NotNull + public CalculatorFragmentHelper createFragmentHelper(int layoutId) { + return new CalculatorFragmentHelperImpl(layoutId); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelper.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelper.java index 265f0ed5..a2cca19c 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelper.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelper.java @@ -1,23 +1,30 @@ -package org.solovyev.android.calculator; - -import android.support.v4.app.Fragment; -import android.view.View; -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 9/26/12 - * Time: 10:14 PM - */ -public interface CalculatorFragmentHelper { - - boolean isPane(@NotNull Fragment fragment); - - void setPaneTitle(@NotNull Fragment fragment, int titleResId); - - void processButtons(@NotNull Fragment fragment, @NotNull View root); - - void onCreate(@NotNull Fragment fragment); - - void onDestroy(@NotNull Fragment fragment); -} +package org.solovyev.android.calculator; + +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.math.edit.AbstractMathEntityListFragment; + +/** + * User: serso + * Date: 9/26/12 + * Time: 10:14 PM + */ +public interface CalculatorFragmentHelper { + + boolean isPane(@NotNull Fragment fragment); + + void setPaneTitle(@NotNull Fragment fragment, int titleResId); + + void onCreate(@NotNull Fragment fragment); + + void onViewCreated(@NotNull Fragment fragment, @NotNull View root); + + void onDestroy(@NotNull Fragment fragment); + + @NotNull + View onCreateView(@NotNull Fragment fragment, @NotNull LayoutInflater inflater, @Nullable ViewGroup container); +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelperImpl.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelperImpl.java index bbe5ab9d..e5fd23f9 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelperImpl.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentHelperImpl.java @@ -1,45 +1,76 @@ -package org.solovyev.android.calculator; - -import android.support.v4.app.Fragment; -import android.view.View; -import android.widget.TextView; -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 9/26/12 - * Time: 10:14 PM - */ -public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper implements CalculatorFragmentHelper { - - @Override - public boolean isPane(@NotNull Fragment fragment) { - return fragment.getActivity() instanceof CalculatorActivity; - } - - public void setPaneTitle(@NotNull Fragment fragment, int titleResId) { - final TextView fragmentTitle = (TextView) fragment.getView().findViewById(R.id.fragmentTitle); - if (fragmentTitle != null) { - if (!isPane(fragment)) { - fragmentTitle.setVisibility(View.GONE); - } else { - fragmentTitle.setText(fragment.getString(titleResId).toUpperCase()); - } - } - } - - @Override - public void processButtons(@NotNull Fragment fragment, @NotNull View root) { - super.processButtons(fragment.getActivity(), root); - } - - @Override - public void onCreate(@NotNull Fragment fragment) { - super.onCreate(fragment.getActivity()); - } - - @Override - public void onDestroy(@NotNull Fragment fragment) { - super.onDestroy(fragment.getActivity()); - } -} +package org.solovyev.android.calculator; + +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.widget.LinearLayout; +import android.widget.TextView; +import com.google.ads.AdView; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.ads.AdsController; + +/** + * User: serso + * Date: 9/26/12 + * Time: 10:14 PM + */ +public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper implements CalculatorFragmentHelper { + + @Nullable + private AdView adView; + + private int layoutId; + + public CalculatorFragmentHelperImpl(int layoutId) { + this.layoutId = layoutId; + } + + @Override + public boolean isPane(@NotNull Fragment fragment) { + return fragment.getActivity() instanceof CalculatorActivity; + } + + public void setPaneTitle(@NotNull Fragment fragment, int titleResId) { + final TextView fragmentTitle = (TextView) fragment.getView().findViewById(R.id.fragmentTitle); + if (fragmentTitle != null) { + if (!isPane(fragment)) { + fragmentTitle.setVisibility(View.GONE); + } else { + fragmentTitle.setText(fragment.getString(titleResId).toUpperCase()); + } + } + } + + @Override + public void onCreate(@NotNull Fragment fragment) { + super.onCreate(fragment.getActivity()); + } + + @Override + public void onViewCreated(@NotNull Fragment fragment, @NotNull View root) { + final ViewGroup mainFragmentLayout = (ViewGroup) root.findViewById(R.id.main_fragment_layout); + if (mainFragmentLayout != null) { + adView = AdsController.getInstance().inflateAd(fragment.getActivity(), mainFragmentLayout, R.id.main_fragment_layout); + } + + processButtons(fragment.getActivity(), root); + } + + @Override + public void onDestroy(@NotNull Fragment fragment) { + super.onDestroy(fragment.getActivity()); + + if (this.adView != null) { + this.adView.destroy(); + } + } + + @NotNull + @Override + public View onCreateView(@NotNull Fragment fragment, @NotNull LayoutInflater inflater, @Nullable ViewGroup container) { + return inflater.inflate(layoutId, container, false); + } +} 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 94a55199..57e4f2c6 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java @@ -1,119 +1,119 @@ -package org.solovyev.android.calculator; - -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import com.actionbarsherlock.app.SherlockFragment; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; - -/** - * User: Solovyev_S - * Date: 25.09.12 - * Time: 12:25 - */ -public class CalculatorKeyboardFragment extends SherlockFragment implements SharedPreferences.OnSharedPreferenceChangeListener { - - @NotNull - private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); - - @NotNull - private CalculatorPreferences.Gui.Theme theme; - - @NotNull - private CalculatorFragmentHelper fragmentHelper; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); - - fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(); - fragmentHelper.onCreate(this); - - preferences.registerOnSharedPreferenceChangeListener(this); - - theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); - - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.calc_keyboard, container, false); - } - - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - fragmentHelper.processButtons(this, root); - } - - @Override - public void onDestroy() { - super.onDestroy(); - - fragmentHelper.onDestroy(this); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); - preferences.unregisterOnSharedPreferenceChangeListener(this); - - } - - /* 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); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { - numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); - } - - if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { - CalculatorButtons.toggleEqualsButton(preferences, this.getActivity(), theme, getView()); - } - - if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { - CalculatorButtons.initMultiplicationButton(getView()); - } - } - - - @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(); - } -} - +package org.solovyev.android.calculator; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import com.actionbarsherlock.app.SherlockFragment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 12:25 + */ +public class CalculatorKeyboardFragment extends SherlockFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + + @NotNull + private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); + + @NotNull + private CalculatorPreferences.Gui.Theme theme; + + @NotNull + private CalculatorFragmentHelper fragmentHelper; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.calc_keyboard); + fragmentHelper.onCreate(this); + + preferences.registerOnSharedPreferenceChangeListener(this); + + theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + fragmentHelper.onViewCreated(this, root); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + fragmentHelper.onDestroy(this); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + preferences.unregisterOnSharedPreferenceChangeListener(this); + + } + + /* 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); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { + numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); + } + + if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { + CalculatorButtons.toggleEqualsButton(preferences, this.getActivity(), theme, getView()); + } + + if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { + CalculatorButtons.initMultiplicationButton(getView()); + } + } + + + @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(); + } +} + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java index f9e69459..7b5286d6 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java @@ -1,336 +1,324 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import android.content.Context; -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListView; -import com.actionbarsherlock.app.SherlockListFragment; -import com.google.ads.AdView; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.ads.AdsController; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.android.menu.AMenuBuilder; -import org.solovyev.android.menu.MenuImpl; -import org.solovyev.common.collections.CollectionsUtils; -import org.solovyev.common.equals.Equalizer; -import org.solovyev.common.filter.Filter; -import org.solovyev.common.filter.FilterRule; -import org.solovyev.common.filter.FilterRulesChain; -import org.solovyev.common.text.StringUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * User: serso - * Date: 10/15/11 - * Time: 1:13 PM - */ -public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - @NotNull - private static final String TAG = "CalculatorHistoryFragment"; - - public static final Comparator COMPARATOR = new Comparator() { - @Override - public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { - if (state1.isSaved() == state2.isSaved()) { - long l = state2.getTime() - state1.getTime(); - return l > 0l ? 1 : (l < 0l ? -1 : 0); - } else if (state1.isSaved()) { - return -1; - } else if (state2.isSaved()) { - return 1; - } - return 0; - } - }; - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - - - @NotNull - private ArrayAdapter adapter; - - @Nullable - private AdView adView; - - @NotNull - private CalculatorFragmentHelper fragmentHelper; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(); - fragmentHelper.onCreate(this); - - logDebug("onCreate"); - } - - private int logDebug(@NotNull String msg) { - return Log.d(TAG + ": " + getTag(), msg); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View result = inflater.inflate(R.layout.history_fragment, container, false); - logDebug("onCreateView"); - return result; - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - logDebug("onViewCreated"); - - fragmentHelper.setPaneTitle(this, getTitleResId()); - - adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList()); - setListAdapter(adapter); - - final ListView lv = getListView(); - lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(final AdapterView parent, - final View view, - final int position, - final long id) { - - useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); - } - }); - - lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { - final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); - - final Context context = getActivity(); - - final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); - - final List menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values()); - - if (historyState.isSaved()) { - menuItems.remove(HistoryItemMenuItem.save); - } else { - if (isAlreadySaved(historyState)) { - menuItems.remove(HistoryItemMenuItem.save); - } - menuItems.remove(HistoryItemMenuItem.remove); - menuItems.remove(HistoryItemMenuItem.edit); - } - - if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) { - menuItems.remove(HistoryItemMenuItem.copy_result); - } - - final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); - menuBuilder.create(data).show(); - - return true; - } - }); - - adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view); - } - - protected abstract int getTitleResId(); - - @Override - public void onDestroy() { - logDebug("onDestroy"); - - if ( this.adView != null ) { - this.adView.destroy(); - } - - fragmentHelper.onDestroy(this); - - super.onDestroy(); - } - - protected abstract int getItemLayoutId(); - - @Override - public void onResume() { - logDebug("onResume"); - - super.onResume(); - - - CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); - - updateAdapter(); - } - - @Override - public void onPause() { - logDebug("onPause"); - - super.onPause(); - - CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); - - } - - private void updateAdapter() { - final List historyList = getHistoryList(); - - final ArrayAdapter adapter = getAdapter(); - try { - adapter.setNotifyOnChange(false); - adapter.clear(); - for (CalculatorHistoryState historyState : historyList) { - adapter.add(historyState); - } - } finally { - adapter.setNotifyOnChange(true); - } - - adapter.notifyDataSetChanged(); - } - - public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) { - assert !historyState.isSaved(); - - boolean result = false; - try { - historyState.setSaved(true); - if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer() { - @Override - public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { - return first != null && second != null && - first.getTime() == second.getTime() && - first.getDisplayState().equals(second.getDisplayState()) && - first.getEditorState().equals(second.getEditorState()); - } - }) ) { - result = true; - } - } finally { - historyState.setSaved(false); - } - return result; - } - - public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); - } - - @NotNull - private List getHistoryList() { - final List calculatorHistoryStates = getHistoryItems(); - - Collections.sort(calculatorHistoryStates, COMPARATOR); - - final FilterRulesChain filterRulesChain = new FilterRulesChain(); - filterRulesChain.addFilterRule(new FilterRule() { - @Override - public boolean isFiltered(CalculatorHistoryState object) { - return object == null || StringUtils.isEmpty(object.getEditorState().getText()); - } - }); - - new Filter(filterRulesChain).filter(calculatorHistoryStates.iterator()); - - return calculatorHistoryStates; - } - - @NotNull - protected abstract List getHistoryItems(); - - @NotNull - public static String getHistoryText(@NotNull CalculatorHistoryState state) { - final StringBuilder result = new StringBuilder(); - result.append(state.getEditorState().getText()); - result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); - final String expressionResult = state.getDisplayState().getEditorState().getText(); - if (expressionResult != null) { - result.append(expressionResult); - } - return result.toString(); - } - - @NotNull - private static String getIdentitySign(@NotNull JsclOperation jsclOperation) { - return jsclOperation == JsclOperation.simplify ? "≡" : "="; - } - - // todo serso: menu -/* @Override - public boolean onCreateOptionsMenu(android.view.Menu menu) { - final MenuInflater menuInflater = getMenuInflater(); - menuInflater.inflate(R.menu.history_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - boolean result; - - switch (item.getItemId()) { - case R.id.history_menu_clear_history: - clearHistory(); - result = true; - break; - default: - result = super.onOptionsItemSelected(item); - } - - return result; - }*/ - - protected abstract void clearHistory(); - - @NotNull - protected ArrayAdapter getAdapter() { - return adapter; - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - if ( calculatorEventType == CalculatorEventType.history_state_added ) { - - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - logDebug("onCalculatorEvent"); - updateAdapter(); - } - }); - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import android.content.Context; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import com.actionbarsherlock.app.SherlockListFragment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.menu.AMenuBuilder; +import org.solovyev.android.menu.MenuImpl; +import org.solovyev.common.collections.CollectionsUtils; +import org.solovyev.common.equals.Equalizer; +import org.solovyev.common.filter.Filter; +import org.solovyev.common.filter.FilterRule; +import org.solovyev.common.filter.FilterRulesChain; +import org.solovyev.common.text.StringUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * User: serso + * Date: 10/15/11 + * Time: 1:13 PM + */ +public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + @NotNull + private static final String TAG = "CalculatorHistoryFragment"; + + public static final Comparator COMPARATOR = new Comparator() { + @Override + public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { + if (state1.isSaved() == state2.isSaved()) { + long l = state2.getTime() - state1.getTime(); + return l > 0l ? 1 : (l < 0l ? -1 : 0); + } else if (state1.isSaved()) { + return -1; + } else if (state2.isSaved()) { + return 1; + } + return 0; + } + }; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + + @NotNull + private ArrayAdapter adapter; + + @NotNull + private CalculatorFragmentHelper fragmentHelper; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.history_fragment); + fragmentHelper.onCreate(this); + + logDebug("onCreate"); + } + + private int logDebug(@NotNull String msg) { + return Log.d(TAG + ": " + getTag(), msg); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + logDebug("onViewCreated"); + + fragmentHelper.onViewCreated(this, root); + fragmentHelper.setPaneTitle(this, getTitleResId()); + + adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList()); + setListAdapter(adapter); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(final AdapterView parent, + final View view, + final int position, + final long id) { + + useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); + } + }); + + lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { + final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); + + final Context context = getActivity(); + + final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); + + final List menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values()); + + if (historyState.isSaved()) { + menuItems.remove(HistoryItemMenuItem.save); + } else { + if (isAlreadySaved(historyState)) { + menuItems.remove(HistoryItemMenuItem.save); + } + menuItems.remove(HistoryItemMenuItem.remove); + menuItems.remove(HistoryItemMenuItem.edit); + } + + if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) { + menuItems.remove(HistoryItemMenuItem.copy_result); + } + + final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); + menuBuilder.create(data).show(); + + return true; + } + }); + } + + protected abstract int getTitleResId(); + + @Override + public void onDestroy() { + logDebug("onDestroy"); + + fragmentHelper.onDestroy(this); + + super.onDestroy(); + } + + protected abstract int getItemLayoutId(); + + @Override + public void onResume() { + logDebug("onResume"); + + super.onResume(); + + + CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); + + updateAdapter(); + } + + @Override + public void onPause() { + logDebug("onPause"); + + super.onPause(); + + CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); + + } + + private void updateAdapter() { + final List historyList = getHistoryList(); + + final ArrayAdapter adapter = getAdapter(); + try { + adapter.setNotifyOnChange(false); + adapter.clear(); + for (CalculatorHistoryState historyState : historyList) { + adapter.add(historyState); + } + } finally { + adapter.setNotifyOnChange(true); + } + + adapter.notifyDataSetChanged(); + } + + public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) { + assert !historyState.isSaved(); + + boolean result = false; + try { + historyState.setSaved(true); + if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer() { + @Override + public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { + return first != null && second != null && + first.getTime() == second.getTime() && + first.getDisplayState().equals(second.getDisplayState()) && + first.getEditorState().equals(second.getEditorState()); + } + }) ) { + result = true; + } + } finally { + historyState.setSaved(false); + } + return result; + } + + public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); + } + + @NotNull + private List getHistoryList() { + final List calculatorHistoryStates = getHistoryItems(); + + Collections.sort(calculatorHistoryStates, COMPARATOR); + + final FilterRulesChain filterRulesChain = new FilterRulesChain(); + filterRulesChain.addFilterRule(new FilterRule() { + @Override + public boolean isFiltered(CalculatorHistoryState object) { + return object == null || StringUtils.isEmpty(object.getEditorState().getText()); + } + }); + + new Filter(filterRulesChain).filter(calculatorHistoryStates.iterator()); + + return calculatorHistoryStates; + } + + @NotNull + protected abstract List getHistoryItems(); + + @NotNull + public static String getHistoryText(@NotNull CalculatorHistoryState state) { + final StringBuilder result = new StringBuilder(); + result.append(state.getEditorState().getText()); + result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); + final String expressionResult = state.getDisplayState().getEditorState().getText(); + if (expressionResult != null) { + result.append(expressionResult); + } + return result.toString(); + } + + @NotNull + private static String getIdentitySign(@NotNull JsclOperation jsclOperation) { + return jsclOperation == JsclOperation.simplify ? "≡" : "="; + } + + // todo serso: menu +/* @Override + public boolean onCreateOptionsMenu(android.view.Menu menu) { + final MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.history_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean result; + + switch (item.getItemId()) { + case R.id.history_menu_clear_history: + clearHistory(); + result = true; + break; + default: + result = super.onOptionsItemSelected(item); + } + + return result; + }*/ + + protected abstract void clearHistory(); + + @NotNull + protected ArrayAdapter getAdapter() { + return adapter; + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + if ( calculatorEventType == CalculatorEventType.history_state_added ) { + + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + logDebug("onCalculatorEvent"); + updateAdapter(); + } + }); + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java index 1fc75f8c..6b3dc620 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java @@ -1,326 +1,321 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.*; -import com.actionbarsherlock.app.SherlockListFragment; -import com.google.ads.AdView; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.ads.AdsController; -import org.solovyev.android.calculator.*; -import org.solovyev.android.menu.AMenuBuilder; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.android.menu.MenuImpl; -import org.solovyev.common.equals.EqualsTool; -import org.solovyev.common.filter.Filter; -import org.solovyev.common.filter.FilterRule; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.text.StringUtils; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; - -/** - * User: serso - * Date: 12/21/11 - * Time: 9:24 PM - */ -public abstract class AbstractMathEntityListFragment extends SherlockListFragment { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; - - protected final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); - - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - - @Nullable - private MathEntityArrayAdapter adapter; - - @Nullable - private String category; - - @Nullable - private AdView adView; - - @NotNull - private CalculatorFragmentHelper fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(); - - - protected int getLayoutResId() { - return R.layout.math_entities; - } - - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Bundle bundle = getArguments(); - if ( bundle != null ) { - category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); - } - - fragmentHelper.onCreate(this); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(getLayoutResId(), container, false); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - this.fragmentHelper.setPaneTitle(this, getTitleResId()); - - final ListView lv = getListView(); - lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(final AdapterView parent, - final View view, - final int position, - final long id) { - - CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName()); - } - }); - - getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final T item = (T) parent.getItemAtPosition(position); - - final List> menuItems = getMenuItemsOnLongClick(item); - - if (!menuItems.isEmpty()) { - final AMenuBuilder, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems)); - menuBuilder.create(item).show(); - } - - return true; - } - }); - - adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view); - } - - protected abstract int getTitleResId(); - - @Override - public void onDestroy() { - if (this.adView != null) { - this.adView.destroy(); - } - - fragmentHelper.onDestroy(this); - - super.onDestroy(); - } - - @NotNull - protected abstract List> getMenuItemsOnLongClick(@NotNull T item); - - @Override - public void onResume() { - super.onResume(); - - adapter = new MathEntityArrayAdapter(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory()); - setListAdapter(adapter); - - sort(); - } - - @NotNull - private List getMathEntitiesByCategory() { - final List result = getMathEntities(); - - new Filter(new FilterRule() { - @Override - public boolean isFiltered(T t) { - return !isInCategory(t); - } - }).filter(result.iterator()); - - return result; - } - - protected boolean isInCategory(@Nullable T t) { - return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category)); - } - - @NotNull - protected abstract MathEntityDescriptionGetter getDescriptionGetter(); - - @NotNull - protected abstract List getMathEntities(); - - @Nullable - abstract String getMathEntityCategory(@NotNull T t); - - protected void sort() { - final MathEntityArrayAdapter localAdapter = adapter; - if (localAdapter != null) { - localAdapter.sort(new Comparator() { - @Override - public int compare(T function1, T function2) { - return function1.getName().compareTo(function2.getName()); - } - }); - - localAdapter.notifyDataSetChanged(); - } - } - - protected static class MathEntityArrayAdapter extends ArrayAdapter { - - @NotNull - private final MathEntityDescriptionGetter descriptionGetter; - - private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, - @NotNull Context context, - int resource, - int textViewResourceId, - @NotNull List objects) { - - super(context, resource, textViewResourceId, objects); - this.descriptionGetter = descriptionGetter; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); - - final T mathEntity = getItem(position); - - final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); - if (!StringUtils.isEmpty(mathEntityDescription)) { - TextView description = (TextView) result.findViewById(R.id.math_entity_description); - if (description == null) { - final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); - final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null); - description = (TextView) itemView.findViewById(R.id.math_entity_description); - itemView.removeView(description); - result.addView(description); - } - description.setText(mathEntityDescription); - } else { - TextView description = (TextView) result.findViewById(R.id.math_entity_description); - if (description != null) { - result.removeView(description); - } - } - - - return result; - } - } - - protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { - - @NotNull - private final CalculatorMathRegistry mathRegistry; - - public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry mathRegistry) { - this.mathRegistry = mathRegistry; - } - - @Override - public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { - return this.mathRegistry.getDescription(mathEntityName); - } - } - - protected static interface MathEntityDescriptionGetter { - - @Nullable - String getDescription(@NotNull Context context, @NotNull String mathEntityName); - } - - public void addToAdapter(@NotNull T mathEntity) { - if (this.adapter != null) { - this.adapter.add(mathEntity); - } - } - - public void removeFromAdapter(@NotNull T mathEntity) { - if (this.adapter != null) { - this.adapter.remove(mathEntity); - } - } - - public void notifyAdapter() { - if (this.adapter != null) { - this.adapter.notifyDataSetChanged(); - } - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - static void createTab(@NotNull Context context, - @NotNull TabHost tabHost, - @NotNull String tabId, - @NotNull String categoryId, - int tabCaptionId, - @NotNull Class activityClass, - @Nullable Intent parentIntent) { - - TabHost.TabSpec spec; - - final Intent intent; - if (parentIntent != null) { - intent = new Intent(parentIntent); - } else { - intent = new Intent(); - } - intent.setClass(context, activityClass); - intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); - - // Initialize a TabSpec for each tab and add it to the TabHost - spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent); - - tabHost.addTab(spec); - } - - @NotNull - public static Bundle createBundleFor(@NotNull String categoryId) { - final Bundle result = new Bundle(1); - result.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); - return result; - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import com.actionbarsherlock.app.SherlockListFragment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.menu.AMenuBuilder; +import org.solovyev.android.menu.LabeledMenuItem; +import org.solovyev.android.menu.MenuImpl; +import org.solovyev.common.equals.EqualsTool; +import org.solovyev.common.filter.Filter; +import org.solovyev.common.filter.FilterRule; +import org.solovyev.common.math.MathEntity; +import org.solovyev.common.text.StringUtils; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +/** + * User: serso + * Date: 12/21/11 + * Time: 9:24 PM + */ +public abstract class AbstractMathEntityListFragment extends SherlockListFragment { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; + + protected final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); + + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + @Nullable + private MathEntityArrayAdapter adapter; + + @Nullable + private String category; + + @NotNull + private CalculatorFragmentHelper fragmentHelper; + + + protected int getLayoutId() { + return R.layout.math_entities_fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Bundle bundle = getArguments(); + if ( bundle != null ) { + category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); + } + + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(getLayoutId()); + fragmentHelper.onCreate(this); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + fragmentHelper.onViewCreated(this, root); + this.fragmentHelper.setPaneTitle(this, getTitleResId()); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(final AdapterView parent, + final View view, + final int position, + final long id) { + + CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName()); + getActivity().finish(); + } + }); + + getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + final T item = (T) parent.getItemAtPosition(position); + + final List> menuItems = getMenuItemsOnLongClick(item); + + if (!menuItems.isEmpty()) { + final AMenuBuilder, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems)); + menuBuilder.create(item).show(); + } + + return true; + } + }); + } + + protected abstract int getTitleResId(); + + @Override + public void onDestroy() { + fragmentHelper.onDestroy(this); + + super.onDestroy(); + } + + @NotNull + protected abstract List> getMenuItemsOnLongClick(@NotNull T item); + + @Override + public void onResume() { + super.onResume(); + + adapter = new MathEntityArrayAdapter(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory()); + setListAdapter(adapter); + + sort(); + } + + @NotNull + private List getMathEntitiesByCategory() { + final List result = getMathEntities(); + + new Filter(new FilterRule() { + @Override + public boolean isFiltered(T t) { + return !isInCategory(t); + } + }).filter(result.iterator()); + + return result; + } + + protected boolean isInCategory(@Nullable T t) { + return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category)); + } + + @NotNull + protected abstract MathEntityDescriptionGetter getDescriptionGetter(); + + @NotNull + protected abstract List getMathEntities(); + + @Nullable + abstract String getMathEntityCategory(@NotNull T t); + + protected void sort() { + final MathEntityArrayAdapter localAdapter = adapter; + if (localAdapter != null) { + localAdapter.sort(new Comparator() { + @Override + public int compare(T function1, T function2) { + return function1.getName().compareTo(function2.getName()); + } + }); + + localAdapter.notifyDataSetChanged(); + } + } + + protected static class MathEntityArrayAdapter extends ArrayAdapter { + + @NotNull + private final MathEntityDescriptionGetter descriptionGetter; + + private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, + @NotNull Context context, + int resource, + int textViewResourceId, + @NotNull List objects) { + + super(context, resource, textViewResourceId, objects); + this.descriptionGetter = descriptionGetter; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); + + final T mathEntity = getItem(position); + + final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); + if (!StringUtils.isEmpty(mathEntityDescription)) { + TextView description = (TextView) result.findViewById(R.id.math_entity_description); + if (description == null) { + final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); + final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null); + description = (TextView) itemView.findViewById(R.id.math_entity_description); + itemView.removeView(description); + result.addView(description); + } + description.setText(mathEntityDescription); + } else { + TextView description = (TextView) result.findViewById(R.id.math_entity_description); + if (description != null) { + result.removeView(description); + } + } + + + return result; + } + } + + protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { + + @NotNull + private final CalculatorMathRegistry mathRegistry; + + public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry mathRegistry) { + this.mathRegistry = mathRegistry; + } + + @Override + public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { + return this.mathRegistry.getDescription(mathEntityName); + } + } + + protected static interface MathEntityDescriptionGetter { + + @Nullable + String getDescription(@NotNull Context context, @NotNull String mathEntityName); + } + + public void addToAdapter(@NotNull T mathEntity) { + if (this.adapter != null) { + this.adapter.add(mathEntity); + } + } + + public void removeFromAdapter(@NotNull T mathEntity) { + if (this.adapter != null) { + this.adapter.remove(mathEntity); + } + } + + public void notifyAdapter() { + if (this.adapter != null) { + this.adapter.notifyDataSetChanged(); + } + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + static void createTab(@NotNull Context context, + @NotNull TabHost tabHost, + @NotNull String tabId, + @NotNull String categoryId, + int tabCaptionId, + @NotNull Class activityClass, + @Nullable Intent parentIntent) { + + TabHost.TabSpec spec; + + final Intent intent; + if (parentIntent != null) { + intent = new Intent(parentIntent); + } else { + intent = new Intent(); + } + intent.setClass(context, activityClass); + intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); + + // Initialize a TabSpec for each tab and add it to the TabHost + spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent); + + tabHost.addTab(spec); + } + + @NotNull + public static Bundle createBundleFor(@NotNull String categoryId) { + final Bundle result = new Bundle(1); + putCategory(result, categoryId); + return result; + } + + static void putCategory(@NotNull Bundle bundle, @NotNull String categoryId) { + bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java index 572aa080..eabab8f7 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java @@ -1,69 +1,68 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.os.Bundle; -import com.actionbarsherlock.app.SherlockFragmentActivity; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.history.CalculatorHistoryFragment; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; -import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; - -/** - * User: serso - * Date: 12/21/11 - * Time: 10:33 PM - */ -public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { - - @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - activityHelper.onCreate(this, savedInstanceState); - - for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { - activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, null, category.getCaptionId(), R.id.main_layout); - - } - - CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - activityHelper.onSaveInstanceState(this, outState); - } - - @Override - protected void onResume() { - super.onResume(); - - activityHelper.onResume(this); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - this.activityHelper.onDestroy(this); - - CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - //To change body of implemented methods use File | Settings | File Templates. - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.os.Bundle; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.history.CalculatorHistoryFragment; +import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; +import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; + +/** + * User: serso + * Date: 12/21/11 + * Time: 10:33 PM + */ +public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { + + @NotNull + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + activityHelper.onCreate(this, savedInstanceState); + + for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { + activityHelper.addTab(this, category.name(), CalculatorFunctionsFragment.class, AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout); + } + + CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + activityHelper.onSaveInstanceState(this, outState); + } + + @Override + protected void onResume() { + super.onResume(); + + activityHelper.onResume(this); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + this.activityHelper.onDestroy(this); + + CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + //To change body of implemented methods use File | Settings | File Templates. + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java index a565b71c..f632b62c 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java @@ -1,310 +1,320 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.os.Bundle; -import android.support.v4.app.FragmentActivity; -import android.text.ClipboardManager; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.EditText; -import android.widget.Toast; -import jscl.math.function.IConstant; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.math.MathType; -import org.solovyev.android.calculator.model.Var; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.common.JPredicate; -import org.solovyev.common.collections.CollectionsUtils; -import org.solovyev.common.text.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * User: serso - * Date: 9/28/11 - * Time: 10:55 PM - */ -public class CalculatorVarsFragment extends AbstractMathEntityListFragment { - - public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var"; - - @Override - protected int getLayoutResId() { - return R.layout.vars_fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Bundle bundle = getArguments(); - if (bundle != null) { - final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING); - if (!StringUtils.isEmpty(varValue)) { - createEditVariableDialog(this, null, null, varValue, null); - - // in order to stop intent for other tabs - bundle.remove(CREATE_VAR_EXTRA_STRING); - } - } - } - - @Override - protected int getTitleResId() { - return R.string.c_vars; - } - - @NotNull - @Override - protected List> getMenuItemsOnLongClick(@NotNull IConstant item) { - final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if ( item.isSystem() ) { - result.remove(LongClickMenuItem.edit); - result.remove(LongClickMenuItem.remove); - } - - if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName())) ) { - result.remove(LongClickMenuItem.copy_description); - } - - if ( StringUtils.isEmpty(item.getValue()) ) { - result.remove(LongClickMenuItem.copy_value); - } - - return result; - } - - @NotNull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry()); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void addVarButtonClickHandler(@NotNull View v) { - createEditVariableDialog(this, null, null, null, null); - } - - @NotNull - @Override - protected List getMathEntities() { - final List result = new ArrayList(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities()); - - CollectionsUtils.removeAll(result, new JPredicate() { - @Override - public boolean apply(@Nullable IConstant var) { - return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); - } - }); - - return result; - } - - @Override - protected String getMathEntityCategory(@NotNull IConstant var) { - return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var); - } - - private static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment fragment, - @Nullable final IConstant var, - @Nullable final String name, - @Nullable final String value, - @Nullable final String description) { - final FragmentActivity activity = fragment.getActivity(); - - if (var == null || !var.isSystem()) { - - final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); - final View editView = layoutInflater.inflate(R.layout.var_edit, null); - - final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted); - - final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); - editName.setText(name); - editName.addTextChangedListener(new TextWatcher() { - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (!acceptableChars.contains(c)) { - s.delete(i, i + 1); - Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); - } - } - } - }); - - final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); - if (!StringUtils.isEmpty(value)) { - editValue.setText(value); - } - - final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); - editDescription.setText(description); - - final Var.Builder varBuilder; - if (var != null) { - varBuilder = new Var.Builder(var); - } else { - varBuilder = new Var.Builder(); - } - - final AlertDialog.Builder builder = new AlertDialog.Builder(activity) - .setCancelable(true) - .setNegativeButton(R.string.c_cancel, null) - .setPositiveButton(R.string.c_save, new VarEditorSaver(varBuilder, var, editView, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator() { - @Override - public void showEditor(@NotNull AbstractMathEntityListFragment activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) { - createEditVariableDialog(activity, editedInstance, name, value, description); - } - })) - .setView(editView); - - if (var != null) { - // EDIT mode - - builder.setTitle(R.string.c_var_edit_var); - builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(var, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - createEditVariableDialog(fragment, var, name, value, description); - } - }, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment)); - } else { - // CREATE mode - - builder.setTitle(R.string.c_var_create_var); - } - - builder.create().show(); - } else { - Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); - } - } - - public static boolean isValidValue(@NotNull String value) { - // now every string might be constant - return true; - } - - // todo serso: menu -/* @Override - public boolean onCreateOptionsMenu(Menu menu) { - final MenuInflater menuInflater = getMenuInflater(); - menuInflater.inflate(R.menu.var_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - boolean result; - - switch (item.getItemId()) { - case R.id.var_menu_add_var: - createEditVariableDialog(this, null, null, null, null); - result = true; - break; - default: - result = super.onOptionsItemSelected(item); - } - - return result; - }*/ - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - private static enum LongClickMenuItem implements LabeledMenuItem{ - use(R.string.c_use) { - @Override - public void onClick(@NotNull IConstant data, @NotNull Context context) { - CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName()); - if (context instanceof Activity) { - ((Activity) context).finish(); - } - } - }, - - edit(R.string.c_edit) { - @Override - public void onClick(@NotNull IConstant data, @NotNull Context context) { - /*if (context instanceof AbstractMathEntityListFragment) { - createEditVariableDialog((AbstractMathEntityListFragment)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription()); - }*/ - } - }, - - remove(R.string.c_remove) { - @Override - public void onClick(@NotNull IConstant data, @NotNull Context context) { - /*if (context instanceof AbstractMathEntityListFragment) { - new MathEntityRemover(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment) context)).showConfirmationDialog(); - }*/ - } - }, - - copy_value(R.string.c_copy_value) { - @Override - public void onClick(@NotNull IConstant data, @NotNull Context context) { - final String text = data.getValue(); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }, - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@NotNull IConstant data, @NotNull Context context) { - final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName()); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @NotNull - @Override - public String getCaption(@NotNull Context context) { - return context.getString(captionId); - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.text.ClipboardManager; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.Toast; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; +import jscl.math.function.IConstant; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.math.MathType; +import org.solovyev.android.calculator.model.Var; +import org.solovyev.android.menu.LabeledMenuItem; +import org.solovyev.common.JPredicate; +import org.solovyev.common.collections.CollectionsUtils; +import org.solovyev.common.text.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * User: serso + * Date: 9/28/11 + * Time: 10:55 PM + */ +public class CalculatorVarsFragment extends AbstractMathEntityListFragment { + + public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var"; + + @Override + protected int getLayoutId() { + return R.layout.vars_fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Bundle bundle = getArguments(); + if (bundle != null) { + final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING); + if (!StringUtils.isEmpty(varValue)) { + createEditVariableDialog(this, null, null, varValue, null); + + // in order to stop intent for other tabs + bundle.remove(CREATE_VAR_EXTRA_STRING); + } + } + + setHasOptionsMenu(true); + } + + @Override + protected int getTitleResId() { + return R.string.c_vars; + } + + @NotNull + @Override + protected List> getMenuItemsOnLongClick(@NotNull IConstant item) { + final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); + + if ( item.isSystem() ) { + result.remove(LongClickMenuItem.edit); + result.remove(LongClickMenuItem.remove); + } + + if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName())) ) { + result.remove(LongClickMenuItem.copy_description); + } + + if ( StringUtils.isEmpty(item.getValue()) ) { + result.remove(LongClickMenuItem.copy_value); + } + + return result; + } + + @NotNull + @Override + protected MathEntityDescriptionGetter getDescriptionGetter() { + return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry()); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void addVarButtonClickHandler(@NotNull View v) { + createEditVariableDialog(this, null, null, null, null); + } + + @NotNull + @Override + protected List getMathEntities() { + final List result = new ArrayList(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities()); + + CollectionsUtils.removeAll(result, new JPredicate() { + @Override + public boolean apply(@Nullable IConstant var) { + return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); + } + }); + + return result; + } + + @Override + protected String getMathEntityCategory(@NotNull IConstant var) { + return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var); + } + + private static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment fragment, + @Nullable final IConstant var, + @Nullable final String name, + @Nullable final String value, + @Nullable final String description) { + final FragmentActivity activity = fragment.getActivity(); + + if (var == null || !var.isSystem()) { + + final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); + final View editView = layoutInflater.inflate(R.layout.var_edit, null); + + final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted); + + final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); + editName.setText(name); + editName.addTextChangedListener(new TextWatcher() { + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!acceptableChars.contains(c)) { + s.delete(i, i + 1); + Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); + } + } + } + }); + + final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); + if (!StringUtils.isEmpty(value)) { + editValue.setText(value); + } + + final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); + editDescription.setText(description); + + final Var.Builder varBuilder; + if (var != null) { + varBuilder = new Var.Builder(var); + } else { + varBuilder = new Var.Builder(); + } + + final AlertDialog.Builder builder = new AlertDialog.Builder(activity) + .setCancelable(true) + .setNegativeButton(R.string.c_cancel, null) + .setPositiveButton(R.string.c_save, new VarEditorSaver(varBuilder, var, editView, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator() { + @Override + public void showEditor(@NotNull AbstractMathEntityListFragment activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) { + createEditVariableDialog(activity, editedInstance, name, value, description); + } + })) + .setView(editView); + + if (var != null) { + // EDIT mode + + builder.setTitle(R.string.c_var_edit_var); + builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(var, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + createEditVariableDialog(fragment, var, name, value, description); + } + }, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment)); + } else { + // CREATE mode + + builder.setTitle(R.string.c_var_create_var); + } + + builder.create().show(); + } else { + Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); + } + } + + public static boolean isValidValue(@NotNull String value) { + // now every string might be constant + return true; + } + + /* + ********************************************************************** + * + * MENU + * + ********************************************************************** + */ + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.var_menu, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean result; + + switch (item.getItemId()) { + case R.id.var_menu_add_var: + createEditVariableDialog(this, null, null, null, null); + result = true; + break; + default: + result = super.onOptionsItemSelected(item); + } + + return result; + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + private static enum LongClickMenuItem implements LabeledMenuItem{ + use(R.string.c_use) { + @Override + public void onClick(@NotNull IConstant data, @NotNull Context context) { + CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName()); + if (context instanceof Activity) { + ((Activity) context).finish(); + } + } + }, + + edit(R.string.c_edit) { + @Override + public void onClick(@NotNull IConstant data, @NotNull Context context) { + /*if (context instanceof AbstractMathEntityListFragment) { + createEditVariableDialog((AbstractMathEntityListFragment)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription()); + }*/ + } + }, + + remove(R.string.c_remove) { + @Override + public void onClick(@NotNull IConstant data, @NotNull Context context) { + /*if (context instanceof AbstractMathEntityListFragment) { + new MathEntityRemover(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment) context)).showConfirmationDialog(); + }*/ + } + }, + + copy_value(R.string.c_copy_value) { + @Override + public void onClick(@NotNull IConstant data, @NotNull Context context) { + final String text = data.getValue(); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }, + + copy_description(R.string.c_copy_description) { + @Override + public void onClick(@NotNull IConstant data, @NotNull Context context) { + final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName()); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }; + private final int captionId; + + LongClickMenuItem(int captionId) { + this.captionId = captionId; + } + + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java index 87fb4eb0..8ef0b9aa 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java @@ -1,82 +1,89 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.content.Intent; -import android.os.Bundle; -import com.actionbarsherlock.app.SherlockFragmentActivity; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.history.CalculatorHistoryFragment; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; -import org.solovyev.android.calculator.model.VarCategory; - -/** - * User: serso - * Date: 12/21/11 - * Time: 11:05 PM - */ -public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { - - @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - activityHelper.onCreate(this, savedInstanceState); - - final Bundle bundle; - - final Intent intent = getIntent(); - if (intent != null) { - bundle = intent.getExtras(); - } else { - bundle = null; - } - - for (VarCategory category : VarCategory.getCategoriesByTabOrder()) { - if (category == VarCategory.my) { - activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, bundle, category.getCaptionId(), R.id.main_layout); - } else { - activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, null, category.getCaptionId(), R.id.main_layout); - } - } - - CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - activityHelper.onSaveInstanceState(this, outState); - } - - @Override - protected void onResume() { - super.onResume(); - - activityHelper.onResume(this); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - this.activityHelper.onDestroy(this); - - CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - //To change body of implemented methods use File | Settings | File Templates. - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.content.Intent; +import android.os.Bundle; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; +import org.solovyev.android.calculator.model.VarCategory; + +/** + * User: serso + * Date: 12/21/11 + * Time: 11:05 PM + */ +public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { + + @NotNull + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + activityHelper.onCreate(this, savedInstanceState); + + final Bundle bundle; + + final Intent intent = getIntent(); + if (intent != null) { + bundle = intent.getExtras(); + } else { + bundle = null; + } + + + for (VarCategory category : VarCategory.getCategoriesByTabOrder()) { + + final Bundle fragmentParameters; + + if (category == VarCategory.my && bundle != null) { + AbstractMathEntityListFragment.putCategory(bundle, category.name()); + fragmentParameters = bundle; + } else { + fragmentParameters = AbstractMathEntityListFragment.createBundleFor(category.name()); + } + + activityHelper.addTab(this, category.name(), CalculatorVarsFragment.class, fragmentParameters, category.getCaptionId(), R.id.main_layout); + + } + + CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + activityHelper.onSaveInstanceState(this, outState); + } + + @Override + protected void onResume() { + super.onResume(); + + activityHelper.onResume(this); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + this.activityHelper.onDestroy(this); + + CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + //To change body of implemented methods use File | Settings | File Templates. + } +}