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 org.lineageos.eleven.utils;
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;
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;
32 * A collection of helpers designed to get and set various preferences across
35 * @author Andrew Neal (andrewdneal@gmail.com)
37 public final class PreferenceUtils {
39 /* Default start page (Artist page) */
40 public static final int DEFFAULT_PAGE = 2;
42 /* Saves the last page the pager was on in {@link MusicBrowserPhoneFragment} */
43 public static final String START_PAGE = "start_page";
45 // Sort order for the artist list
46 public static final String ARTIST_SORT_ORDER = "artist_sort_order";
48 // Sort order for the artist song list
49 public static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order";
51 // Sort order for the artist album list
52 public static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order";
54 // Sort order for the album list
55 public static final String ALBUM_SORT_ORDER = "album_sort_order";
57 // Sort order for the album song list
58 public static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order";
60 // Sort order for the song list
61 public static final String SONG_SORT_ORDER = "song_sort_order";
63 // Key used to download images only on Wi-Fi
64 public static final String ONLY_ON_WIFI = "only_on_wifi";
66 // Key that gives permissions to download missing album covers
67 public static final String DOWNLOAD_MISSING_ARTWORK = "download_missing_artwork";
69 // Key that gives permissions to download missing artist images
70 public static final String DOWNLOAD_MISSING_ARTIST_IMAGES = "download_missing_artist_images";
72 // Key used to set the overall theme color
73 public static final String DEFAULT_THEME_COLOR = "default_theme_color";
75 // datetime cutoff for determining which songs go in last added playlist
76 public static final String LAST_ADDED_CUTOFF = "last_added_cutoff";
79 public static final String SHOW_LYRICS = "show_lyrics";
81 // show visualizer flag
82 public static final String SHOW_VISUALIZER = "music_visualization";
85 public static final String SHAKE_TO_PLAY = "shake_to_play";
87 // show/hide album art on lockscreen
88 public static final String SHOW_ALBUM_ART_ON_LOCKSCREEN = "lockscreen_album_art";
90 private static final int PERMISSION_REQUEST_RECORD_AUDIO = 1;
92 private static PreferenceUtils sInstance;
94 private final SharedPreferences mPreferences;
97 * Constructor for <code>PreferenceUtils</code>
99 * @param context The {@link Context} to use.
101 public PreferenceUtils(final Context context) {
102 mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
106 * @param context The {@link Context} to use.
107 * @return A singleton of this class
109 public static final PreferenceUtils getInstance(final Context context) {
110 if (sInstance == null) {
111 sInstance = new PreferenceUtils(context.getApplicationContext());
117 * Saves the current page the user is on when they close the app.
119 * @param value The last page the pager was on when the onDestroy is called
120 * in {@link MusicBrowserPhoneFragment}.
122 public void setStartPage(final int value) {
123 ElevenUtils.execute(false, new AsyncTask<Void, Void, Void>() {
125 protected Void doInBackground(final Void... unused) {
126 final SharedPreferences.Editor editor = mPreferences.edit();
127 editor.putInt(START_PAGE, value);
136 * Set the listener for preference change
139 public void setOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
140 mPreferences.registerOnSharedPreferenceChangeListener(listener);
144 * Returns the last page the user was on when the app was exited.
146 * @return The page to start on when the app is opened.
148 public final int getStartPage() {
149 return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
153 * Sets the new theme color.
155 * @param value The new theme color to use.
157 public void setDefaultThemeColor(final int value) {
158 ElevenUtils.execute(false, new AsyncTask<Void, Void, Void>() {
160 protected Void doInBackground(final Void... unused) {
161 final SharedPreferences.Editor editor = mPreferences.edit();
162 editor.putInt(DEFAULT_THEME_COLOR, value);
171 * Returns the current theme color.
173 * @param context The {@link Context} to use.
174 * @return The default theme color.
176 public final int getDefaultThemeColor(final Context context) {
177 return mPreferences.getInt(DEFAULT_THEME_COLOR,
178 context.getResources().getColor(R.color.blue));
182 * @return True if the user has checked to only download images on Wi-Fi,
185 public final boolean onlyOnWifi() {
186 return mPreferences.getBoolean(ONLY_ON_WIFI, true);
190 * @return True if the user has checked to download missing album covers,
193 public final boolean downloadMissingArtwork() {
194 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
198 * @return True if the user has checked to download missing artist images,
201 public final boolean downloadMissingArtistImages() {
202 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
206 * Saves the sort order for a list.
208 * @param key Which sort order to change
209 * @param value The new sort order
211 private void setSortOrder(final String key, final String value) {
212 ElevenUtils.execute(false, new AsyncTask<Void, Void, Void>() {
214 protected Void doInBackground(final Void... unused) {
215 final SharedPreferences.Editor editor = mPreferences.edit();
216 editor.putString(key, value);
225 * Sets the sort order for the artist list.
227 * @param value The new sort order
229 public void setArtistSortOrder(final String value) {
230 setSortOrder(ARTIST_SORT_ORDER, value);
234 * @return The sort order used for the artist list in {@link ArtistFragment}
236 public final String getArtistSortOrder() {
237 return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
241 * Sets the sort order for the artist song list.
243 * @param value The new sort order
245 public void setArtistSongSortOrder(final String value) {
246 setSortOrder(ARTIST_SONG_SORT_ORDER, value);
250 * @return The sort order used for the artist song list in
251 * {@link ArtistDetailSongAdapter}
253 public final String getArtistSongSortOrder() {
254 return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
255 SortOrder.ArtistSongSortOrder.SONG_A_Z);
259 * Sets the sort order for the artist album list.
261 * @param value The new sort order
263 public void setArtistAlbumSortOrder(final String value) {
264 setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
268 * @return The sort order used for the artist album list in
269 * {@link org.lineageos.eleven.ui.fragments.ArtistDetailFragment}
271 public final String getArtistAlbumSortOrder() {
272 return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
273 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
277 * Sets the sort order for the album list.
279 * @param value The new sort order
281 public void setAlbumSortOrder(final String value) {
282 setSortOrder(ALBUM_SORT_ORDER, value);
286 * @return The sort order used for the album list in {@link AlbumFragment}
288 public final String getAlbumSortOrder() {
289 return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
293 * Sets the sort order for the album song list.
295 * @param value The new sort order
297 public void setAlbumSongSortOrder(final String value) {
298 setSortOrder(ALBUM_SONG_SORT_ORDER, value);
302 * @return The sort order used for the album song in
303 * {@link AlbumSongFragment}
305 public final String getAlbumSongSortOrder() {
306 return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
307 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
311 * Sets the sort order for the song list.
313 * @param value The new sort order
315 public void setSongSortOrder(final String value) {
316 setSortOrder(SONG_SORT_ORDER, value);
320 * @return The sort order used for the song list in {@link SongFragment}
322 public final String getSongSortOrder() {
323 return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
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();
331 public long getLastAddedCutoff() {
332 return mPreferences.getLong(LAST_ADDED_CUTOFF, 0L);
336 * @return Whether we want to show lyrics
338 public final boolean getShowLyrics() {
339 return mPreferences.getBoolean(SHOW_LYRICS, true);
342 public static boolean canRecordAudio(Activity activity) {
343 return activity.checkSelfPermission(permission.RECORD_AUDIO) ==
344 PackageManager.PERMISSION_GRANTED;
347 public static void requestRecordAudio(Activity activity) {
348 activity.requestPermissions(
349 new String[] {permission.RECORD_AUDIO},
350 PERMISSION_REQUEST_RECORD_AUDIO);
353 public boolean getShowVisualizer() {
354 return mPreferences.getBoolean(SHOW_VISUALIZER, false);
357 public boolean getShakeToPlay() {
358 return mPreferences.getBoolean(SHAKE_TO_PLAY, false);
361 public boolean getShowAlbumArtOnLockscreen() {
362 return mPreferences.getBoolean(SHOW_ALBUM_ART_ON_LOCKSCREEN, true);