package com.kabouzeid.gramophone.util; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.preference.PreferenceManager; import androidx.annotation.NonNull; import androidx.annotation.StyleRes; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import com.kabouzeid.gramophone.R; import com.kabouzeid.gramophone.helper.SortOrder; import com.kabouzeid.gramophone.model.CategoryInfo; import com.kabouzeid.gramophone.ui.fragments.mainactivity.folders.FoldersFragment; import com.kabouzeid.gramophone.ui.fragments.player.NowPlayingScreen; import java.io.File; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public final class PreferenceUtil { public static final String GENERAL_THEME = "general_theme"; public static final String REMEMBER_LAST_TAB = "remember_last_tab"; public static final String LAST_PAGE = "last_start_page"; public static final String LAST_MUSIC_CHOOSER = "last_music_chooser"; public static final String NOW_PLAYING_SCREEN_ID = "now_playing_screen_id"; public static final String ARTIST_SORT_ORDER = "artist_sort_order"; public static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order"; public static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order"; public static final String ALBUM_SORT_ORDER = "album_sort_order"; public static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order"; public static final String SONG_SORT_ORDER = "song_sort_order"; public static final String GENRE_SORT_ORDER = "genre_sort_order"; public static final String ALBUM_GRID_SIZE = "album_grid_size"; public static final String ALBUM_GRID_SIZE_LAND = "album_grid_size_land"; public static final String SONG_GRID_SIZE = "song_grid_size"; public static final String SONG_GRID_SIZE_LAND = "song_grid_size_land"; public static final String ARTIST_GRID_SIZE = "artist_grid_size"; public static final String ARTIST_GRID_SIZE_LAND = "artist_grid_size_land"; public static final String ALBUM_COLORED_FOOTERS = "album_colored_footers"; public static final String SONG_COLORED_FOOTERS = "song_colored_footers"; public static final String ARTIST_COLORED_FOOTERS = "artist_colored_footers"; public static final String ALBUM_ARTIST_COLORED_FOOTERS = "album_artist_colored_footers"; public static final String FORCE_SQUARE_ALBUM_COVER = "force_square_album_art"; public static final String COLORED_NOTIFICATION = "colored_notification"; public static final String CLASSIC_NOTIFICATION = "classic_notification"; public static final String COLORED_APP_SHORTCUTS = "colored_app_shortcuts"; public static final String AUDIO_DUCKING = "audio_ducking"; public static final String GAPLESS_PLAYBACK = "gapless_playback"; public static final String LAST_ADDED_CUTOFF = "last_added_interval"; public static final String ALBUM_ART_ON_LOCKSCREEN = "album_art_on_lockscreen"; public static final String BLURRED_ALBUM_ART = "blurred_album_art"; public static final String LAST_SLEEP_TIMER_VALUE = "last_sleep_timer_value"; public static final String NEXT_SLEEP_TIMER_ELAPSED_REALTIME = "next_sleep_timer_elapsed_real_time"; public static final String SLEEP_TIMER_FINISH_SONG = "sleep_timer_finish_music"; public static final String IGNORE_MEDIA_STORE_ARTWORK = "ignore_media_store_artwork"; public static final String LAST_CHANGELOG_VERSION = "last_changelog_version"; public static final String INTRO_SHOWN = "intro_shown"; public static final String AUTO_DOWNLOAD_IMAGES_POLICY = "auto_download_images_policy"; public static final String START_DIRECTORY = "start_directory"; public static final String SYNCHRONIZED_LYRICS_SHOW = "synchronized_lyrics_show"; public static final String INITIALIZED_BLACKLIST = "initialized_blacklist"; public static final String LIBRARY_CATEGORIES = "library_categories"; private static final String REMEMBER_SHUFFLE = "remember_shuffle"; private static PreferenceUtil sInstance; private final SharedPreferences mPreferences; private PreferenceUtil(@NonNull final Context context) { mPreferences = PreferenceManager.getDefaultSharedPreferences(context); } public static PreferenceUtil getInstance(@NonNull final Context context) { if (sInstance == null) { sInstance = new PreferenceUtil(context.getApplicationContext()); } return sInstance; } public static boolean isAllowedToDownloadMetadata(final Context context) { switch (getInstance(context).autoDownloadImagesPolicy()) { case "always": return true; case "only_wifi": final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); return netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI && netInfo.isConnectedOrConnecting(); case "never": default: return false; } } public void registerOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { mPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); } public void unregisterOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); } @StyleRes public int getGeneralTheme() { return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "light")); } public void setGeneralTheme(String theme) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(GENERAL_THEME, theme); editor.commit(); } @StyleRes public static int getThemeResFromPrefValue(String themePrefValue) { switch (themePrefValue) { case "dark": return R.style.Theme_Phonograph; case "black": return R.style.Theme_Phonograph_Black; case "light": default: return R.style.Theme_Phonograph_Light; } } public final boolean rememberLastTab() { return mPreferences.getBoolean(REMEMBER_LAST_TAB, true); } public void setLastPage(final int value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(LAST_PAGE, value); editor.apply(); } public final int getLastPage() { return mPreferences.getInt(LAST_PAGE, 0); } public void setLastMusicChooser(final int value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(LAST_MUSIC_CHOOSER, value); editor.apply(); } public final int getLastMusicChooser() { return mPreferences.getInt(LAST_MUSIC_CHOOSER, 0); } public final NowPlayingScreen getNowPlayingScreen() { int id = mPreferences.getInt(NOW_PLAYING_SCREEN_ID, 0); for (NowPlayingScreen nowPlayingScreen : NowPlayingScreen.values()) { if (nowPlayingScreen.id == id) return nowPlayingScreen; } return NowPlayingScreen.CARD; } @SuppressLint("CommitPrefEdits") public void setNowPlayingScreen(NowPlayingScreen nowPlayingScreen) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(NOW_PLAYING_SCREEN_ID, nowPlayingScreen.id); editor.commit(); } public final boolean coloredNotification() { return mPreferences.getBoolean(COLORED_NOTIFICATION, true); } public final boolean classicNotification() { return mPreferences.getBoolean(CLASSIC_NOTIFICATION, false); } public void setColoredNotification(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(COLORED_NOTIFICATION, value); editor.apply(); } public void setClassicNotification(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(CLASSIC_NOTIFICATION, value); editor.apply(); } public void setColoredAppShortcuts(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(COLORED_APP_SHORTCUTS, value); editor.apply(); } public final boolean coloredAppShortcuts() { return mPreferences.getBoolean(COLORED_APP_SHORTCUTS, true); } public final boolean gaplessPlayback() { return mPreferences.getBoolean(GAPLESS_PLAYBACK, false); } public final boolean audioDucking() { return mPreferences.getBoolean(AUDIO_DUCKING, true); } public final boolean albumArtOnLockscreen() { return mPreferences.getBoolean(ALBUM_ART_ON_LOCKSCREEN, true); } public final boolean blurredAlbumArt() { return mPreferences.getBoolean(BLURRED_ALBUM_ART, false); } public final boolean ignoreMediaStoreArtwork() { return mPreferences.getBoolean(IGNORE_MEDIA_STORE_ARTWORK, false); } public final String getArtistSortOrder() { return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z); } public void setArtistSortOrder(final String sortOrder) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(ARTIST_SORT_ORDER, sortOrder); editor.commit(); } public final String getArtistSongSortOrder() { return mPreferences.getString(ARTIST_SONG_SORT_ORDER, SortOrder.ArtistSongSortOrder.SONG_A_Z); } public final String getArtistAlbumSortOrder() { return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER, SortOrder.ArtistAlbumSortOrder.ALBUM_YEAR); } public final String getAlbumSortOrder() { return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z); } public void setAlbumSortOrder(final String sortOrder) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(ALBUM_SORT_ORDER, sortOrder); editor.commit(); } public final String getAlbumSongSortOrder() { return mPreferences.getString(ALBUM_SONG_SORT_ORDER, SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST); } public final String getSongSortOrder() { return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z); } public void setSongSortOrder(final String sortOrder) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(SONG_SORT_ORDER, sortOrder); editor.commit(); } public final String getGenreSortOrder() { return mPreferences.getString(GENRE_SORT_ORDER, SortOrder.GenreSortOrder.GENRE_A_Z); } public long getLastAddedCutoff() { final CalendarUtil calendarUtil = new CalendarUtil(); long interval; switch (mPreferences.getString(LAST_ADDED_CUTOFF, "")) { case "today": interval = calendarUtil.getElapsedToday(); break; case "this_week": interval = calendarUtil.getElapsedWeek(); break; case "past_seven_days": interval = calendarUtil.getElapsedDays(7); break; case "past_three_months": interval = calendarUtil.getElapsedMonths(3); break; case "this_year": interval = calendarUtil.getElapsedYear(); break; case "this_month": default: interval = calendarUtil.getElapsedMonth(); break; } return (System.currentTimeMillis() - interval) / 1000; } public int getLastSleepTimerValue() { return mPreferences.getInt(LAST_SLEEP_TIMER_VALUE, 30); } public void setLastSleepTimerValue(final int value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(LAST_SLEEP_TIMER_VALUE, value); editor.apply(); } public long getNextSleepTimerElapsedRealTime() { return mPreferences.getLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, -1); } public void setNextSleepTimerElapsedRealtime(final long value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, value); editor.apply(); } public boolean getSleepTimerFinishMusic() { return mPreferences.getBoolean(SLEEP_TIMER_FINISH_SONG, false); } public void setSleepTimerFinishMusic(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(SLEEP_TIMER_FINISH_SONG, value); editor.apply(); } public void setAlbumGridSize(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(ALBUM_GRID_SIZE, gridSize); editor.apply(); } public final int getAlbumGridSize(Context context) { return mPreferences.getInt(ALBUM_GRID_SIZE, context.getResources().getInteger(R.integer.default_grid_columns)); } public void setSongGridSize(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(SONG_GRID_SIZE, gridSize); editor.apply(); } public final int getSongGridSize(Context context) { return mPreferences.getInt(SONG_GRID_SIZE, context.getResources().getInteger(R.integer.default_list_columns)); } public void setArtistGridSize(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(ARTIST_GRID_SIZE, gridSize); editor.apply(); } public final int getArtistGridSize(Context context) { return mPreferences.getInt(ARTIST_GRID_SIZE, context.getResources().getInteger(R.integer.default_list_columns)); } public void setAlbumGridSizeLand(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(ALBUM_GRID_SIZE_LAND, gridSize); editor.apply(); } public final int getAlbumGridSizeLand(Context context) { return mPreferences.getInt(ALBUM_GRID_SIZE_LAND, context.getResources().getInteger(R.integer.default_grid_columns_land)); } public void setSongGridSizeLand(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(SONG_GRID_SIZE_LAND, gridSize); editor.apply(); } public final int getSongGridSizeLand(Context context) { return mPreferences.getInt(SONG_GRID_SIZE_LAND, context.getResources().getInteger(R.integer.default_list_columns_land)); } public void setArtistGridSizeLand(final int gridSize) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putInt(ARTIST_GRID_SIZE_LAND, gridSize); editor.apply(); } public final int getArtistGridSizeLand(Context context) { return mPreferences.getInt(ARTIST_GRID_SIZE_LAND, context.getResources().getInteger(R.integer.default_list_columns_land)); } public void setAlbumColoredFooters(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(ALBUM_COLORED_FOOTERS, value); editor.apply(); } public final boolean albumColoredFooters() { return mPreferences.getBoolean(ALBUM_COLORED_FOOTERS, true); } public void setAlbumArtistColoredFooters(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(ALBUM_ARTIST_COLORED_FOOTERS, value); editor.apply(); } public final boolean albumArtistColoredFooters() { return mPreferences.getBoolean(ALBUM_ARTIST_COLORED_FOOTERS, true); } public void setSongColoredFooters(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(SONG_COLORED_FOOTERS, value); editor.apply(); } public final boolean songColoredFooters() { return mPreferences.getBoolean(SONG_COLORED_FOOTERS, true); } public void setArtistColoredFooters(final boolean value) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(ARTIST_COLORED_FOOTERS, value); editor.apply(); } public final boolean artistColoredFooters() { return mPreferences.getBoolean(ARTIST_COLORED_FOOTERS, true); } public void setLastChangeLogVersion(int version) { mPreferences.edit().putInt(LAST_CHANGELOG_VERSION, version).apply(); } public final int getLastChangelogVersion() { return mPreferences.getInt(LAST_CHANGELOG_VERSION, -1); } @SuppressLint("CommitPrefEdits") public void setIntroShown() { // don't use apply here mPreferences.edit().putBoolean(INTRO_SHOWN, true).commit(); } public final boolean introShown() { return mPreferences.getBoolean(INTRO_SHOWN, false); } public final boolean rememberShuffle() { return mPreferences.getBoolean(REMEMBER_SHUFFLE, true); } public final String autoDownloadImagesPolicy() { return mPreferences.getString(AUTO_DOWNLOAD_IMAGES_POLICY, "only_wifi"); } public final File getStartDirectory() { return new File(mPreferences.getString(START_DIRECTORY, FoldersFragment.getDefaultStartDirectory().getPath())); } public void setStartDirectory(File file) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(START_DIRECTORY, FileUtil.safeGetCanonicalPath(file)); editor.apply(); } public final boolean synchronizedLyricsShow() { return mPreferences.getBoolean(SYNCHRONIZED_LYRICS_SHOW, true); } public void setInitializedBlacklist() { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean(INITIALIZED_BLACKLIST, true); editor.apply(); } public final boolean initializedBlacklist() { return mPreferences.getBoolean(INITIALIZED_BLACKLIST, false); } public void setLibraryCategoryInfos(List<CategoryInfo> categories) { Gson gson = new Gson(); Type collectionType = new TypeToken<List<CategoryInfo>>() { }.getType(); final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType)); editor.apply(); } public List<CategoryInfo> getLibraryCategoryInfos() { String data = mPreferences.getString(LIBRARY_CATEGORIES, null); if (data != null) { Gson gson = new Gson(); Type collectionType = new TypeToken<List<CategoryInfo>>() { }.getType(); try { return gson.fromJson(data, collectionType); } catch (JsonSyntaxException e) { e.printStackTrace(); } } return getDefaultLibraryCategoryInfos(); } public List<CategoryInfo> getDefaultLibraryCategoryInfos() { List<CategoryInfo> defaultCategoryInfos = new ArrayList<>(5); defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.SONGS, true)); defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true)); defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ARTISTS, true)); defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.GENRES, true)); defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.PLAYLISTS, true)); return defaultCategoryInfos; } }