This commit is contained in:
Sergey Solovyev 2012-10-09 21:07:49 +04:00
parent c9bd16fa4c
commit 4fd6e94e96
4 changed files with 470 additions and 490 deletions

View File

@ -1,350 +1,323 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import net.robotmedia.billing.BillingController; import org.jetbrains.annotations.NotNull;
import net.robotmedia.billing.IBillingObserver; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull; import org.solovyev.android.AndroidUtils;
import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.about.CalculatorFragmentType;
import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment;
import org.solovyev.android.calculator.about.CalculatorFragmentType; import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; import org.solovyev.android.prefs.Preference;
import org.solovyev.android.fragments.FragmentUtils; import org.solovyev.android.view.ColorButton;
import org.solovyev.android.prefs.Preference; import org.solovyev.common.equals.EqualsTool;
import org.solovyev.android.view.ColorButton; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.equals.EqualsTool; import org.solovyev.common.text.StringUtils;
import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.text.StringUtils; public class CalculatorActivity extends SherlockFragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
public class CalculatorActivity extends SherlockFragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @NotNull
public static final String TAG = CalculatorActivity.class.getSimpleName();
@NotNull
public static final String TAG = CalculatorActivity.class.getSimpleName(); private boolean useBackAsPrev;
@Nullable @NotNull
private IBillingObserver billingObserver; private CalculatorActivityHelper activityHelper;
private boolean useBackAsPrev; /**
* Called when the activity is first created.
@NotNull */
private CalculatorActivityHelper activityHelper; @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
/** final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
* Called when the activity is first created.
*/ final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
@Override
public void onCreate(@Nullable Bundle savedInstanceState) { activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG);
/*final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);*/ activityHelper.logDebug("onCreate");
activityHelper.onCreate(this, savedInstanceState);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
super.onCreate(savedInstanceState);
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); activityHelper.logDebug("super.onCreate");
activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG); if (findViewById(R.id.main_second_pane) != null) {
activityHelper.logDebug("onCreate"); activityHelper.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane);
activityHelper.onCreate(this, savedInstanceState); activityHelper.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane);
super.onCreate(savedInstanceState); activityHelper.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane);
activityHelper.logDebug("super.onCreate"); activityHelper.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.plotter, null, R.id.main_second_pane);
if (findViewById(R.id.main_second_pane) != null) { activityHelper.addTab(this, CalculatorFragmentType.faq, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane); } else {
activityHelper.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane); getSupportActionBar().hide();
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); FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor");
activityHelper.addTab(this, CalculatorFragmentType.plotter, null, R.id.main_second_pane); FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display");
activityHelper.addTab(this, CalculatorFragmentType.faq, null, R.id.main_second_pane); FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard");
} else {
getSupportActionBar().hide(); this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
} firstTimeInit(preferences, this);
FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor"); toggleOrientationChange(preferences);
FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display");
FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard"); preferences.registerOnSharedPreferenceChangeListener(this);
}
/*if (customTitleSupported) {
try { @NotNull
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); private AndroidCalculator getCalculator() {
final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator());
additionalAdditionalTitleText.init(preferences); }
preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText);
} catch (ClassCastException e) { private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) {
// super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports) final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences);
Log.e(CalculatorActivity.class.getName(), e.getMessage(), e); if (appOpenedCounter != null) {
} CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1);
}*/ }
billingObserver = new CalculatorBillingObserver(this); final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences);
BillingController.registerObserver(billingObserver);
final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
firstTimeInit(preferences, this); CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
// init billing controller boolean dialogShown = false;
BillingController.checkBillingSupported(this); if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start
toggleOrientationChange(preferences); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.setPositiveButton(android.R.string.ok, null);
preferences.registerOnSharedPreferenceChangeListener(this); builder.setTitle(R.string.c_first_start_text_title);
} builder.create().show();
dialogShown = true;
@NotNull } else {
private AndroidCalculator getCalculator() { if (savedVersion < appVersion) {
return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences);
} if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesFragment.getReleaseNotes(context, savedVersion + 1);
private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) { if (!StringUtils.isEmpty(releaseNotes)) {
final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
if (appOpenedCounter != null) { builder.setPositiveButton(android.R.string.ok, null);
CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1); builder.setTitle(R.string.c_release_notes);
} builder.create().show();
dialogShown = true;
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; //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!");
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { if (!dialogShown) {
// new start if (appOpenedCounter != null && appOpenedCounter > 10) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context);
builder.setPositiveButton(android.R.string.ok, null); }
builder.setTitle(R.string.c_first_start_text_title); }
builder.create().show();
dialogShown = true; if (!dialogShown) {
} else { dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
if (savedVersion < appVersion) { }
final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); }
if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesFragment.getReleaseNotes(context, savedVersion + 1); private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) {
if (!StringUtils.isEmpty(releaseNotes)) { boolean result = false;
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
builder.setPositiveButton(android.R.string.ok, null); final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences);
builder.setTitle(R.string.c_release_notes); if ( specialWindowShown != null && !specialWindowShown ) {
builder.create().show(); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
dialogShown = true; 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);
//Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); builder.create().show();
if (!dialogShown) {
if (appOpenedCounter != null && appOpenedCounter > 10) { result = true;
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); specialWindowShownPref.putPreference(preferences, true);
} }
}
return result;
if (!dialogShown) { }
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
} @Override
} public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) { if (useBackAsPrev) {
boolean result = false; getCalculator().doHistoryAction(HistoryAction.undo);
return true;
final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); }
if ( specialWindowShown != null && !specialWindowShown ) { }
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); return super.onKeyDown(keyCode, event);
final View view = layoutInflater.inflate(layoutId, null); }
final TextView feedbackTextView = (TextView) view.findViewById(textViewId); @SuppressWarnings({"UnusedDeclaration"})
feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); public void equalsButtonClickHandler(@NotNull View v) {
getCalculator().evaluate();
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view); }
builder.setPositiveButton(android.R.string.ok, null);
builder.create().show(); @Override
protected void onPause() {
result = true; this.activityHelper.onPause(this);
specialWindowShownPref.putPreference(preferences, true);
} super.onPause();
}
return result;
} @Override
protected void onResume() {
@Override super.onResume();
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (useBackAsPrev) { final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences);
getCalculator().doHistoryAction(HistoryAction.undo); if ( newLayout != activityHelper.getLayout() ) {
return true; AndroidUtils.restartActivity(this);
} }
}
return super.onKeyDown(keyCode, event); this.activityHelper.onResume(this);
} }
@SuppressWarnings({"UnusedDeclaration"}) @Override
public void equalsButtonClickHandler(@NotNull View v) { protected void onDestroy() {
getCalculator().evaluate(); activityHelper.onDestroy(this);
}
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
@Override
protected void onPause() { super.onDestroy();
this.activityHelper.onPause(this); }
super.onPause(); @Override
} public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) {
@Override useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
protected void onResume() { }
super.onResume();
if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); toggleOrientationChange(preferences);
final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); }
if ( newLayout != activityHelper.getLayout() ) { }
AndroidUtils.restartActivity(this);
} @Override
protected void onSaveInstanceState(Bundle outState) {
this.activityHelper.onResume(this); super.onSaveInstanceState(outState);
}
activityHelper.onSaveInstanceState(this, outState);
@Override }
protected void onDestroy() {
if (billingObserver != null) { private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
BillingController.unregisterObserver(billingObserver); preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences;
} if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
activityHelper.onDestroy(this); } else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onDestroy(); }
} }
@Override /*
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { **********************************************************************
if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) { *
useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); * BUTTON HANDLERS
} *
**********************************************************************
if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) { */
toggleOrientationChange(preferences);
} @SuppressWarnings({"UnusedDeclaration"})
} public void elementaryButtonClickHandler(@NotNull View v) {
throw new UnsupportedOperationException("Not implemented yet!");
@Override }
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); @SuppressWarnings({"UnusedDeclaration"})
public void historyButtonClickHandler(@NotNull View v) {
activityHelper.onSaveInstanceState(this, outState); CalculatorActivityLauncher.showHistory(this);
} }
private void toggleOrientationChange(@Nullable SharedPreferences preferences) { @SuppressWarnings({"UnusedDeclaration"})
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; public void eraseButtonClickHandler(@NotNull View v) {
if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { CalculatorLocatorImpl.getInstance().getEditor().erase();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); }
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); @SuppressWarnings({"UnusedDeclaration"})
} public void simplifyButtonClickHandler(@NotNull View v) {
} throw new UnsupportedOperationException("Not implemented yet!");
}
/*
********************************************************************** @SuppressWarnings({"UnusedDeclaration"})
* public void moveLeftButtonClickHandler(@NotNull View v) {
* BUTTON HANDLERS getKeyboard().moveCursorLeft();
* }
**********************************************************************
*/ @SuppressWarnings({"UnusedDeclaration"})
public void moveRightButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) getKeyboard().moveCursorRight();
public void elementaryButtonClickHandler(@NotNull View v) { }
throw new UnsupportedOperationException("Not implemented yet!");
} @SuppressWarnings({"UnusedDeclaration"})
public void pasteButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) getKeyboard().pasteButtonPressed();
public void historyButtonClickHandler(@NotNull View v) { }
CalculatorActivityLauncher.showHistory(this);
} @SuppressWarnings({"UnusedDeclaration"})
public void copyButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) getKeyboard().copyButtonPressed();
public void eraseButtonClickHandler(@NotNull View v) { }
CalculatorLocatorImpl.getInstance().getEditor().erase();
} @NotNull
private static CalculatorKeyboard getKeyboard() {
@SuppressWarnings({"UnusedDeclaration"}) return CalculatorLocatorImpl.getInstance().getKeyboard();
public void simplifyButtonClickHandler(@NotNull View v) { }
throw new UnsupportedOperationException("Not implemented yet!");
} @SuppressWarnings({"UnusedDeclaration"})
public void clearButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) getKeyboard().clearButtonPressed();
public void moveLeftButtonClickHandler(@NotNull View v) { }
getKeyboard().moveCursorLeft();
} @SuppressWarnings({"UnusedDeclaration"})
public void digitButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
public void moveRightButtonClickHandler(@NotNull View v) { if (((ColorButton) v).isShowText()) {
getKeyboard().moveCursorRight(); getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString());
} }
}
@SuppressWarnings({"UnusedDeclaration"})
public void pasteButtonClickHandler(@NotNull View v) { @SuppressWarnings({"UnusedDeclaration"})
getKeyboard().pasteButtonPressed(); public void functionsButtonClickHandler(@NotNull View v) {
} CalculatorActivityLauncher.showFunctions(this);
}
@SuppressWarnings({"UnusedDeclaration"})
public void copyButtonClickHandler(@NotNull View v) { @SuppressWarnings({"UnusedDeclaration"})
getKeyboard().copyButtonPressed(); public void operatorsButtonClickHandler(@NotNull View v) {
} CalculatorActivityLauncher.showOperators(this);
}
@NotNull
private static CalculatorKeyboard getKeyboard() { public static void operatorsButtonClickHandler(@NotNull Activity activity) {
return CalculatorLocatorImpl.getInstance().getKeyboard(); CalculatorActivityLauncher.showOperators(activity);
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void clearButtonClickHandler(@NotNull View v) { public void varsButtonClickHandler(@NotNull View v) {
getKeyboard().clearButtonPressed(); CalculatorActivityLauncher.showVars(this);
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void digitButtonClickHandler(@NotNull View v) { public void donateButtonClickHandler(@NotNull View v) {
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); CalculatorApplication.showDonationDialog(this);
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);
}
} }

View File

@ -1,137 +1,143 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingController;
import org.acra.ACRA; import org.acra.ACRA;
import org.acra.ReportingInteractionMode; import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes; import org.acra.annotation.ReportsCrashes;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ads.AdsController; import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.solovyev.android.calculator.history.AndroidCalculatorHistory;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
/** /**
* User: serso * User: serso
* Date: 12/1/11 * Date: 12/1/11
* Time: 1:21 PM * Time: 1:21 PM
*/ */
@ReportsCrashes(formKey = "", @ReportsCrashes(formKey = "",
mailTo = "se.solovyev+programming+calculatorpp+crashes@gmail.com", mailTo = "se.solovyev+programming+calculatorpp+crashes@gmail.com",
mode = ReportingInteractionMode.DIALOG, mode = ReportingInteractionMode.DIALOG,
resToastText = R.string.crashed, resToastText = R.string.crashed,
resDialogTitle = R.string.crash_dialog_title, resDialogTitle = R.string.crash_dialog_title,
resDialogText = R.string.crash_dialog_text) resDialogText = R.string.crash_dialog_text)
public class CalculatorApplication extends android.app.Application { 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&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; 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&currency_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_PRODUCT_ID = "ad_free";
public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_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 ADMOB_USER_ID = "a14f02cf9c80cbc";
@NotNull @NotNull
private static CalculatorApplication instance; private static CalculatorApplication instance;
public CalculatorApplication() { public CalculatorApplication() {
instance = this; instance = this;
} }
@NotNull @NotNull
public static CalculatorApplication getInstance() { public static CalculatorApplication getInstance() {
return instance; return instance;
} }
@Override @Override
public void onCreate() { public void onCreate() {
ACRA.init(this); ACRA.init(this);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
CalculatorPreferences.setDefaultValues(preferences); CalculatorPreferences.setDefaultValues(preferences);
setTheme(preferences); setTheme(preferences);
super.onCreate(); super.onCreate();
final AndroidCalculator calculator = new AndroidCalculator(); final AndroidCalculator calculator = new AndroidCalculator();
CalculatorLocatorImpl.getInstance().init(calculator, CalculatorLocatorImpl.getInstance().init(calculator,
new AndroidCalculatorEngine(this), new AndroidCalculatorEngine(this),
new AndroidCalculatorClipboard(this), new AndroidCalculatorClipboard(this),
new AndroidCalculatorNotifier(this), new AndroidCalculatorNotifier(this),
new AndroidCalculatorHistory(this, calculator)); new AndroidCalculatorHistory(this, calculator));
CalculatorLocatorImpl.getInstance().getCalculator().init(); CalculatorLocatorImpl.getInstance().getCalculator().init();
AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {
@Override @Override
public byte[] getObfuscationSalt() { 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}; return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82};
} }
@Override @Override
public String getPublicKey() { public String getPublicKey() {
return CalculatorSecurity.getPK(); return CalculatorSecurity.getPK();
} }
}); });
}
private void setTheme(@NotNull SharedPreferences preferences) { BillingController.registerObserver(new CalculatorBillingObserver(this));
final CalculatorPreferences.Gui.Theme theme = CalculatorPreferences.Gui.getTheme(preferences); // init billing controller
setTheme(theme.getThemeId()); BillingController.checkBillingSupported(this);
}
}
public static void showDonationDialog(@NotNull final Context context) {
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); private void setTheme(@NotNull SharedPreferences preferences) {
final View view = layoutInflater.inflate(R.layout.donate, null); final CalculatorPreferences.Gui.Theme theme = CalculatorPreferences.Gui.getTheme(preferences);
setTheme(theme.getThemeId());
final TextView donate = (TextView) view.findViewById(R.id.donateText); }
donate.setMovementMethod(LinkMovementMethod.getInstance());
public static void showDonationDialog(@NotNull final Context context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context) final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
.setCancelable(true) final View view = layoutInflater.inflate(R.layout.donate, null);
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { final TextView donate = (TextView) view.findViewById(R.id.donateText);
@Override donate.setMovementMethod(LinkMovementMethod.getInstance());
public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW); final AlertDialog.Builder builder = new AlertDialog.Builder(context)
i.setData(Uri.parse(paypalDonateUrl)); .setCancelable(true)
context.startActivity(i); .setNegativeButton(R.string.c_cancel, null)
} .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
}) @Override
.setView(view); public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW);
builder.create().show(); i.setData(Uri.parse(paypalDonateUrl));
} context.startActivity(i);
}
@NotNull })
public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) { .setView(view);
return new CalculatorActivityHelperImpl(layoutResId, logTag);
} builder.create().show();
}
@NotNull
public CalculatorFragmentHelper createFragmentHelper(int layoutId) { @NotNull
return new CalculatorFragmentHelperImpl(layoutId); public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) {
} return new CalculatorActivityHelperImpl(layoutResId, logTag);
}
@NotNull
public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId) { @NotNull
return new CalculatorFragmentHelperImpl(layoutId, titleResId); public CalculatorFragmentHelper createFragmentHelper(int layoutId) {
} return new CalculatorFragmentHelperImpl(layoutId);
@NotNull }
public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId, boolean listenersOnCreate) {
return new CalculatorFragmentHelperImpl(layoutId, titleResId, listenersOnCreate); @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);
}
}

View File

@ -6,7 +6,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.content.Context;
import net.robotmedia.billing.ResponseCode; import net.robotmedia.billing.ResponseCode;
import net.robotmedia.billing.helper.AbstractBillingObserver; import net.robotmedia.billing.helper.AbstractBillingObserver;
import net.robotmedia.billing.model.Transaction; import net.robotmedia.billing.model.Transaction;
@ -19,8 +19,8 @@ import org.jetbrains.annotations.NotNull;
*/ */
public class CalculatorBillingObserver extends AbstractBillingObserver { public class CalculatorBillingObserver extends AbstractBillingObserver {
public CalculatorBillingObserver(@NotNull Activity activity) { public CalculatorBillingObserver(@NotNull Context context) {
super(activity); super(context);
} }
@Override @Override

View File

@ -52,7 +52,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im
BillingController.checkBillingSupported(CalculatorPreferencesActivity.this); BillingController.checkBillingSupported(CalculatorPreferencesActivity.this);
final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, AndroidCalculatorEngine.Preferences.roundResult.getKey()); onSharedPreferenceChanged(preferences, AndroidCalculatorEngine.Preferences.roundResult.getKey());
onSharedPreferenceChanged(preferences, VibratorContainer.Preferences.hapticFeedbackEnabled.getKey()); onSharedPreferenceChanged(preferences, VibratorContainer.Preferences.hapticFeedbackEnabled.getKey());
@ -126,6 +126,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im
@Override @Override
protected void onDestroy() { protected void onDestroy() {
BillingController.unregisterObserver(this); BillingController.unregisterObserver(this);
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy(); super.onDestroy();
} }