Android Open Source - android-utils Setting Utils






From Project

Back to project page android-utils.

License

The source code is released under:

Apache License

If you think the Android project android-utils 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 2014 Zhenguo Jin (jingle1267@163.com)
 *//from  w w  w. j av a2 s  .  c  om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.worthed.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * ????????
 *
 * @author jingle1267@163.com
 */
@SuppressLint("CommitPrefEdits")
public class SettingUtils {

    private final static boolean DEBUG = true;
    private final static String TAG = "SettingUtils";

    public static boolean contains(Context context, int resId) {
        return contains(context, context.getString(resId));
    }

    public static boolean contains(Context context, String key) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.contains(key);
    }

    public static void remove(Context context, int resId) {
        remove(context, context.getString(resId));
    }

    public static void remove(Context context, String key) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove(key);
        commitOrApply(editor);
    }

    public static void set(Context context, int resId, boolean value) {
        set(context, context.getString(resId), value);
    }

    public static void set(Context context, String key, boolean value) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(key, value);
        commitOrApply(editor);
    }

    public static void set(Context context, int resId, float value) {
        set(context, context.getString(resId), value);
    }

    public static void set(Context context, String key, float value) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putFloat(key, value);
        commitOrApply(editor);
    }

    public static void set(Context context, int resId, int value) {
        set(context, context.getString(resId), value);
    }

    public static void set(Context context, String key, int value) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(key, value);
        commitOrApply(editor);
    }

    public static void set(Context context, int resId, long value) {
        set(context, context.getString(resId), value);
    }

    public static void set(Context context, String key, long value) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(key, value);
        commitOrApply(editor);
    }

    public static void set(Context context, int resId, String value) {
        set(context, context.getString(resId), value);
    }

    public static void set(Context context, String key, String value) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        commitOrApply(editor);
    }

    public static boolean get(Context context, int resId, boolean defValue) {
        return get(context, context.getString(resId), defValue);
    }

    public static boolean get(Context context, String key, boolean defValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getBoolean(key, defValue);
    }

    public static float get(Context context, int resId, float defValue) {
        return get(context, context.getString(resId), defValue);
    }

    public static float get(Context context, String key, float defValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getFloat(key, defValue);
    }

    public static int get(Context context, int resId, int defValue) {
        return get(context, context.getString(resId), defValue);
    }

    public static int get(Context context, String key, int defValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getInt(key, defValue);
    }

    public static long get(Context context, int resId, long defValue) {
        return get(context, context.getString(resId), defValue);
    }

    public static long get(Context context, String key, long defValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getLong(key, defValue);
    }

    public static String get(Context context, int resId, String defValue) {
        return get(context, context.getString(resId), defValue);
    }

    public static String get(Context context, String key, String defValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getString(key, defValue);
    }

    public static SharedPreferences.Editor getEditor(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).edit();
    }

    // ////////////////////////////////////////////////////////////////////////
    // Apply method via reflection

    private static final Method APPLY_METHOD = findApplyMethod();

    private static Method findApplyMethod() {
        try {
            Class<Editor> cls = SharedPreferences.Editor.class;
            return cls.getMethod("apply");
        } catch (NoSuchMethodException unused) {
            if (DEBUG) {
                LogUtils.d(TAG, "Failed to retrieve Editor.apply(); probably doesn't exist on this phone's OS.  Using Editor.commit() instead.");
            }
            return null;
        }
    }

    public static void commitOrApply(Editor editor) {
        if (APPLY_METHOD != null) {
            try {
                APPLY_METHOD.invoke(editor);
                return;
            } catch (InvocationTargetException e) {
                if (DEBUG) {
                    LogUtils.d(TAG, "Failed while using Editor.apply().  Using Editor.commit() instead.");
                }
            } catch (IllegalAccessException e) {
                if (DEBUG) {
                    LogUtils.d(TAG, "Failed while using Editor.apply().  Using Editor.commit() instead.");
                }
            }
        }

        editor.commit();
    }
}




Java Source Code List

com.worthed.BuildConfig.java
com.worthed.app.BaseApplication.java
com.worthed.app.BaseCrashHandler.java
com.worthed.app.RebootThreadExceptionHandler.java
com.worthed.app.StartAppReceiver.java
com.worthed.demo.BitmapActivity.java
com.worthed.demo.MainActivity.java
com.worthed.demo.ViewFinderActivity.java
com.worthed.util.AnimationUtils.java
com.worthed.util.AppUtils.java
com.worthed.util.AssetDatabaseOpenHelper.java
com.worthed.util.BitmapUtil.java
com.worthed.util.CipherUtils.java
com.worthed.util.Colors.java
com.worthed.util.CommonUtil.java
com.worthed.util.DataCleanManager.java
com.worthed.util.DatabaseExportUtils.java
com.worthed.util.DateUtils.java
com.worthed.util.DeviceStatusUtils.java
com.worthed.util.DisplayUtils.java
com.worthed.util.DoubleKeyValueMap.java
com.worthed.util.DownloadManagerPro.java
com.worthed.util.FileUtils.java
com.worthed.util.HanziToPinyin.java
com.worthed.util.ImsiUtil.java
com.worthed.util.LocationUtils.java
com.worthed.util.LogUtils.java
com.worthed.util.NetUtil.java
com.worthed.util.PackageUtils.java
com.worthed.util.PhoneUtil.java
com.worthed.util.PollingUtils.java
com.worthed.util.PreferencesCookieStore.java
com.worthed.util.RUtils.java
com.worthed.util.RandomUtils.java
com.worthed.util.RegUtils.java
com.worthed.util.ResourceUtils.java
com.worthed.util.SDCardUtils.java
com.worthed.util.SettingUtils.java
com.worthed.util.ShellUtils.java
com.worthed.util.ShortCutUtils.java
com.worthed.util.Singleton.java
com.worthed.util.StringUtils.java
com.worthed.util.ViewAnimationUtils.java
com.worthed.util.ViewFinder.java
com.worthed.util.ViewUtils.java
com.worthed.util.WindowUtils.java