shrink preference utilities class

This commit is contained in:
dkanada 2021-05-23 23:06:12 +09:00
commit d4189f2bfa
2 changed files with 69 additions and 91 deletions

View file

@ -23,7 +23,6 @@ import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process; import android.os.Process;
import android.preference.PreferenceManager;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
@ -410,25 +409,17 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
App.getDatabase().queueSongDao().setQueue(new ArrayList<>(originalPlayingQueue), 1); App.getDatabase().queueSongDao().setQueue(new ArrayList<>(originalPlayingQueue), 1);
} }
private void savePosition() {
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(PreferenceUtil.POSITION, getPosition()).apply();
}
private void saveProgress() {
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(PreferenceUtil.PROGRESS, getSongProgressMillis()).apply();
}
public void saveState() { public void saveState() {
queueHandler.removeMessages(SAVE_QUEUE); queueHandler.removeMessages(SAVE_QUEUE);
queueHandler.sendEmptyMessage(SAVE_QUEUE); queueHandler.sendEmptyMessage(SAVE_QUEUE);
savePosition(); PreferenceUtil.getInstance(this).setPosition(getPosition());
saveProgress(); PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
} }
private void restoreState() { private void restoreState() {
shuffleMode = PreferenceManager.getDefaultSharedPreferences(this).getInt(PreferenceUtil.SHUFFLE, 0); shuffleMode = PreferenceUtil.getInstance(this).getShuffle();
repeatMode = PreferenceManager.getDefaultSharedPreferences(this).getInt(PreferenceUtil.REPEAT, 0); repeatMode = PreferenceUtil.getInstance(this).getRepeat();
notifyChange(SHUFFLE_MODE_CHANGED); notifyChange(SHUFFLE_MODE_CHANGED);
notifyChange(REPEAT_MODE_CHANGED); notifyChange(REPEAT_MODE_CHANGED);
@ -442,8 +433,8 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
List<Song> restoredQueue = App.getDatabase().queueSongDao().getQueue(0); List<Song> restoredQueue = App.getDatabase().queueSongDao().getQueue(0);
List<Song> restoredOriginalQueue = App.getDatabase().queueSongDao().getQueue(1); List<Song> restoredOriginalQueue = App.getDatabase().queueSongDao().getQueue(1);
int restoredPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(PreferenceUtil.POSITION, -1); int restoredPosition = PreferenceUtil.getInstance(this).getPosition();
int restoredPositionInTrack = PreferenceManager.getDefaultSharedPreferences(this).getInt(PreferenceUtil.PROGRESS, -1); int restoredProgress = PreferenceUtil.getInstance(this).getProgress();
if (restoredQueue.size() > 0 && restoredQueue.size() == restoredOriginalQueue.size() && restoredPosition != -1) { if (restoredQueue.size() > 0 && restoredQueue.size() == restoredOriginalQueue.size() && restoredPosition != -1) {
this.originalPlayingQueue = restoredOriginalQueue; this.originalPlayingQueue = restoredOriginalQueue;
@ -452,7 +443,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
position = restoredPosition; position = restoredPosition;
openCurrent(); openCurrent();
if (restoredPositionInTrack > 0) seek(restoredPositionInTrack); if (restoredProgress > 0) seek(restoredProgress);
notHandledMetaChangedForCurrentTrack = true; notHandledMetaChangedForCurrentTrack = true;
handleChangeInternal(META_CHANGED); handleChangeInternal(META_CHANGED);
@ -690,9 +681,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
case REPEAT_MODE_ALL: case REPEAT_MODE_ALL:
case REPEAT_MODE_THIS: case REPEAT_MODE_THIS:
this.repeatMode = repeatMode; this.repeatMode = repeatMode;
PreferenceManager.getDefaultSharedPreferences(this).edit() PreferenceUtil.getInstance(this).setRepeat(repeatMode);
.putInt(PreferenceUtil.REPEAT, repeatMode)
.apply();
prepareNext(); prepareNext();
notifyChange(REPEAT_MODE_CHANGED); notifyChange(REPEAT_MODE_CHANGED);
break; break;
@ -942,9 +931,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
} }
public void setShuffleMode(final int shuffleMode) { public void setShuffleMode(final int shuffleMode) {
PreferenceManager.getDefaultSharedPreferences(this).edit() PreferenceUtil.getInstance(this).setShuffle(shuffleMode);
.putInt(PreferenceUtil.SHUFFLE, shuffleMode)
.apply();
switch (shuffleMode) { switch (shuffleMode) {
case SHUFFLE_MODE_SHUFFLE: case SHUFFLE_MODE_SHUFFLE:
@ -987,16 +974,19 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private void handleChangeInternal(@NonNull final String what) { private void handleChangeInternal(@NonNull final String what) {
switch (what) { switch (what) {
case STATE_CHANGED: case STATE_CHANGED:
if (!isPlaying()) {
PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
}
updateNotification(); updateNotification();
updateMediaSessionState(); updateMediaSessionState();
if (!isPlaying()) saveProgress();
break; break;
case META_CHANGED: case META_CHANGED:
updateNotification(); updateNotification();
updateMediaSessionMetadata(); updateMediaSessionMetadata();
updateMediaSessionState(); updateMediaSessionState();
savePosition(); PreferenceUtil.getInstance(this).setPosition(getPosition());
saveProgress(); PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
break; break;
case QUEUE_CHANGED: case QUEUE_CHANGED:
// because playing queue size might have changed // because playing queue size might have changed

View file

@ -141,6 +141,38 @@ public final class PreferenceUtil {
mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
} }
public int getShuffle() {
return mPreferences.getInt(SHUFFLE, 0);
}
public void setShuffle(int shuffle) {
mPreferences.edit().putInt(SHUFFLE, shuffle).apply();
}
public int getRepeat() {
return mPreferences.getInt(REPEAT, 0);
}
public void setRepeat(int repeat) {
mPreferences.edit().putInt(REPEAT, repeat).apply();
}
public int getPosition() {
return mPreferences.getInt(POSITION, -1);
}
public void setPosition(int position) {
mPreferences.edit().putInt(POSITION, position).apply();
}
public int getProgress() {
return mPreferences.getInt(PROGRESS, -1);
}
public void setProgress(int progress) {
mPreferences.edit().putInt(PROGRESS, progress).apply();
}
public Theme getTheme() { public Theme getTheme() {
return Theme.valueOf(mPreferences.getString(GENERAL_THEME, Theme.DARK.toString())); return Theme.valueOf(mPreferences.getString(GENERAL_THEME, Theme.DARK.toString()));
} }
@ -167,9 +199,7 @@ public final class PreferenceUtil {
} }
public void setLastTab(final int value) { public void setLastTab(final int value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(TAB, value).apply();
editor.putInt(TAB, value);
editor.apply();
} }
public final NowPlayingScreen getNowPlayingScreen() { public final NowPlayingScreen getNowPlayingScreen() {
@ -182,9 +212,7 @@ public final class PreferenceUtil {
} }
public void setNowPlayingScreen(NowPlayingScreen nowPlayingScreen) { public void setNowPlayingScreen(NowPlayingScreen nowPlayingScreen) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(NOW_PLAYING_SCREEN, nowPlayingScreen.id).apply();
editor.putInt(NOW_PLAYING_SCREEN, nowPlayingScreen.id);
editor.apply();
} }
public final boolean getColoredNotification() { public final boolean getColoredNotification() {
@ -232,9 +260,7 @@ public final class PreferenceUtil {
} }
public void setAlbumSortOrder(SortOrder sortOrder) { public void setAlbumSortOrder(SortOrder sortOrder) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(ALBUM_SORT_ORDER, sortOrder.toString()).apply();
editor.putString(ALBUM_SORT_ORDER, sortOrder.toString());
editor.apply();
} }
public final SortOrder getSongSortOrder() { public final SortOrder getSongSortOrder() {
@ -242,9 +268,7 @@ public final class PreferenceUtil {
} }
public void setSongSortOrder(SortOrder sortOrder) { public void setSongSortOrder(SortOrder sortOrder) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(SONG_SORT_ORDER, sortOrder.toString()).apply();
editor.putString(SONG_SORT_ORDER, sortOrder.toString());
editor.apply();
} }
public final SortMethod getAlbumSortMethod() { public final SortMethod getAlbumSortMethod() {
@ -252,9 +276,7 @@ public final class PreferenceUtil {
} }
public void setAlbumSortMethod(SortMethod sortMethod) { public void setAlbumSortMethod(SortMethod sortMethod) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(ALBUM_SORT_METHOD, sortMethod.toString()).apply();
editor.putString(ALBUM_SORT_METHOD, sortMethod.toString());
editor.apply();
} }
public final SortMethod getSongSortMethod() { public final SortMethod getSongSortMethod() {
@ -262,9 +284,7 @@ public final class PreferenceUtil {
} }
public void setSongSortMethod(SortMethod sortMethod) { public void setSongSortMethod(SortMethod sortMethod) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(SONG_SORT_METHOD, sortMethod.toString()).apply();
editor.putString(SONG_SORT_METHOD, sortMethod.toString());
editor.apply();
} }
public int getLastSleepTimerValue() { public int getLastSleepTimerValue() {
@ -272,9 +292,7 @@ public final class PreferenceUtil {
} }
public void setLastSleepTimerValue(final int value) { public void setLastSleepTimerValue(final int value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(SLEEP_TIMER_LAST_VALUE, value).apply();
editor.putInt(SLEEP_TIMER_LAST_VALUE, value);
editor.apply();
} }
public long getNextSleepTimerElapsedRealTime() { public long getNextSleepTimerElapsedRealTime() {
@ -282,9 +300,7 @@ public final class PreferenceUtil {
} }
public void setNextSleepTimerElapsedRealtime(final long value) { public void setNextSleepTimerElapsedRealtime(final long value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putLong(SLEEP_TIMER_ELAPSED_REALTIME, value).apply();
editor.putLong(SLEEP_TIMER_ELAPSED_REALTIME, value);
editor.apply();
} }
public boolean getSleepTimerFinishMusic() { public boolean getSleepTimerFinishMusic() {
@ -292,9 +308,7 @@ public final class PreferenceUtil {
} }
public void setSleepTimerFinishMusic(final boolean value) { public void setSleepTimerFinishMusic(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putBoolean(SLEEP_TIMER_FINISH_SONG, value).apply();
editor.putBoolean(SLEEP_TIMER_FINISH_SONG, value);
editor.apply();
} }
public final int getAlbumGridSize(Context context) { public final int getAlbumGridSize(Context context) {
@ -302,9 +316,7 @@ public final class PreferenceUtil {
} }
public void setAlbumGridSize(final int gridSize) { public void setAlbumGridSize(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(ALBUM_GRID_SIZE, gridSize).apply();
editor.putInt(ALBUM_GRID_SIZE, gridSize);
editor.apply();
} }
public final int getSongGridSize(Context context) { public final int getSongGridSize(Context context) {
@ -312,9 +324,7 @@ public final class PreferenceUtil {
} }
public void setSongGridSize(final int gridSize) { public void setSongGridSize(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(SONG_GRID_SIZE, gridSize).apply();
editor.putInt(SONG_GRID_SIZE, gridSize);
editor.apply();
} }
public final int getArtistGridSize(Context context) { public final int getArtistGridSize(Context context) {
@ -322,9 +332,7 @@ public final class PreferenceUtil {
} }
public void setArtistGridSize(final int gridSize) { public void setArtistGridSize(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(ARTIST_GRID_SIZE, gridSize).apply();
editor.putInt(ARTIST_GRID_SIZE, gridSize);
editor.apply();
} }
public final int getAlbumGridSizeLand(Context context) { public final int getAlbumGridSizeLand(Context context) {
@ -332,9 +340,7 @@ public final class PreferenceUtil {
} }
public void setAlbumGridSizeLand(final int gridSize) { public void setAlbumGridSizeLand(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(ALBUM_GRID_SIZE_LAND, gridSize).apply();
editor.putInt(ALBUM_GRID_SIZE_LAND, gridSize);
editor.apply();
} }
public final int getSongGridSizeLand(Context context) { public final int getSongGridSizeLand(Context context) {
@ -342,9 +348,7 @@ public final class PreferenceUtil {
} }
public void setSongGridSizeLand(final int gridSize) { public void setSongGridSizeLand(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(SONG_GRID_SIZE_LAND, gridSize).apply();
editor.putInt(SONG_GRID_SIZE_LAND, gridSize);
editor.apply();
} }
public final int getArtistGridSizeLand(Context context) { public final int getArtistGridSizeLand(Context context) {
@ -352,9 +356,7 @@ public final class PreferenceUtil {
} }
public void setArtistGridSizeLand(final int gridSize) { public void setArtistGridSizeLand(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putInt(ARTIST_GRID_SIZE_LAND, gridSize).apply();
editor.putInt(ARTIST_GRID_SIZE_LAND, gridSize);
editor.apply();
} }
public final boolean getAlbumColoredFooters() { public final boolean getAlbumColoredFooters() {
@ -362,9 +364,7 @@ public final class PreferenceUtil {
} }
public void setAlbumColoredFooters(final boolean value) { public void setAlbumColoredFooters(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putBoolean(ALBUM_COLORED_FOOTERS, value).apply();
editor.putBoolean(ALBUM_COLORED_FOOTERS, value);
editor.apply();
} }
public final boolean getSongColoredFooters() { public final boolean getSongColoredFooters() {
@ -372,9 +372,7 @@ public final class PreferenceUtil {
} }
public void setSongColoredFooters(final boolean value) { public void setSongColoredFooters(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putBoolean(SONG_COLORED_FOOTERS, value).apply();
editor.putBoolean(SONG_COLORED_FOOTERS, value);
editor.apply();
} }
public final boolean getArtistColoredFooters() { public final boolean getArtistColoredFooters() {
@ -382,9 +380,7 @@ public final class PreferenceUtil {
} }
public void setArtistColoredFooters(final boolean value) { public void setArtistColoredFooters(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putBoolean(ARTIST_COLORED_FOOTERS, value).apply();
editor.putBoolean(ARTIST_COLORED_FOOTERS, value);
editor.apply();
} }
public final boolean getAlbumArtistColoredFooters() { public final boolean getAlbumArtistColoredFooters() {
@ -392,9 +388,7 @@ public final class PreferenceUtil {
} }
public void setAlbumArtistColoredFooters(final boolean value) { public void setAlbumArtistColoredFooters(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putBoolean(ALBUM_ARTIST_COLORED_FOOTERS, value).apply();
editor.putBoolean(ALBUM_ARTIST_COLORED_FOOTERS, value);
editor.apply();
} }
public final String getLocationDownload() { public final String getLocationDownload() {
@ -436,9 +430,7 @@ public final class PreferenceUtil {
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(); mPreferences.edit().putString(CATEGORIES, gson.toJson(categories, collectionType)).apply();
editor.putString(CATEGORIES, gson.toJson(categories, collectionType));
editor.apply();
} }
public List<CategoryInfo> getDefaultCategories() { public List<CategoryInfo> getDefaultCategories() {
@ -471,9 +463,7 @@ public final class PreferenceUtil {
} }
public void setServer(String server) { public void setServer(String server) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(SERVER, server).apply();
editor.putString(SERVER, server);
editor.apply();
} }
public String getUser() { public String getUser() {
@ -481,8 +471,6 @@ public final class PreferenceUtil {
} }
public void setUser(String user) { public void setUser(String user) {
final SharedPreferences.Editor editor = mPreferences.edit(); mPreferences.edit().putString(USER, user).apply();
editor.putString(USER, user);
editor.apply();
} }
} }