Fix preferences
This commit is contained in:
parent
e917e7f955
commit
b7d5ea03ce
@ -22,15 +22,6 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import static android.os.Build.VERSION_CODES.GINGERBREAD_MR1;
|
||||
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
|
||||
import static org.solovyev.android.calculator.Preferences.Gui.preventScreenFromFading;
|
||||
import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes;
|
||||
import static org.solovyev.android.wizard.WizardUi.continueWizard;
|
||||
import static org.solovyev.android.wizard.WizardUi.createLaunchIntent;
|
||||
import static org.solovyev.android.wizard.WizardUi.startWizard;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
@ -41,13 +32,10 @@ import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.view.Window;
|
||||
import android.view.*;
|
||||
import android.widget.TextView;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import org.solovyev.android.Activities;
|
||||
import org.solovyev.android.Android;
|
||||
import org.solovyev.android.calculator.history.History;
|
||||
@ -59,13 +47,17 @@ import org.solovyev.android.wizard.Wizard;
|
||||
import org.solovyev.android.wizard.Wizards;
|
||||
import org.solovyev.common.Objects;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.os.Build.VERSION_CODES.GINGERBREAD_MR1;
|
||||
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
|
||||
import static org.solovyev.android.calculator.Preferences.Gui.preventScreenFromFading;
|
||||
import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes;
|
||||
import static org.solovyev.android.wizard.WizardUi.*;
|
||||
|
||||
public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Nonnull
|
||||
@ -92,16 +84,16 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
|
||||
}
|
||||
|
||||
private static void firstTimeInit(@Nonnull SharedPreferences preferences, @Nonnull Context context) {
|
||||
final SharedPreferences.Editor editor = preferences.edit();
|
||||
final Integer appOpenedCounter = Preferences.appOpenedCounter.getPreference(preferences);
|
||||
if (appOpenedCounter != null) {
|
||||
Preferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1);
|
||||
}
|
||||
Preferences.appOpenedCounter.putPreference(editor, appOpenedCounter == null ? 1 : appOpenedCounter + 1);
|
||||
|
||||
final Integer savedVersion = Preferences.appVersion.getPreference(preferences);
|
||||
|
||||
final int appVersion = Android.getAppVersionCode(context);
|
||||
|
||||
Preferences.appVersion.putPreference(preferences, appVersion);
|
||||
Preferences.appVersion.putPreference(editor, appVersion);
|
||||
editor.apply();
|
||||
|
||||
if (!App.isMonkeyRunner(context)) {
|
||||
|
||||
|
@ -175,7 +175,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor);
|
||||
migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor);
|
||||
}
|
||||
Preferences.version.putDefault(preferences);
|
||||
Preferences.version.putDefault(editor);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
|
@ -60,17 +60,19 @@ public final class Preferences {
|
||||
}
|
||||
|
||||
static void setDefaultValues(@Nonnull SharedPreferences preferences) {
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
// renew value after each application start
|
||||
Gui.showFixableErrorDialog.putDefault(preferences);
|
||||
Gui.lastPreferredPreferencesCheck.putDefault(preferences);
|
||||
Gui.showFixableErrorDialog.putDefault(editor);
|
||||
Gui.lastPreferredPreferencesCheck.putDefault(editor);
|
||||
|
||||
final Integer version = Preferences.appVersion.getPreference(preferences);
|
||||
if (version == null) {
|
||||
setInitialDefaultValues(preferences);
|
||||
setInitialDefaultValues(preferences, editor);
|
||||
}
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private static void setInitialDefaultValues(@Nonnull SharedPreferences preferences) {
|
||||
private static void setInitialDefaultValues(@Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) {
|
||||
if (!Engine.Preferences.groupingSeparator.isSet(preferences)) {
|
||||
final Locale locale = Locale.getDefault();
|
||||
if (locale != null) {
|
||||
@ -83,44 +85,44 @@ public final class Preferences {
|
||||
groupingSeparator = " ";
|
||||
}
|
||||
|
||||
Engine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator);
|
||||
Engine.Preferences.groupingSeparator.putPreference(editor, groupingSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
Engine.Preferences.angleUnit.tryPutDefault(preferences);
|
||||
Engine.Preferences.numeralBase.tryPutDefault(preferences);
|
||||
Engine.Preferences.angleUnit.tryPutDefault(preferences, editor);
|
||||
Engine.Preferences.numeralBase.tryPutDefault(preferences, editor);
|
||||
|
||||
if (!Engine.Preferences.multiplicationSign.isSet(preferences)) {
|
||||
if (isPhoneModel(samsung_galaxy_s) || isPhoneModel(samsung_galaxy_s_2)) {
|
||||
// workaround ofr samsung galaxy s phones
|
||||
Engine.Preferences.multiplicationSign.putPreference(preferences, "*");
|
||||
Engine.Preferences.multiplicationSign.putPreference(editor, "*");
|
||||
}
|
||||
}
|
||||
|
||||
Gui.theme.tryPutDefault(preferences);
|
||||
Gui.layout.tryPutDefault(preferences);
|
||||
Gui.theme.tryPutDefault(preferences, editor);
|
||||
Gui.layout.tryPutDefault(preferences, editor);
|
||||
if (Gui.layout.getPreference(preferences) == Gui.Layout.main_cellphone) {
|
||||
Gui.layout.putDefault(preferences);
|
||||
Gui.layout.putDefault(editor);
|
||||
}
|
||||
Gui.feedbackWindowShown.tryPutDefault(preferences);
|
||||
Gui.showReleaseNotes.tryPutDefault(preferences);
|
||||
Gui.usePrevAsBack.tryPutDefault(preferences);
|
||||
Gui.showEqualsButton.tryPutDefault(preferences);
|
||||
Gui.autoOrientation.tryPutDefault(preferences);
|
||||
Gui.hideNumeralBaseDigits.tryPutDefault(preferences);
|
||||
Gui.preventScreenFromFading.tryPutDefault(preferences);
|
||||
Gui.language.tryPutDefault(preferences);
|
||||
Gui.feedbackWindowShown.tryPutDefault(preferences, editor);
|
||||
Gui.showReleaseNotes.tryPutDefault(preferences, editor);
|
||||
Gui.usePrevAsBack.tryPutDefault(preferences, editor);
|
||||
Gui.showEqualsButton.tryPutDefault(preferences, editor);
|
||||
Gui.autoOrientation.tryPutDefault(preferences, editor);
|
||||
Gui.hideNumeralBaseDigits.tryPutDefault(preferences, editor);
|
||||
Gui.preventScreenFromFading.tryPutDefault(preferences, editor);
|
||||
Gui.language.tryPutDefault(preferences, editor);
|
||||
|
||||
Graph.plotImag.tryPutDefault(preferences);
|
||||
Calculations.calculateOnFly.tryPutDefault(preferences);
|
||||
Calculations.preferredAngleUnits.tryPutDefault(preferences);
|
||||
Calculations.preferredNumeralBase.tryPutDefault(preferences);
|
||||
Graph.plotImag.tryPutDefault(preferences, editor);
|
||||
Calculations.calculateOnFly.tryPutDefault(preferences, editor);
|
||||
Calculations.preferredAngleUnits.tryPutDefault(preferences, editor);
|
||||
Calculations.preferredNumeralBase.tryPutDefault(preferences, editor);
|
||||
|
||||
Onscreen.showAppIcon.tryPutDefault(preferences);
|
||||
Onscreen.startOnBoot.tryPutDefault(preferences);
|
||||
Onscreen.theme.tryPutDefault(preferences);
|
||||
Onscreen.showAppIcon.tryPutDefault(preferences, editor);
|
||||
Onscreen.startOnBoot.tryPutDefault(preferences, editor);
|
||||
Onscreen.theme.tryPutDefault(preferences, editor);
|
||||
|
||||
Widget.theme.tryPutDefault(preferences);
|
||||
Widget.theme.tryPutDefault(preferences, editor);
|
||||
}
|
||||
|
||||
public enum SimpleTheme {
|
||||
|
@ -74,18 +74,32 @@ public abstract class AbstractPreference<T> implements Preference<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDefault(@Nonnull SharedPreferences.Editor editor) {
|
||||
putPreference(editor, this.defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDefault(@Nonnull SharedPreferences preferences) {
|
||||
putPreference(preferences, this.defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPreference(@Nonnull SharedPreferences preferences, @Nullable T value) {
|
||||
if (value != null) {
|
||||
final SharedPreferences.Editor editor = preferences.edit();
|
||||
putPersistedValue(editor, value);
|
||||
editor.commit();
|
||||
public void putPreference(@Nonnull SharedPreferences.Editor editor, @Nullable T value) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
putPersistedValue(editor, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPreference(@Nonnull SharedPreferences preferences, @Nullable T value) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
final SharedPreferences.Editor editor = preferences.edit();
|
||||
putPersistedValue(editor, value);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -95,16 +109,20 @@ public abstract class AbstractPreference<T> implements Preference<T> {
|
||||
|
||||
@Override
|
||||
public final boolean tryPutDefault(@Nonnull SharedPreferences preferences) {
|
||||
final boolean result;
|
||||
final SharedPreferences.Editor editor = preferences.edit();
|
||||
final boolean changed = tryPutDefault(preferences, editor);
|
||||
editor.apply();
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean tryPutDefault(@Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) {
|
||||
if (isSet(preferences)) {
|
||||
result = false;
|
||||
} else {
|
||||
putDefault(preferences);
|
||||
result = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return result;
|
||||
putDefault(editor);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,155 +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.prefs;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import org.solovyev.common.text.Mapper;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Locale;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class LocalePreference implements Preference<Locale> {
|
||||
|
||||
@Nonnull
|
||||
private final StringPreference<Locale> stringPreference;
|
||||
|
||||
private LocalePreference(@Nonnull String id, @Nullable Locale defaultValue, @Nonnull Mapper<Locale> localeMapper) {
|
||||
this.stringPreference = new StringPreference<Locale>(id, defaultValue, localeMapper);
|
||||
}
|
||||
|
||||
private LocalePreference(@Nonnull String id, @Nullable Locale defaultValue) {
|
||||
this.stringPreference = new StringPreference<Locale>(id, defaultValue, DefaultLocaleMapper.getInstance());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static LocalePreference of(@Nonnull String id, @Nullable Locale defaultValue, @Nonnull Mapper<Locale> localeMapper) {
|
||||
return new LocalePreference(id, defaultValue, localeMapper);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static LocalePreference of(@Nonnull String id, @Nullable Locale defaultValue) {
|
||||
return new LocalePreference(id, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public String getKey() {
|
||||
return stringPreference.getKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getDefaultValue() {
|
||||
return stringPreference.getDefaultValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getPreference(@Nonnull SharedPreferences preferences) {
|
||||
return stringPreference.getPreference(preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getPreferenceNoError(@Nonnull SharedPreferences preferences) {
|
||||
return stringPreference.getPreferenceNoError(preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDefault(@Nonnull SharedPreferences preferences) {
|
||||
stringPreference.putDefault(preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPreference(@Nonnull SharedPreferences preferences, @Nullable Locale value) {
|
||||
stringPreference.putPreference(preferences, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSet(@Nonnull SharedPreferences preferences) {
|
||||
return stringPreference.isSet(preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryPutDefault(@Nonnull SharedPreferences preferences) {
|
||||
return stringPreference.tryPutDefault(preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameKey(@Nonnull String key) {
|
||||
return stringPreference.isSameKey(key);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final class DefaultLocaleMapper implements Mapper<Locale> {
|
||||
|
||||
@Nonnull
|
||||
private static final String delimiter = ";";
|
||||
|
||||
@Nonnull
|
||||
private static Mapper<Locale> instance = new DefaultLocaleMapper();
|
||||
|
||||
private DefaultLocaleMapper() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Mapper<Locale> getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(@Nullable Locale locale) throws IllegalArgumentException {
|
||||
assert locale != null;
|
||||
return locale.getLanguage() + delimiter + locale.getCountry() + delimiter + locale.getVariant();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale parseValue(@Nullable String s) throws IllegalArgumentException {
|
||||
final StringTokenizer st = new StringTokenizer(s, delimiter, false);
|
||||
|
||||
final String language = st.nextToken();
|
||||
|
||||
final String country;
|
||||
if (st.hasMoreTokens()) {
|
||||
country = st.nextToken();
|
||||
} else {
|
||||
country = "";
|
||||
}
|
||||
|
||||
final String variant;
|
||||
if (st.hasMoreTokens()) {
|
||||
variant = st.nextToken();
|
||||
} else {
|
||||
variant = "";
|
||||
}
|
||||
|
||||
return new Locale(language, country, variant);
|
||||
}
|
||||
}
|
||||
}
|
@ -66,18 +66,19 @@ public interface Preference<T> {
|
||||
|
||||
/**
|
||||
* Method puts (saves) preference represented by <code>value</code> in <code>preferences</code> container
|
||||
*
|
||||
* @param preferences preferences container
|
||||
* @param editor preferences container
|
||||
* @param value value to be saved
|
||||
*/
|
||||
void putPreference(@Nonnull SharedPreferences.Editor editor, @Nullable T value);
|
||||
void putPreference(@Nonnull SharedPreferences preferences, @Nullable T value);
|
||||
|
||||
/**
|
||||
* Method saves default value in <code>preferences</code> container.
|
||||
* Should behave exactly as <code>p.putPreference(preferences, p.getDefaultValue())</code>
|
||||
*
|
||||
* @param preferences preferences container
|
||||
* @param editor preferences editor
|
||||
*/
|
||||
void putDefault(@Nonnull SharedPreferences.Editor editor);
|
||||
void putDefault(@Nonnull SharedPreferences preferences);
|
||||
|
||||
/**
|
||||
@ -89,9 +90,12 @@ public interface Preference<T> {
|
||||
/**
|
||||
* Method applies default value to preference only if explicit value is not set
|
||||
*
|
||||
* @param preferences preferences container
|
||||
*
|
||||
* @param preferences preferences
|
||||
* @param editor preferences editor
|
||||
* @return true if default values have been applied, false otherwise
|
||||
*/
|
||||
boolean tryPutDefault(@Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor);
|
||||
boolean tryPutDefault(@Nonnull SharedPreferences preferences);
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user