From 85552cfc4e43182b6a8be06bd4647958047dc6bb Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Wed, 4 Jan 2012 02:25:28 +0400 Subject: [PATCH] ads --- AndroidManifest.xml | 20 +- res/layout/help.xml | 30 -- res/layout/history_activity.xml | 1 + res/layout/math_entities.xml | 1 + res/layout/vars.xml | 1 + res/values-ru/text_strings.xml | 2 + res/values/text_strings.xml | 2 + res/xml/main_preferences.xml | 9 +- .../org/solovyev/android/AndroidUtils.java | 18 +- .../calculator/ApplicationContext.java | 44 --- .../calculator/CalculatorActivity.java | 3 +- .../CalculatorActivityLauncher.java | 18 +- .../calculator/CalculatorApplication.java | 95 ++++++ .../CalculatorPreferencesActivity.java | 33 +- .../calculator/CalculatorSecurity.java | 20 ++ .../calculator/help/AbstractHelpActivity.java | 47 +++ .../help/CalculatorHelpTabActivity.java | 2 +- .../calculator/help/HelpFaqActivity.java | 12 +- .../calculator/help/HelpHintsActivity.java | 12 +- .../calculator/help/HelpScreensActivity.java | 13 +- .../history/AbstractHistoryActivity.java | 15 + ...java => CalculatorHistoryTabActivity.java} | 3 +- .../edit/AbstractMathEntityListActivity.java | 19 +- .../edit/CalculatorFunctionsActivity.java | 220 ++++++++++++-- .../edit/CalculatorFunctionsTabActivity.java | 219 ++------------ .../math/edit/CalculatorVarsActivity.java | 285 ++++++++++++++++-- .../math/edit/CalculatorVarsTabActivity.java | 285 ++---------------- .../math/edit/FunctionEditorSaver.java | 2 +- .../calculator/math/edit/VarEditorSaver.java | 2 +- 29 files changed, 771 insertions(+), 662 deletions(-) delete mode 100644 res/layout/help.xml delete mode 100644 src/main/java/org/solovyev/android/calculator/ApplicationContext.java create mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorApplication.java create mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorSecurity.java create mode 100644 src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java rename src/main/java/org/solovyev/android/calculator/history/{CalculatorHistoryActivity.java => CalculatorHistoryTabActivity.java} (93%) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ca01c0b1..edac3a4e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3,8 +3,8 @@ + a:versionCode="24" + a:versionName="1.2.24"> @@ -16,7 +16,7 @@ + a:name=".CalculatorApplication"> - @@ -73,11 +73,11 @@ a:label="@string/c_help" a:configChanges="orientation|keyboardHidden"/> - - @@ -85,18 +85,18 @@ a:label="@string/c_operators" a:configChanges="orientation|keyboardHidden"/> - - - + \ No newline at end of file diff --git a/res/layout/help.xml b/res/layout/help.xml deleted file mode 100644 index d25fabe6..00000000 --- a/res/layout/help.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/res/layout/history_activity.xml b/res/layout/history_activity.xml index c0165d13..b5845c7d 100644 --- a/res/layout/history_activity.xml +++ b/res/layout/history_activity.xml @@ -7,6 +7,7 @@ --> diff --git a/res/layout/math_entities.xml b/res/layout/math_entities.xml index c0165d13..65a414c4 100644 --- a/res/layout/math_entities.xml +++ b/res/layout/math_entities.xml @@ -8,6 +8,7 @@ diff --git a/res/layout/vars.xml b/res/layout/vars.xml index 20341704..d334aa5a 100644 --- a/res/layout/vars.xml +++ b/res/layout/vars.xml @@ -8,6 +8,7 @@ diff --git a/res/values-ru/text_strings.xml b/res/values-ru/text_strings.xml index d425bc56..3ebdce66 100644 --- a/res/values-ru/text_strings.xml +++ b/res/values-ru/text_strings.xml @@ -167,4 +167,6 @@ Ошибка Биллинг не поддерживается! + Избавиться от рекламы! + \ No newline at end of file diff --git a/res/values/text_strings.xml b/res/values/text_strings.xml index 74b1e29f..4ee8c837 100644 --- a/res/values/text_strings.xml +++ b/res/values/text_strings.xml @@ -167,4 +167,6 @@ Error Billing is not supported! + Get rid of ads! + \ No newline at end of file diff --git a/res/xml/main_preferences.xml b/res/xml/main_preferences.xml index c3bd8ca8..9263993c 100644 --- a/res/xml/main_preferences.xml +++ b/res/xml/main_preferences.xml @@ -3,12 +3,11 @@ - + /> diff --git a/src/main/java/org/solovyev/android/AndroidUtils.java b/src/main/java/org/solovyev/android/AndroidUtils.java index 432d78da..e4907702 100644 --- a/src/main/java/org/solovyev/android/AndroidUtils.java +++ b/src/main/java/org/solovyev/android/AndroidUtils.java @@ -20,6 +20,8 @@ import com.google.ads.AdSize; import com.google.ads.AdView; import org.jetbrains.annotations.NotNull; +import java.util.List; + /** * User: serso * Date: 12/21/11 @@ -92,7 +94,10 @@ public final class AndroidUtils { } @NotNull - public static AdView createAndInflateAdView(@NotNull Activity activity, int layoutId, String admobAccountId) { + public static AdView createAndInflateAdView(@NotNull Activity activity, + @NotNull String admobAccountId, + int layoutId, + @NotNull List keywords) { // Create the adView final AdView adView = new AdView(activity, AdSize.BANNER, admobAccountId); @@ -104,7 +109,16 @@ public final class AndroidUtils { layout.addView(adView); // Initiate a generic request to load it with an ad - adView.loadAd(new AdRequest()); + final AdRequest adRequest = new AdRequest(); + + // todo serso: revert - only for tests + adRequest.addTestDevice(AdRequest.TEST_EMULATOR); + adRequest.addTestDevice("DB3C2F605A1296971898F0E60224A927"); + + for (String keyword : keywords) { + adRequest.addKeyword(keyword); + } + adView.loadAd(adRequest); return adView; } diff --git a/src/main/java/org/solovyev/android/calculator/ApplicationContext.java b/src/main/java/org/solovyev/android/calculator/ApplicationContext.java deleted file mode 100644 index d022e233..00000000 --- a/src/main/java/org/solovyev/android/calculator/ApplicationContext.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.solovyev.android.calculator; - -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 12/1/11 - * Time: 1:21 PM - */ -public class ApplicationContext extends android.app.Application { - - public static final String AD_FREE = "ad_free"; - public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; - - @NotNull - private static ApplicationContext instance; - - public ApplicationContext() { - instance = this; - } - - @NotNull - public static ApplicationContext getInstance() { - return instance; - } - - @Override - public void onCreate() { - super.onCreate(); - /*BillingController.setDebug(true); - BillingController.setConfiguration(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 "org.solovyev.android.calculator"; - } - });*/ - } -} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index bb373b44..22902526 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -52,7 +52,6 @@ import java.util.Map; public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { private static final int HVGA_WIDTH_PIXELS = 320; - public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; public static class Preferences { @NotNull @@ -121,7 +120,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } } - ResourceCache.instance.initCaptions(ApplicationContext.getInstance(), R.string.class); + ResourceCache.instance.initCaptions(CalculatorApplication.getInstance(), R.string.class); firstTimeInit(preferences); vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index ea9481e0..95599dd4 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -9,11 +9,11 @@ import org.achartengine.ChartFactory; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.about.CalculatorAboutTabActivity; import org.solovyev.android.calculator.help.CalculatorHelpTabActivity; -import org.solovyev.android.calculator.history.CalculatorHistoryActivity; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryTabActivity; +import org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity; import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity; -import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity; import org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity; +import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity; import org.solovyev.common.utils.StringUtils; /** @@ -24,7 +24,7 @@ import org.solovyev.common.utils.StringUtils; public class CalculatorActivityLauncher { public static void showHistory(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorHistoryActivity.class)); + context.startActivity(new Intent(context, CalculatorHistoryTabActivity.class)); } public static void showHelp(@NotNull final Context context) { @@ -40,7 +40,7 @@ public class CalculatorActivityLauncher { } public static void showFunctions(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorFunctionsActivity.class)); + context.startActivity(new Intent(context, CalculatorFunctionsTabActivity.class)); } public static void showOperators(@NotNull final Context context) { @@ -48,7 +48,7 @@ public class CalculatorActivityLauncher { } public static void showVars(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorVarsActivity.class)); + context.startActivity(new Intent(context, CalculatorVarsTabActivity.class)); } public static void plotGraph(@NotNull final Context context, @NotNull Generic generic, @NotNull Constant constant){ @@ -63,9 +63,9 @@ public class CalculatorActivityLauncher { if (calculatorModel.getDisplay().isValid() ) { final String varValue = calculatorModel.getDisplay().getText().toString(); if (!StringUtils.isEmpty(varValue)) { - if (CalculatorVarsTabActivity.isValidValue(varValue)) { - final Intent intent = new Intent(context, CalculatorVarsActivity.class); - intent.putExtra(CalculatorVarsTabActivity.CREATE_VAR_EXTRA_STRING, varValue); + if (CalculatorVarsActivity.isValidValue(varValue)) { + final Intent intent = new Intent(context, CalculatorVarsTabActivity.class); + intent.putExtra(CalculatorVarsActivity.CREATE_VAR_EXTRA_STRING, varValue); context.startActivity(intent); } else { Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show(); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java new file mode 100644 index 00000000..fa4115fc --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -0,0 +1,95 @@ +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.content.Context; +import android.util.Log; +import com.google.ads.AdView; +import net.robotmedia.billing.BillingController; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; + +import java.util.Collections; +import java.util.List; + +/** + * User: serso + * Date: 12/1/11 + * Time: 1:21 PM + */ +public class CalculatorApplication extends android.app.Application { + + 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; + } + + public static boolean isAdFreeApprox(@NotNull Context context) { + return BillingController.isPurchased(context.getApplicationContext(), AD_FREE_PRODUCT_ID); + } + + public static boolean isAdFree(@NotNull Context context) { + // check if user already bought this product + boolean purchased = isAdFreeApprox(context); + if (!purchased) { + // we must to restore all transactions done by user to guarantee that product was purchased or not + BillingController.restoreTransactions(context); + + // todo serso: may be call net.robotmedia.billing.BillingController.restoreTransactions() always before first check and get rid of second check + // check the billing one more time + purchased = isAdFreeApprox(context); + } + return purchased; + } + + @Nullable + public static AdView inflateAd(@NotNull Activity activity) { + return inflateAd(activity, R.id.ad_parent_view); + } + + @Nullable + public static AdView inflateAd(@NotNull Activity activity, int parentViewId) { + AdView result = null; + if ( !isAdFreeApprox(activity) ) { + Log.d(activity.getClass().getName(), "Application is not ad free - inflating ad!"); + //final List keywords = Arrays.asList("math", "mathematics", "finance", "physics", "dynamics"); + final List keywords = Collections.emptyList(); + result = AndroidUtils.createAndInflateAdView(activity, ADMOB_USER_ID, parentViewId, keywords); + } else { + Log.d(activity.getClass().getName(), "Application is ad free - no ads!"); + } + + return result; + } + + @Override + public void onCreate() { + super.onCreate(); + + BillingController.setConfiguration(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.PK; + } + }); + BillingController.checkBillingSupported(this); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java index 210d5a0a..2d3af252 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java @@ -27,34 +27,25 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements addPreferencesFromResource(R.xml.main_preferences); - /*final Preference buyPref = findPreference(ApplicationContext.AD_FREE_APPLICATION_P_KEY); - buyPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - // при нажатии на кнопку Убрать рекламу в настройках + final Preference addFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY); + + addFreePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { - // проверяем поддерживается ли покупка в приложениях + + // check billing availability if (BillingController.checkBillingSupported(CalculatorPreferencesActivity.this) != BillingController.BillingStatus.SUPPORTED) { - // показываем сообщение, что покупка не поддерживается + // warn about not supported billing new AlertDialog.Builder(CalculatorPreferencesActivity.this).setTitle(R.string.c_error).setMessage(R.string.c_billing_error).create().show(); } else { - // проверяем не купил ли пользователь уже нашу опцию - boolean purchased = BillingController.isPurchased(getApplicationContext(), ApplicationContext.AD_FREE_APPLICATION); - if (!purchased) { - // если не купил (или мы просто об этом пока не знаем? пользователь удалял - // приложение со всем данными?), то пытаемся восстановить транзакции - BillingController.restoreTransactions(CalculatorPreferencesActivity.this); - // следующая строка (проверка еще раз не купил ли пользователь приложение) - - // не очень правильный подход - вызвав restoreTransactions, - // ответ мы получим не сразу - purchased = BillingController.isPurchased(getApplicationContext(), ApplicationContext.AD_FREE_APPLICATION); - if (!purchased) { - // наконец, показываем пользователю стандартное окно для покупки опции - BillingController.requestPurchase(CalculatorPreferencesActivity.this, ApplicationContext.AD_FREE_APPLICATION); - } + if (!CalculatorApplication.isAdFree(CalculatorPreferencesActivity.this)) { + // not purchased => show purchase window for user + BillingController.requestPurchase(CalculatorPreferencesActivity.this, CalculatorApplication.AD_FREE_PRODUCT_ID); } } + return true; } - });*/ + }); final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); preferences.registerOnSharedPreferenceChangeListener(this); @@ -62,7 +53,7 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements onSharedPreferenceChanged(preferences, VibratorContainer.HAPTIC_FEEDBACK_P_KEY); } - @Override + @Override public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { if (CalculatorEngine.Preferences.roundResult.getKey().equals(key)) { findPreference(CalculatorEngine.Preferences.roundResult.getKey()).setEnabled(preferences.getBoolean(key, CalculatorEngine.Preferences.roundResult.getDefaultValue())); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorSecurity.java b/src/main/java/org/solovyev/android/calculator/CalculatorSecurity.java new file mode 100644 index 00000000..d97d8423 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorSecurity.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2009-2012. 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; + +/** + * User: serso + * Date: 1/4/12 + * Time: 1:23 AM + */ +public final class CalculatorSecurity { + + public static final String PK = "org.solovyev.android.calculator"; + + private CalculatorSecurity() { + } +} diff --git a/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java b/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java new file mode 100644 index 00000000..26d8262a --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2009-2012. 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.help; + +import android.app.Activity; +import android.os.Bundle; +import com.google.ads.AdView; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorApplication; + +/** + * User: serso + * Date: 1/4/12 + * Time: 12:34 AM + */ +public class AbstractHelpActivity extends Activity { + + private final int layoutId; + + @Nullable + private AdView adView; + + protected AbstractHelpActivity(int layoutId) { + this.layoutId = layoutId; + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(layoutId); + + adView = CalculatorApplication.inflateAd(this); + } + + @Override + protected void onDestroy() { + if (this.adView != null) { + this.adView.destroy(); + } + super.onDestroy(); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java b/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java index ac2ad236..48a68960 100644 --- a/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java @@ -27,7 +27,7 @@ public class CalculatorHelpTabActivity extends TabActivity { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.help); + setContentView(R.layout.tabs); final TabHost tabHost = getTabHost(); diff --git a/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java b/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java index 5f32c5fc..bbbc8cc6 100644 --- a/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java @@ -6,9 +6,6 @@ package org.solovyev.android.calculator.help; -import android.app.Activity; -import android.os.Bundle; -import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; /** @@ -16,12 +13,9 @@ import org.solovyev.android.calculator.R; * Date: 11/19/11 * Time: 11:37 AM */ -public class HelpFaqActivity extends Activity { +public class HelpFaqActivity extends AbstractHelpActivity { - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.help_faq); + public HelpFaqActivity() { + super(R.layout.help_faq); } } \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java b/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java index c4b20a4b..4e2b61b6 100644 --- a/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java @@ -6,9 +6,6 @@ package org.solovyev.android.calculator.help; -import android.app.Activity; -import android.os.Bundle; -import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; /** @@ -16,12 +13,9 @@ import org.solovyev.android.calculator.R; * Date: 11/19/11 * Time: 11:37 AM */ -public class HelpHintsActivity extends Activity { +public class HelpHintsActivity extends AbstractHelpActivity { - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.help_hints); + public HelpHintsActivity() { + super(R.layout.help_hints); } } \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java b/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java index 1336f8ea..6ab7aff8 100644 --- a/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java @@ -6,9 +6,6 @@ package org.solovyev.android.calculator.help; -import android.app.Activity; -import android.os.Bundle; -import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; /** @@ -16,12 +13,10 @@ import org.solovyev.android.calculator.R; * Date: 11/19/11 * Time: 11:38 AM */ -public class HelpScreensActivity extends Activity { +public class HelpScreensActivity extends AbstractHelpActivity{ - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.help_screens); + public HelpScreensActivity() { + super(R.layout.help_screens); } + } \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java index 76990d9a..62579004 100644 --- a/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryActivity.java @@ -16,8 +16,10 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; +import com.google.ads.AdView; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -56,12 +58,17 @@ public abstract class AbstractHistoryActivity extends ListActivity { @NotNull private ArrayAdapter adapter; + @Nullable + private AdView adView; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.history_activity); + adView = CalculatorApplication.inflateAd(this); + adapter = new HistoryArrayAdapter(this, getLayoutId(), R.id.history_item, new ArrayList()); setListAdapter(adapter); @@ -111,6 +118,14 @@ public abstract class AbstractHistoryActivity extends ListActivity { }); } + @Override + protected void onDestroy() { + if ( this.adView != null ) { + this.adView.destroy(); + } + super.onDestroy(); + } + protected abstract int getLayoutId(); @Override diff --git a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryTabActivity.java similarity index 93% rename from src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java rename to src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryTabActivity.java index a0b6e05a..91a76982 100644 --- a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryTabActivity.java @@ -18,7 +18,7 @@ import org.solovyev.android.calculator.R; * Date: 12/18/11 * Time: 7:37 PM */ -public class CalculatorHistoryActivity extends TabActivity { +public class CalculatorHistoryTabActivity extends TabActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -35,5 +35,4 @@ public class CalculatorHistoryActivity extends TabActivity { AndroidUtils.centerAndWrapTabsFor(tabHost); } - } diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListActivity.java index 5c2eb7fb..0e5456a2 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListActivity.java @@ -15,8 +15,10 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.*; +import com.google.ads.AdView; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.model.AndroidMathRegistry; @@ -50,7 +52,10 @@ public abstract class AbstractMathEntityListActivity exten @Nullable private String category; - static void createTab(@NotNull Context context, + @Nullable + private AdView adView; + + static void createTab(@NotNull Context context, @NotNull TabHost tabHost, @NotNull String tabId, @NotNull String categoryId, @@ -85,7 +90,9 @@ public abstract class AbstractMathEntityListActivity exten setContentView(getLayoutId()); - final Intent intent = getIntent(); + adView = CalculatorApplication.inflateAd(this); + + final Intent intent = getIntent(); if ( intent != null ) { category = intent.getStringExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING); } @@ -122,6 +129,14 @@ public abstract class AbstractMathEntityListActivity exten }); } + @Override + protected void onDestroy() { + if (this.adView != null) { + this.adView.destroy(); + } + super.onDestroy(); + } + @NotNull protected abstract List> getMenuItemsOnLongClick(@NotNull T item); diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index 926cfcd7..24c0c14b 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -6,35 +6,217 @@ package org.solovyev.android.calculator.math.edit; -import android.app.TabActivity; +import android.app.Activity; +import android.content.Context; import android.os.Bundle; -import android.widget.TabHost; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; +import android.text.ClipboardManager; +import jscl.math.function.Function; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; -import org.solovyev.android.AndroidUtils; +import org.solovyev.android.calculator.model.CalculatorEngine; +import org.solovyev.android.view.AMenuItem; +import org.solovyev.common.utils.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * User: serso - * Date: 12/21/11 - * Time: 10:33 PM + * Date: 10/29/11 + * Time: 4:55 PM */ -public class CalculatorFunctionsActivity extends TabActivity { +public class CalculatorFunctionsActivity extends AbstractMathEntityListActivity { - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + private static enum LongClickMenuItem implements AMenuItem{ + use(R.string.c_use) { + @Override + public void doAction(@NotNull Function data, @NotNull Context context) { + CalculatorModel.instance.processDigitButtonAction(data.getName(), false); + if (context instanceof Activity) { + ((Activity) context).finish(); + } + } + }, - setContentView(R.layout.tabs); + /*edit(R.string.c_edit) { + @Override + public void doAction(@NotNull Function data, @NotNull Context context) { + if (context instanceof AbstractMathEntityListActivity) { + } + } + },*/ - final TabHost tabHost = getTabHost(); + copy_description(R.string.c_copy_description) { + @Override + public void doAction(@NotNull Function data, @NotNull Context context) { + final String text = CalculatorEngine.instance.getFunctionsRegistry().getDescription(context, data.getName()); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }; + private final int captionId; - for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { - AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorFunctionsTabActivity.class, null); - } + LongClickMenuItem(int captionId) { + this.captionId = captionId; + } - AndroidUtils.centerAndWrapTabsFor(tabHost); - } + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } + public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + /*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + final Function function = (Function) parent.getItemAtPosition(position); + if (function instanceof CustomFunction) { + createEditVariableDialog(CalculatorFunctionsTabActivity.this, + ((CustomFunction) function), + function.getName(), + ((CustomFunction) function).getContent(), + ((CustomFunction) function).getParameterNames(), + null); + } + return true; + } + });*/ + + /*final Intent intent = getIntent(); + if (intent != null) { + final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING); + if (!StringUtils.isEmpty(varValue)) { + createEditVariableDialog(this, null, null, varValue, null, null); + + // in order to stop intent for other tabs + intent.removeExtra(CREATE_FUN_EXTRA_STRING); + } + }*/ + } + + @NotNull + @Override + protected List> getMenuItemsOnLongClick(@NotNull Function item) { + List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); + + if ( StringUtils.isEmpty(CalculatorEngine.instance.getFunctionsRegistry().getDescription(this, item.getName())) ) { + result.remove(LongClickMenuItem.copy_description); + } + + return result; + } + +/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, + @Nullable final CustomFunction function, + @Nullable final String name, + @Nullable final String expression, + @Nullable final String[] parameterNames, + @Nullable final String description) { + if (function == null || !function.isSystem()) { + + final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); + final View editView = layoutInflater.inflate(R.layout.var_edit, null); + + final String errorMsg = activity.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(expression)) { + editValue.setText(expression); + } + + final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); + editDescription.setText(description); + + final CustomFunction.Builder functionBuilder; + if (function != null) { + functionBuilder = new CustomFunction.Builder(function); + } else { + functionBuilder = new CustomFunction.Builder(); + } + + final AlertDialog.Builder builder = new AlertDialog.Builder(activity) + .setCancelable(true) + .setNegativeButton(R.string.c_cancel, null) + .setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator() { + + @Override + public void showEditor(@NotNull AbstractMathEntityListActivity activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) { + createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description); + } + })) + .setView(editView); + + if (function != null) { + // EDIT mode + + builder.setTitle(R.string.c_var_edit_var); + builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(function, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + createEditVariableDialog(activity, function, name, expression, parameterNames, description); + } + }, CalculatorEngine.instance.getFunctionsRegistry(), activity)); + } else { + // CREATE mode + + builder.setTitle(R.string.c_var_create_var); + } + + builder.create().show(); + } else { + Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); + } + }*/ + + @NotNull + @Override + protected MathEntityDescriptionGetter getDescriptionGetter() { + return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getFunctionsRegistry()); + } + + @NotNull + @Override + protected List getMathEntities() { + return new ArrayList(CalculatorEngine.instance.getFunctionsRegistry().getEntities()); + } + + @Override + protected String getMathEntityCategory(@NotNull Function function) { + return CalculatorEngine.instance.getFunctionsRegistry().getCategory(function); + } } diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsTabActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsTabActivity.java index ab2395f6..c3e4c37b 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsTabActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsTabActivity.java @@ -6,217 +6,34 @@ package org.solovyev.android.calculator.math.edit; -import android.app.Activity; -import android.content.Context; +import android.app.TabActivity; import android.os.Bundle; -import android.text.ClipboardManager; -import jscl.math.function.Function; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorModel; +import android.widget.TabHost; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.model.CalculatorEngine; -import org.solovyev.android.view.AMenuItem; -import org.solovyev.common.utils.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; /** * User: serso - * Date: 10/29/11 - * Time: 4:55 PM + * Date: 12/21/11 + * Time: 10:33 PM */ -public class CalculatorFunctionsTabActivity extends AbstractMathEntityListActivity { +public class CalculatorFunctionsTabActivity extends TabActivity { - private static enum LongClickMenuItem implements AMenuItem{ - use(R.string.c_use) { - @Override - public void doAction(@NotNull Function data, @NotNull Context context) { - CalculatorModel.instance.processDigitButtonAction(data.getName(), false); - if (context instanceof Activity) { - ((Activity) context).finish(); - } - } - }, + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - /*edit(R.string.c_edit) { - @Override - public void doAction(@NotNull Function data, @NotNull Context context) { - if (context instanceof AbstractMathEntityListActivity) { - } - } - },*/ + setContentView(R.layout.tabs); - copy_description(R.string.c_copy_description) { - @Override - public void doAction(@NotNull Function data, @NotNull Context context) { - final String text = CalculatorEngine.instance.getFunctionsRegistry().getDescription(context, data.getName()); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; + final TabHost tabHost = getTabHost(); - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } + for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { + AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorFunctionsActivity.class, null); + } - @NotNull - @Override - public String getCaption(@NotNull Context context) { - return context.getString(captionId); - } - } + AndroidUtils.centerAndWrapTabsFor(tabHost); + } - public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun"; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - /*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final Function function = (Function) parent.getItemAtPosition(position); - if (function instanceof CustomFunction) { - createEditVariableDialog(CalculatorFunctionsTabActivity.this, - ((CustomFunction) function), - function.getName(), - ((CustomFunction) function).getContent(), - ((CustomFunction) function).getParameterNames(), - null); - } - return true; - } - });*/ - - /*final Intent intent = getIntent(); - if (intent != null) { - final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING); - if (!StringUtils.isEmpty(varValue)) { - createEditVariableDialog(this, null, null, varValue, null, null); - - // in order to stop intent for other tabs - intent.removeExtra(CREATE_FUN_EXTRA_STRING); - } - }*/ - } - - @NotNull - @Override - protected List> getMenuItemsOnLongClick(@NotNull Function item) { - List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if ( StringUtils.isEmpty(CalculatorEngine.instance.getFunctionsRegistry().getDescription(this, item.getName())) ) { - result.remove(LongClickMenuItem.copy_description); - } - - return result; - } - -/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, - @Nullable final CustomFunction function, - @Nullable final String name, - @Nullable final String expression, - @Nullable final String[] parameterNames, - @Nullable final String description) { - if (function == null || !function.isSystem()) { - - final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); - final View editView = layoutInflater.inflate(R.layout.var_edit, null); - - final String errorMsg = activity.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(expression)) { - editValue.setText(expression); - } - - final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); - editDescription.setText(description); - - final CustomFunction.Builder functionBuilder; - if (function != null) { - functionBuilder = new CustomFunction.Builder(function); - } else { - functionBuilder = new CustomFunction.Builder(); - } - - final AlertDialog.Builder builder = new AlertDialog.Builder(activity) - .setCancelable(true) - .setNegativeButton(R.string.c_cancel, null) - .setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator() { - - @Override - public void showEditor(@NotNull AbstractMathEntityListActivity activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) { - createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description); - } - })) - .setView(editView); - - if (function != null) { - // EDIT mode - - builder.setTitle(R.string.c_var_edit_var); - builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(function, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - createEditVariableDialog(activity, function, name, expression, parameterNames, description); - } - }, CalculatorEngine.instance.getFunctionsRegistry(), activity)); - } else { - // CREATE mode - - builder.setTitle(R.string.c_var_create_var); - } - - builder.create().show(); - } else { - Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); - } - }*/ - - @NotNull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getFunctionsRegistry()); - } - - @NotNull - @Override - protected List getMathEntities() { - return new ArrayList(CalculatorEngine.instance.getFunctionsRegistry().getEntities()); - } - - @Override - protected String getMathEntityCategory(@NotNull Function function) { - return CalculatorEngine.instance.getFunctionsRegistry().getCategory(function); - } } diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index ad674e1a..8a0b7f8c 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -6,39 +6,280 @@ package org.solovyev.android.calculator.math.edit; -import android.app.TabActivity; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; import android.os.Bundle; -import android.widget.TabHost; +import android.text.ClipboardManager; +import android.text.Editable; +import android.text.TextWatcher; +import android.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.AndroidUtils; +import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.model.VarCategory; +import org.solovyev.android.calculator.math.MathType; +import org.solovyev.android.calculator.model.CalculatorEngine; +import org.solovyev.android.calculator.model.Var; +import org.solovyev.android.view.AMenuItem; +import org.solovyev.common.utils.CollectionsUtils; +import org.solovyev.common.utils.Finder; +import org.solovyev.common.utils.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * User: serso - * Date: 12/21/11 - * Time: 11:05 PM + * Date: 9/28/11 + * Time: 10:55 PM */ -public class CalculatorVarsActivity extends TabActivity { +public class CalculatorVarsActivity extends AbstractMathEntityListActivity { - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + private static enum LongClickMenuItem implements AMenuItem{ + use(R.string.c_use) { + @Override + public void doAction(@NotNull IConstant data, @NotNull Context context) { + CalculatorModel.instance.processDigitButtonAction(data.getName(), false); + if (context instanceof Activity) { + ((Activity) context).finish(); + } + } + }, - setContentView(R.layout.tabs); + edit(R.string.c_edit) { + @Override + public void doAction(@NotNull IConstant data, @NotNull Context context) { + if (context instanceof AbstractMathEntityListActivity) { + createEditVariableDialog((AbstractMathEntityListActivity)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription()); + } + } + }, - final TabHost tabHost = getTabHost(); + copy_value(R.string.c_copy_value) { + @Override + public void doAction(@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); + } + } + }, - for (VarCategory category : VarCategory.getCategoriesByTabOrder()) { - if (category == VarCategory.my) { - AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsTabActivity.class, getIntent()); - } else { - AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsTabActivity.class, null); - } - } + copy_description(R.string.c_copy_description) { + @Override + public void doAction(@NotNull IConstant data, @NotNull Context context) { + final String text = CalculatorEngine.instance.getVarsRegistry().getDescription(context, data.getName()); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }; + private final int captionId; - tabHost.setCurrentTab(0); - AndroidUtils.centerAndWrapTabsFor(tabHost); - } + LongClickMenuItem(int captionId) { + this.captionId = captionId; + } + + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } + + 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; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Intent intent = getIntent(); + if (intent != null) { + final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING); + if (!StringUtils.isEmpty(varValue)) { + createEditVariableDialog(this, null, null, varValue, null); + + // in order to stop intent for other tabs + intent.removeExtra(CREATE_VAR_EXTRA_STRING); + } + } + } + + @NotNull + @Override + protected List> getMenuItemsOnLongClick(@NotNull IConstant item) { + final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); + + if ( item.isSystem() ) { + result.remove(LongClickMenuItem.edit); + } + + if ( StringUtils.isEmpty(CalculatorEngine.instance.getVarsRegistry().getDescription(this, 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(CalculatorEngine.instance.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(CalculatorEngine.instance.getVarsRegistry().getEntities()); + + CollectionsUtils.removeAll(result, new Finder() { + @Override + public boolean isFound(@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 CalculatorEngine.instance.getVarsRegistry().getCategory(var); + } + + private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, + @Nullable final IConstant var, + @Nullable final String name, + @Nullable final String value, + @Nullable final String description) { + if (var == null || !var.isSystem()) { + + final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); + final View editView = layoutInflater.inflate(R.layout.var_edit, null); + + final String errorMsg = activity.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, activity, CalculatorEngine.instance.getVarsRegistry(), new VarEditorSaver.EditorCreator() { + @Override + public void showEditor(@NotNull AbstractMathEntityListActivity 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(activity, var, name, value, description); + } + }, CalculatorEngine.instance.getVarsRegistry(), activity)); + } else { + // CREATE mode + + builder.setTitle(R.string.c_var_create_var); + } + + builder.create().show(); + } else { + Toast.makeText(activity, activity.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; + } + + @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; + } } diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsTabActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsTabActivity.java index 3c70770e..7cfd20eb 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsTabActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsTabActivity.java @@ -6,280 +6,39 @@ 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.content.Intent; +import android.app.TabActivity; import android.os.Bundle; -import android.text.ClipboardManager; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.*; -import android.widget.EditText; -import android.widget.Toast; -import jscl.math.function.IConstant; -import org.jetbrains.annotations.NotNull; +import android.widget.TabHost; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.CalculatorModel; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.math.MathType; -import org.solovyev.android.calculator.model.CalculatorEngine; -import org.solovyev.android.calculator.model.Var; -import org.solovyev.android.view.AMenuItem; -import org.solovyev.common.utils.CollectionsUtils; -import org.solovyev.common.utils.Finder; -import org.solovyev.common.utils.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import org.solovyev.android.calculator.model.VarCategory; /** * User: serso - * Date: 9/28/11 - * Time: 10:55 PM + * Date: 12/21/11 + * Time: 11:05 PM */ -public class CalculatorVarsTabActivity extends AbstractMathEntityListActivity { +public class CalculatorVarsTabActivity extends TabActivity { - private static enum LongClickMenuItem implements AMenuItem{ - use(R.string.c_use) { - @Override - public void doAction(@NotNull IConstant data, @NotNull Context context) { - CalculatorModel.instance.processDigitButtonAction(data.getName(), false); - if (context instanceof Activity) { - ((Activity) context).finish(); - } - } - }, + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - edit(R.string.c_edit) { - @Override - public void doAction(@NotNull IConstant data, @NotNull Context context) { - if (context instanceof AbstractMathEntityListActivity) { - createEditVariableDialog((AbstractMathEntityListActivity)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription()); - } - } - }, + setContentView(R.layout.tabs); - copy_value(R.string.c_copy_value) { - @Override - public void doAction(@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); - } - } - }, + final TabHost tabHost = getTabHost(); - copy_description(R.string.c_copy_description) { - @Override - public void doAction(@NotNull IConstant data, @NotNull Context context) { - final String text = CalculatorEngine.instance.getVarsRegistry().getDescription(context, data.getName()); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; + for (VarCategory category : VarCategory.getCategoriesByTabOrder()) { + if (category == VarCategory.my) { + AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsActivity.class, getIntent()); + } else { + AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsActivity.class, null); + } + } - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @NotNull - @Override - public String getCaption(@NotNull Context context) { - return context.getString(captionId); - } - } - - 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; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Intent intent = getIntent(); - if (intent != null) { - final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING); - if (!StringUtils.isEmpty(varValue)) { - createEditVariableDialog(this, null, null, varValue, null); - - // in order to stop intent for other tabs - intent.removeExtra(CREATE_VAR_EXTRA_STRING); - } - } - } - - @NotNull - @Override - protected List> getMenuItemsOnLongClick(@NotNull IConstant item) { - final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if ( item.isSystem() ) { - result.remove(LongClickMenuItem.edit); - } - - if ( StringUtils.isEmpty(CalculatorEngine.instance.getVarsRegistry().getDescription(this, 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(CalculatorEngine.instance.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(CalculatorEngine.instance.getVarsRegistry().getEntities()); - - CollectionsUtils.removeAll(result, new Finder() { - @Override - public boolean isFound(@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 CalculatorEngine.instance.getVarsRegistry().getCategory(var); - } - - private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, - @Nullable final IConstant var, - @Nullable final String name, - @Nullable final String value, - @Nullable final String description) { - if (var == null || !var.isSystem()) { - - final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); - final View editView = layoutInflater.inflate(R.layout.var_edit, null); - - final String errorMsg = activity.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, activity, CalculatorEngine.instance.getVarsRegistry(), new VarEditorSaver.EditorCreator() { - @Override - public void showEditor(@NotNull AbstractMathEntityListActivity 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(activity, var, name, value, description); - } - }, CalculatorEngine.instance.getVarsRegistry(), activity)); - } else { - // CREATE mode - - builder.setTitle(R.string.c_var_create_var); - } - - builder.create().show(); - } else { - Toast.makeText(activity, activity.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; - } - - @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; - } + tabHost.setCurrentTab(0); + AndroidUtils.centerAndWrapTabsFor(tabHost); + } } diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/FunctionEditorSaver.java b/src/main/java/org/solovyev/android/calculator/math/edit/FunctionEditorSaver.java index d3cc8c7b..04d283fd 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/FunctionEditorSaver.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/FunctionEditorSaver.java @@ -112,7 +112,7 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{ error = null; } else { // value is not empty => must be a number - boolean valid = CalculatorVarsTabActivity.isValidValue(value); + boolean valid = CalculatorVarsActivity.isValidValue(value); if (valid) { varBuilder.setName(name); diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java b/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java index c978c2a4..1c608c8d 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java @@ -109,7 +109,7 @@ class VarEditorSaver implements DialogInterface.OnClickLis error = null; } else { // value is not empty => must be a number - boolean valid = CalculatorVarsTabActivity.isValidValue(value); + boolean valid = CalculatorVarsActivity.isValidValue(value); if (valid) { varBuilder.setName(name);