7 | * Created by Prashant on 1/13/2016. 8 | *
9 | * Email: solankisrp2@gmail.com
10 | * Github: @prashantsolanki3
11 | *
12 | * Just an alias to SecurePreferenceManager
13 | */
14 | public class SPM extends SecurePrefManager {
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/secureprefs/src/main/java/com/prashantsolanki/secureprefmanager/SecurePrefManager.java:
--------------------------------------------------------------------------------
1 | package com.prashantsolanki.secureprefmanager;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.preference.PreferenceManager;
6 | import android.support.annotation.Nullable;
7 |
8 | import com.prashantsolanki.secureprefmanager.utils.HidePreferences;
9 | import com.prashantsolanki.secureprefmanager.utils.SecureString;
10 |
11 | import java.util.Map;
12 |
13 | import static com.prashantsolanki.secureprefmanager.SecurePrefManagerInit.Configuration;
14 | import static com.prashantsolanki.secureprefmanager.SecurePrefManagerInit.isInit;
15 |
16 |
17 | /**
18 | *
19 | * Created by Prashant on 11/5/2015.
20 | */
21 | public class SecurePrefManager {
22 |
23 | // Shared Preferences
24 | SharedPreferences pref;
25 | // Editor for Shared preferences
26 | SharedPreferences.Editor editor;
27 | public static boolean isHidden;
28 |
29 | private Context context;
30 |
31 | Configuration configuration=null;
32 |
33 | protected SecurePrefManager() {
34 |
35 | }
36 |
37 | /**
38 | * Uses the default Encryption with provided file name.
39 | *@param fileName Preference File Name
40 | **/
41 | protected SecurePrefManager(Context context,@Nullable String fileName) {
42 | this.context=context;
43 |
44 | if(fileName==null)
45 | pref = PreferenceManager.getDefaultSharedPreferences(context);
46 | else
47 | pref = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
48 |
49 | configuration = SecurePrefManagerInit.getDefaultConfiguration();
50 | editor = pref.edit();
51 | }
52 |
53 | protected SecurePrefManager(Context context) {
54 | this.context=context;
55 | configuration = SecurePrefManagerInit.getDefaultConfiguration();
56 | if(configuration.getPreferenceFile()!=null&&!configuration.getPreferenceFile().isEmpty())
57 | pref = context.getSharedPreferences(configuration.getPreferenceFile(), Context.MODE_PRIVATE);
58 | else
59 | pref = PreferenceManager.getDefaultSharedPreferences(context);
60 | editor = pref.edit();
61 | }
62 |
63 |
64 | protected SecurePrefManager(Configuration configuration) {
65 |
66 | this.context = configuration.getContext();
67 | this.configuration = configuration;
68 |
69 | if(configuration.getPreferenceFile()!=null&&!configuration.getPreferenceFile().isEmpty())
70 | pref = context.getSharedPreferences(configuration.getPreferenceFile(), Context.MODE_PRIVATE);
71 | else
72 | pref = PreferenceManager.getDefaultSharedPreferences(context);
73 |
74 | editor = pref.edit();
75 | }
76 |
77 | //TODO: Make a constructor with Configuration param.
78 | /**Uses the default Configurations set in SecurePreferenceManagerInit.*/
79 | public static SecurePrefManager with(Context context){
80 | if(!isInit())
81 | throw new IllegalStateException("SecurePrefManagerInit must be initialized before calling SecurePrefManager");
82 |
83 | return new SecurePrefManager(context);
84 | }
85 |
86 | @Deprecated
87 | public static SecurePrefManager with(Context context,String preferenceFileName){
88 | if(!isInit())
89 | throw new IllegalStateException("SecurePrefManagerInit must be initialized before calling SecurePrefManager");
90 |
91 | return new SecurePrefManager(context,preferenceFileName);
92 | }
93 |
94 | public static SecurePrefManager with(Configuration configuration){
95 | return new SecurePrefManager(configuration);
96 | }
97 |
98 | public Deleter clear(){
99 | return new Deleter(this,null);
100 | }
101 |
102 | public Deleter remove(String key){
103 | try {
104 | return new Deleter(this,configuration.getEncryptor().encrypt(key));
105 | }catch (Exception e){
106 | e.printStackTrace();
107 | return null;
108 | }
109 | }
110 |
111 | public void hide(HidePreferences.PreferenceUpdateListener listener){
112 | new HidePreferences(context,true,listener);
113 | }
114 |
115 | public void unhide(HidePreferences.PreferenceUpdateListener listener){
116 | new HidePreferences(context,false,listener);
117 | }
118 |
119 | public Setter set(String key){
120 | try {
121 | return new Setter(configuration.getEncryptor().encrypt(key),this);
122 | }catch (Exception e){
123 | e.printStackTrace();
124 | return null;
125 | }
126 | }
127 |
128 | @Deprecated
129 | public Setter set(SecureString key) {
130 | try{
131 | return new Setter(key.getSecureString(),this);
132 | }catch (Exception e){
133 | e.printStackTrace();
134 | return null;
135 | }
136 | }
137 |
138 | @Deprecated
139 | public Getter get(SecureString key){
140 | try {
141 | return new Getter(key.getSecureString(),this);
142 | }catch (Exception e){
143 | e.printStackTrace();
144 | return null;
145 | }
146 |
147 | }
148 |
149 | public Getter get(String key) {
150 | try{
151 | return new Getter(configuration.getEncryptor().encrypt(key),this);
152 | }catch (Exception e){
153 | e.printStackTrace();
154 | return null;
155 | }
156 | }
157 |
158 | public static class Getter{
159 | public String key;
160 | private SecurePrefManager manager;
161 |
162 | public Getter(String key, SecurePrefManager manager) {
163 | this.key = key;
164 | this.manager = manager;
165 | }
166 |
167 | public DefaultValueString defaultValue(String defaultValue){
168 | return new DefaultValueString(key,manager,defaultValue);
169 | }
170 |
171 | public DefaultValueBoolean defaultValue(Boolean defaultValue){
172 | return new DefaultValueBoolean(key,manager,defaultValue);
173 | }
174 |
175 | public DefaultValueInteger defaultValue(Integer defaultValue){
176 | return new DefaultValueInteger(key,manager,defaultValue);
177 | }
178 |
179 | public DefaultValueFloat defaultValue(Float defaultValue){
180 | return new DefaultValueFloat(key,manager,defaultValue);
181 | }
182 |
183 | public DefaultValueLong defaultValue(Long defaultValue){
184 | return new DefaultValueLong(key,manager,defaultValue);
185 | }
186 |
187 | public static class DefaultValueString extends DefaultValue
15 | * Created by Prashant on 1/12/2016.
16 | *
17 | * Email: solankisrp2@gmail.com
18 | * Github: @prashantsolanki3
19 | */
20 | public class PreferenceMigration {
21 |
22 | Configuration oldConfig=null,newConfig=null;
23 |
24 | public PreferenceMigration migrate(final String key, final Integer defaultValue){
25 | Shoot.once(key, new OnShootListener() {
26 | @Override
27 | public void onExecute(int scope, String TAG, int iteration) {
28 | SPM.with(newConfig)
29 | .set(key)
30 | .value(getValue(key,defaultValue))
31 | .go();
32 | removePreference(key);
33 | }
34 | });
35 | return this;
36 | }
37 |
38 | public PreferenceMigration migrate(final String key, final Float defaultValue){
39 | Shoot.once(key, new OnShootListener() {
40 | @Override
41 | public void onExecute(int scope, String TAG, int iteration) {
42 | SPM.with(newConfig)
43 | .set(key)
44 | .value(getValue(key,defaultValue))
45 | .go();
46 | removePreference(key);
47 | }
48 | });
49 | return this;
50 | }
51 |
52 | public PreferenceMigration migrate(final String key, final String defaultValue){
53 | Shoot.once(key, new OnShootListener() {
54 | @Override
55 | public void onExecute(int scope, String TAG, int iteration) {
56 | SPM.with(newConfig)
57 | .set(key)
58 | .value(getValue(key,defaultValue))
59 | .go();
60 | removePreference(key);
61 | }
62 | });
63 | return this;
64 | }
65 |
66 | public PreferenceMigration migrate(final String key, final Long defaultValue){
67 | Shoot.once(key, new OnShootListener() {
68 | @Override
69 | public void onExecute(int scope, String TAG, int iteration) {
70 | SPM.with(newConfig)
71 | .set(key)
72 | .value(getValue(key,defaultValue))
73 | .go();
74 | removePreference(key);
75 | }
76 | });
77 | return this;
78 | }
79 |
80 | public PreferenceMigration migrate(final String key, final Boolean defaultValue){
81 | Shoot.once(key, new OnShootListener() {
82 | @Override
83 | public void onExecute(int scope, String TAG, int iteration) {
84 | SPM.with(newConfig)
85 | .set(key)
86 | .value(getValue(key, defaultValue))
87 | .go();
88 | removePreference(key);
89 | }
90 | });
91 | return this;
92 | }
93 |
94 | /*public void migrateAll(final MigrationListener listener){
95 | Shoot.once(oldConfig.getPreferenceFile() + newConfig.getPreferenceFile(), new OnShootListener() {
96 | @Override
97 | public void onExecute(int a, String s, int a1) {
98 | Map map = SPM.with(oldConfig)
99 | .getAllPreferences();
100 | int i = 0;
101 |
102 | for (Object o : map.keySet()) {
103 | i++;
104 |
105 |
106 | if (map.get(o) instanceof Integer)
107 | migrate((String) o, (Integer) map.get(o));
108 |
109 | else if (map.get(o) instanceof Long)
110 | migrate((String) o,(Long) map.get(o));
111 |
112 | else if (map.get(o) instanceof Boolean)
113 | migrate((String) o, (Boolean) map.get(o));
114 |
115 | else if (map.get(o) instanceof Float)
116 | migrate((String) o, (Float) map.get(o));
117 |
118 | if (map.get(o) instanceof String)
119 | migrate((String) o, (String) map.get(o));
120 | else
121 | Log.e("Preference Migration","Type not supported");
122 |
123 | listener.onPreferenceMigrated((String) o, getValue((String) o, (String) map.get(o)), i, map.keySet().size());
124 | }
125 |
126 | listener.onComplete(i);
127 | }
128 | });
129 |
130 | }*/
131 |
132 |
133 | private void removePreference(String key){
134 | SPM.with(oldConfig)
135 | .remove(key)
136 | .confirm();
137 | }
138 |
139 | private Integer getValue(String key,Integer defaultValue){
140 | return SPM.with(oldConfig)
141 | .get(key)
142 | .defaultValue(defaultValue)
143 | .go();
144 | }
145 |
146 | private String getValue(String key,String defaultValue){
147 | return SPM.with(oldConfig)
148 | .get(key)
149 | .defaultValue(defaultValue)
150 | .go();
151 | }
152 |
153 | private Boolean getValue(String key,Boolean defaultValue){
154 | return SPM.with(oldConfig)
155 | .get(key)
156 | .defaultValue(defaultValue)
157 | .go();
158 | }
159 |
160 | private Long getValue(String key,Long defaultValue){
161 | return SPM.with(oldConfig)
162 | .get(key)
163 | .defaultValue(defaultValue)
164 | .go();
165 | }
166 |
167 | /* private String getValue(String key, Object defaultValue){
168 | return SPM.with(oldConfig)
169 | .get(key)
170 | .defaultValue(defaultValue)
171 | .go();
172 | }*/
173 |
174 | private Float getValue(String key, Float defaultValue){
175 | return SPM.with(oldConfig)
176 | .get(key)
177 | .defaultValue(defaultValue)
178 | .go();
179 | }
180 |
181 | private PreferenceMigration(@NonNull Configuration oldConfig,
182 | @NonNull Configuration newConfig) {
183 | this.oldConfig = oldConfig;
184 | this.newConfig = newConfig;
185 | Shoot.with(newConfig.getContext());
186 | }
187 |
188 | public interface MigrationListener{
189 | void onPreferenceMigrated(String key, Object value,int current,int total);
190 | void onComplete(int totalMigrations);
191 | }
192 |
193 | /**
194 | * Builder
195 | * */
196 | public static class Builder {
197 |
198 | Configuration oldConfig=null,newConfig=null;
199 |
200 | public Builder setOldConfiguration(@NonNull Configuration oldConfig) {
201 | this.oldConfig = oldConfig;
202 | return this;
203 | }
204 |
205 | public Builder setNewConfiguration(@NonNull Configuration newConfig) {
206 | this.newConfig = newConfig;
207 | return this;
208 | }
209 |
210 | public PreferenceMigration build(){
211 |
212 | if(newConfig==null||oldConfig==null)
213 | throw new RuntimeException("Configurations cannot be null");
214 |
215 | return new PreferenceMigration(oldConfig,newConfig);
216 | }
217 | }
218 |
219 | }
220 |
--------------------------------------------------------------------------------
/secureprefs/src/main/java/com/prashantsolanki/secureprefmanager/utils/HidePreferences.java:
--------------------------------------------------------------------------------
1 | package com.prashantsolanki.secureprefmanager.utils;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.os.AsyncTask;
6 | import android.os.SystemClock;
7 | import android.preference.PreferenceManager;
8 | import android.util.Base64;
9 | import android.util.Log;
10 |
11 | import com.prashantsolanki.secureprefmanager.SecurePrefManager;
12 |
13 | import java.io.BufferedReader;
14 | import java.io.BufferedWriter;
15 | import java.io.FileReader;
16 | import java.io.FileWriter;
17 | import java.nio.charset.Charset;
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | /**
22 | * Created by Prashant on 11/7/2015.
23 | */
24 | public class HidePreferences {
25 |
26 | public static void addToCache(Context ctx,String data, String tag) {
27 | FileWriter fileWriter=null;
28 | BufferedWriter bufferedWriter=null;
29 |
30 | try {
31 | fileWriter = new FileWriter(ctx.getFileStreamPath(tag));
32 | bufferedWriter = new BufferedWriter(fileWriter);
33 | bufferedWriter.write(data);
34 | } catch (Exception e) {
35 | e.printStackTrace();
36 | Log.d("INTERNAL STORAGE WRITE", "ERROR: " + e.getMessage());
37 | }finally {
38 | try {
39 | if(bufferedWriter!=null)
40 | bufferedWriter.close();
41 | }catch (Exception e){
42 | e.printStackTrace();
43 | }
44 | }
45 | }
46 |
47 | public static String getCache(Context ctx,String tag) {
48 | BufferedReader bufferedReader=null;
49 | FileReader fileReader;
50 | String temp;
51 | StringBuilder stringBuilder = new StringBuilder();
52 | try {
53 | fileReader = new FileReader(ctx.getFileStreamPath(tag));
54 | bufferedReader = new BufferedReader(fileReader);
55 | while ((temp=bufferedReader.readLine())!=null)
56 | stringBuilder.append(temp);
57 | } catch (Exception e) {
58 | Log.d("INTERNAL STORAGE READ","ERROR: "+e.getMessage());
59 | return null;
60 | }finally {
61 | try {
62 | if(bufferedReader!=null)
63 | bufferedReader.close();
64 | }catch (Exception e){
65 | e.printStackTrace();
66 | }
67 | }
68 | return stringBuilder.toString();
69 | }
70 |
71 | Context context;
72 | SharedPreferences preferences;
73 | String fileName;
74 |
75 |
76 | public HidePreferences(Context context,boolean hide,PreferenceUpdateListener listener) {
77 | this.context = context;
78 | preferences = PreferenceManager.getDefaultSharedPreferences(context);
79 | if(hide)
80 | saveAndClearPreferences(listener);
81 | else
82 | addPreferencesToXml(listener);
83 |
84 | fileName= Base64.encodeToString(context.getApplicationInfo().packageName.getBytes(Charset.forName("UTF-8")), Base64.NO_PADDING);
85 |
86 | }
87 |
88 | private void saveAndClearPreferences(PreferenceUpdateListener listener){
89 | new AsyncXmlToFile(listener).execute();
90 | }
91 |
92 | private Map