XgimiSettingsSP.java 11.1 KB
package com.xgimi.smartscreen.service;

import android.content.Context;
import android.content.SharedPreferences;

public class XgimiSettingsSP {

    private static final String PREFERENCE_NAME = "local_settings";  //sp名字

    private static final String PKG_NAME = "com.android.settings";  //设置包名

    public static final String ID_USER_NAME = "xgimi_out_username";


    /**
     * put string preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putString(Context context, String key, String value) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString(key, value);
            return editor.commit();
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * get string preferences
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws
     * ClassCastException if there is a preference with this name that
     * is not a string
     * @see #getString(Context, String, String)
     */
    public static String getString(Context context, String key) {
        return getString(context, key, "");
    }

    /**
     * get string preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that
     * is not a string
     */
    public static String getString(Context context, String key, String defaultValue) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences share = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
            return share.getString(key, defaultValue);
        } catch (Exception e) {

        }
        return defaultValue;
    }

    /**
     * put int preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putInt(Context context, String key, int value) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt(key, value);
            return editor.commit();
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * get int preferences
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     * ClassCastException if there is a preference with this name that
     * is not a int
     * @see #getInt(Context, String, int)
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    /**
     * get int preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that
     * is not a int
     */
    public static int getInt(Context context, String key, int defaultValue) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences share = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
            return share.getInt(key, defaultValue);
        } catch (Exception e) {

        }
        return defaultValue;
    }

    /**
     * put long preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putLong(Context context, String key, long value) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = settings.edit();
            editor.putLong(key, value);
            return editor.commit();
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * get long preferences
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     * ClassCastException if there is a preference with this name that
     * is not a long
     * @see #getLong(Context, String, long)
     */
    public static long getLong(Context context, String key) {
        return getLong(context, key, -1);
    }

    /**
     * get long preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that
     * is not a long
     */
    public static long getLong(Context context, String key, long defaultValue) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences share = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
            return share.getLong(key, defaultValue);
        } catch (Exception e) {

        }
        return defaultValue;
    }

    /**
     * put float preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putFloat(Context context, String key, float value) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = settings.edit();
            editor.putFloat(key, value);
            return editor.commit();
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * get float preferences
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     * ClassCastException if there is a preference with this name that
     * is not a float
     * @see #getFloat(Context, String, float)
     */
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1);
    }

    /**
     * get float preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that
     * is not a float
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences share = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
            return share.getFloat(key, defaultValue);
        } catch (Exception e) {

        }
        return defaultValue;
    }

    /**
     * put boolean preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putBoolean(Context context, String key, boolean value) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean(key, value);
            return editor.commit();
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * get boolean preferences, default is false
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws
     * ClassCastException if there is a preference with this name that
     * is not a boolean
     * @see #getBoolean(Context, String, boolean)
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * get boolean preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that
     * is not a boolean
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        try {
            Context otherAppsContext = context.createPackageContext(PKG_NAME, Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences share = otherAppsContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
            return share.getBoolean(key, defaultValue);
        } catch (Exception e) {

        }
        return defaultValue;
    }
}