OSDN Git Service

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