OSDN Git Service

Eleven: Cleanup all the whitespace
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / utils / PreferenceUtils.java
1 /*
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.
12  */
13
14 package com.cyanogenmod.eleven.utils;
15
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;
21
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;
27
28 /**
29  * A collection of helpers designed to get and set various preferences across
30  * Apollo.
31  *
32  * @author Andrew Neal (andrewdneal@gmail.com)
33  */
34 public final class PreferenceUtils {
35
36     /* Default start page (Artist page) */
37     public static final int DEFFAULT_PAGE = 2;
38
39     /* Saves the last page the pager was on in {@link MusicBrowserPhoneFragment} */
40     public static final String START_PAGE = "start_page";
41
42     // Sort order for the artist list
43     public static final String ARTIST_SORT_ORDER = "artist_sort_order";
44
45     // Sort order for the artist song list
46     public static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order";
47
48     // Sort order for the artist album list
49     public static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order";
50
51     // Sort order for the album list
52     public static final String ALBUM_SORT_ORDER = "album_sort_order";
53
54     // Sort order for the album song list
55     public static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order";
56
57     // Sort order for the song list
58     public static final String SONG_SORT_ORDER = "song_sort_order";
59
60     // Key used to download images only on Wi-Fi
61     public static final String ONLY_ON_WIFI = "only_on_wifi";
62
63     // Key that gives permissions to download missing album covers
64     public static final String DOWNLOAD_MISSING_ARTWORK = "download_missing_artwork";
65
66     // Key that gives permissions to download missing artist images
67     public static final String DOWNLOAD_MISSING_ARTIST_IMAGES = "download_missing_artist_images";
68
69     // Key used to set the overall theme color
70     public static final String DEFAULT_THEME_COLOR = "default_theme_color";
71
72     // datetime cutoff for determining which songs go in last added playlist
73     public static final String LAST_ADDED_CUTOFF = "last_added_cutoff";
74
75     // show lyrics option
76     public static final String SHOW_LYRICS = "show_lyrics";
77
78     // show visualizer flag
79     public static final String SHOW_VISUALIZER = "music_visualization";
80
81     // shake to play flag
82     public static final String SHAKE_TO_PLAY = "shake_to_play";
83
84     // show/hide album art on lockscreen
85     public static final String SHOW_ALBUM_ART_ON_LOCKSCREEN = "lockscreen_album_art";
86
87     private static PreferenceUtils sInstance;
88
89     private final SharedPreferences mPreferences;
90
91     /**
92      * Constructor for <code>PreferenceUtils</code>
93      *
94      * @param context The {@link Context} to use.
95      */
96     public PreferenceUtils(final Context context) {
97         mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
98     }
99
100     /**
101      * @param context The {@link Context} to use.
102      * @return A singleton of this class
103      */
104     public static final PreferenceUtils getInstance(final Context context) {
105         if (sInstance == null) {
106             sInstance = new PreferenceUtils(context.getApplicationContext());
107         }
108         return sInstance;
109     }
110
111     /**
112      * Saves the current page the user is on when they close the app.
113      *
114      * @param value The last page the pager was on when the onDestroy is called
115      *            in {@link MusicBrowserPhoneFragment}.
116      */
117     public void setStartPage(final int value) {
118         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
119             @Override
120             protected Void doInBackground(final Void... unused) {
121                 final SharedPreferences.Editor editor = mPreferences.edit();
122                 editor.putInt(START_PAGE, value);
123                 editor.apply();
124
125                 return null;
126             }
127         }, (Void[])null);
128     }
129
130     /**
131      * Set the listener for preference change
132      * @param listener
133      */
134     public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
135         mPreferences.registerOnSharedPreferenceChangeListener(listener);
136     }
137
138     /**
139      * Returns the last page the user was on when the app was exited.
140      *
141      * @return The page to start on when the app is opened.
142      */
143     public final int getStartPage() {
144         return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
145     }
146
147     /**
148      * Sets the new theme color.
149      *
150      * @param value The new theme color to use.
151      */
152     public void setDefaultThemeColor(final int value) {
153         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
154             @Override
155             protected Void doInBackground(final Void... unused) {
156                 final SharedPreferences.Editor editor = mPreferences.edit();
157                 editor.putInt(DEFAULT_THEME_COLOR, value);
158                 editor.apply();
159
160                 return null;
161             }
162         }, (Void[])null);
163     }
164
165     /**
166      * Returns the current theme color.
167      *
168      * @param context The {@link Context} to use.
169      * @return The default theme color.
170      */
171     public final int getDefaultThemeColor(final Context context) {
172         return mPreferences.getInt(DEFAULT_THEME_COLOR,
173                 context.getResources().getColor(R.color.blue));
174     }
175
176     /**
177      * @return True if the user has checked to only download images on Wi-Fi,
178      *         false otherwise
179      */
180     public final boolean onlyOnWifi() {
181         return mPreferences.getBoolean(ONLY_ON_WIFI, true);
182     }
183
184     /**
185      * @return True if the user has checked to download missing album covers,
186      *         false otherwise.
187      */
188     public final boolean downloadMissingArtwork() {
189         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
190     }
191
192     /**
193      * @return True if the user has checked to download missing artist images,
194      *         false otherwise.
195      */
196     public final boolean downloadMissingArtistImages() {
197         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
198     }
199
200     /**
201      * Saves the sort order for a list.
202      *
203      * @param key Which sort order to change
204      * @param value The new sort order
205      */
206     private void setSortOrder(final String key, final String value) {
207         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
208             @Override
209             protected Void doInBackground(final Void... unused) {
210                 final SharedPreferences.Editor editor = mPreferences.edit();
211                 editor.putString(key, value);
212                 editor.apply();
213
214                 return null;
215             }
216         }, (Void[])null);
217     }
218
219     /**
220      * Sets the sort order for the artist list.
221      *
222      * @param value The new sort order
223      */
224     public void setArtistSortOrder(final String value) {
225         setSortOrder(ARTIST_SORT_ORDER, value);
226     }
227
228     /**
229      * @return The sort order used for the artist list in {@link ArtistFragment}
230      */
231     public final String getArtistSortOrder() {
232         return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
233     }
234
235     /**
236      * Sets the sort order for the artist song list.
237      *
238      * @param value The new sort order
239      */
240     public void setArtistSongSortOrder(final String value) {
241         setSortOrder(ARTIST_SONG_SORT_ORDER, value);
242     }
243
244     /**
245      * @return The sort order used for the artist song list in
246      *         {@link ArtistDetailSongAdapter}
247      */
248     public final String getArtistSongSortOrder() {
249         return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
250                 SortOrder.ArtistSongSortOrder.SONG_A_Z);
251     }
252
253     /**
254      * Sets the sort order for the artist album list.
255      *
256      * @param value The new sort order
257      */
258     public void setArtistAlbumSortOrder(final String value) {
259         setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
260     }
261
262     /**
263      * @return The sort order used for the artist album list in
264      *         {@link com.cyanogenmod.eleven.ui.fragments.ArtistDetailFragment}
265      */
266     public final String getArtistAlbumSortOrder() {
267         return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
268                 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
269     }
270
271     /**
272      * Sets the sort order for the album list.
273      *
274      * @param value The new sort order
275      */
276     public void setAlbumSortOrder(final String value) {
277         setSortOrder(ALBUM_SORT_ORDER, value);
278     }
279
280     /**
281      * @return The sort order used for the album list in {@link AlbumFragment}
282      */
283     public final String getAlbumSortOrder() {
284         return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
285     }
286
287     /**
288      * Sets the sort order for the album song list.
289      *
290      * @param value The new sort order
291      */
292     public void setAlbumSongSortOrder(final String value) {
293         setSortOrder(ALBUM_SONG_SORT_ORDER, value);
294     }
295
296     /**
297      * @return The sort order used for the album song in
298      *         {@link AlbumSongFragment}
299      */
300     public final String getAlbumSongSortOrder() {
301         return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
302                 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
303     }
304
305     /**
306      * Sets the sort order for the song list.
307      *
308      * @param value The new sort order
309      */
310     public void setSongSortOrder(final String value) {
311         setSortOrder(SONG_SORT_ORDER, value);
312     }
313
314     /**
315      * @return The sort order used for the song list in {@link SongFragment}
316      */
317     public final String getSongSortOrder() {
318         return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
319     }
320
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();
324     }
325
326     public long getLastAddedCutoff() {
327         return mPreferences.getLong(LAST_ADDED_CUTOFF, 0L);
328     }
329
330     /**
331      * @return Whether we want to show lyrics
332      */
333     public final boolean getShowLyrics() {
334         return mPreferences.getBoolean(SHOW_LYRICS, true);
335     }
336
337     public boolean getShowVisualizer() {
338         return mPreferences.getBoolean(SHOW_VISUALIZER, true);
339     }
340
341     public boolean getShakeToPlay() {
342         return mPreferences.getBoolean(SHAKE_TO_PLAY, false);
343     }
344
345     public boolean getShowAlbumArtOnLockscreen() {
346         return mPreferences.getBoolean(SHOW_ALBUM_ART_ON_LOCKSCREEN, true);
347     }
348 }