use static variables for all preference keys

This commit is contained in:
dkanada 2020-06-19 07:07:23 +09:00
commit 690dfb0c10
7 changed files with 28 additions and 27 deletions

View file

@ -35,7 +35,7 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) { public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) {
super(fragmentManager); super(fragmentManager);
mContext = context; mContext = context;
setCategoryInfos(PreferenceUtil.getInstance(context).getLibraryCategories()); setCategoryInfos(PreferenceUtil.getInstance(context).getCategories());
} }
public void setCategoryInfos(@NonNull List<CategoryInfo> categoryInfos) { public void setCategoryInfos(@NonNull List<CategoryInfo> categoryInfos) {

View file

@ -31,9 +31,9 @@ public class LibraryPreferenceDialog extends DialogFragment {
List<CategoryInfo> categoryInfos; List<CategoryInfo> categoryInfos;
if (savedInstanceState != null) { if (savedInstanceState != null) {
categoryInfos = savedInstanceState.getParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES); categoryInfos = savedInstanceState.getParcelableArrayList(PreferenceUtil.CATEGORIES);
} else { } else {
categoryInfos = PreferenceUtil.getInstance(getContext()).getLibraryCategories(); categoryInfos = PreferenceUtil.getInstance(getContext()).getCategories();
} }
adapter = new CategoryInfoAdapter(categoryInfos); adapter = new CategoryInfoAdapter(categoryInfos);
@ -51,7 +51,7 @@ public class LibraryPreferenceDialog extends DialogFragment {
.negativeText(android.R.string.cancel) .negativeText(android.R.string.cancel)
.neutralText(R.string.reset_action) .neutralText(R.string.reset_action)
.autoDismiss(false) .autoDismiss(false)
.onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultLibraryCategories())) .onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultCategories()))
.onNegative((dialog, action) -> dismiss()) .onNegative((dialog, action) -> dismiss())
.onPositive((dialog, action) -> { .onPositive((dialog, action) -> {
updateCategories(adapter.getCategoryInfos()); updateCategories(adapter.getCategoryInfos());
@ -63,13 +63,13 @@ public class LibraryPreferenceDialog extends DialogFragment {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES, new ArrayList<>(adapter.getCategoryInfos())); outState.putParcelableArrayList(PreferenceUtil.CATEGORIES, new ArrayList<>(adapter.getCategoryInfos()));
} }
private void updateCategories(List<CategoryInfo> categories) { private void updateCategories(List<CategoryInfo> categories) {
if (getSelected(categories) == 0) return; if (getSelected(categories) == 0) return;
PreferenceUtil.getInstance(getContext()).setLibraryCategories(categories); PreferenceUtil.getInstance(getContext()).setCategories(categories);
} }
private int getSelected(List<CategoryInfo> categories) { private int getSelected(List<CategoryInfo> categories) {

View file

@ -75,6 +75,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
new DynamicShortcutManager(this).updateDynamicShortcuts(); new DynamicShortcutManager(this).updateDynamicShortcuts();
} }
recreate(); recreate();
} }
@ -106,9 +107,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
ListPreference listPreference = (ListPreference) preference; ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue); int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary( preference.setSummary(
index >= 0 index >= 0 ? listPreference.getEntries()[index] : null);
? listPreference.getEntries()[index]
: null);
} else { } else {
preference.setSummary(stringValue); preference.setSummary(stringValue);
} }
@ -132,6 +131,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
} else if (preference instanceof LibraryPreference) { } else if (preference instanceof LibraryPreference) {
return LibraryPreferenceDialog.newInstance(); return LibraryPreferenceDialog.newInstance();
} }
return super.onCreatePreferenceDialog(preference); return super.onCreatePreferenceDialog(preference);
} }
@ -150,7 +150,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
} }
private void invalidateSettings() { private void invalidateSettings() {
final Preference generalTheme = findPreference("general_theme"); final Preference generalTheme = findPreference(PreferenceUtil.GENERAL_THEME);
setSummary(generalTheme); setSummary(generalTheme);
generalTheme.setOnPreferenceChangeListener((preference, o) -> { generalTheme.setOnPreferenceChangeListener((preference, o) -> {
String themeName = (String) o; String themeName = (String) o;
@ -193,7 +193,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
return true; return true;
}); });
TwoStatePreference colorNavBar = (TwoStatePreference) findPreference("should_color_navigation_bar"); TwoStatePreference colorNavBar = (TwoStatePreference) findPreference(PreferenceUtil.COLORED_NAVIGATION_BAR);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
colorNavBar.setVisible(false); colorNavBar.setVisible(false);
} else { } else {
@ -207,7 +207,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
}); });
} }
final TwoStatePreference classicNotification = (TwoStatePreference) findPreference("classic_notification"); final TwoStatePreference classicNotification = (TwoStatePreference) findPreference(PreferenceUtil.CLASSIC_NOTIFICATION);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
classicNotification.setVisible(false); classicNotification.setVisible(false);
} else { } else {
@ -219,7 +219,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
}); });
} }
final TwoStatePreference coloredNotification = (TwoStatePreference) findPreference("colored_notification"); final TwoStatePreference coloredNotification = (TwoStatePreference) findPreference(PreferenceUtil.COLORED_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
coloredNotification.setEnabled(PreferenceUtil.getInstance(getActivity()).getClassicNotification()); coloredNotification.setEnabled(PreferenceUtil.getInstance(getActivity()).getClassicNotification());
} else { } else {
@ -258,14 +258,14 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
break; break;
case PreferenceUtil.CLASSIC_NOTIFICATION: case PreferenceUtil.CLASSIC_NOTIFICATION:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
findPreference("colored_notification").setEnabled(sharedPreferences.getBoolean(key, false)); findPreference(PreferenceUtil.COLORED_NOTIFICATION).setEnabled(sharedPreferences.getBoolean(key, false));
} }
break; break;
} }
} }
private void updateNowPlayingScreenSummary() { private void updateNowPlayingScreenSummary() {
findPreference("now_playing_screen_id").setSummary(PreferenceUtil.getInstance(getActivity()).getNowPlayingScreen().titleRes); findPreference(PreferenceUtil.NOW_PLAYING_SCREEN).setSummary(PreferenceUtil.getInstance(getActivity()).getNowPlayingScreen().titleRes);
} }
} }
} }

View file

@ -99,9 +99,9 @@ public class LibraryFragment extends AbsMainActivityFragment implements CabHolde
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (PreferenceUtil.LIBRARY_CATEGORIES.equals(key)) { if (PreferenceUtil.CATEGORIES.equals(key)) {
Fragment current = getCurrentFragment(); Fragment current = getCurrentFragment();
pagerAdapter.setCategoryInfos(PreferenceUtil.getInstance(getActivity()).getLibraryCategories()); pagerAdapter.setCategoryInfos(PreferenceUtil.getInstance(getActivity()).getCategories());
pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1); pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1);
int position = pagerAdapter.getItemPosition(current); int position = pagerAdapter.getItemPosition(current);
if (position < 0) position = 0; if (position < 0) position = 0;

View file

@ -21,7 +21,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class PreferenceUtil { public final class PreferenceUtil {
public static final String LIBRARY_CATEGORIES = "library_categories"; public static final String CATEGORIES = "library_categories";
public static final String REMEMBER_LAST_TAB = "remember_last_tab"; public static final String REMEMBER_LAST_TAB = "remember_last_tab";
public static final String LAST_TAB = "last_tab"; public static final String LAST_TAB = "last_tab";
@ -51,6 +51,7 @@ public final class PreferenceUtil {
public static final String CLASSIC_NOTIFICATION = "classic_notification"; public static final String CLASSIC_NOTIFICATION = "classic_notification";
public static final String GENERAL_THEME = "general_theme"; public static final String GENERAL_THEME = "general_theme";
public static final String COLORED_NAVIGATION_BAR = "should_color_navigation_bar";
public static final String COLORED_SHORTCUTS = "colored_shortcuts"; public static final String COLORED_SHORTCUTS = "colored_shortcuts";
public static final String TRANSCODE_CODEC = "transcode_codec"; public static final String TRANSCODE_CODEC = "transcode_codec";
@ -366,8 +367,8 @@ public final class PreferenceUtil {
editor.apply(); editor.apply();
} }
public List<CategoryInfo> getLibraryCategories() { public List<CategoryInfo> getCategories() {
String data = mPreferences.getString(LIBRARY_CATEGORIES, null); String data = mPreferences.getString(CATEGORIES, null);
if (data != null) { if (data != null) {
Gson gson = new Gson(); Gson gson = new Gson();
Type collectionType = new TypeToken<List<CategoryInfo>>() { Type collectionType = new TypeToken<List<CategoryInfo>>() {
@ -380,19 +381,19 @@ public final class PreferenceUtil {
} }
} }
return getDefaultLibraryCategories(); return getDefaultCategories();
} }
public void setLibraryCategories(List<CategoryInfo> categories) { public void setCategories(List<CategoryInfo> categories) {
Gson gson = new Gson(); Gson gson = new Gson();
Type collectionType = new TypeToken<List<CategoryInfo>>() {}.getType(); Type collectionType = new TypeToken<List<CategoryInfo>>() {}.getType();
final SharedPreferences.Editor editor = mPreferences.edit(); final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType)); editor.putString(CATEGORIES, gson.toJson(categories, collectionType));
editor.apply(); editor.apply();
} }
public List<CategoryInfo> getDefaultLibraryCategories() { public List<CategoryInfo> getDefaultCategories() {
List<CategoryInfo> defaultCategories = new ArrayList<>(5); List<CategoryInfo> defaultCategories = new ArrayList<>(5);
defaultCategories.add(new CategoryInfo(CategoryInfo.Category.SONGS, true)); defaultCategories.add(new CategoryInfo(CategoryInfo.Category.SONGS, true));
defaultCategories.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true)); defaultCategories.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true));

View file

@ -96,7 +96,7 @@
<string name="pref_header_lockscreen">Lockscreen</string> <string name="pref_header_lockscreen">Lockscreen</string>
<string name="pref_header_notification">Notification</string> <string name="pref_header_notification">Notification</string>
<string name="library_categories">Library Categories</string> <string name="library_categories">Categories</string>
<string name="primary_color">Primary Color</string> <string name="primary_color">Primary Color</string>
<string name="accent_color">Accent Color</string> <string name="accent_color">Accent Color</string>
<string name="colored_footers">Colored footers</string> <string name="colored_footers">Colored footers</string>
@ -128,7 +128,7 @@
<string name="pref_summary_remember_queue">Save the queue when closing the app so it can persist across sessions.</string> <string name="pref_summary_remember_queue">Save the queue when closing the app so it can persist across sessions.</string>
<string name="pref_summary_colored_app_shortcuts">Colors the app shortcuts in the primary color.</string> <string name="pref_summary_colored_app_shortcuts">Colors the app shortcuts in the primary color.</string>
<string name="pref_summary_remember_last_tab">Go to the last opened tab on launch.</string> <string name="pref_summary_remember_last_tab">Go to the last opened tab on launch.</string>
<string name="pref_summary_library_categories">Configure visibility and order of library categories.</string> <string name="pref_summary_library_categories">Configure visibility and order of display categories.</string>
<string name="delete_action">Delete</string> <string name="delete_action">Delete</string>
<string name="remove_action">Remove</string> <string name="remove_action">Remove</string>

View file

@ -6,7 +6,7 @@
<com.dkanada.gramophone.preferences.NowPlayingScreenPreference <com.dkanada.gramophone.preferences.NowPlayingScreenPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:key="now_playing_screen_id" android:key="now_playing_screen"
android:title="@string/pref_title_now_playing_screen_appearance" /> android:title="@string/pref_title_now_playing_screen_appearance" />
</com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory> </com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory>