add helper for shared preference to organize code more (#901)

* add helper for shared preference to organize code more

* requested changes to pull request #901
-add the usual copyright / license boilerplate to SharedPrefHelper.
-edit the comment on method getSettings in SharedPrefHelper

* requested changes to pull request #901
-add the usual copyright / license boilerplate to SharedPrefHelper.
-edit the comment on method getSettings in SharedPrefHelper
This commit is contained in:
Michael Sam 2019-01-26 10:25:10 +02:00 committed by Adrian Ulrich
parent b72f1ed832
commit 3bb0998d76
21 changed files with 87 additions and 68 deletions

View File

@ -91,7 +91,7 @@ public class FileUtils {
* for browsing directories * for browsing directories
*/ */
public static File getFilesystemBrowseStart(Context context) { public static File getFilesystemBrowseStart(Context context) {
SharedPreferences prefs = PlaybackService.getSettings(context); SharedPreferences prefs = SharedPrefHelper.getSettings(context);
String folder = prefs.getString(PrefKeys.FILESYSTEM_BROWSE_START, PrefDefaults.FILESYSTEM_BROWSE_START); String folder = prefs.getString(PrefKeys.FILESYSTEM_BROWSE_START, PrefDefaults.FILESYSTEM_BROWSE_START);
return new File( folder.equals("") ? Environment.getExternalStorageDirectory().getAbsolutePath() : folder ); return new File( folder.equals("") ? Environment.getExternalStorageDirectory().getAbsolutePath() : folder );
} }

View File

@ -17,7 +17,6 @@
package ch.blinkenlights.android.vanilla; package ch.blinkenlights.android.vanilla;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -39,7 +38,7 @@ public class FilebrowserStartActivity extends FolderPickerActivity {
@Override @Override
public void onFolderPicked(File directory, ArrayList<String> a, ArrayList<String> b) { public void onFolderPicked(File directory, ArrayList<String> a, ArrayList<String> b) {
SharedPreferences.Editor editor = PlaybackService.getSettings(this).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putString(PrefKeys.FILESYSTEM_BROWSE_START, directory.getAbsolutePath()); editor.putString(PrefKeys.FILESYSTEM_BROWSE_START, directory.getAbsolutePath());
editor.apply(); editor.apply();
finish(); finish();

View File

@ -111,7 +111,7 @@ public class FullPlaybackActivity extends SlidingPlaybackActivity
setTitle(R.string.playback_view); setTitle(R.string.playback_view);
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
int displayMode = Integer.parseInt(settings.getString(PrefKeys.DISPLAY_MODE, PrefDefaults.DISPLAY_MODE)); int displayMode = Integer.parseInt(settings.getString(PrefKeys.DISPLAY_MODE, PrefDefaults.DISPLAY_MODE));
mDisplayMode = displayMode; mDisplayMode = displayMode;
@ -174,7 +174,7 @@ public class FullPlaybackActivity extends SlidingPlaybackActivity
{ {
super.onStart(); super.onStart();
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
if (mDisplayMode != Integer.parseInt(settings.getString(PrefKeys.DISPLAY_MODE, PrefDefaults.DISPLAY_MODE))) { if (mDisplayMode != Integer.parseInt(settings.getString(PrefKeys.DISPLAY_MODE, PrefDefaults.DISPLAY_MODE))) {
finish(); finish();
startActivity(new Intent(this, FullPlaybackActivity.class)); startActivity(new Intent(this, FullPlaybackActivity.class));
@ -578,7 +578,7 @@ public class FullPlaybackActivity extends SlidingPlaybackActivity
{ {
switch (message.what) { switch (message.what) {
case MSG_SAVE_CONTROLS: { case MSG_SAVE_CONTROLS: {
SharedPreferences.Editor editor = PlaybackService.getSettings(this).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putBoolean(PrefKeys.VISIBLE_CONTROLS, mControlsVisible); editor.putBoolean(PrefKeys.VISIBLE_CONTROLS, mControlsVisible);
editor.putBoolean(PrefKeys.VISIBLE_EXTRA_INFO, mExtraInfoVisible); editor.putBoolean(PrefKeys.VISIBLE_EXTRA_INFO, mExtraInfoVisible);
editor.apply(); editor.apply();
@ -676,7 +676,7 @@ public class FullPlaybackActivity extends SlidingPlaybackActivity
if (expansion != SlidingView.EXPANSION_PARTIAL) { if (expansion != SlidingView.EXPANSION_PARTIAL) {
setExtraInfoVisible(false); setExtraInfoVisible(false);
} else { } else {
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
setExtraInfoVisible(settings.getBoolean(PrefKeys.VISIBLE_EXTRA_INFO, PrefDefaults.VISIBLE_EXTRA_INFO)); setExtraInfoVisible(settings.getBoolean(PrefKeys.VISIBLE_EXTRA_INFO, PrefDefaults.VISIBLE_EXTRA_INFO));
} }
} }

View File

@ -42,11 +42,9 @@ import android.os.Message;
import android.support.iosched.tabs.VanillaTabLayout; import android.support.iosched.tabs.VanillaTabLayout;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CheckBox; import android.widget.CheckBox;
@ -178,7 +176,7 @@ public class LibraryActivity
pager.setAdapter(pagerAdapter); pager.setAdapter(pagerAdapter);
mViewPager = pager; mViewPager = pager;
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
mBottomBarControls = (BottomBarControls)findViewById(R.id.bottombar_controls); mBottomBarControls = (BottomBarControls)findViewById(R.id.bottombar_controls);
mBottomBarControls.setOnClickListener(this); mBottomBarControls.setOnClickListener(this);
@ -220,7 +218,7 @@ public class LibraryActivity
* Load settings and cache them. * Load settings and cache them.
*/ */
private void loadPreferences() { private void loadPreferences() {
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
mDefaultAction = Integer.parseInt(settings.getString(PrefKeys.DEFAULT_ACTION_INT, PrefDefaults.DEFAULT_ACTION_INT)); mDefaultAction = Integer.parseInt(settings.getString(PrefKeys.DEFAULT_ACTION_INT, PrefDefaults.DEFAULT_ACTION_INT));
mJumpToEnqueuedOnPlay = settings.getBoolean(PrefKeys.JUMP_TO_ENQUEUED_ON_PLAY, PrefDefaults.JUMP_TO_ENQUEUED_ON_PLAY); mJumpToEnqueuedOnPlay = settings.getBoolean(PrefKeys.JUMP_TO_ENQUEUED_ON_PLAY, PrefDefaults.JUMP_TO_ENQUEUED_ON_PLAY);
} }
@ -242,7 +240,7 @@ public class LibraryActivity
*/ */
private void checkForLaunch(Intent intent) private void checkForLaunch(Intent intent)
{ {
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
if (settings.getBoolean(PrefKeys.PLAYBACK_ON_STARTUP, PrefDefaults.PLAYBACK_ON_STARTUP) && Intent.ACTION_MAIN.equals(intent.getAction())) { if (settings.getBoolean(PrefKeys.PLAYBACK_ON_STARTUP, PrefDefaults.PLAYBACK_ON_STARTUP) && Intent.ACTION_MAIN.equals(intent.getAction())) {
startActivity(new Intent(this, FullPlaybackActivity.class)); startActivity(new Intent(this, FullPlaybackActivity.class));
} }
@ -908,7 +906,7 @@ public class LibraryActivity
{ {
switch (message.what) { switch (message.what) {
case MSG_SAVE_PAGE: { case MSG_SAVE_PAGE: {
SharedPreferences.Editor editor = PlaybackService.getSettings(this).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putInt("library_page", message.arg1); editor.putInt("library_page", message.arg1);
editor.apply(); editor.apply();
super.adjustSpines(); super.adjustSpines();

View File

@ -34,8 +34,6 @@ import android.os.Message;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -217,7 +215,7 @@ public class LibraryPagerAdapter
*/ */
public boolean loadTabOrder() public boolean loadTabOrder()
{ {
String in = PlaybackService.getSettings(mActivity).getString(PrefKeys.TAB_ORDER, PrefDefaults.TAB_ORDER); String in = SharedPrefHelper.getSettings(mActivity).getString(PrefKeys.TAB_ORDER, PrefDefaults.TAB_ORDER);
int[] order = new int[MAX_ADAPTER_COUNT]; int[] order = new int[MAX_ADAPTER_COUNT];
int count = 0; int count = 0;
if (in != null && in.length() == MAX_ADAPTER_COUNT) { if (in != null && in.length() == MAX_ADAPTER_COUNT) {
@ -683,7 +681,7 @@ public class LibraryPagerAdapter
} }
case MSG_SAVE_SORT: { case MSG_SAVE_SORT: {
SortableAdapter adapter = (SortableAdapter)message.obj; SortableAdapter adapter = (SortableAdapter)message.obj;
SharedPreferences.Editor editor = PlaybackService.getSettings(mActivity).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(mActivity).edit();
editor.putInt(adapter.getSortSettingsKey(), adapter.getSortMode()); editor.putInt(adapter.getSortSettingsKey(), adapter.getSortMode());
editor.apply(); editor.apply();
break; break;
@ -777,7 +775,7 @@ public class LibraryPagerAdapter
{ {
String key = adapter.getSortSettingsKey(); String key = adapter.getSortSettingsKey();
int def = adapter.getDefaultSortMode(); int def = adapter.getDefaultSortMode();
int sort = PlaybackService.getSettings(mActivity).getInt(key, def); int sort = SharedPrefHelper.getSettings(mActivity).getInt(key, def);
adapter.setSortMode(sort); adapter.setSortMode(sort);
} }
@ -908,7 +906,7 @@ public class LibraryPagerAdapter
return; return;
// scroll to song on song change if opted-in // scroll to song on song change if opted-in
SharedPreferences sharedPrefs = PlaybackService.getSettings(mActivity); SharedPreferences sharedPrefs = SharedPrefHelper.getSettings(mActivity);
boolean shouldScroll = sharedPrefs.getBoolean(PrefKeys.ENABLE_SCROLL_TO_SONG, PrefDefaults.ENABLE_SCROLL_TO_SONG); boolean shouldScroll = sharedPrefs.getBoolean(PrefKeys.ENABLE_SCROLL_TO_SONG, PrefDefaults.ENABLE_SCROLL_TO_SONG);
if(shouldScroll) { if(shouldScroll) {
for (int pos = 0; pos < view.getCount(); pos++) { for (int pos = 0; pos < view.getCount(); pos++) {

View File

@ -24,7 +24,6 @@
package ch.blinkenlights.android.vanilla; package ch.blinkenlights.android.vanilla;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -76,7 +75,7 @@ public class MediaButtonReceiver extends BroadcastReceiver {
private static void beep(Context context) private static void beep(Context context)
{ {
if (sBeep == -1) { if (sBeep == -1) {
SharedPreferences settings = PlaybackService.getSettings(context); SharedPreferences settings = SharedPrefHelper.getSettings(context);
sBeep = settings.getBoolean(PrefKeys.MEDIA_BUTTON_BEEP, PrefDefaults.MEDIA_BUTTON_BEEP) ? 1 : 0; sBeep = settings.getBoolean(PrefKeys.MEDIA_BUTTON_BEEP, PrefDefaults.MEDIA_BUTTON_BEEP) ? 1 : 0;
} }
@ -109,7 +108,7 @@ public class MediaButtonReceiver extends BroadcastReceiver {
public static boolean useHeadsetControls(Context context) public static boolean useHeadsetControls(Context context)
{ {
if (sUseControls == -1) { if (sUseControls == -1) {
SharedPreferences settings = PlaybackService.getSettings(context); SharedPreferences settings = SharedPrefHelper.getSettings(context);
sUseControls = settings.getBoolean(PrefKeys.MEDIA_BUTTON, PrefDefaults.MEDIA_BUTTON) ? 1 : 0; sUseControls = settings.getBoolean(PrefKeys.MEDIA_BUTTON, PrefDefaults.MEDIA_BUTTON) ? 1 : 0;
} }

View File

@ -89,7 +89,7 @@ public class OneCellWidget extends AppWidgetProvider {
if (!sEnabled) if (!sEnabled)
return; return;
SharedPreferences settings = PlaybackService.getSettings(context); SharedPreferences settings = SharedPrefHelper.getSettings(context);
boolean doubleTap = settings.getBoolean(PrefKeys.DOUBLE_TAP, PrefDefaults.DOUBLE_TAP); boolean doubleTap = settings.getBoolean(PrefKeys.DOUBLE_TAP, PrefDefaults.DOUBLE_TAP);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget);

View File

@ -154,7 +154,7 @@ public abstract class PlaybackActivity extends Activity
else else
startService(new Intent(this, PlaybackService.class)); startService(new Intent(this, PlaybackService.class));
SharedPreferences prefs = PlaybackService.getSettings(this); SharedPreferences prefs = SharedPrefHelper.getSettings(this);
mUpAction = Action.getAction(prefs, PrefKeys.SWIPE_UP_ACTION, PrefDefaults.SWIPE_UP_ACTION); mUpAction = Action.getAction(prefs, PrefKeys.SWIPE_UP_ACTION, PrefDefaults.SWIPE_UP_ACTION);
mDownAction = Action.getAction(prefs, PrefKeys.SWIPE_DOWN_ACTION, PrefDefaults.SWIPE_DOWN_ACTION); mDownAction = Action.getAction(prefs, PrefKeys.SWIPE_DOWN_ACTION, PrefDefaults.SWIPE_DOWN_ACTION);

View File

@ -55,7 +55,6 @@ import android.os.Message;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process; import android.os.Process;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@ -298,10 +297,6 @@ public final class PlaybackService extends Service
* Static referenced-array to PlaybackActivities, used for callbacks * Static referenced-array to PlaybackActivities, used for callbacks
*/ */
private static final ArrayList<TimelineCallback> sCallbacks = new ArrayList<TimelineCallback>(5); private static final ArrayList<TimelineCallback> sCallbacks = new ArrayList<TimelineCallback>(5);
/**
* Cached app-wide SharedPreferences instance.
*/
private static SharedPreferences sSettings;
boolean mHeadsetPause; boolean mHeadsetPause;
private boolean mScrobble; private boolean mScrobble;
@ -465,7 +460,7 @@ public final class PlaybackService extends Service
mNotificationHelper = new NotificationHelper(this, NOTIFICATION_CHANNEL, getString(R.string.app_name)); mNotificationHelper = new NotificationHelper(this, NOTIFICATION_CHANNEL, getString(R.string.app_name));
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE); mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
SharedPreferences settings = getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
settings.registerOnSharedPreferenceChangeListener(this); settings.registerOnSharedPreferenceChangeListener(this);
mNotificationVisibility = Integer.parseInt(settings.getString(PrefKeys.NOTIFICATION_VISIBILITY, PrefDefaults.NOTIFICATION_VISIBILITY)); mNotificationVisibility = Integer.parseInt(settings.getString(PrefKeys.NOTIFICATION_VISIBILITY, PrefDefaults.NOTIFICATION_VISIBILITY));
mNotificationNag = settings.getBoolean(PrefKeys.NOTIFICATION_NAG, PrefDefaults.NOTIFICATION_NAG); mNotificationNag = settings.getBoolean(PrefKeys.NOTIFICATION_NAG, PrefDefaults.NOTIFICATION_NAG);
@ -480,7 +475,7 @@ public final class PlaybackService extends Service
mHeadsetOnly = settings.getBoolean(PrefKeys.HEADSET_ONLY, PrefDefaults.HEADSET_ONLY); mHeadsetOnly = settings.getBoolean(PrefKeys.HEADSET_ONLY, PrefDefaults.HEADSET_ONLY);
mStockBroadcast = settings.getBoolean(PrefKeys.STOCK_BROADCAST, PrefDefaults.STOCK_BROADCAST); mStockBroadcast = settings.getBoolean(PrefKeys.STOCK_BROADCAST, PrefDefaults.STOCK_BROADCAST);
mNotificationAction = createNotificationAction(settings); mNotificationAction = createNotificationAction(settings);
mHeadsetPause = getSettings(this).getBoolean(PrefKeys.HEADSET_PAUSE, PrefDefaults.HEADSET_PAUSE); mHeadsetPause = SharedPrefHelper.getSettings(this).getBoolean(PrefKeys.HEADSET_PAUSE, PrefDefaults.HEADSET_PAUSE);
mShakeAction = settings.getBoolean(PrefKeys.ENABLE_SHAKE, PrefDefaults.ENABLE_SHAKE) ? Action.getAction(settings, PrefKeys.SHAKE_ACTION, PrefDefaults.SHAKE_ACTION) : Action.Nothing; mShakeAction = settings.getBoolean(PrefKeys.ENABLE_SHAKE, PrefDefaults.ENABLE_SHAKE) ? Action.getAction(settings, PrefKeys.SHAKE_ACTION, PrefDefaults.SHAKE_ACTION) : Action.Nothing;
mShakeThreshold = settings.getInt(PrefKeys.SHAKE_THRESHOLD, PrefDefaults.SHAKE_THRESHOLD) / 10.0f; mShakeThreshold = settings.getInt(PrefKeys.SHAKE_THRESHOLD, PrefDefaults.SHAKE_THRESHOLD) / 10.0f;
@ -636,7 +631,7 @@ public final class PlaybackService extends Service
enterSleepState(); enterSleepState();
// stop getting preference changes. // stop getting preference changes.
getSettings(this).unregisterOnSharedPreferenceChangeListener(this); SharedPrefHelper.getSettings(this).unregisterOnSharedPreferenceChangeListener(this);
// shutdown all observers. // shutdown all observers.
MediaLibrary.unregisterLibraryObserver(mObserver); MediaLibrary.unregisterLibraryObserver(mObserver);
@ -862,17 +857,6 @@ public final class PlaybackService extends Service
} }
} }
/**
* Return the SharedPreferences instance containing the PlaybackService
* settings, creating it if necessary.
*/
public static SharedPreferences getSettings(Context context)
{
if (sSettings == null)
sSettings = PreferenceManager.getDefaultSharedPreferences(context);
return sSettings;
}
/** /**
* Setup the accelerometer. * Setup the accelerometer.
*/ */
@ -891,7 +875,7 @@ public final class PlaybackService extends Service
private void loadPreference(String key) private void loadPreference(String key)
{ {
SharedPreferences settings = getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
if (PrefKeys.HEADSET_PAUSE.equals(key)) { if (PrefKeys.HEADSET_PAUSE.equals(key)) {
mHeadsetPause = settings.getBoolean(PrefKeys.HEADSET_PAUSE, PrefDefaults.HEADSET_PAUSE); mHeadsetPause = settings.getBoolean(PrefKeys.HEADSET_PAUSE, PrefDefaults.HEADSET_PAUSE);
} else if (PrefKeys.NOTIFICATION_ACTION.equals(key)) { } else if (PrefKeys.NOTIFICATION_ACTION.equals(key)) {

View File

@ -135,7 +135,7 @@ public class PlaylistActivity extends Activity
public void onStart() public void onStart()
{ {
super.onStart(); super.onStart();
SharedPreferences settings = PlaybackService.getSettings(this); SharedPreferences settings = SharedPrefHelper.getSettings(this);
mDefaultAction = Integer.parseInt(settings.getString(PrefKeys.DEFAULT_PLAYLIST_ACTION, PrefDefaults.DEFAULT_PLAYLIST_ACTION)); mDefaultAction = Integer.parseInt(settings.getString(PrefKeys.DEFAULT_PLAYLIST_ACTION, PrefDefaults.DEFAULT_PLAYLIST_ACTION));
} }

View File

@ -17,7 +17,6 @@
package ch.blinkenlights.android.vanilla; package ch.blinkenlights.android.vanilla;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -32,14 +31,14 @@ public class PlaylistObserverDirActivity extends FolderPickerActivity {
setTitle(R.string.filebrowser_start); setTitle(R.string.filebrowser_start);
// Start at currently configured directory. // Start at currently configured directory.
String current = PlaybackService.getSettings(this).getString(PrefKeys.PLAYLIST_SYNC_FOLDER, PrefDefaults.PLAYLIST_SYNC_FOLDER); String current = SharedPrefHelper.getSettings(this).getString(PrefKeys.PLAYLIST_SYNC_FOLDER, PrefDefaults.PLAYLIST_SYNC_FOLDER);
setCurrentDir(new File(current)); setCurrentDir(new File(current));
} }
@Override @Override
public void onFolderPicked(File directory, ArrayList<String> a, ArrayList<String> b) { public void onFolderPicked(File directory, ArrayList<String> a, ArrayList<String> b) {
SharedPreferences.Editor editor = PlaybackService.getSettings(this).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putString(PrefKeys.PLAYLIST_SYNC_FOLDER, directory.getAbsolutePath()); editor.putString(PrefKeys.PLAYLIST_SYNC_FOLDER, directory.getAbsolutePath());
editor.apply(); editor.apply();
finish(); finish();

View File

@ -24,18 +24,14 @@
package ch.blinkenlights.android.vanilla; package ch.blinkenlights.android.vanilla;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
@ -72,13 +68,13 @@ public class PreferencesActivity extends PreferenceActivity
{ {
ThemeHelper.setTheme(this, R.style.BackActionBar); ThemeHelper.setTheme(this, R.style.BackActionBar);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
PlaybackService.getSettings(this).registerOnSharedPreferenceChangeListener(this); SharedPrefHelper.getSettings(this).registerOnSharedPreferenceChangeListener(this);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
PlaybackService.getSettings(this).unregisterOnSharedPreferenceChangeListener(this); SharedPrefHelper.getSettings(this).unregisterOnSharedPreferenceChangeListener(this);
} }
@Override @Override

View File

@ -28,7 +28,6 @@ import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.util.Log;
public class PreferencesTheme extends PreferenceFragment public class PreferencesTheme extends PreferenceFragment
@ -66,7 +65,7 @@ public class PreferencesTheme extends PreferenceFragment
@Override @Override
public boolean onPreferenceClick(Preference pref) { public boolean onPreferenceClick(Preference pref) {
SharedPreferences.Editor editor = PlaybackService.getSettings(mContext).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(mContext).edit();
editor.putString(PrefKeys.SELECTED_THEME, pref.getKey()); editor.putString(PrefKeys.SELECTED_THEME, pref.getKey());
editor.apply(); editor.apply();
return true; return true;

View File

@ -127,7 +127,7 @@ public class RemoteControlImplICS implements RemoteControl.Client {
boolean isPlaying = ((state & PlaybackService.FLAG_PLAYING) != 0); boolean isPlaying = ((state & PlaybackService.FLAG_PLAYING) != 0);
if (mShowCover == -1) { if (mShowCover == -1) {
SharedPreferences settings = PlaybackService.getSettings(mContext); SharedPreferences settings = SharedPrefHelper.getSettings(mContext);
mShowCover = settings.getBoolean(PrefKeys.COVER_ON_LOCKSCREEN, PrefDefaults.COVER_ON_LOCKSCREEN) ? 1 : 0; mShowCover = settings.getBoolean(PrefKeys.COVER_ON_LOCKSCREEN, PrefDefaults.COVER_ON_LOCKSCREEN) ? 1 : 0;
} }

View File

@ -133,7 +133,7 @@ public class RemoteControlImplLp implements RemoteControl.Client {
boolean isPlaying = ((state & PlaybackService.FLAG_PLAYING) != 0); boolean isPlaying = ((state & PlaybackService.FLAG_PLAYING) != 0);
if (mShowCover == -1) { if (mShowCover == -1) {
SharedPreferences settings = PlaybackService.getSettings(mContext); SharedPreferences settings = SharedPrefHelper.getSettings(mContext);
mShowCover = settings.getBoolean(PrefKeys.COVER_ON_LOCKSCREEN, PrefDefaults.COVER_ON_LOCKSCREEN) ? 1 : 0; mShowCover = settings.getBoolean(PrefKeys.COVER_ON_LOCKSCREEN, PrefDefaults.COVER_ON_LOCKSCREEN) ? 1 : 0;
} }

View File

@ -229,13 +229,13 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
} }
private void saveCheckBoxPreference(boolean enabled) { private void saveCheckBoxPreference(boolean enabled) {
SharedPreferences.Editor editor = PlaybackService.getSettings(mContext).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(mContext).edit();
editor.putBoolean(mCheckBoxKey, enabled); editor.putBoolean(mCheckBoxKey, enabled);
editor.apply(); editor.apply();
} }
private boolean getCheckBoxPreference() { private boolean getCheckBoxPreference() {
SharedPreferences settings = PlaybackService.getSettings(mContext); SharedPreferences settings = SharedPrefHelper.getSettings(mContext);
return settings.getBoolean(mCheckBoxKey, false); return settings.getBoolean(mCheckBoxKey, false);
} }

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Michael Samuel <michaelsam00@yahoo.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ch.blinkenlights.android.vanilla;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public final class SharedPrefHelper {
/**
* Cached app-wide SharedPreferences instance.
*/
private static SharedPreferences sSettings;
private SharedPrefHelper(){};
/**
* Return the SharedPreferences instance containing
* Application settings.
*/
public static SharedPreferences getSettings(Context context)
{
if (sSettings == null)
sSettings = PreferenceManager.getDefaultSharedPreferences(context);
return sSettings;
}
}

View File

@ -25,11 +25,8 @@ import android.annotation.SuppressLint;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
@ -259,7 +256,7 @@ public class ShowQueueFragment extends Fragment
*/ */
public void setSong(long uptime, Song song) { public void setSong(long uptime, Song song) {
if (PlaybackService.hasInstance()) { if (PlaybackService.hasInstance()) {
boolean scroll = PlaybackService boolean scroll = SharedPrefHelper
.getSettings(getActivity().getApplicationContext()) .getSettings(getActivity().getApplicationContext())
.getBoolean(PrefKeys.QUEUE_ENABLE_SCROLL_TO_SONG, .getBoolean(PrefKeys.QUEUE_ENABLE_SCROLL_TO_SONG,
PrefDefaults.QUEUE_ENABLE_SCROLL_TO_SONG); PrefDefaults.QUEUE_ENABLE_SCROLL_TO_SONG);

View File

@ -115,7 +115,7 @@ public class TabOrderActivity extends Activity
out[i] = (char)(list.isItemChecked(i) ? 128 + ids[i] : 127 - ids[i]); out[i] = (char)(list.isItemChecked(i) ? 128 + ids[i] : 127 - ids[i]);
} }
SharedPreferences.Editor editor = PlaybackService.getSettings(this).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putString(PrefKeys.TAB_ORDER, new String(out)); editor.putString(PrefKeys.TAB_ORDER, new String(out));
editor.apply(); editor.apply();
} }
@ -126,7 +126,7 @@ public class TabOrderActivity extends Activity
*/ */
public void load() public void load()
{ {
String in = PlaybackService.getSettings(this).getString(PrefKeys.TAB_ORDER, PrefDefaults.TAB_ORDER); String in = SharedPrefHelper.getSettings(this).getString(PrefKeys.TAB_ORDER, PrefDefaults.TAB_ORDER);
if (in != null && in.length() == LibraryPagerAdapter.MAX_ADAPTER_COUNT) { if (in != null && in.length() == LibraryPagerAdapter.MAX_ADAPTER_COUNT) {
char[] chars = in.toCharArray(); char[] chars = in.toCharArray();
int[] ids = new int[LibraryPagerAdapter.MAX_ADAPTER_COUNT]; int[] ids = new int[LibraryPagerAdapter.MAX_ADAPTER_COUNT];

View File

@ -96,7 +96,7 @@ public class ThemeHelper {
* @return integer of the selected theme * @return integer of the selected theme
*/ */
final private static int getSelectedThemeIndex(Context context) { final private static int getSelectedThemeIndex(Context context) {
SharedPreferences settings = PlaybackService.getSettings(context); SharedPreferences settings = SharedPrefHelper.getSettings(context);
String prefValue = settings.getString(PrefKeys.SELECTED_THEME, PrefDefaults.SELECTED_THEME); String prefValue = settings.getString(PrefKeys.SELECTED_THEME, PrefDefaults.SELECTED_THEME);
final String[] ids = context.getResources().getStringArray(R.array.theme_ids); final String[] ids = context.getResources().getStringArray(R.array.theme_ids);