Skip to content

Commit

Permalink
fix listpref summary fails to update
Browse files Browse the repository at this point in the history
fixed listpref summary fails to update (api 18 and under)
  • Loading branch information
forrestguice committed Sep 22, 2016
1 parent e2fb1d4 commit bbaa9c8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -82,15 +78,11 @@ public void onCreate(Bundle icicle)
initLegacyPrefs();
}

private HashMap<String, LegacyListPref> legacyPrefs;

/**
* legacy pref api used for pre honeycomb devices, while honeycomb+ uses the fragment based api.
*/
private void initLegacyPrefs()
{
legacyPrefs = new HashMap<String, LegacyListPref>();

String action = getIntent().getAction();
if (action != null)
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -222,7 +196,6 @@ public void onBuildHeaders(List<Header> target)
}

/**
* more fragment related bullshit
* @param fragmentName
* @return
*/
Expand Down Expand Up @@ -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();
}
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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;
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/xml/preference_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
<PreferenceCategory
android:title="@string/configLabel_general">

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="appwidget_0_general_calculator"
android:title="@string/configLabel_general_calculator"
android:dialogTitle="@string/configLabel_general_calculator"
android:summary="%s"
android:entries="@null" android:entryValues="@null" android:defaultValue="any" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="getFix_maxElapsed"
android:title="@string/configLabel_getFix_maxElapse"
android:dialogTitle="@string/configLabel_getFix_maxElapse"
android:summary="@string/configLabel_getFix_maxElapse_summary"
android:entries="@array/getFix_maxElapse_display" android:entryValues="@array/getFix_maxElapse_values"
android:defaultValue="60000" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="getFix_maxAge"
android:title="@string/configLabel_getFix_maxAge"
android:dialogTitle="@string/configLabel_getFix_maxAge"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/xml/preference_locale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<PreferenceCategory
android:title="@string/configLabel_locale">

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_locale_mode"
android:title="@string/configLabel_localeMode"
android:dialogTitle="@string/configLabel_localeMode"
android:summary="@string/configLabel_localeMode_summary"
android:entries="@array/localeMode_display" android:entryValues="@array/localeMode_values"
android:defaultValue="SYSTEM_LOCALE" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_locale"
android:title="@string/configLabel_localeSelect"
android:dialogTitle="@string/configLabel_localeSelect"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/xml/preference_userinterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
<PreferenceCategory
android:title="@string/configLabel_ui">

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_appearance_theme"
android:title="@string/configLabel_appearance_theme"
android:dialogTitle="@string/configLabel_appearance_theme"
android:summary="%s"
android:entries="@array/appThemes_display" android:entryValues="@array/appThemes_values"
android:defaultValue="dark" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_ui_clocktapaction"
android:title="@string/configLabel_action_onClockTap"
android:dialogTitle="@string/configLabel_action_onClockTap"
android:summary="%s"
android:entries="@array/clockTapActions_display" android:entryValues="@array/clockTapActions_values"
android:defaultValue="ALARM" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_ui_notetapaction"
android:title="@string/configLabel_action_onNoteTap"
android:dialogTitle="@string/configLabel_action_onNoteTap"
android:summary="%s"
android:entries="@array/noteTapActions_display" android:entryValues="@array/noteTapActions_values"
android:defaultValue="NEXT_NOTE" />

<ListPreference
<com.forrestguice.suntimeswidget.settings.ListPreference
android:key="app_ui_datetapaction"
android:title="@string/configLabel_action_onDateTap"
android:dialogTitle="@string/configLabel_action_onDateTap"
Expand Down

0 comments on commit bbaa9c8

Please sign in to comment.