From 83dce53d15136276cadd62244c2a79a4a9d824ec Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Mon, 24 Jun 2013 15:50:27 +0400 Subject: [PATCH] on screen calculator wizard step --- android-app-core/res/values/text_wizard.xml | 1 + ....xml => cpp_wizard_step_choose_layout.xml} | 0 .../res/layout/cpp_wizard_step_onscreen.xml | 19 ++++ android-app/res/xml/preferences_other.xml | 4 + .../CalculatorPreferencesActivity.java | 16 +++- .../wizard/ChooseLayoutWizardStep.java | 4 +- .../wizard/OnScreenCalculatorWizardStep.java | 90 +++++++++++++++++++ .../android/calculator/wizard/WizardStep.java | 29 ++++++ .../OnScreenCalculatorWizardStepTest.java | 77 ++++++++++++++++ 9 files changed, 235 insertions(+), 5 deletions(-) rename android-app/res/layout/{cpp_wizard_step_tablet.xml => cpp_wizard_step_choose_layout.xml} (100%) create mode 100644 android-app/res/layout/cpp_wizard_step_onscreen.xml create mode 100644 android-app/src/main/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStep.java create mode 100644 android-app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java diff --git a/android-app-core/res/values/text_wizard.xml b/android-app-core/res/values/text_wizard.xml index 25aedd57..506e597e 100644 --- a/android-app-core/res/values/text_wizard.xml +++ b/android-app-core/res/values/text_wizard.xml @@ -15,4 +15,5 @@ Engineer In engineer mode special functions will be used on the main screen. Result is is not rounded and is presented in engineer notation. + Start wizard \ No newline at end of file diff --git a/android-app/res/layout/cpp_wizard_step_tablet.xml b/android-app/res/layout/cpp_wizard_step_choose_layout.xml similarity index 100% rename from android-app/res/layout/cpp_wizard_step_tablet.xml rename to android-app/res/layout/cpp_wizard_step_choose_layout.xml diff --git a/android-app/res/layout/cpp_wizard_step_onscreen.xml b/android-app/res/layout/cpp_wizard_step_onscreen.xml new file mode 100644 index 00000000..85933dcd --- /dev/null +++ b/android-app/res/layout/cpp_wizard_step_onscreen.xml @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/android-app/res/xml/preferences_other.xml b/android-app/res/xml/preferences_other.xml index 68c88d4c..b7b58a52 100644 --- a/android-app/res/xml/preferences_other.xml +++ b/android-app/res/xml/preferences_other.xml @@ -23,6 +23,10 @@ a:summary="@string/c_clear_billing_info_summary" a:title="@string/c_clear_billing_info_title" /> + + \ No newline at end of file diff --git a/android-app/src/main/java/org/solovyev/android/calculator/preferences/CalculatorPreferencesActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/preferences/CalculatorPreferencesActivity.java index ec5f13bc..4baf63ed 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/preferences/CalculatorPreferencesActivity.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/preferences/CalculatorPreferencesActivity.java @@ -26,6 +26,8 @@ import org.solovyev.android.App; import org.solovyev.android.ads.AdsController; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; +import org.solovyev.android.calculator.wizard.CalculatorWizardActivity; +import org.solovyev.android.calculator.wizard.Wizard; import org.solovyev.android.msg.AndroidMessage; import org.solovyev.android.view.VibratorContainer; import org.solovyev.common.msg.Message; @@ -38,7 +40,8 @@ import org.solovyev.common.msg.MessageType; */ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, IBillingObserver { - public static final String CLEAR_BILLING_INFO = "clear_billing_info"; + public static final String PREFERENCE_CLEAR_BILLING_INFO = "clear_billing_info"; + public static final String PREFERENCE_RESTART_WIZARD = "restart_wizard"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -53,6 +56,15 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im addPreferencesFromResource(R.xml.preferences_other); addPreferencesFromResource(R.xml.preferences_onscreen); + final Preference restartWizardPreference = findPreference(PREFERENCE_RESTART_WIZARD); + restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + CalculatorWizardActivity.startWizard(Wizard.DEFAULT_WIZARD_FLOW, CalculatorPreferencesActivity.this); + return true; + } + }); + final Preference adFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY); adFreePreference.setEnabled(false); @@ -66,7 +78,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im onSharedPreferenceChanged(preferences, AndroidCalculatorEngine.Preferences.roundResult.getKey()); onSharedPreferenceChanged(preferences, VibratorContainer.Preferences.hapticFeedbackEnabled.getKey()); - final Preference clearBillingInfoPreference = findPreference(CLEAR_BILLING_INFO); + final Preference clearBillingInfoPreference = findPreference(PREFERENCE_CLEAR_BILLING_INFO); if (clearBillingInfoPreference != null) { clearBillingInfoPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseLayoutWizardStep.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseLayoutWizardStep.java index 091a2ef8..cbc9d6e5 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseLayoutWizardStep.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseLayoutWizardStep.java @@ -1,12 +1,10 @@ package org.solovyev.android.calculator.wizard; -import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RadioButton; -import android.widget.TextView; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -67,7 +65,7 @@ public class ChooseLayoutWizardStep extends SherlockFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.cpp_wizard_step_tablet, null); + return inflater.inflate(R.layout.cpp_wizard_step_choose_layout, null); } @Override diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStep.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStep.java new file mode 100644 index 00000000..b7a6e3b1 --- /dev/null +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStep.java @@ -0,0 +1,90 @@ +package org.solovyev.android.calculator.wizard; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; + +import javax.annotation.Nullable; + +import org.solovyev.android.calculator.CalculatorPreferences; +import org.solovyev.android.calculator.R; + +import com.actionbarsherlock.app.SherlockFragment; + +public class OnScreenCalculatorWizardStep extends SherlockFragment { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + static final String ONSCREEN_CALCULATOR_ENABLED = "onscreen_calculator_enabled"; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + @Nullable + private CheckBox onscreenCalculatorCheckbox; + + private Boolean onscreenCalculatorEnabled; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if(savedInstanceState != null && savedInstanceState.containsKey(ONSCREEN_CALCULATOR_ENABLED)) { + onscreenCalculatorEnabled = savedInstanceState.getBoolean(ONSCREEN_CALCULATOR_ENABLED); + } + + if (onscreenCalculatorEnabled == null) { + onscreenCalculatorEnabled = getArguments().getBoolean(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getDefaultValue()); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.cpp_wizard_step_onscreen, null); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + onscreenCalculatorCheckbox = (CheckBox) root.findViewById(R.id.wizard_onscreen_app_enabled_checkbox); + onscreenCalculatorCheckbox.setChecked(onscreenCalculatorEnabled); + } + + public Boolean isOnscreenCalculatorEnabled() { + boolean enabled = CalculatorPreferences.OnscreenCalculator.showAppIcon.getDefaultValue(); + + if (onscreenCalculatorCheckbox != null) { + enabled = onscreenCalculatorCheckbox.isChecked(); + } + + return enabled; + } + + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + outState.putBoolean(ONSCREEN_CALCULATOR_ENABLED, onscreenCalculatorEnabled); + } + + @Nullable + CheckBox getOnscreenCalculatorCheckbox() { + return onscreenCalculatorCheckbox; + } +} + diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java index 3a36debf..1c136dfb 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java @@ -17,6 +17,7 @@ import javax.annotation.Nullable; import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE; import static org.solovyev.android.calculator.wizard.ChooseLayoutWizardStep.LAYOUT; +import static org.solovyev.android.calculator.wizard.OnScreenCalculatorWizardStep.ONSCREEN_CALCULATOR_ENABLED; /** * User: serso @@ -104,6 +105,34 @@ enum WizardStep { bundle.putSerializable(MODE, CalculatorMode.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(preferences))); return bundle; } + }, + + on_screen_calculator(OnScreenCalculatorWizardStep.class) { + @Override + boolean onNext(@Nonnull Fragment f) { + final OnScreenCalculatorWizardStep fragment = (OnScreenCalculatorWizardStep) f; + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity()); + + CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(preferences, fragment.isOnscreenCalculatorEnabled()); + + return true; + } + + @Override + boolean onPrev(@Nonnull Fragment fragment) { + return true; + } + + @Nullable + @Override + Bundle getFragmentArgs() { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance()); + + final Bundle bundle = new Bundle(); + bundle.putSerializable(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getPreference(preferences)); + return bundle; + } }; @Nonnull diff --git a/android-app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java b/android-app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java new file mode 100644 index 00000000..e6a81689 --- /dev/null +++ b/android-app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java @@ -0,0 +1,77 @@ +package org.solovyev.android.calculator.wizard; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import javax.annotation.Nonnull; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.util.ActivityController; +import org.solovyev.android.calculator.CalculatorPreferences; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricTestRunner.class) +public class OnScreenCalculatorWizardStepTest { + + @Nonnull + private OnScreenCalculatorWizardStep fragment; + + @Nonnull + private CalculatorWizardActivity activity; + + @Nonnull + private ActivityController controller; + + @Before + public void setUp() throws Exception { + createActivity(); + setFragment(); + } + + private void createActivity() { + controller = Robolectric.buildActivity(CalculatorWizardActivity.class).create().start().resume(); + activity = controller.get(); + } + + private void setFragment() { + activity.setStep(WizardStep.on_screen_calculator); + activity.getSupportFragmentManager().executePendingTransactions(); + fragment = (OnScreenCalculatorWizardStep) activity.getSupportFragmentManager().findFragmentByTag(WizardStep.on_screen_calculator.getFragmentTag()); + } + + @Test + public void testShouldRestoreStateOnRestart() throws Exception { + fragment.getOnscreenCalculatorCheckbox().setChecked(true); + controller.restart(); + assertTrue(fragment.getOnscreenCalculatorCheckbox().isChecked()); + + fragment.getOnscreenCalculatorCheckbox().setChecked(false); + controller.restart(); + assertFalse(fragment.getOnscreenCalculatorCheckbox().isChecked()); + } + + @Test + public void testShouldBeEnabledIfIconIsShown() throws Exception { + testShouldBeEqualsToIconState(true); + } + + @Test + public void testShouldBeDisabledIfIconIsNotShown() throws Exception { + testShouldBeEqualsToIconState(false); + } + + private void testShouldBeEqualsToIconState(boolean iconEnabled) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Robolectric.application); + CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(preferences, iconEnabled); + createActivity(); + setFragment(); + assertEquals(iconEnabled, fragment.isOnscreenCalculatorEnabled()); + } +}