OSDN Git Service

17b25bd9890175250cf42c1c443725b629627f4b
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / utils / PreferenceUtils.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.andrew.apollo.utils;
13
14 import android.content.Context;
15 import android.content.SharedPreferences;
16 import android.os.AsyncTask;
17 import android.preference.PreferenceManager;
18
19 import com.andrew.apollo.R;
20 import com.andrew.apollo.ui.fragments.AlbumFragment;
21 import com.andrew.apollo.ui.fragments.ArtistFragment;
22 import com.andrew.apollo.ui.fragments.SongFragment;
23 import com.andrew.apollo.ui.fragments.phone.MusicBrowserPhoneFragment;
24 import com.andrew.apollo.ui.fragments.profile.AlbumSongFragment;
25 import com.andrew.apollo.ui.fragments.profile.ArtistAlbumFragment;
26 import com.andrew.apollo.ui.fragments.profile.ArtistSongFragment;
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     // Sets the type of layout to use for the artist list
61     public static final String ARTIST_LAYOUT = "artist_layout";
62
63     // Sets the type of layout to use for the album list
64     public static final String ALBUM_LAYOUT = "album_layout";
65
66     // Sets the type of layout to use for the recent list
67     public static final String RECENT_LAYOUT = "recent_layout";
68
69     // Key used to download images only on Wi-Fi
70     public static final String ONLY_ON_WIFI = "only_on_wifi";
71
72     // Key that gives permissions to download missing album covers
73     public static final String DOWNLOAD_MISSING_ARTWORK = "download_missing_artwork";
74
75     // Key that gives permissions to download missing artist images
76     public static final String DOWNLOAD_MISSING_ARTIST_IMAGES = "download_missing_artist_images";
77
78     // Key used to set the overall theme color
79     public static final String DEFAULT_THEME_COLOR = "default_theme_color";
80
81     private static PreferenceUtils sInstance;
82
83     private final SharedPreferences mPreferences;
84
85     /**
86      * Constructor for <code>PreferenceUtils</code>
87      * 
88      * @param context The {@link Context} to use.
89      */
90     public PreferenceUtils(final Context context) {
91         mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
92     }
93
94     /**
95      * @param context The {@link Context} to use.
96      * @return A singleton of this class
97      */
98     public static final PreferenceUtils getInstance(final Context context) {
99         if (sInstance == null) {
100             sInstance = new PreferenceUtils(context.getApplicationContext());
101         }
102         return sInstance;
103     }
104
105     /**
106      * Saves the current page the user is on when they close the app.
107      * 
108      * @param value The last page the pager was on when the onDestroy is called
109      *            in {@link MusicBrowserPhoneFragment}.
110      */
111     public void setStartPage(final int value) {
112         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
113             @Override
114             protected Void doInBackground(final Void... unused) {
115                 final SharedPreferences.Editor editor = mPreferences.edit();
116                 editor.putInt(START_PAGE, value);
117                 editor.apply();
118
119                 return null;
120             }
121         }, (Void[])null);
122     }
123
124     /**
125      * Returns the last page the user was on when the app was exited.
126      * 
127      * @return The page to start on when the app is opened.
128      */
129     public final int getStartPage() {
130         return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
131     }
132
133     /**
134      * Sets the new theme color.
135      * 
136      * @param value The new theme color to use.
137      */
138     public void setDefaultThemeColor(final int value) {
139         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
140             @Override
141             protected Void doInBackground(final Void... unused) {
142                 final SharedPreferences.Editor editor = mPreferences.edit();
143                 editor.putInt(DEFAULT_THEME_COLOR, value);
144                 editor.apply();
145
146                 return null;
147             }
148         }, (Void[])null);
149     }
150
151     /**
152      * Returns the current theme color.
153      * 
154      * @param context The {@link Context} to use.
155      * @return The default theme color.
156      */
157     public final int getDefaultThemeColor(final Context context) {
158         return mPreferences.getInt(DEFAULT_THEME_COLOR,
159                 context.getResources().getColor(R.color.holo_blue_light));
160     }
161
162     /**
163      * @return True if the user has checked to only download images on Wi-Fi,
164      *         false otherwise
165      */
166     public final boolean onlyOnWifi() {
167         return mPreferences.getBoolean(ONLY_ON_WIFI, true);
168     }
169
170     /**
171      * @return True if the user has checked to download missing album covers,
172      *         false otherwise.
173      */
174     public final boolean downloadMissingArtwork() {
175         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
176     }
177
178     /**
179      * @return True if the user has checked to download missing artist images,
180      *         false otherwise.
181      */
182     public final boolean downloadMissingArtistImages() {
183         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
184     }
185
186     /**
187      * Saves the sort order for a list.
188      * 
189      * @param key Which sort order to change
190      * @param value The new sort order
191      */
192     private void setSortOrder(final String key, final String value) {
193         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
194             @Override
195             protected Void doInBackground(final Void... unused) {
196                 final SharedPreferences.Editor editor = mPreferences.edit();
197                 editor.putString(key, value);
198                 editor.apply();
199
200                 return null;
201             }
202         }, (Void[])null);
203     }
204
205     /**
206      * Sets the sort order for the artist list.
207      * 
208      * @param value The new sort order
209      */
210     public void setArtistSortOrder(final String value) {
211         setSortOrder(ARTIST_SORT_ORDER, value);
212     }
213
214     /**
215      * @return The sort order used for the artist list in {@link ArtistFragment}
216      */
217     public final String getArtistSortOrder() {
218         return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
219     }
220
221     /**
222      * Sets the sort order for the artist song list.
223      * 
224      * @param value The new sort order
225      */
226     public void setArtistSongSortOrder(final String value) {
227         setSortOrder(ARTIST_SONG_SORT_ORDER, value);
228     }
229
230     /**
231      * @return The sort order used for the artist song list in
232      *         {@link ArtistSongFragment}
233      */
234     public final String getArtistSongSortOrder() {
235         return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
236                 SortOrder.ArtistSongSortOrder.SONG_A_Z);
237     }
238
239     /**
240      * Sets the sort order for the artist album list.
241      * 
242      * @param value The new sort order
243      */
244     public void setArtistAlbumSortOrder(final String value) {
245         setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
246     }
247
248     /**
249      * @return The sort order used for the artist album list in
250      *         {@link ArtistAlbumFragment}
251      */
252     public final String getArtistAlbumSortOrder() {
253         return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
254                 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
255     }
256
257     /**
258      * Sets the sort order for the album list.
259      * 
260      * @param value The new sort order
261      */
262     public void setAlbumSortOrder(final String value) {
263         setSortOrder(ALBUM_SORT_ORDER, value);
264     }
265
266     /**
267      * @return The sort order used for the album list in {@link AlbumFragment}
268      */
269     public final String getAlbumSortOrder() {
270         return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
271     }
272
273     /**
274      * Sets the sort order for the album song list.
275      * 
276      * @param value The new sort order
277      */
278     public void setAlbumSongSortOrder(final String value) {
279         setSortOrder(ALBUM_SONG_SORT_ORDER, value);
280     }
281
282     /**
283      * @return The sort order used for the album song in
284      *         {@link AlbumSongFragment}
285      */
286     public final String getAlbumSongSortOrder() {
287         return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
288                 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
289     }
290
291     /**
292      * Sets the sort order for the song list.
293      * 
294      * @param value The new sort order
295      */
296     public void setSongSortOrder(final String value) {
297         setSortOrder(SONG_SORT_ORDER, value);
298     }
299
300     /**
301      * @return The sort order used for the song list in {@link SongFragment}
302      */
303     public final String getSongSortOrder() {
304         return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
305     }
306
307     /**
308      * Saves the layout type for a list
309      * 
310      * @param key Which layout to change
311      * @param value The new layout type
312      */
313     private void setLayoutType(final String key, final String value) {
314         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
315             @Override
316             protected Void doInBackground(final Void... unused) {
317                 final SharedPreferences.Editor editor = mPreferences.edit();
318                 editor.putString(key, value);
319                 editor.apply();
320
321                 return null;
322             }
323         }, (Void[])null);
324     }
325
326     /**
327      * Sets the layout type for the artist list
328      * 
329      * @param value The new layout type
330      */
331     public void setArtistLayout(final String value) {
332         setLayoutType(ARTIST_LAYOUT, value);
333     }
334
335     /**
336      * Sets the layout type for the album list
337      * 
338      * @param value The new layout type
339      */
340     public void setAlbumLayout(final String value) {
341         setLayoutType(ALBUM_LAYOUT, value);
342     }
343
344     /**
345      * Sets the layout type for the recent list
346      * 
347      * @param value The new layout type
348      */
349     public void setRecentLayout(final String value) {
350         setLayoutType(RECENT_LAYOUT, value);
351     }
352
353     /**
354      * @param context The {@link Context} to use.
355      * @param which Which list to check.
356      * @return True if the layout type is the simple layout, false otherwise.
357      */
358     public boolean isSimpleLayout(final String which, final Context context) {
359         final String simple = "simple";
360         final String defaultValue = "grid";
361         return mPreferences.getString(which, defaultValue).equals(simple);
362     }
363
364     /**
365      * @param context The {@link Context} to use.
366      * @param which Which list to check.
367      * @return True if the layout type is the simple layout, false otherwise.
368      */
369     public boolean isDetailedLayout(final String which, final Context context) {
370         final String detailed = "detailed";
371         final String defaultValue = "grid";
372         return mPreferences.getString(which, defaultValue).equals(detailed);
373     }
374
375     /**
376      * @param context The {@link Context} to use.
377      * @param which Which list to check.
378      * @return True if the layout type is the simple layout, false otherwise.
379      */
380     public boolean isGridLayout(final String which, final Context context) {
381         final String grid = "grid";
382         final String defaultValue = "simple";
383         return mPreferences.getString(which, defaultValue).equals(grid);
384     }
385
386 }