2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.utils;
16 import android.content.Context;
17 import android.content.SharedPreferences;
18 import android.os.AsyncTask;
19 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
20 import android.preference.PreferenceManager;
22 import com.cyanogenmod.eleven.R;
23 import com.cyanogenmod.eleven.ui.fragments.AlbumFragment;
24 import com.cyanogenmod.eleven.ui.fragments.ArtistFragment;
25 import com.cyanogenmod.eleven.ui.fragments.SongFragment;
26 import com.cyanogenmod.eleven.ui.fragments.phone.MusicBrowserPhoneFragment;
29 * A collection of helpers designed to get and set various preferences across
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public final class PreferenceUtils {
36 /* Default start page (Artist page) */
37 public static final int DEFFAULT_PAGE = 2;
39 /* Saves the last page the pager was on in {@link MusicBrowserPhoneFragment} */
40 public static final String START_PAGE = "start_page";
42 // Sort order for the artist list
43 public static final String ARTIST_SORT_ORDER = "artist_sort_order";
45 // Sort order for the artist song list
46 public static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order";
48 // Sort order for the artist album list
49 public static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order";
51 // Sort order for the album list
52 public static final String ALBUM_SORT_ORDER = "album_sort_order";
54 // Sort order for the album song list
55 public static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order";
57 // Sort order for the song list
58 public static final String SONG_SORT_ORDER = "song_sort_order";
60 // Key used to download images only on Wi-Fi
61 public static final String ONLY_ON_WIFI = "only_on_wifi";
63 // Key that gives permissions to download missing album covers
64 public static final String DOWNLOAD_MISSING_ARTWORK = "download_missing_artwork";
66 // Key that gives permissions to download missing artist images
67 public static final String DOWNLOAD_MISSING_ARTIST_IMAGES = "download_missing_artist_images";
69 // Key used to set the overall theme color
70 public static final String DEFAULT_THEME_COLOR = "default_theme_color";
72 // datetime cutoff for determining which songs go in last added playlist
73 public static final String LAST_ADDED_CUTOFF = "last_added_cutoff";
76 public static final String SHOW_LYRICS = "show_lyrics";
78 // show visualizer flag
79 public static final String SHOW_VISUALIZER = "music_visualization";
82 public static final String SHAKE_TO_PLAY = "shake_to_play";
84 // show/hide album art on lockscreen
85 public static final String SHOW_ALBUM_ART_ON_LOCKSCREEN = "lockscreen_album_art";
87 private static PreferenceUtils sInstance;
89 private final SharedPreferences mPreferences;
92 * Constructor for <code>PreferenceUtils</code>
94 * @param context The {@link Context} to use.
96 public PreferenceUtils(final Context context) {
97 mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
101 * @param context The {@link Context} to use.
102 * @return A singleton of this class
104 public static final PreferenceUtils getInstance(final Context context) {
105 if (sInstance == null) {
106 sInstance = new PreferenceUtils(context.getApplicationContext());
112 * Saves the current page the user is on when they close the app.
114 * @param value The last page the pager was on when the onDestroy is called
115 * in {@link MusicBrowserPhoneFragment}.
117 public void setStartPage(final int value) {
118 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
120 protected Void doInBackground(final Void... unused) {
121 final SharedPreferences.Editor editor = mPreferences.edit();
122 editor.putInt(START_PAGE, value);
131 * Set the listener for preference change
134 public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
135 mPreferences.registerOnSharedPreferenceChangeListener(listener);
139 * Returns the last page the user was on when the app was exited.
141 * @return The page to start on when the app is opened.
143 public final int getStartPage() {
144 return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
148 * Sets the new theme color.
150 * @param value The new theme color to use.
152 public void setDefaultThemeColor(final int value) {
153 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
155 protected Void doInBackground(final Void... unused) {
156 final SharedPreferences.Editor editor = mPreferences.edit();
157 editor.putInt(DEFAULT_THEME_COLOR, value);
166 * Returns the current theme color.
168 * @param context The {@link Context} to use.
169 * @return The default theme color.
171 public final int getDefaultThemeColor(final Context context) {
172 return mPreferences.getInt(DEFAULT_THEME_COLOR,
173 context.getResources().getColor(R.color.blue));
177 * @return True if the user has checked to only download images on Wi-Fi,
180 public final boolean onlyOnWifi() {
181 return mPreferences.getBoolean(ONLY_ON_WIFI, true);
185 * @return True if the user has checked to download missing album covers,
188 public final boolean downloadMissingArtwork() {
189 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
193 * @return True if the user has checked to download missing artist images,
196 public final boolean downloadMissingArtistImages() {
197 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
201 * Saves the sort order for a list.
203 * @param key Which sort order to change
204 * @param value The new sort order
206 private void setSortOrder(final String key, final String value) {
207 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
209 protected Void doInBackground(final Void... unused) {
210 final SharedPreferences.Editor editor = mPreferences.edit();
211 editor.putString(key, value);
220 * Sets the sort order for the artist list.
222 * @param value The new sort order
224 public void setArtistSortOrder(final String value) {
225 setSortOrder(ARTIST_SORT_ORDER, value);
229 * @return The sort order used for the artist list in {@link ArtistFragment}
231 public final String getArtistSortOrder() {
232 return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
236 * Sets the sort order for the artist song list.
238 * @param value The new sort order
240 public void setArtistSongSortOrder(final String value) {
241 setSortOrder(ARTIST_SONG_SORT_ORDER, value);
245 * @return The sort order used for the artist song list in
246 * {@link ArtistDetailSongAdapter}
248 public final String getArtistSongSortOrder() {
249 return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
250 SortOrder.ArtistSongSortOrder.SONG_A_Z);
254 * Sets the sort order for the artist album list.
256 * @param value The new sort order
258 public void setArtistAlbumSortOrder(final String value) {
259 setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
263 * @return The sort order used for the artist album list in
264 * {@link com.cyanogenmod.eleven.ui.fragments.ArtistDetailFragment}
266 public final String getArtistAlbumSortOrder() {
267 return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
268 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
272 * Sets the sort order for the album list.
274 * @param value The new sort order
276 public void setAlbumSortOrder(final String value) {
277 setSortOrder(ALBUM_SORT_ORDER, value);
281 * @return The sort order used for the album list in {@link AlbumFragment}
283 public final String getAlbumSortOrder() {
284 return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
288 * Sets the sort order for the album song list.
290 * @param value The new sort order
292 public void setAlbumSongSortOrder(final String value) {
293 setSortOrder(ALBUM_SONG_SORT_ORDER, value);
297 * @return The sort order used for the album song in
298 * {@link AlbumSongFragment}
300 public final String getAlbumSongSortOrder() {
301 return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
302 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
306 * Sets the sort order for the song list.
308 * @param value The new sort order
310 public void setSongSortOrder(final String value) {
311 setSortOrder(SONG_SORT_ORDER, value);
315 * @return The sort order used for the song list in {@link SongFragment}
317 public final String getSongSortOrder() {
318 return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
321 /** @parm lastAddedMillis timestamp in millis used as a cutoff for last added playlist */
322 public void setLastAddedCutoff(long lastAddedMillis) {
323 mPreferences.edit().putLong(LAST_ADDED_CUTOFF, lastAddedMillis).commit();
326 public long getLastAddedCutoff() {
327 return mPreferences.getLong(LAST_ADDED_CUTOFF, 0L);
331 * @return Whether we want to show lyrics
333 public final boolean getShowLyrics() {
334 return mPreferences.getBoolean(SHOW_LYRICS, true);
337 public boolean getShowVisualizer() {
338 return mPreferences.getBoolean(SHOW_VISUALIZER, true);
341 public boolean getShakeToPlay() {
342 return mPreferences.getBoolean(SHAKE_TO_PLAY, false);
345 public boolean getShowAlbumArtOnLockscreen() {
346 return mPreferences.getBoolean(SHOW_ALBUM_ART_ON_LOCKSCREEN, true);