diff --git a/android-app/src/main/AndroidManifest.xml b/android-app/src/main/AndroidManifest.xml index 4a81e4f0..f7bfe50c 100644 --- a/android-app/src/main/AndroidManifest.xml +++ b/android-app/src/main/AndroidManifest.xml @@ -45,7 +45,7 @@ - + @@ -87,8 +87,6 @@ - - diff --git a/android-app/src/main/java/org/solovyev/android/calculator/Preferences.java b/android-app/src/main/java/org/solovyev/android/calculator/Preferences.java index e6090643..3632c58f 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -33,7 +33,6 @@ import jscl.AngleUnit; import jscl.NumeralBase; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; -import org.solovyev.android.calculator.preferences.BasePreferencesActivity; import org.solovyev.android.calculator.preferences.PurchaseDialogActivity; import org.solovyev.android.calculator.wizard.WizardActivity; import org.solovyev.android.prefs.*; @@ -48,11 +47,6 @@ import static org.solovyev.android.Android.isPhoneModel; import static org.solovyev.android.DeviceModel.samsung_galaxy_s; import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2; -/** - * User: serso - * Date: 4/20/12 - * Time: 12:42 PM - */ public final class Preferences { private Preferences() { @@ -115,24 +109,22 @@ public final class Preferences { metro_purple_theme(R.style.cpp_metro_purple_theme), metro_green_theme(R.style.cpp_metro_green_theme), material_theme(R.style.Cpp_Theme_Material), - material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Settings_Light, R.style.Cpp_Theme_Dialog_Material_Light), + material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Dialog_Material_Light), ; private static final SparseArray textColors = new SparseArray<>(); private final int themeId; private final int wizardThemeId; - private final int settingsThemeId; private final int dialogThemeId; Theme(@StyleRes int themeId) { - this(themeId, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Settings, R.style.Cpp_Theme_Dialog_Material); + this(themeId, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Dialog_Material); } - Theme(@StyleRes int themeId, @StyleRes int wizardThemeId, @StyleRes int settingsThemeId, int dialogThemeId) { + Theme(@StyleRes int themeId, @StyleRes int wizardThemeId, int dialogThemeId) { this.themeId = themeId; this.wizardThemeId = wizardThemeId; - this.settingsThemeId = settingsThemeId; this.dialogThemeId = dialogThemeId; } @@ -144,9 +136,6 @@ public final class Preferences { if (context instanceof WizardActivity) { return wizardThemeId; } - if (context instanceof BasePreferencesActivity) { - return settingsThemeId; - } if (context instanceof PurchaseDialogActivity) { return dialogThemeId; } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java index cce737de..3f0e462e 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java @@ -42,6 +42,7 @@ import org.solovyev.android.Android; import org.solovyev.android.Threads; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.preferences.PreferencesActivity; import org.solovyev.android.menu.*; import org.solovyev.common.JPredicate; import org.solovyev.common.msg.MessageType; @@ -411,7 +412,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment preferences(R.id.menu_plot_settings) { @Override public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - context.startActivity(new Intent(context, PlotPreferenceActivity.class)); + PreferencesActivity.start(context, R.xml.preferences_plot, R.string.prefs_graph_screen_title); } }; diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/PlotPreferenceActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/PlotPreferenceActivity.java deleted file mode 100644 index f72b94fb..00000000 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/PlotPreferenceActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.plot; - -import android.os.Bundle; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.preferences.BasePreferencesActivity; - -public class PlotPreferenceActivity extends BasePreferencesActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - //noinspection deprecation - addPreferencesFromResource(R.xml.preferences_plot); - } -} diff --git a/android-app/src/main/java/org/solovyev/android/calculator/preferences/BasePreferencesActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/preferences/BasePreferencesActivity.java deleted file mode 100644 index bbba8ac7..00000000 --- a/android-app/src/main/java/org/solovyev/android/calculator/preferences/BasePreferencesActivity.java +++ /dev/null @@ -1,185 +0,0 @@ -package org.solovyev.android.calculator.preferences; - -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Build; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import android.support.v7.widget.Toolbar; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.ListView; -import org.solovyev.android.Activities; -import org.solovyev.android.Views; -import org.solovyev.android.calculator.*; -import org.solovyev.android.checkout.ActivityCheckout; -import org.solovyev.android.checkout.Checkout; -import org.solovyev.android.checkout.Inventory; -import org.solovyev.android.checkout.ProductTypes; - -import javax.annotation.Nonnull; - -public abstract class BasePreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { - - private static boolean SUPPORT_HEADERS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; - private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts()); - private Inventory inventory; - private AdView adView; - private Toolbar actionBar; - private Preferences.Gui.Theme theme; - private boolean paused = true; - - @Override - protected void onCreate(Bundle savedInstanceState) { - final SharedPreferences preferences = App.getPreferences(); - preferences.registerOnSharedPreferenceChangeListener(this); - theme = Preferences.Gui.getTheme(preferences); - setTheme(theme.getThemeId(this)); - - super.onCreate(savedInstanceState); - - actionBar.setTitle(getTitle()); - - checkout.start(); - inventory = checkout.loadInventory(); - } - - @Override - public void setContentView(int layout) { - final LayoutInflater inflater = LayoutInflater.from(this); - final ViewGroup contentView = (ViewGroup) inflater.inflate(R.layout.cpp_activity_settings, new LinearLayout(this), true); - - actionBar = (Toolbar) contentView.findViewById(R.id.action_bar); - actionBar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - finish(); - } - }); - - final ViewGroup contentWrapper = (ViewGroup) contentView.findViewById(R.id.content_wrapper); - inflater.inflate(layout, contentWrapper, true); - - // let's fix padding for parent view of list view - Views.processViewsOfType(contentWrapper, ViewGroup.class, new Views.ViewProcessor() { - @Override - public void process(@Nonnull ViewGroup view) { - for (int i = 0; i < view.getChildCount(); i++) { - final View child = view.getChildAt(i); - if (child.getId() == android.R.id.list) { - view.setPadding(0, 0, 0, 0); - } - } - } - }); - - getWindow().setContentView(contentView); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (!paused && Preferences.Gui.theme.isSameKey(key)) { - ActivityUi.restartIfThemeChanged(this, theme); - } - } - - private class InventoryListener implements Inventory.Listener { - @Override - public void onLoaded(@Nonnull Inventory.Products products) { - final Inventory.Product product = products.get(ProductTypes.IN_APP); - final boolean adFree = product.isPurchased("ad_free"); - onShowAd(!adFree); - } - } - - protected void onShowAd(boolean show) { - if (!supportsHeaders()) { - return; - } - - final ListView listView = getListView(); - if (show) { - if (adView != null) { - return; - } - adView = (AdView) LayoutInflater.from(this).inflate(R.layout.ad, null); - adView.show(); - try { - listView.addHeaderView(adView); - } catch (IllegalStateException e) { - // doesn't support header views - SUPPORT_HEADERS = false; - adView.hide(); - adView = null; - } - } else { - if (adView == null) { - return; - } - listView.removeHeaderView(adView); - adView.hide(); - adView = null; - } - } - - private boolean supportsHeaders() { - return SUPPORT_HEADERS; - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (checkout.onActivityResult(requestCode, resultCode, data)) { - return; - } - - super.onActivityResult(requestCode, resultCode, data); - } - - @Override - protected void onResume() { - super.onResume(); - paused = false; - if (adView != null) { - adView.resume(); - } - inventory.whenLoaded(new InventoryListener()); - - final Preferences.Gui.Theme newTheme = Preferences.Gui.theme.getPreference(App.getPreferences()); - if (!theme.equals(newTheme)) { - Activities.restartActivity(this); - } - } - - @Override - protected void onStart() { - super.onStart(); - ActivityUi.reportActivityStart(this); - } - - @Override - protected void onStop() { - ActivityUi.reportActivityStop(this); - super.onStop(); - } - - @Override - protected void onPause() { - if (adView != null) { - adView.pause(); - } - paused = true; - super.onPause(); - } - - @Override - protected void onDestroy() { - if (adView != null) { - adView.destroy(); - } - checkout.stop(); - App.getPreferences().unregisterOnSharedPreferenceChangeListener(this); - super.onDestroy(); - } -} diff --git a/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java index 3d89b413..84d3fbab 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java @@ -1,51 +1,33 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - package org.solovyev.android.calculator.preferences; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.Preference; +import android.support.annotation.StringRes; +import android.support.annotation.XmlRes; +import android.text.TextUtils; import android.util.SparseArray; + +import org.solovyev.android.calculator.ActivityUi; import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorApplication; +import org.solovyev.android.calculator.BaseActivity; +import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; +import org.solovyev.android.checkout.ActivityCheckout; +import org.solovyev.android.checkout.Checkout; import javax.annotation.Nonnull; -import static org.solovyev.android.calculator.CalculatorApplication.AD_FREE_P_KEY; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.precision; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.roundResult; -import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; -import static org.solovyev.android.view.VibratorContainer.Preferences.hapticFeedbackDuration; -import static org.solovyev.android.view.VibratorContainer.Preferences.hapticFeedbackEnabled; -import static org.solovyev.android.wizard.WizardUi.startWizard; +import static android.support.v7.app.ActionBar.NAVIGATION_MODE_STANDARD; -@SuppressWarnings("deprecation") -public class PreferencesActivity extends BasePreferencesActivity { +public class PreferencesActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener { + + static final String EXTRA_PREFERENCE = "preference"; + static final String EXTRA_PREFERENCE_TITLE = "preference-title"; @Nonnull - private static final SparseArray preferences = new SparseArray(); + private static final SparseArray preferences = new SparseArray<>(); static { preferences.append(R.xml.preferences, "screen-main"); @@ -56,90 +38,86 @@ public class PreferencesActivity extends BasePreferencesActivity { preferences.append(R.xml.preferences_onscreen, "screen-onscreen"); } - private Preference adFreePreference; + @Nonnull + private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts()); - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); + private boolean paused = true; + + public PreferencesActivity() { + super(R.layout.main_empty); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + App.getPreferences().registerOnSharedPreferenceChangeListener(this); final Intent intent = getIntent(); - final int preference = intent.getIntExtra("preference", R.xml.preferences); - final String title = intent.getStringExtra("preference-title"); - setPreference(preference, preferences.get(preference)); - if (preference == R.xml.preferences) { - for (int i = 0; i < preferences.size(); i++) { - final int xml = preferences.keyAt(i); - final String name = preferences.valueAt(i); - setPreferenceIntent(xml, name); - } - final Preference restartWizardPreference = findPreference("restart_wizard"); - restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startWizard(CalculatorApplication.getInstance().getWizards(), DEFAULT_WIZARD_FLOW, PreferencesActivity.this); - return true; - } - }); - - adFreePreference = findPreference(AD_FREE_P_KEY); - adFreePreference.setEnabled(false); - adFreePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(PreferencesActivity.this, PurchaseDialogActivity.class)); - return true; - } - }); - } - if (title != null) { - setTitle(title); + final String preferenceTitle = intent.getStringExtra(EXTRA_PREFERENCE_TITLE); + if (!TextUtils.isEmpty(preferenceTitle)) { + setTitle(preferenceTitle); } - final SharedPreferences preferences = App.getPreferences(); - onSharedPreferenceChanged(preferences, roundResult.getKey()); - onSharedPreferenceChanged(preferences, hapticFeedbackEnabled.getKey()); + if (savedInstanceState == null) { + final int preference = intent.getIntExtra(EXTRA_PREFERENCE, R.xml.preferences); + getSupportFragmentManager().beginTransaction() + .add(R.id.main_layout, PreferencesFragment.create(preference, R.layout.fragment_preferences)) + .commit(); + } + + getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD); + + checkout.start(); } - private void setPreference(int xml, @Nonnull String name) { - addPreferencesFromResource(xml); - } - - private void setPreferenceIntent(int xml, @Nonnull String name) { - final Preference preference = findPreference(name); - if (preference != null) { - final Intent intent = new Intent(getApplicationContext(), PreferencesActivity.class); - intent.putExtra("preference", xml); - intent.putExtra("preference-title", preference.getTitle()); - preference.setIntent(intent); + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (!paused && Preferences.Gui.theme.isSameKey(key)) { + ActivityUi.restartIfThemeChanged(this, ui.getTheme()); } } @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - super.onSharedPreferenceChanged(preferences, key); - if (roundResult.getKey().equals(key)) { - final Preference preference = findPreference(precision.getKey()); - if (preference != null) { - preference.setEnabled(preferences.getBoolean(key, roundResult.getDefaultValue())); - } - } else if (hapticFeedbackEnabled.getKey().equals(key)) { - final Preference preference = findPreference(hapticFeedbackDuration.getKey()); - if (preference != null) { - preference.setEnabled(hapticFeedbackEnabled.getPreference(preferences)); - } - } + protected void onResume() { + super.onResume(); + paused = false; } @Override - protected void onShowAd(boolean show) { - super.onShowAd(show); - if (adFreePreference != null) { - adFreePreference.setEnabled(show); + protected void onPause() { + paused = true; + super.onPause(); + } + + @Override + protected void onDestroy() { + checkout.stop(); + App.getPreferences().unregisterOnSharedPreferenceChangeListener(this); + super.onDestroy(); + } + + @Nonnull + static SparseArray getPreferences() { + return preferences; + } + + public static void start(@Nonnull Context context, @XmlRes int preference, @StringRes int title) { + final Intent intent = makeIntent(context, preference, title); + context.startActivity(intent); + } + + @Nonnull + static Intent makeIntent(@Nonnull Context context, int preference, int title) { + final Intent intent = new Intent(context, PreferencesActivity.class); + intent.putExtra(EXTRA_PREFERENCE, preference); + if (title != 0) { + intent.putExtra(EXTRA_PREFERENCE_TITLE, context.getString(title)); } + return intent; + } + + @Nonnull + ActivityCheckout getCheckout() { + return checkout; } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java b/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java new file mode 100644 index 00000000..8348ff6b --- /dev/null +++ b/android-app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java @@ -0,0 +1,187 @@ +package org.solovyev.android.calculator.preferences; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; +import android.preference.Preference; +import android.util.SparseArray; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ListView; + +import org.solovyev.android.calculator.AdView; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.CalculatorApplication; +import org.solovyev.android.calculator.R; +import org.solovyev.android.checkout.BillingRequests; +import org.solovyev.android.checkout.Checkout; +import org.solovyev.android.checkout.ProductTypes; +import org.solovyev.android.checkout.RequestListener; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.precision; +import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.roundResult; +import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; +import static org.solovyev.android.view.VibratorContainer.Preferences.hapticFeedbackDuration; +import static org.solovyev.android.view.VibratorContainer.Preferences.hapticFeedbackEnabled; +import static org.solovyev.android.wizard.WizardUi.startWizard; + +public class PreferencesFragment extends org.solovyev.android.material.preferences.PreferencesFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + + private static boolean SUPPORT_HEADERS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; + + @Nonnull + public static PreferencesFragment create(int preferencesResId, int layoutResId) { + final PreferencesFragment fragment = new PreferencesFragment(); + fragment.setArguments(createArguments(preferencesResId, layoutResId, NO_THEME)); + return fragment; + } + + @Nullable + private Preference buyPremiumPreference; + + @Nullable + private AdView adView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + App.getPreferences().registerOnSharedPreferenceChangeListener(this); + } + + private void setPreferenceIntent(int xml, @Nonnull String name) { + final Preference preference = findPreference(name); + if (preference != null) { + final Intent intent = new Intent(getActivity(), PreferencesActivity.class); + intent.putExtra(PreferencesActivity.EXTRA_PREFERENCE, xml); + intent.putExtra(PreferencesActivity.EXTRA_PREFERENCE_TITLE, preference.getTitle()); + preference.setIntent(intent); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + final int preference = getPreferencesResId(); + if (preference == R.xml.preferences) { + final SparseArray preferences = PreferencesActivity.getPreferences(); + for (int i = 0; i < preferences.size(); i++) { + final int xml = preferences.keyAt(i); + final String name = preferences.valueAt(i); + setPreferenceIntent(xml, name); + } + final Preference restartWizardPreference = findPreference("restart_wizard"); + restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startWizard(CalculatorApplication.getInstance().getWizards(), DEFAULT_WIZARD_FLOW, getActivity()); + return true; + } + }); + + buyPremiumPreference = findPreference("buy_premium"); + if (buyPremiumPreference != null) { + buyPremiumPreference.setEnabled(false); + buyPremiumPreference.setSelectable(false); + buyPremiumPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivity(new Intent(getActivity(), PurchaseDialogActivity.class)); + return true; + } + }); + } + } + + getCheckout().whenReady(new Checkout.ListenerAdapter() { + @Override + public void onReady(@Nonnull BillingRequests requests) { + requests.isPurchased(ProductTypes.IN_APP, CalculatorApplication.AD_FREE_PRODUCT_ID, new RequestListener() { + @Override + public void onSuccess(@Nonnull Boolean purchased) { + if (buyPremiumPreference != null) { + buyPremiumPreference.setEnabled(!purchased); + buyPremiumPreference.setSelectable(!purchased); + } + onShowAd(!purchased); + } + + @Override + public void onError(int i, @Nonnull Exception e) { + onShowAd(false); + } + }); + } + }); + + final SharedPreferences preferences = App.getPreferences(); + onSharedPreferenceChanged(preferences, roundResult.getKey()); + onSharedPreferenceChanged(preferences, hapticFeedbackEnabled.getKey()); + } + + @Nonnull + private Checkout getCheckout() { + return ((PreferencesActivity) getActivity()).getCheckout(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (roundResult.getKey().equals(key)) { + final Preference preference = findPreference(precision.getKey()); + if (preference != null) { + preference.setEnabled(preferences.getBoolean(key, roundResult.getDefaultValue())); + } + } else if (hapticFeedbackEnabled.getKey().equals(key)) { + final Preference preference = findPreference(hapticFeedbackDuration.getKey()); + if (preference != null) { + preference.setEnabled(hapticFeedbackEnabled.getPreference(preferences)); + } + } + } + + @Override + public void onDestroy() { + App.getPreferences().unregisterOnSharedPreferenceChangeListener(this); + super.onDestroy(); + } + + private boolean supportsHeaders() { + return SUPPORT_HEADERS; + } + + protected void onShowAd(boolean show) { + if (!supportsHeaders()) { + return; + } + + final ListView listView = getListView(); + if (show) { + if (adView != null) { + return; + } + adView = (AdView) LayoutInflater.from(getActivity()).inflate(R.layout.ad, null); + adView.show(); + try { + listView.addHeaderView(adView); + } catch (IllegalStateException e) { + // doesn't support header views + SUPPORT_HEADERS = false; + adView.hide(); + adView = null; + } + } else { + if (adView == null) { + return; + } + listView.removeHeaderView(adView); + adView.hide(); + adView = null; + } + } + +} diff --git a/android-app/src/main/res/layout/cpp_activity_settings.xml b/android-app/src/main/res/layout/cpp_activity_settings.xml deleted file mode 100644 index 500591c2..00000000 --- a/android-app/src/main/res/layout/cpp_activity_settings.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/android-app/src/main/res/layout/fragment_preferences.xml b/android-app/src/main/res/layout/fragment_preferences.xml new file mode 100644 index 00000000..e5a642ca --- /dev/null +++ b/android-app/src/main/res/layout/fragment_preferences.xml @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/android-app/src/main/res/values/attributes.xml b/android-app/src/main/res/values/attributes.xml index 4375e397..4830b914 100644 --- a/android-app/src/main/res/values/attributes.xml +++ b/android-app/src/main/res/values/attributes.xml @@ -30,7 +30,6 @@ - diff --git a/android-app/src/main/res/values/preferences.xml b/android-app/src/main/res/values/preferences.xml index cb7f5bd9..02a14348 100644 --- a/android-app/src/main/res/values/preferences.xml +++ b/android-app/src/main/res/values/preferences.xml @@ -88,7 +88,6 @@ " " - org.solovyev.android.calculator_ad_free false \ No newline at end of file diff --git a/android-app/src/main/res/values/styles.xml b/android-app/src/main/res/values/styles.xml index b4647bf4..c6235d9e 100644 --- a/android-app/src/main/res/values/styles.xml +++ b/android-app/src/main/res/values/styles.xml @@ -293,25 +293,4 @@ @color/cpp_material_actionbar_light - - - - \ No newline at end of file diff --git a/android-app/src/main/res/values/theme.xml b/android-app/src/main/res/values/theme.xml index 6d0a5aa1..76ade755 100644 --- a/android-app/src/main/res/values/theme.xml +++ b/android-app/src/main/res/values/theme.xml @@ -114,27 +114,4 @@ @color/cpp_wizard_button_selector_light - - - - - - - - - \ No newline at end of file diff --git a/android-app/src/main/res/xml/preferences.xml b/android-app/src/main/res/xml/preferences.xml index 8cdb40f8..518cf49b 100644 --- a/android-app/src/main/res/xml/preferences.xml +++ b/android-app/src/main/res/xml/preferences.xml @@ -26,7 +26,7 @@ a:key="screen-main">