Android Open Source - Calma Preferences






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. java 2s. com*/
 *
 * 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.preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.scto.android.calma.CalmaApp;

import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A helper class for access and manage the preferences of the application.
 */
public final class Preferences {

    private static final String TAG = "Preferences"; //$NON-NLS-1$

    /**
     * A variable that holds the term of the last textual search.
     */
    private static String sLastSearch = ""; //$NON-NLS-1$

    /**
     * The name of the file manager settings file.
     * @hide
     */
    public static final String SETTINGS_FILENAME = "com.scto.android.calma"; //$NON-NLS-1$

    /**
     * The list of configuration listeners.
     */
    private static final List<ConfigurationListener> CONFIGURATION_LISTENERS =
            Collections.synchronizedList(new ArrayList<ConfigurationListener>());


    /**
     * Constructor of <code>Preferences</code>.
     */
    private Preferences() {
        super();
    }

    /**
     * Method that returns the last search do in the current application execution.
     *
     * @return String The last search do in the current application execution
     */
    public static String getLastSearch() {
        return sLastSearch;
    }

    /**
     * Method that sets the last search do in the current application execution.
     *
     * @param lastSearch The last search do in the current application execution
     */
    public static void setLastSearch(String lastSearch) {
        Preferences.sLastSearch = lastSearch;
    }

    /**
     * Method that initializes the defaults preferences of the application.
     */
    public static void loadDefaults() {
        //Sets the default preferences if no value is set yet
        try {
            Map<CalmaSettings, Object> defaultPrefs =
                    new HashMap<CalmaSettings, Object>();
            CalmaSettings[] values = CalmaSettings.values();
            int cc = values.length;
            for (int i = 0; i < cc; i++) {
                defaultPrefs.put(values[i], values[i].getDefaultValue());
            }
            savePreferences(defaultPrefs, false, true);
        } catch (Exception ex) {
            Log.e(TAG, "Save default settings fails", ex); //$NON-NLS-1$
        }
    }

    /**
     * Method that adds a new configuration listener.
     *
     * @param listener The new configuration listener
     */
    public static void addConfigurationListener(ConfigurationListener listener) {
        CONFIGURATION_LISTENERS.add(listener);
    }

    /**
     * Method that removes the configuration listener.
     *
     * @param listener The configuration listener to be removed
     */
    public static void removeConfigurationListener(ConfigurationListener listener) {
        CONFIGURATION_LISTENERS.remove(listener);
    }

    /**
     * Method that returns the shared preferences of the application.
     *
     * @return SharedPreferences The shared preferences of the application
     * @hide
     */
    public static SharedPreferences getSharedPreferences() {
        return CalmaApp.getInstance().getSharedPreferences(
                SETTINGS_FILENAME, Context.MODE_PRIVATE);
    }

    /**
     * Method that saves a preference.
     *
     * @param pref The preference identifier
     * @param value The value of the preference
     * @param applied If the preference was applied
     * @throws InvalidClassException If the value of the preference is not of the
     * type of the preference
     */
    public static void savePreference(CalmaSettings pref, Object value, boolean applied)
            throws InvalidClassException {
        Map<CalmaSettings, Object> prefs =
                new HashMap<CalmaSettings, Object>();
        prefs.put(pref, value);
        savePreferences(prefs, applied);
    }

    /**
     * Method that saves the preferences passed as argument.
     *
     * @param prefs The preferences to be saved
     * @param applied If the preference was applied
     * @throws InvalidClassException If the value of a preference is not of the
     * type of the preference
     */
    public static void savePreferences(Map<CalmaSettings, Object> prefs, boolean applied)
            throws InvalidClassException {
        savePreferences(prefs, true, applied);
    }

    /**
     * Method that saves the preferences passed as argument.
     *
     * @param prefs The preferences to be saved
     * @param noSaveIfExists No saves if the preference if has a value
     * @param applied If the preference was applied
     * @throws InvalidClassException If the value of a preference is not of the
     * type of the preference
     */
    @SuppressWarnings("unchecked")
    private static void savePreferences(
            Map<CalmaSettings, Object> prefs, boolean noSaveIfExists, boolean applied)
            throws InvalidClassException {
        //Get the preferences editor
        SharedPreferences sp = getSharedPreferences();
        Editor editor = sp.edit();

        //Save all settings
        Iterator<CalmaSettings> it = prefs.keySet().iterator();
        while (it.hasNext()) {
            CalmaSettings pref = it.next();
            if (!noSaveIfExists && sp.contains(pref.getId())) {
                //The preference already has a value
                continue;
            }

            //Known and valid types
            Object value = prefs.get(pref);
            if (value instanceof Boolean && pref.getDefaultValue() instanceof Boolean) {
                editor.putBoolean(pref.getId(), ((Boolean)value).booleanValue());
            } else if (value instanceof String && pref.getDefaultValue() instanceof String) {
                editor.putString(pref.getId(), (String)value);
            } else if (value instanceof Set && pref.getDefaultValue() instanceof Set) {
                editor.putStringSet(pref.getId(), (Set<String>)value);
            } else if (value instanceof ObjectIdentifier
                    && pref.getDefaultValue() instanceof ObjectIdentifier) {
                editor.putInt(pref.getId(), ((ObjectIdentifier)value).getId());
            } else if (value instanceof ObjectStringIdentifier
                    && pref.getDefaultValue() instanceof ObjectStringIdentifier) {
                editor.putString(pref.getId(), ((ObjectStringIdentifier)value).getId());
            } else {
                //The object is not of the appropriate type
                String msg = String.format(
                                    "%s: %s",  //$NON-NLS-1$
                                    pref.getId(),
                                    value.getClass().getName());
                Log.e(TAG, String.format(
                                "Configuration error. InvalidClassException: %s",  //$NON-NLS-1$
                                msg));
                throw new InvalidClassException(msg);
            }
        }

        //Commit settings
        editor.commit();

        //Now its time to communicate the configuration change
        if (CONFIGURATION_LISTENERS != null && CONFIGURATION_LISTENERS.size() > 0) {
            it = prefs.keySet().iterator();
            while (it.hasNext()) {
                CalmaSettings pref = it.next();
                Object value = prefs.get(pref);
                int cc = CONFIGURATION_LISTENERS.size();
                for (int i = 0; i < cc; i++) {
                    CONFIGURATION_LISTENERS.get(i).onConfigurationChanged(pref, value, applied);
                }
            }
        }
    }
}




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