Back to project page Calma.
The source code is released under:
Apache License
If you think the Android project Calma listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 Thomas Schmid/* w w w .j a v a 2s . c o m*/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ package com.scto.android.calma.utils; import com.scto.android.calma.R; import com.scto.android.calma.CalmaApp; import com.scto.android.calma.utils.SortOrder; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; /** * A collection of helpers designed to get and set various preferences across * Calma. * * @author Thomas (tschmid35@gmail.com) */ public final class PreferenceUtils { /* Default start path (Home path) */ public static final String DEFFAULT_PATH = "/storage/sdcard"; /* Saves the last path the app was on in {@link ContentFragment} */ public static final String START_PATH = "start_path"; // Sort order for the file system list public static final String SORT_ORDER = "sort_order"; // Sets the type of layout to use for the file system list public static final String CONTENT_LAYOUT = "content_layout"; // Sets the type of layout to use for the recent list public static final String RECENT_LAYOUT = "recent_layout"; // The key for theme public static final String THEME = "theme"; // Sets the type of theme to use for the app (Holo.Light) public static final String LIGHT_THEME = "theme_light"; // Sets the type of theme to use for the app (Holo) public static final String DARK_THEME = "theme_dark"; // Sets the type of theme to use for the app (Holo.Light.DarkAcionBar) public static final String DARK_ACTIONBAR_THEME = "theme_dark_actionbar"; // The key for showcase public static final String SHOWCASE = "showcase"; // Sets the type of showcase to use for the app (Welcome etc.) public static final String SHOWCASE_WELCOME = "showcase_welcome"; private static PreferenceUtils sInstance; private final SharedPreferences mPreferences; /** * Constructor for <code>PreferenceUtils</code> * * @param context The {@link Context} to use. */ public PreferenceUtils(final Context context) { mPreferences = PreferenceManager.getDefaultSharedPreferences(context); } /** * @param context The {@link Context} to use. * @return A singelton of this class */ public static final PreferenceUtils getInstace(final Context context) { if (sInstance == null) { sInstance = new PreferenceUtils(context.getApplicationContext()); } return sInstance; } /** * Saves the current path the user is on when they close the app. * * @param value The last path the app was on when the onDestroy is called * in {@link ContentFragment}. */ public void setStartPath(final String path) { CalmaUtils.execute(false, new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(START_PATH, path); SharedPreferencesCompat.apply(editor); return null; } }, (Void[])null); } /** * Returns the last path the user was on when the app was exited. * * @return The path to start on when the app is opened. */ public final String getStartPath() { return mPreferences.getString(START_PATH, DEFFAULT_PATH); } /** * Saves the sort order for a list. * * @param key Which sort order to change * @param value The new sort order */ private void setSortOrder(final String key, final String value) { CalmaUtils.execute(false, new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(key, value); SharedPreferencesCompat.apply(editor); return null; } }, (Void[])null); } /** * Sets the sort order for the file system list in {@link ContentFragment}. * * @param value The new sort order */ public void setSortOrder(final String value) { setSortOrder(SORT_ORDER, value); } /** * @return The sort order used for file system list in {@link ContentFragment} */ public final String getSortOrder() { return mPreferences.getString(SORT_ORDER, SortOrder.SortOrder.A_Z); } /** * Saves the layout type for a list * * @param key Which layout to change * @param value The new layout type */ private void setLayoutType(final String key, final String value) { CalmaUtils.execute(false, new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(key, value); SharedPreferencesCompat.apply(editor); return null; } }, (Void[])null); } /** * Sets the layout type for the file system list * * @param value The new layout type */ public void setContentLayout(final String value) { setLayoutType(CONTENT_LAYOUT, value); } /** * @param context The {@link Context} to use. * @param which Which list to check. * @return True if the layout type is the simple layout, false otherwise. */ public boolean isSimpleLayout(final String which) { final String simple = "simple"; final String defaultValue = "list"; return mPreferences.getString(which, defaultValue).equals(simple); } /** * @param context The {@link Context} to use. * @param which Which list to check. * @return True if the layout type is the simple layout, false otherwise. */ public boolean isDetailedLayout(final String which) { final String detailed = "detailed"; final String defaultValue = "list"; return mPreferences.getString(which, defaultValue).equals(detailed); } /** * @param context The {@link Context} to use. * @param which Which list to check. * @return True if the layout type is the simple layout, false otherwise. */ public boolean isGridLayout(final String which) { final String grid = "grid"; final String defaultValue = "list"; return mPreferences.getString(which, defaultValue).equals(grid); } /** * Saves the theme type for a list * * @param key Which theme to change * @param value The new theme type */ private void setTheme(final String key, final String value) { CalmaUtils.execute(false, new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(key, value); SharedPreferencesCompat.apply(editor); return null; } }, (Void[])null); } /** * Sets the theme for the app. * * @param value The new theme type */ public void setTheme(final String value) { setTheme(THEME, value); } /** * Returns the last theme the user was on when the app was exited. * * @return The theme to start on when the app is opened. */ public final int getTheme() { String theme = mPreferences.getString(THEME, LIGHT_THEME); if (LIGHT_THEME.equalsIgnoreCase(theme)) { return CalmaApp.THEME_WHITE; } else if (DARK_ACTIONBAR_THEME.equalsIgnoreCase(theme)) { return CalmaApp.THEME_WHITE_BLACK; } else { return CalmaApp.THEME_BLACK; } } /** * Returns the last theme the user was on when the app was exited. * * @return The theme to start on when the app is opened. */ public final String getThemeString() { return mPreferences.getString(THEME, LIGHT_THEME); } /** * Saves the showcase type for a list * * @param key Which showcase type to change * @param value The new showcase state */ private void setShowcase(final String key, final String value) { CalmaUtils.execute(false, new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(key, value); SharedPreferencesCompat.apply(editor); return null; } }, (Void[])null); } /** * Sets the showcase type. * * @param value The new showcase state */ public void setShowcaseWelcome(final String value) { setShowcase(SHOWCASE_WELCOME, value); } /** * Returns the showcase welcome state. * * @return The showcase welcome state. */ public final String getShowcaseWelcome() { return mPreferences.getString(SHOWCASE_WELCOME, "1"); } }