OSDN Git Service

Eleven: shake to play next song, only available while music is playing.
[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     private static PreferenceUtils sInstance;
85
86     private final SharedPreferences mPreferences;
87
88     /**
89      * Constructor for <code>PreferenceUtils</code>
90      * 
91      * @param context The {@link Context} to use.
92      */
93     public PreferenceUtils(final Context context) {
94         mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
95     }
96
97     /**
98      * @param context The {@link Context} to use.
99      * @return A singleton of this class
100      */
101     public static final PreferenceUtils getInstance(final Context context) {
102         if (sInstance == null) {
103             sInstance = new PreferenceUtils(context.getApplicationContext());
104         }
105         return sInstance;
106     }
107
108     /**
109      * Saves the current page the user is on when they close the app.
110      * 
111      * @param value The last page the pager was on when the onDestroy is called
112      *            in {@link MusicBrowserPhoneFragment}.
113      */
114     public void setStartPage(final int value) {
115         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
116             @Override
117             protected Void doInBackground(final Void... unused) {
118                 final SharedPreferences.Editor editor = mPreferences.edit();
119                 editor.putInt(START_PAGE, value);
120                 editor.apply();
121
122                 return null;
123             }
124         }, (Void[])null);
125     }
126     
127     /**
128      * Set the listener for preference change
129      * @param listener
130      */
131     public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
132         mPreferences.registerOnSharedPreferenceChangeListener(listener);
133     }
134
135     /**
136      * Returns the last page the user was on when the app was exited.
137      * 
138      * @return The page to start on when the app is opened.
139      */
140     public final int getStartPage() {
141         return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
142     }
143
144     /**
145      * Sets the new theme color.
146      * 
147      * @param value The new theme color to use.
148      */
149     public void setDefaultThemeColor(final int value) {
150         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
151             @Override
152             protected Void doInBackground(final Void... unused) {
153                 final SharedPreferences.Editor editor = mPreferences.edit();
154                 editor.putInt(DEFAULT_THEME_COLOR, value);
155                 editor.apply();
156
157                 return null;
158             }
159         }, (Void[])null);
160     }
161
162     /**
163      * Returns the current theme color.
164      * 
165      * @param context The {@link Context} to use.
166      * @return The default theme color.
167      */
168     public final int getDefaultThemeColor(final Context context) {
169         return mPreferences.getInt(DEFAULT_THEME_COLOR,
170                 context.getResources().getColor(R.color.holo_blue_light));
171     }
172
173     /**
174      * @return True if the user has checked to only download images on Wi-Fi,
175      *         false otherwise
176      */
177     public final boolean onlyOnWifi() {
178         return mPreferences.getBoolean(ONLY_ON_WIFI, true);
179     }
180
181     /**
182      * @return True if the user has checked to download missing album covers,
183      *         false otherwise.
184      */
185     public final boolean downloadMissingArtwork() {
186         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
187     }
188
189     /**
190      * @return True if the user has checked to download missing artist images,
191      *         false otherwise.
192      */
193     public final boolean downloadMissingArtistImages() {
194         return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
195     }
196
197     /**
198      * Saves the sort order for a list.
199      * 
200      * @param key Which sort order to change
201      * @param value The new sort order
202      */
203     private void setSortOrder(final String key, final String value) {
204         ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
205             @Override
206             protected Void doInBackground(final Void... unused) {
207                 final SharedPreferences.Editor editor = mPreferences.edit();
208                 editor.putString(key, value);
209                 editor.apply();
210
211                 return null;
212             }
213         }, (Void[])null);
214     }
215
216     /**
217      * Sets the sort order for the artist list.
218      * 
219      * @param value The new sort order
220      */
221     public void setArtistSortOrder(final String value) {
222         setSortOrder(ARTIST_SORT_ORDER, value);
223     }
224
225     /**
226      * @return The sort order used for the artist list in {@link ArtistFragment}
227      */
228     public final String getArtistSortOrder() {
229         return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
230     }
231
232     /**
233      * Sets the sort order for the artist song list.
234      * 
235      * @param value The new sort order
236      */
237     public void setArtistSongSortOrder(final String value) {
238         setSortOrder(ARTIST_SONG_SORT_ORDER, value);
239     }
240
241     /**
242      * @return The sort order used for the artist song list in
243      *         {@link ArtistDetailSongAdapter}
244      */
245     public final String getArtistSongSortOrder() {
246         return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
247                 SortOrder.ArtistSongSortOrder.SONG_A_Z);
248     }
249
250     /**
251      * Sets the sort order for the artist album list.
252      * 
253      * @param value The new sort order
254      */
255     public void setArtistAlbumSortOrder(final String value) {
256         setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
257     }
258
259     /**
260      * @return The sort order used for the artist album list in
261      *         {@link com.cyanogenmod.eleven.ui.fragments.ArtistDetailFragment}
262      */
263     public final String getArtistAlbumSortOrder() {
264         return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
265                 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
266     }
267
268     /**
269      * Sets the sort order for the album list.
270      * 
271      * @param value The new sort order
272      */
273     public void setAlbumSortOrder(final String value) {
274         setSortOrder(ALBUM_SORT_ORDER, value);
275     }
276
277     /**
278      * @return The sort order used for the album list in {@link AlbumFragment}
279      */
280     public final String getAlbumSortOrder() {
281         return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
282     }
283
284     /**
285      * Sets the sort order for the album song list.
286      * 
287      * @param value The new sort order
288      */
289     public void setAlbumSongSortOrder(final String value) {
290         setSortOrder(ALBUM_SONG_SORT_ORDER, value);
291     }
292
293     /**
294      * @return The sort order used for the album song in
295      *         {@link AlbumSongFragment}
296      */
297     public final String getAlbumSongSortOrder() {
298         return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
299                 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
300     }
301
302     /**
303      * Sets the sort order for the song list.
304      * 
305      * @param value The new sort order
306      */
307     public void setSongSortOrder(final String value) {
308         setSortOrder(SONG_SORT_ORDER, value);
309     }
310
311     /**
312      * @return The sort order used for the song list in {@link SongFragment}
313      */
314     public final String getSongSortOrder() {
315         return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
316     }
317
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();
321     }
322
323     public long getLastAddedCutoff() {
324         return mPreferences.getLong(LAST_ADDED_CUTOFF, 0L);
325     }
326
327     /**
328      * @return Whether we want to show lyrics
329      */
330     public final boolean getShowLyrics() {
331         return mPreferences.getBoolean(SHOW_LYRICS, true);
332     }
333
334     public boolean getShowVisualizer() {
335         return mPreferences.getBoolean(SHOW_VISUALIZER, true);
336     }
337     
338     public boolean getShakeToPlay() {
339         return mPreferences.getBoolean(SHAKE_TO_PLAY, false);
340     }
341 }