Last active
April 5, 2016 09:48
-
-
Save vaibhav-jani/6e1d0b13ca7c7042719091e5f4ee49ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.content.SharedPreferences; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| import android.util.Log; | |
| import java.lang.reflect.Field; | |
| import java.util.Set; | |
| import java.util.prefs.AbstractPreferences; | |
| /** | |
| * Created by vaibha.jani on 1/4/2016. | |
| */ | |
| public class AppPreferences { | |
| public static final class App { | |
| public static final String SAMPLE = "sample"; | |
| } | |
| public static final class User { | |
| public static final String SAMPLE = "sample"; | |
| } | |
| public static final String APP_PREFERENCES_FILE_NAME = "app_preferences"; | |
| private static SharedPreferences sharedPreferencesSingleton; | |
| private static synchronized SharedPreferences getInstance() { | |
| if (sharedPreferencesSingleton == null) { | |
| Context context = BaseApplication.getInstance(); | |
| sharedPreferencesSingleton = context.getSharedPreferences(APP_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE); | |
| } | |
| return sharedPreferencesSingleton; | |
| } | |
| public static void deletePreference(String prefKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| if (sharedPreferences.contains(prefKey)) { | |
| sharedPreferences.edit().remove(prefKey).apply(); | |
| } | |
| } | |
| public static void deleteAllPreference() { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| SharedPreferences.Editor editor = sharedPreferences.edit(); | |
| editor.clear(); | |
| editor.apply(); | |
| editor.commit(); | |
| } | |
| public static void deleteUserPreference() { | |
| deletePreferenceForClass(User.class); | |
| } | |
| public static void deleteAppPreference() { | |
| deletePreferenceForClass(App.class); | |
| } | |
| private static void deletePreferenceForClass(Class<?> className) { | |
| Class<?> userClass = className; | |
| Field[] fields = userClass.getDeclaredFields(); | |
| for (Field field : fields) { | |
| try { | |
| String fieldValue = (String) field.get(null); | |
| Log.d("DELETING PREF KEY", fieldValue); | |
| deletePreference(fieldValue); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| public static boolean contains(String key) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.contains(key); | |
| } | |
| //Booleans | |
| public static boolean getBoolean(String booleanKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getBoolean(booleanKey, false); | |
| } | |
| public static boolean getBoolean(String booleanKey, boolean defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getBoolean(booleanKey, defaultValue); | |
| } | |
| public static boolean putBoolean(String booleanKey, boolean value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putBoolean(booleanKey, value).commit(); | |
| } | |
| //Integers | |
| public static int getInt(String intKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getInt(intKey, 0); | |
| } | |
| public static int getInt(String intKey, int defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getInt(intKey, defaultValue); | |
| } | |
| public static boolean putInt(String intKey, int value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putInt(intKey, value).commit(); | |
| } | |
| //Strings | |
| public static String getString(String stringKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getString(stringKey, null); | |
| } | |
| public static String getString(String stringKey, @Nullable String defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getString(stringKey, defaultValue); | |
| } | |
| public static boolean putString(String stringKey, String value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putString(stringKey, value).commit(); | |
| } | |
| //Floats | |
| public static Float getFloat(String stringKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getFloat(stringKey, 0); | |
| } | |
| public static Float getFloat(String stringKey, float defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getFloat(stringKey, defaultValue); | |
| } | |
| public static boolean putFloat(String stringKey, float value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putFloat(stringKey, value).commit(); | |
| } | |
| //Strings Sets | |
| public static Set<String> getStringSet(String stringSetKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getStringSet(stringSetKey, null); | |
| } | |
| public static Set<String> getStringSet(String stringSetKey, @Nullable Set<String> defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getStringSet(stringSetKey, defaultValue); | |
| } | |
| public static boolean putStringSet(String stringSetKey, Set<String> value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putStringSet(stringSetKey, value).commit(); | |
| } | |
| //Longs | |
| public static long getLong(String longKey) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getLong(longKey, 0); | |
| } | |
| public static long getLong(String longKey, long defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.getLong(longKey, defaultValue); | |
| } | |
| public static boolean putLong(String longKey, long longValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| return sharedPreferences.edit().putLong(longKey, longValue).commit(); | |
| } | |
| //Encrypted | |
| public static boolean putStringEncrypted(String key, String value) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| value = EncryptionUtils.encrypt(value, BaseApplication.getInstance()); | |
| return sharedPreferences.edit().putString(key, value).commit(); | |
| } | |
| public static String getStringEncrypted(String key, String defaultValue) { | |
| SharedPreferences sharedPreferences = getInstance(); | |
| if (sharedPreferences.contains(key)) { | |
| String value = sharedPreferences.getString(key, defaultValue); | |
| return EncryptionUtils.decrypt(value, BaseApplication.getInstance()); | |
| } else { | |
| return defaultValue; | |
| } | |
| } | |
| public static String getStringEncrypted(String key) { | |
| return AppPreferences.getStringEncrypted(key, null); | |
| } | |
| public static boolean putLongEncrypted(String key, long value) { | |
| return AppPreferences.putStringEncrypted(key, Long.toString(value)); | |
| } | |
| public static long getLongEncrypted(String key, long defaultValue) { | |
| String value = AppPreferences.getStringEncrypted(key, null); | |
| if(value == null) { | |
| return defaultValue; | |
| } else { | |
| long longValue = defaultValue; | |
| try { | |
| longValue = Long.parseLong(value); | |
| } catch (NumberFormatException e) { | |
| e.printStackTrace(); | |
| } | |
| return longValue; | |
| } | |
| } | |
| public static long getLongEncrypted(String key) { | |
| return AppPreferences.getLongEncrypted(key, 0); | |
| } | |
| public static boolean putIntEncrypted(String key, int value) { | |
| return AppPreferences.putStringEncrypted(key, Integer.toString(value)); | |
| } | |
| public static int getIntEncrypted(String key, int defaultValue) { | |
| String value = AppPreferences.getStringEncrypted(key, null); | |
| if(value == null) { | |
| return defaultValue; | |
| } else { | |
| int intValue = defaultValue; | |
| try { | |
| intValue = Integer.parseInt(value); | |
| } catch (NumberFormatException e) { | |
| e.printStackTrace(); | |
| } | |
| return intValue; | |
| } | |
| } | |
| public static int getIntEncrypted(String key) { | |
| return AppPreferences.getIntEncrypted(key, 0); | |
| } | |
| public static boolean putBooleanEncrypted(String key, boolean value) { | |
| return AppPreferences.putStringEncrypted(key, Boolean.toString(value)); | |
| } | |
| public static boolean getBooleanEncrypted(String key, boolean defaultValue) { | |
| String value = AppPreferences.getStringEncrypted(key, null); | |
| if(value == null) { | |
| return defaultValue; | |
| } else { | |
| boolean boolValue = defaultValue; | |
| try { | |
| boolValue = Boolean.parseBoolean(value); | |
| } catch (NumberFormatException e) { | |
| e.printStackTrace(); | |
| } | |
| return boolValue; | |
| } | |
| } | |
| public static boolean getBooleanEncrypted(String key) { | |
| return AppPreferences.getBooleanEncrypted(key, false); | |
| } | |
| public static boolean putFloatEncrypted(String key, float value) { | |
| return AppPreferences.putStringEncrypted(key, Float.toString(value)); | |
| } | |
| public static float getFloatEncrypted(String key, float defaultValue) { | |
| String value = AppPreferences.getStringEncrypted(key, null); | |
| if(value == null) { | |
| return defaultValue; | |
| } else { | |
| float floatValue = defaultValue; | |
| try { | |
| floatValue = Float.parseFloat(value); | |
| } catch (NumberFormatException e) { | |
| e.printStackTrace(); | |
| } | |
| return floatValue; | |
| } | |
| } | |
| public static float getFloatEncrypted(String key) { | |
| return AppPreferences.getFloatEncrypted(key, 0); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.provider.Settings; | |
| import android.util.Base64; | |
| import javax.crypto.Cipher; | |
| import javax.crypto.SecretKey; | |
| import javax.crypto.SecretKeyFactory; | |
| import javax.crypto.spec.PBEKeySpec; | |
| import javax.crypto.spec.PBEParameterSpec; | |
| /** | |
| * Created by vaibhav on 31/3/16. | |
| */ | |
| public class EncryptionUtils { | |
| protected static final String UTF8 = "utf-8"; | |
| private static final char[] SEKRIT = new char[] {'1', '2', '3', '4', '5','6'} ; | |
| // INSERT A RANDOM PASSWORD HERE. | |
| // Don't use anything you wouldn't want to | |
| // get out there if someone decompiled | |
| // your app. | |
| public static String encrypt(String value, Context context) { | |
| try { | |
| final byte[] bytes = value != null ? value.getBytes(UTF8) : new byte[0]; | |
| SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); | |
| SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT)); | |
| Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); | |
| pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(), Settings.System.ANDROID_ID).getBytes(UTF8), 20)); | |
| return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP), UTF8); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| public static String decrypt(String value, Context context) { | |
| try { | |
| final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0]; | |
| SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); | |
| SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT)); | |
| Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); | |
| pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(), Settings.System.ANDROID_ID).getBytes(UTF8), 20)); | |
| return new String(pbeCipher.doFinal(bytes), UTF8); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment