diff options
author | Julian_Chu <walkingice@0xlab.org> | 2011-07-13 19:31:33 +0800 |
---|---|---|
committer | Julian_Chu <walkingice@0xlab.org> | 2011-07-15 15:00:01 +0800 |
commit | dfb7df1f496b6a6f836a92fc2c07f6b40be7c200 (patch) | |
tree | 8d960b0749ac09d5d72b808f5aae264904ef11d1 | |
parent | 321626f983fd8f9eea6fbbbd142ea2cfee4b0a7b (diff) | |
download | 0xbench-dfb7df1f496b6a6f836a92fc2c07f6b40be7c200.tar.gz |
Add org.zeroxlab.utils.Util
Store/Restore preference is very useful.
Get a class which is in charge of these jobs.
-rw-r--r-- | src/org/zeroxlab/utils/Util.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/org/zeroxlab/utils/Util.java b/src/org/zeroxlab/utils/Util.java new file mode 100644 index 0000000..cde4b25 --- /dev/null +++ b/src/org/zeroxlab/utils/Util.java @@ -0,0 +1,95 @@ +/* + * Authored By Julian Chu <walkingice@0xlab.org> + * + * Copyright (C) 2011 0xlab - http://0xlab.org/ + * + * 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 org.zeroxlab.utils; + +import android.util.Log; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.Environment; +import android.os.Handler; +import android.os.Message; + +public class Util { + public final static int PREF_MODE = Context.MODE_PRIVATE; + public final static boolean DEFAULT_PREF_BOOLEAN = false; + public final static int DEFAULT_PREF_INT = -1; + public final static String DEFAULT_PREF_STRING = ""; + + public static void launchActivity(Activity parent, String targetFullName) { + final Intent intent = new Intent(); + intent.setClassName(parent, targetFullName); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + parent.startActivity(intent); + } + + public static void storePrefBool(Context context, String name, String key, boolean value) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + pref.edit().putBoolean(key, value).commit(); + } + + public static void storePrefInt(Context context, String name, String key, int value) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + pref.edit().putInt(key, value).commit(); + } + + public static void storePrefString(Context context, String name, String key, String value) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + pref.edit().putString(key, value).commit(); + } + + public static boolean restorePrefBool(Context context, String name, String key, boolean defaultValue) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + boolean value = pref.getBoolean(key, defaultValue); + return value; + } + + public static int restorePrefInt(Context context, String name, String key, int defaultValue) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + int value = pref.getInt(key, defaultValue); + return value; + } + + public static String restorePrefString(Context context, String name, String key, String defaultValue) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + String value = pref.getString(key, defaultValue); + return value; + } + + public static boolean restorePrefBool(Context context, String name, String key) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + boolean value = pref.getBoolean(key, DEFAULT_PREF_BOOLEAN); + return value; + } + + public static int restorePrefInt(Context context, String name, String key) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + int value = pref.getInt(key, DEFAULT_PREF_INT); + return value; + } + + public static String restorePrefString(Context context, String name, String key) { + SharedPreferences pref = context.getSharedPreferences(name, PREF_MODE); + String value = pref.getString(key, DEFAULT_PREF_STRING); + return value; + } +} |