diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesSettingsActivity.java b/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesSettingsActivity.java index 97710d944..7801add03 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesSettingsActivity.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesSettingsActivity.java @@ -22,7 +22,6 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; -import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -36,7 +35,6 @@ import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; import android.util.Log; import android.util.TypedValue; import android.widget.Toast; @@ -48,8 +46,6 @@ import com.forrestguice.suntimeswidget.settings.WidgetSettings; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; /** @@ -82,15 +78,11 @@ public void onCreate(Bundle icicle) initLegacyPrefs(); } - private HashMap legacyPrefs; - /** * legacy pref api used for pre honeycomb devices, while honeycomb+ uses the fragment based api. */ private void initLegacyPrefs() { - legacyPrefs = new HashMap(); - String action = getIntent().getAction(); if (action != null) { @@ -130,24 +122,6 @@ private void initLegacyPrefs() } } - private static class LegacyListPref - { - private ListPreference listPref; - private String summary; - - public LegacyListPref( @NonNull ListPreference pref ) - { - listPref = pref; - summary = listPref.getSummary().toString(); - updateSummary(); - } - - public void updateSummary() - { - listPref.setSummary(String.format(summary, listPref.getEntry())); - } - } - @Override public void onResume() { @@ -222,7 +196,6 @@ public void onBuildHeaders(List
target) } /** - * more fragment related bullshit * @param fragmentName * @return */ @@ -255,13 +228,6 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin startActivity(getIntent()); } } - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB - && legacyPrefs.containsKey(key)) - { - LegacyListPref legacyPref = legacyPrefs.get(key); - legacyPref.updateSummary(); - } } ////////////////////////////////////////////////// @@ -310,27 +276,18 @@ public void onAttach(Activity activity) */ private void initPref_general() { - String key = AppSettings.PREF_KEY_GETFIX_MAXAGE; - ListPreference gpsMaxAgePref = (ListPreference)findPreference(key); - if (gpsMaxAgePref != null) - { - legacyPrefs.put(key, new LegacyListPref(gpsMaxAgePref)); - } + //String key = AppSettings.PREF_KEY_GETFIX_MAXAGE; + //ListPreference gpsMaxAgePref = (ListPreference)findPreference(key); - key = AppSettings.PREF_KEY_GETFIX_MAXELAPSED; - ListPreference gpsMaxElapsedPref = (ListPreference)findPreference(key); - if (gpsMaxElapsedPref != null) - { - legacyPrefs.put(key, new LegacyListPref(gpsMaxElapsedPref)); - } + //key = AppSettings.PREF_KEY_GETFIX_MAXELAPSED; + //ListPreference gpsMaxElapsedPref = (ListPreference)findPreference(key); - key = WidgetSettings.PREF_PREFIX_KEY + "0" + WidgetSettings.PREF_PREFIX_KEY_GENERAL + WidgetSettings.PREF_KEY_GENERAL_CALCULATOR; + String key = WidgetSettings.PREF_PREFIX_KEY + "0" + WidgetSettings.PREF_PREFIX_KEY_GENERAL + WidgetSettings.PREF_KEY_GENERAL_CALCULATOR; ListPreference calculatorPref = (ListPreference)findPreference(key); if (calculatorPref != null) { initPref_general(calculatorPref); loadPref_general(this, calculatorPref); - legacyPrefs.put(key, new LegacyListPref(calculatorPref)); } } private static void initPref_general(PreferenceFragment fragment) @@ -398,13 +355,13 @@ public void onCreate(Bundle savedInstanceState) */ private void initPref_locale() { - String key = AppSettings.PREF_KEY_LOCALE_MODE; - ListPreference modePref = (ListPreference)findPreference(key); - legacyPrefs.put(key, new LegacyListPref(modePref)); + //String key = AppSettings.PREF_KEY_LOCALE_MODE; + //ListPreference modePref = (ListPreference)findPreference(key); + //legacyPrefs.put(key, new LegacyListPref(modePref)); - key = AppSettings.PREF_KEY_LOCALE; + String key = AppSettings.PREF_KEY_LOCALE; ListPreference localePref = (ListPreference)findPreference(key); - legacyPrefs.put(key, new LegacyListPref(localePref)); + //legacyPrefs.put(key, new LegacyListPref(localePref)); initPref_locale(this, localePref); } @@ -711,33 +668,17 @@ public void onCreate(Bundle savedInstanceState) */ private void initPref_ui() { - String key = AppSettings.PREF_KEY_APPEARANCE_THEME; - ListPreference themePref = (ListPreference)findPreference(key); - if (themePref != null) - { - legacyPrefs.put(key, new LegacyListPref(themePref)); - } + //String key = AppSettings.PREF_KEY_APPEARANCE_THEME; + //ListPreference themePref = (ListPreference)findPreference(key); - key = AppSettings.PREF_KEY_UI_CLOCKTAPACTION; - ListPreference clockTapPref = (ListPreference)findPreference(key); - if (clockTapPref != null) - { - legacyPrefs.put(key, new LegacyListPref(clockTapPref)); - } + //key = AppSettings.PREF_KEY_UI_CLOCKTAPACTION; + //ListPreference clockTapPref = (ListPreference)findPreference(key); - key = AppSettings.PREF_KEY_UI_DATETAPACTION; - ListPreference dateTapPref = (ListPreference)findPreference(key); - if (dateTapPref != null) - { - legacyPrefs.put(key, new LegacyListPref(dateTapPref)); - } + //key = AppSettings.PREF_KEY_UI_DATETAPACTION; + //ListPreference dateTapPref = (ListPreference)findPreference(key); - key = AppSettings.PREF_KEY_UI_NOTETAPACTION; - ListPreference noteTapPref = (ListPreference)findPreference(key); - if (noteTapPref != null) - { - legacyPrefs.put(key, new LegacyListPref(noteTapPref)); - } + //key = AppSettings.PREF_KEY_UI_NOTETAPACTION; + //ListPreference noteTapPref = (ListPreference)findPreference(key); } } diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/settings/ListPreference.java b/app/src/main/java/com/forrestguice/suntimeswidget/settings/ListPreference.java new file mode 100644 index 000000000..85013ff6d --- /dev/null +++ b/app/src/main/java/com/forrestguice/suntimeswidget/settings/ListPreference.java @@ -0,0 +1,104 @@ +/** + Copyright (C) 2016 Forrest Guice + This file is part of SuntimesWidget. + + SuntimesWidget is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SuntimesWidget is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SuntimesWidget. If not, see . +*/ + +package com.forrestguice.suntimeswidget.settings; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +import android.text.TextUtils; +import android.util.AttributeSet; + +/** + * A custom ListPreference to work around some bugs pre api 19. + * @see "https://android.googlesource.com/platform/frameworks/base/+/94c02a1a1a6d7e6900e5a459e9cc699b9510e5a2" + */ +public class ListPreference extends android.preference.ListPreference +{ + public ListPreference(Context context) + { + super(context); + } + + public ListPreference(Context context, AttributeSet attrs) + { + super(context, attrs); + } + + @TargetApi(21) + public ListPreference(Context context, AttributeSet attrs, int defStyleAttr) + { + super(context, attrs, defStyleAttr); + } + + @TargetApi(21) + public ListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) + { + super(context, attrs, defStyleAttr, defStyleRes); + } + + /** + * the bug: listpref summary doesn't update when value is changed (pre api 19) + * this solution based on answer provided by "Dreaming in Code" @stackoverflow + * :: from http://stackoverflow.com/a/21642401/4721910 + * :: based on https://android.googlesource.com/platform/frameworks/base/+/94c02a1a1a6d7e6900e5a459e9cc699b9510e5a2 + * @param value + */ + @Override + public void setValue(String value) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) + { + super.setValue(value); // fixed in api 19 (kitkat); call through to super + + } else { + boolean isChanged = !TextUtils.equals(getValue(), value); + super.setValue(value); + + if (isChanged) // pre api 19; we need to make the missing call to notifyChanged + { + notifyChanged(); + } + } + } + + /** + * the bug: listpref doesn't format %s summary values (pre api 11) + * @return + */ + @Override + public CharSequence getSummary() + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) + { + return super.getSummary(); // fixed in api 11 (honeycomb); call through to super + + } else { + CharSequence summary = super.getSummary(); + if (summary != null) + { + CharSequence displayValue = getEntry(); + if (displayValue != null) // pre api 11; we need to format the summary (%s) + { + return String.format(summary.toString(), displayValue); + } + } + return summary; + } + } +} diff --git a/app/src/main/res/xml/preference_general.xml b/app/src/main/res/xml/preference_general.xml index 6f5d9a410..66b0d2fd8 100644 --- a/app/src/main/res/xml/preference_general.xml +++ b/app/src/main/res/xml/preference_general.xml @@ -4,14 +4,14 @@ - - - - - - - - -