```java
package com.allynav.systemservice.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class SharedUtil {
public static void putMap(Context context, String fileName, Map<String, String> map) {
if (map != null && map.size() > 0) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = map.get(key);
editor.putString(key, value);
}
editor.commit();
}
}
public static void putString(Context context, String fileName, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getString(Context context, String fileName, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.getString(key, "");
}
return "";
}
@SuppressLint("NewApi")
public static void putBoolean(Context context, String fileName, String key, boolean value) {
WeakReference<SharedPreferences> weakpreferences = new WeakReference<SharedPreferences>(
context.getSharedPreferences(fileName, Context.MODE_PRIVATE));
SharedPreferences preferences = weakpreferences.get();
Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context context, String fileName, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
return false;
}
public static void putMap(Context context, Map<String, String> map) {
if (map != null && map.size() > 0) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = map.get(key);
editor.putString(key, value);
}
editor.commit();
}
}
public static void putString(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getString(Context context, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
Context.MODE_PRIVATE);
return preferences.getString(key, "");
}
return "";
}
public static void putBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBoolean(Context context, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
return false;
}
public static void putLong(Context context, String fileName, String key, long value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putLong(key, value);
editor.commit();
}
public static long getLong(Context context, String fileName, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.getLong(key, -1);
}
return -1;
}
@SuppressLint("NewApi")
public static void putInt(Context context, String fileName, String key, int value) {
WeakReference<SharedPreferences> weakpreferences = new WeakReference<SharedPreferences>(
context.getSharedPreferences(fileName, Context.MODE_PRIVATE));
SharedPreferences preferences = weakpreferences.get();
Editor editor = preferences.edit();
editor.putInt(key, value);
editor.apply();
}
public static int getInt(Context context, String fileName, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.getInt(key, 0);
}
return 0;
}
public static void putFloat(Context context, String fileName, String key, float value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putFloat(key, value);
editor.commit();
}
public static float getFloat(Context context, String fileName, String key) {
if (context != null) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.getFloat(key, 0);
}
return 0;
}
public static boolean clearAllValue(Context context, String fileName) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
return preferences.edit().clear().commit();
}
}