Android Open Source - Calma Preference Utils






From Project

Back to project page Calma.

License

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.

Java Source Code

/*
 * 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");
    }
}




Java Source Code List

com.cyanogenmod.filemanager.activities.preferences.SettingsPreferences.java
com.espian.showcaseview.OnShowcaseEventListener.java
com.espian.showcaseview.ShowcaseViewBuilder.java
com.espian.showcaseview.ShowcaseView.java
com.espian.showcaseview.ShowcaseViews.java
com.espian.showcaseview.actionbar.ActionBarViewWrapper.java
com.espian.showcaseview.actionbar.reflection.ActionBarReflector.java
com.espian.showcaseview.actionbar.reflection.AppCompatReflector.java
com.espian.showcaseview.actionbar.reflection.BaseReflector.java
com.espian.showcaseview.actionbar.reflection.SherlockReflector.java
com.espian.showcaseview.anim.AnimationUtils.java
com.espian.showcaseview.drawing.ClingDrawerImpl.java
com.espian.showcaseview.drawing.ClingDrawer.java
com.espian.showcaseview.drawing.TextDrawerImpl.java
com.espian.showcaseview.drawing.TextDrawer.java
com.espian.showcaseview.targets.ActionItemTarget.java
com.espian.showcaseview.targets.ActionViewTarget.java
com.espian.showcaseview.targets.PointTarget.java
com.espian.showcaseview.targets.Target.java
com.espian.showcaseview.targets.ViewTarget.java
com.espian.showcaseview.utils.Calculator.java
com.espian.showcaseview.utils.PointAnimator.java
com.espian.showcaseview.utils.ShowcaseAreaCalculator.java
com.scto.android.calma.CalmaApp.java
com.scto.android.calma.activities.BaseActivity.java
com.scto.android.calma.activities.CalmaActivity.java
com.scto.android.calma.activities.SettingsActivity.java
com.scto.android.calma.activities.preferences.AboutPreferenceFragment.java
com.scto.android.calma.activities.preferences.DisplayPreferenceFragment.java
com.scto.android.calma.activities.preferences.GeneralPreferenceFragment.java
com.scto.android.calma.activities.preferences.NavigationPreferenceFragment.java
com.scto.android.calma.activities.preferences.SettingsActivity.java
com.scto.android.calma.activities.preferences.TitlePreferenceFragment.java
com.scto.android.calma.adapter.NavDrawerListAdapter.java
com.scto.android.calma.fragments.CommunityFragment.java
com.scto.android.calma.fragments.FindPeopleFragment.java
com.scto.android.calma.fragments.HomeFragment.java
com.scto.android.calma.fragments.PagesFragment.java
com.scto.android.calma.fragments.PhotosFragment.java
com.scto.android.calma.fragments.WhatsHotFragment.java
com.scto.android.calma.model.NavDrawerItem.java
com.scto.android.calma.preferences.AccessMode.java
com.scto.android.calma.preferences.CalmaSettings.java
com.scto.android.calma.preferences.ConfigurationListener.java
com.scto.android.calma.preferences.ObjectIdentifier.java
com.scto.android.calma.preferences.ObjectStringIdentifier.java
com.scto.android.calma.preferences.Preferences.java
com.scto.android.calma.ui.MyPrerference.java
com.scto.android.calma.utils.CalmaUtils.java
com.scto.android.calma.utils.Compress.java
com.scto.android.calma.utils.Decompress.java
com.scto.android.calma.utils.FileUtils.java
com.scto.android.calma.utils.LinuxShell.java
com.scto.android.calma.utils.PreferenceUtils.java
com.scto.android.calma.utils.SharedPreferencesCompat.java
com.scto.android.calma.utils.SortOrder.java
com.scto.android.calma.utils.Utils.java
com.stericson.RootTools.Constants.java
com.stericson.RootTools.RootTools.java
com.stericson.RootToolsTests.NativeJavaClass.java
com.stericson.RootToolsTests.SanityCheckRootTools.java
com.stericson.RootTools.containers.Mount.java
com.stericson.RootTools.containers.Permissions.java
com.stericson.RootTools.containers.RootClass.java
com.stericson.RootTools.containers.Symlink.java
com.stericson.RootTools.exceptions.RootDeniedException.java
com.stericson.RootTools.execution.CommandCapture.java
com.stericson.RootTools.execution.Command.java
com.stericson.RootTools.execution.JavaCommandCapture.java
com.stericson.RootTools.execution.Shell.java
com.stericson.RootTools.internal.Installer.java
com.stericson.RootTools.internal.InternalVariables.java
com.stericson.RootTools.internal.Remounter.java
com.stericson.RootTools.internal.RootToolsInternalMethods.java
com.stericson.RootTools.internal.Runner.java