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 81fc3623..1d079a26 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -1,350 +1,323 @@ -/* - * 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.CalculatorFragmentType; -import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; -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) { - /*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, CalculatorFragmentType.history, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.plotter, null, R.id.main_second_pane); - activityHelper.addTab(this, CalculatorFragmentType.faq, null, R.id.main_second_pane); - } 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); - - 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 = CalculatorReleaseNotesFragment.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 onPause() { - this.activityHelper.onPause(this); - - super.onPause(); - } - - @Override - protected void onResume() { - super.onResume(); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); - if ( newLayout != activityHelper.getLayout() ) { - 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 org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; +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(); + + private boolean useBackAsPrev; + + @NotNull + private CalculatorActivityHelper activityHelper; + + /** + * Called when the activity is first created. + */ + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + 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, CalculatorFragmentType.history, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.plotter, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.faq, null, R.id.main_second_pane); + } 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"); + + this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); + firstTimeInit(preferences, this); + + toggleOrientationChange(preferences); + + 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 = CalculatorReleaseNotesFragment.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 onPause() { + this.activityHelper.onPause(this); + + super.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); + if ( newLayout != activityHelper.getLayout() ) { + AndroidUtils.restartActivity(this); + } + + this.activityHelper.onResume(this); + } + + @Override + protected void onDestroy() { + activityHelper.onDestroy(this); + + PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(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/CalculatorApplication.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 0e50c82a..c215bb9c 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -1,137 +1,143 @@ -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.acra.ACRA; -import org.acra.ReportingInteractionMode; -import org.acra.annotation.ReportsCrashes; -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 - */ -@ReportsCrashes(formKey = "", - mailTo = "se.solovyev+programming+calculatorpp+crashes@gmail.com", - mode = ReportingInteractionMode.DIALOG, - resToastText = R.string.crashed, - resDialogTitle = R.string.crash_dialog_title, - resDialogText = R.string.crash_dialog_text) -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"; - - @NotNull - private static CalculatorApplication instance; - - public CalculatorApplication() { - instance = this; - } - - @NotNull - public static CalculatorApplication getInstance() { - return instance; - } - - @Override - public void onCreate() { - ACRA.init(this); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - CalculatorPreferences.setDefaultValues(preferences); - - 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(); - } - }); - } - - 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(); - } - - @NotNull - public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) { - return new CalculatorActivityHelperImpl(layoutResId, logTag); - } - - @NotNull - public CalculatorFragmentHelper createFragmentHelper(int layoutId) { - return new CalculatorFragmentHelperImpl(layoutId); - } - - @NotNull - public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId) { - return new CalculatorFragmentHelperImpl(layoutId, titleResId); - } - @NotNull - public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId, boolean listenersOnCreate) { - return new CalculatorFragmentHelperImpl(layoutId, titleResId, listenersOnCreate); - } - -} +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.acra.ACRA; +import org.acra.ReportingInteractionMode; +import org.acra.annotation.ReportsCrashes; +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 + */ +@ReportsCrashes(formKey = "", + mailTo = "se.solovyev+programming+calculatorpp+crashes@gmail.com", + mode = ReportingInteractionMode.DIALOG, + resToastText = R.string.crashed, + resDialogTitle = R.string.crash_dialog_title, + resDialogText = R.string.crash_dialog_text) +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"; + + @NotNull + private static CalculatorApplication instance; + + public CalculatorApplication() { + instance = this; + } + + @NotNull + public static CalculatorApplication getInstance() { + return instance; + } + + @Override + public void onCreate() { + ACRA.init(this); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + CalculatorPreferences.setDefaultValues(preferences); + + 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(); + } + }); + + + BillingController.registerObserver(new CalculatorBillingObserver(this)); + // init billing controller + BillingController.checkBillingSupported(this); + + } + + 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(); + } + + @NotNull + public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) { + return new CalculatorActivityHelperImpl(layoutResId, logTag); + } + + @NotNull + public CalculatorFragmentHelper createFragmentHelper(int layoutId) { + return new CalculatorFragmentHelperImpl(layoutId); + } + + @NotNull + public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId) { + return new CalculatorFragmentHelperImpl(layoutId, titleResId); + } + @NotNull + public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId, boolean listenersOnCreate) { + return new CalculatorFragmentHelperImpl(layoutId, titleResId, listenersOnCreate); + } + +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorBillingObserver.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorBillingObserver.java index 11aed624..2d3854b9 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorBillingObserver.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorBillingObserver.java @@ -6,7 +6,7 @@ package org.solovyev.android.calculator; -import android.app.Activity; +import android.content.Context; import net.robotmedia.billing.ResponseCode; import net.robotmedia.billing.helper.AbstractBillingObserver; import net.robotmedia.billing.model.Transaction; @@ -19,8 +19,8 @@ import org.jetbrains.annotations.NotNull; */ public class CalculatorBillingObserver extends AbstractBillingObserver { - public CalculatorBillingObserver(@NotNull Activity activity) { - super(activity); + public CalculatorBillingObserver(@NotNull Context context) { + super(context); } @Override diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java index 98926673..8b454cef 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java @@ -52,7 +52,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im BillingController.checkBillingSupported(CalculatorPreferencesActivity.this); - final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); preferences.registerOnSharedPreferenceChangeListener(this); onSharedPreferenceChanged(preferences, AndroidCalculatorEngine.Preferences.roundResult.getKey()); onSharedPreferenceChanged(preferences, VibratorContainer.Preferences.hapticFeedbackEnabled.getKey()); @@ -126,6 +126,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im @Override protected void onDestroy() { BillingController.unregisterObserver(this); + PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); super.onDestroy(); }