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.
12 package com.andrew.apollo.utils;
14 import android.content.Context;
15 import android.content.SharedPreferences;
16 import android.os.AsyncTask;
17 import android.preference.PreferenceManager;
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;
29 * A collection of helpers designed to get and set various preferences across
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public final class PreferenceUtils {
36 /* Default start page (Artist page) */
37 public static final int DEFFAULT_PAGE = 2;
39 /* Saves the last page the pager was on in {@link MusicBrowserPhoneFragment} */
40 public static final String START_PAGE = "start_page";
42 // Sort order for the artist list
43 public static final String ARTIST_SORT_ORDER = "artist_sort_order";
45 // Sort order for the artist song list
46 public static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order";
48 // Sort order for the artist album list
49 public static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order";
51 // Sort order for the album list
52 public static final String ALBUM_SORT_ORDER = "album_sort_order";
54 // Sort order for the album song list
55 public static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order";
57 // Sort order for the song list
58 public static final String SONG_SORT_ORDER = "song_sort_order";
60 // Sets the type of layout to use for the artist list
61 public static final String ARTIST_LAYOUT = "artist_layout";
63 // Sets the type of layout to use for the album list
64 public static final String ALBUM_LAYOUT = "album_layout";
66 // Sets the type of layout to use for the recent list
67 public static final String RECENT_LAYOUT = "recent_layout";
69 // Key used to download images only on Wi-Fi
70 public static final String ONLY_ON_WIFI = "only_on_wifi";
72 // Key that gives permissions to download missing album covers
73 public static final String DOWNLOAD_MISSING_ARTWORK = "download_missing_artwork";
75 // Key that gives permissions to download missing artist images
76 public static final String DOWNLOAD_MISSING_ARTIST_IMAGES = "download_missing_artist_images";
78 // Key used to set the overall theme color
79 public static final String DEFAULT_THEME_COLOR = "default_theme_color";
81 private static PreferenceUtils sInstance;
83 private final SharedPreferences mPreferences;
86 * Constructor for <code>PreferenceUtils</code>
88 * @param context The {@link Context} to use.
90 public PreferenceUtils(final Context context) {
91 mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
95 * @param context The {@link Context} to use.
96 * @return A singleton of this class
98 public static final PreferenceUtils getInstance(final Context context) {
99 if (sInstance == null) {
100 sInstance = new PreferenceUtils(context.getApplicationContext());
106 * Saves the current page the user is on when they close the app.
108 * @param value The last page the pager was on when the onDestroy is called
109 * in {@link MusicBrowserPhoneFragment}.
111 public void setStartPage(final int value) {
112 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
114 protected Void doInBackground(final Void... unused) {
115 final SharedPreferences.Editor editor = mPreferences.edit();
116 editor.putInt(START_PAGE, value);
125 * Returns the last page the user was on when the app was exited.
127 * @return The page to start on when the app is opened.
129 public final int getStartPage() {
130 return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
134 * Sets the new theme color.
136 * @param value The new theme color to use.
138 public void setDefaultThemeColor(final int value) {
139 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
141 protected Void doInBackground(final Void... unused) {
142 final SharedPreferences.Editor editor = mPreferences.edit();
143 editor.putInt(DEFAULT_THEME_COLOR, value);
152 * Returns the current theme color.
154 * @param context The {@link Context} to use.
155 * @return The default theme color.
157 public final int getDefaultThemeColor(final Context context) {
158 return mPreferences.getInt(DEFAULT_THEME_COLOR,
159 context.getResources().getColor(R.color.holo_blue_light));
163 * @return True if the user has checked to only download images on Wi-Fi,
166 public final boolean onlyOnWifi() {
167 return mPreferences.getBoolean(ONLY_ON_WIFI, true);
171 * @return True if the user has checked to download missing album covers,
174 public final boolean downloadMissingArtwork() {
175 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTWORK, true);
179 * @return True if the user has checked to download missing artist images,
182 public final boolean downloadMissingArtistImages() {
183 return mPreferences.getBoolean(DOWNLOAD_MISSING_ARTIST_IMAGES, true);
187 * Saves the sort order for a list.
189 * @param key Which sort order to change
190 * @param value The new sort order
192 private void setSortOrder(final String key, final String value) {
193 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
195 protected Void doInBackground(final Void... unused) {
196 final SharedPreferences.Editor editor = mPreferences.edit();
197 editor.putString(key, value);
206 * Sets the sort order for the artist list.
208 * @param value The new sort order
210 public void setArtistSortOrder(final String value) {
211 setSortOrder(ARTIST_SORT_ORDER, value);
215 * @return The sort order used for the artist list in {@link ArtistFragment}
217 public final String getArtistSortOrder() {
218 return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
222 * Sets the sort order for the artist song list.
224 * @param value The new sort order
226 public void setArtistSongSortOrder(final String value) {
227 setSortOrder(ARTIST_SONG_SORT_ORDER, value);
231 * @return The sort order used for the artist song list in
232 * {@link ArtistSongFragment}
234 public final String getArtistSongSortOrder() {
235 return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
236 SortOrder.ArtistSongSortOrder.SONG_A_Z);
240 * Sets the sort order for the artist album list.
242 * @param value The new sort order
244 public void setArtistAlbumSortOrder(final String value) {
245 setSortOrder(ARTIST_ALBUM_SORT_ORDER, value);
249 * @return The sort order used for the artist album list in
250 * {@link ArtistAlbumFragment}
252 public final String getArtistAlbumSortOrder() {
253 return mPreferences.getString(ARTIST_ALBUM_SORT_ORDER,
254 SortOrder.ArtistAlbumSortOrder.ALBUM_A_Z);
258 * Sets the sort order for the album list.
260 * @param value The new sort order
262 public void setAlbumSortOrder(final String value) {
263 setSortOrder(ALBUM_SORT_ORDER, value);
267 * @return The sort order used for the album list in {@link AlbumFragment}
269 public final String getAlbumSortOrder() {
270 return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
274 * Sets the sort order for the album song list.
276 * @param value The new sort order
278 public void setAlbumSongSortOrder(final String value) {
279 setSortOrder(ALBUM_SONG_SORT_ORDER, value);
283 * @return The sort order used for the album song in
284 * {@link AlbumSongFragment}
286 public final String getAlbumSongSortOrder() {
287 return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
288 SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
292 * Sets the sort order for the song list.
294 * @param value The new sort order
296 public void setSongSortOrder(final String value) {
297 setSortOrder(SONG_SORT_ORDER, value);
301 * @return The sort order used for the song list in {@link SongFragment}
303 public final String getSongSortOrder() {
304 return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
308 * Saves the layout type for a list
310 * @param key Which layout to change
311 * @param value The new layout type
313 private void setLayoutType(final String key, final String value) {
314 ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
316 protected Void doInBackground(final Void... unused) {
317 final SharedPreferences.Editor editor = mPreferences.edit();
318 editor.putString(key, value);
327 * Sets the layout type for the artist list
329 * @param value The new layout type
331 public void setArtistLayout(final String value) {
332 setLayoutType(ARTIST_LAYOUT, value);
336 * Sets the layout type for the album list
338 * @param value The new layout type
340 public void setAlbumLayout(final String value) {
341 setLayoutType(ALBUM_LAYOUT, value);
345 * Sets the layout type for the recent list
347 * @param value The new layout type
349 public void setRecentLayout(final String value) {
350 setLayoutType(RECENT_LAYOUT, value);
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.
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);
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.
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);
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.
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);