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 private static PreferenceUtils sInstance;
86 private final SharedPreferences mPreferences;
89 * Constructor for <code>PreferenceUtils</code>
91 * @param context The {@link Context} to use.
93 public PreferenceUtils(final Context context) {
94 mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
98 * @param context The {@link Context} to use.
99 * @return A singleton of this class
101 public static final PreferenceUtils getInstance(final Context context) {
102 if (sInstance == null) {
103 sInstance = new PreferenceUtils(context.getApplicationContext());
109 * Saves the current page the user is on when they close the app.
111 * @param value The last page the pager was on when the onDestroy is called
112 * in {@link MusicBrowserPhoneFragment}.
114 public void setStartPage(final int value) {
115 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
117 protected Void doInBackground(final Void... unused) {
118 final SharedPreferences.Editor editor = mPreferences.edit();
119 editor.putInt(START_PAGE, value);
128 * Set the listener for preference change
131 public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
132 mPreferences.registerOnSharedPreferenceChangeListener(listener);
136 * Returns the last page the user was on when the app was exited.
138 * @return The page to start on when the app is opened.
140 public final int getStartPage() {
141 return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
145 * Sets the new theme color.
147 * @param value The new theme color to use.
149 public void setDefaultThemeColor(final int value) {
150 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
152 protected Void doInBackground(final Void... unused) {
153 final SharedPreferences.Editor editor = mPreferences.edit();
154 editor.putInt(DEFAULT_THEME_COLOR, value);
163 * Returns the current theme color.
165 * @param context The {@link Context} to use.
166 * @return The default theme color.
168 public final int getDefaultThemeColor(final Context context) {
169 return mPreferences.getInt(DEFAULT_THEME_COLOR,
170 context.getResources().getColor(R.color.holo_blue_light));
174 * @return True if the user has checked to only download images on Wi-Fi,
177 public final boolean onlyOnWifi() {
178 return mPreferences.getBoolean(ONLY_ON_WIFI, true);
182 * @return True if the user has checked to download missing album covers,
185 public final boolean downloadMissingArtwork() {
186 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
190 * @return True if the user has checked to download missing artist images,
193 public final boolean downloadMissingArtistImages() {
194 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
198 * Saves the sort order for a list.
200 * @param key Which sort order to change
201 * @param value The new sort order
203 private void setSortOrder(final String key, final String value) {
204 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
206 protected Void doInBackground(final Void... unused) {
207 final SharedPreferences.Editor editor = mPreferences.edit();
208 editor.putString(key, value);
217 * Sets the sort order for the artist list.
219 * @param value The new sort order
221 public void setArtistSortOrder(final String value) {
222 setSortOrder(ARTIST_SORT_ORDER, value);
226 * @return The sort order used for the artist list in {@link ArtistFragment}
228 public final String getArtistSortOrder() {
229 return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
233 * Sets the sort order for the artist song list.
235 * @param value The new sort order
237 public void setArtistSongSortOrder(final String value) {
238 setSortOrder(ARTIST_SONG_SORT_ORDER, value);
242 * @return The sort order used for the artist song list in
243 * {@link ArtistDetailSongAdapter}
245 public final String getArtistSongSortOrder() {
246 return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
247 SortOrder.ArtistSongSortOrder.SONG_A_Z);
251 * Sets the sort order for the artist album list.
253 * @param value The new sort order
255 public void setArtistAlbumSortOrder(final String value) {
256 setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
260 * @return The sort order used for the artist album list in
261 * {@link com.cyanogenmod.eleven.ui.fragments.ArtistDetailFragment}
263 public final String getArtistAlbumSortOrder() {
264 return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
265 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
269 * Sets the sort order for the album list.
271 * @param value The new sort order
273 public void setAlbumSortOrder(final String value) {
274 setSortOrder(ALBUM_SORT_ORDER, value);
278 * @return The sort order used for the album list in {@link AlbumFragment}
280 public final String getAlbumSortOrder() {
281 return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
285 * Sets the sort order for the album song list.
287 * @param value The new sort order
289 public void setAlbumSongSortOrder(final String value) {
290 setSortOrder(ALBUM_SONG_SORT_ORDER, value);
294 * @return The sort order used for the album song in
295 * {@link AlbumSongFragment}
297 public final String getAlbumSongSortOrder() {
298 return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
299 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
303 * Sets the sort order for the song list.
305 * @param value The new sort order
307 public void setSongSortOrder(final String value) {
308 setSortOrder(SONG_SORT_ORDER, value);
312 * @return The sort order used for the song list in {@link SongFragment}
314 public final String getSongSortOrder() {
315 return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
318 /** @parm lastAddedMillis timestamp in millis used as a cutoff for last added playlist */
319 public void setLastAddedCutoff(long lastAddedMillis) {
320 mPreferences.edit().putLong(LAST_ADDED_CUTOFF, lastAddedMillis).commit();
323 public long getLastAddedCutoff() {
324 return mPreferences.getLong(LAST_ADDED_CUTOFF, 0L);
328 * @return Whether we want to show lyrics
330 public final boolean getShowLyrics() {
331 return mPreferences.getBoolean(SHOW_LYRICS, true);
334 public boolean getShowVisualizer() {
335 return mPreferences.getBoolean(SHOW_VISUALIZER, true);
338 public boolean getShakeToPlay() {
339 return mPreferences.getBoolean(SHAKE_TO_PLAY, false);