Skip to content

Commit 7e6c628

Browse files
committed
use overloading method
1 parent a87209e commit 7e6c628

File tree

11 files changed

+278
-267
lines changed

11 files changed

+278
-267
lines changed

README.md

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Conceal provides a set of Java APIs to perform cryptography on Android. It was d
1111
Gradle
1212
```gradle
1313
dependencies {
14-
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.5.2'
14+
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.5.5'
1515
1616
//or
1717
18-
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.5.2' {
18+
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.5.5' {
1919
exclude group: 'com.google.code.gson', module: 'gson'
2020
}
2121
}
@@ -50,7 +50,7 @@ Permission need to use in your project. Please Allow it first, or it will affect
5050
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
5151
```
5252

53-
SharedPreferences initialize
53+
##### Initialize
5454
```java
5555
ConcealPrefRepository concealPrefRepository = new ConcealPrefRepository.PreferencesBuilder(this)
5656
//.useThisPrefStorage("Android_Prefs")
@@ -71,68 +71,79 @@ ConcealPrefRepository concealPrefRepository = new ConcealPrefRepository.Preferen
7171
- for files - in folder /files
7272
```
7373

74-
<b>Save data</b>
74+
##### Save data
7575

7676
```java
77-
concealPrefRepository.putString(KEY,"Hello");
78-
concealPrefRepository.putInt(KEY,1000000);
79-
concealPrefRepository.putDouble(KEY,100.00);
80-
concealPrefRepository.putbyte(KEY,new byte[]);
81-
concealPrefRepository.putMap(KEY,new Map<String,String>());
77+
concealPrefRepository.put(KEY,"Hello");
78+
concealPrefRepository.put(KEY,1000000);
79+
concealPrefRepository.put(KEY,100.00);
80+
concealPrefRepository.put(KEY,new byte[]);
81+
concealPrefRepository.put(KEY,new Map<String,String>());
82+
...
83+
...
84+
```
8285

83-
//using gson
86+
for complex object might need use `putModel` which use gson
87+
```
8488
concealPrefRepository.putModel(KEY, new Gson().fromJson(loadJSONFromAsset(context, "users.json"), User.class));
8589
concealPrefRepository.putModel(KEY, new Gson().fromJson(loadJSONFromAsset(context, "users.json"), new TypeToken<ArrayList<Users>>(){}.getType()));
90+
```
8691

92+
93+
Files and Images
94+
```
8795
// Files and Images will be encrypted
8896
// prefix of this encrypted images and files start with "conceal_enc_";
8997
File getFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/testing.pdf");
9098
concealPrefRepository.putFile(KEY,getFile,boolean deleteOldFiles);
9199
// deleteOldFiles - true or false.. true - will delete choosen file and move to new path
92100
93101
//put images
94-
concealPrefRepository.putImage(KEY, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
95-
concealPrefRepository.putImage(KEY, File file);
96-
...........
102+
concealPrefRepository.put(KEY, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
103+
concealPrefRepository.put(KEY, File file);
104+
concealPrefRepository.putDrawable(KEY, Drawable ID);
105+
...
106+
...
97107
```
98108

99-
OR
109+
##### <b>For Method Chaining</b>
100110
```java
101111
new ConcealPrefRepository.Editor("PREFIX") // optional - get default from global prefix
102-
.putString(KEY,"Hello")
103-
.putInt(KEY,1000000)
104-
.putBoolean(KEY,true)
105-
.putByte(KEY,new byte[])
106-
.putFile(KEY,getFile,boolean deleteOldFiles);
107-
.putImage(KEY, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
108-
.putImage(KEY, imageFile)
109-
.putListString(KEY,STRING_LIST)
110-
.putListFloat(KEY,FLOAT_LIST)
111-
.........
112+
.put(KEY,"Hello")
113+
.put(KEY,1000000)
114+
.put(KEY,true)
115+
.put(KEY,new byte[])
116+
.put(KEY,getFile,boolean deleteOldFiles);
117+
.put(KEY, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
118+
.put(KEY, imageFile)
119+
.put(KEY,STRING_LIST)
120+
.put(KEY,FLOAT_LIST)
121+
...
122+
...
112123
.apply(); //.commit();
113124
```
114125

115-
<b>Get total data</b>
126+
##### <b>Get total data</b>
116127
```java
117128
System.out.println(concealPrefRepository.getPrefsSize());
118129
```
119130

120-
<b>Get all sharedpreferences data</b>
131+
##### <b>Get all sharedpreferences data</b>
121132
```java
122133
Map<String,String> getAll = concealPrefRepository.getAllSharedPrefData();
123134
```
124135

125-
<b>Get all sharedpreferences data in List String</b>
136+
##### <b>Get all sharedpreferences data in List String</b>
126137
```java
127138
List<String> getAll = concealPrefRepository.getAllSharedPrefDataToString();
128139
```
129140

130-
<b>Get all encrypted Files inside created folder</b>
141+
##### <b>Get all encrypted Files inside created folder</b>
131142
```java
132143
List<CryptoFile> getFiles = concealPrefRepository.getAllConcealEncryptedFiles();
133144
```
134145

135-
<b>Fetching data</b>
146+
##### <b>Fetching data</b>
136147

137148
```java
138149
concealPrefRepository.getString(KEY);
@@ -154,7 +165,7 @@ File enc_file = concealPrefRepository.getFile(KEY,true); //return File
154165
........
155166
```
156167

157-
<b>Clear key and SharedPreferences</b>
168+
##### <b>Clear key and SharedPreferences</b>
158169

159170
```java
160171
concealPrefRepository.destroyCrypto(); //clear key
@@ -164,17 +175,17 @@ concealPrefRepository.remove(KEY1,KEY2,KEY3,KEY4) //String... keys
164175
concealPrefRepository.removeFile(KEY); //delete assosiate file (images and files) return boolean
165176
```
166177

167-
<b>Check if key exists</b>
178+
##### <b>Check if key exists</b>
168179
```java
169180
concealPrefRepository.contains(KEY); // return boolean
170181
```
171182

172-
<b>Get SharedPreferences</b>
183+
##### <b>Get SharedPreferences</b>
173184
```java
174185
concealPrefRepository.getPreferences();
175186
```
176187

177-
<b>Listener Data Changes</b>
188+
##### <b>Listener Data Changes</b>
178189
```java
179190
public class BaseActivity extends AppCompatActivity implements OnDataChangeListener{
180191
....
@@ -185,8 +196,7 @@ public class BaseActivity extends AppCompatActivity implements OnDataChangeListe
185196
}
186197
```
187198

188-
<b>Easier Save User Detail Preferences</b>
189-
199+
##### <b>Easier Save User Detail Preferences</b>
190200
```java
191201
new ConcealPrefRepository.UserPref()
192202
.setFirstName("Firstname")
@@ -203,15 +213,15 @@ ConcealPrefRepository.UserPref().applyFirstName("Firstname"); //directly apply
203213
ConcealPrefRepository.UserPref().applyLastName("Firstname"); //directly apply
204214
```
205215

206-
<b>Get User Detail</b>
216+
##### <b>Get User Detail</b>
207217
```java
208218
new ConcealPrefRepository.UserPref().getFirstName()
209219
new ConcealPrefRepository.UserPref().getLastName()
210220
new ConcealPrefRepository.UserPref().getEmail()
211221
.....
212222
```
213223

214-
<b>Key prefix - Apply key with prefix</b>
224+
##### <b>Key prefix - Apply key with prefix</b>
215225
```java
216226
new ConcealPrefRepository.UserPref("KEY PREFIX").setFirstName("Firstname").apply();
217227
new ConcealPrefRepository.UserPref("KEY PREFIX").setLastName("Firstname").apply();
@@ -238,13 +248,13 @@ ConcealCrypto concealCrypto = new ConcealCrypto.CryptoBuilder(this)
238248
.create();
239249
```
240250

241-
#### Hash
251+
##### Hash
242252

243253
```
244254
concealCrypto.hashKey(plaintext); // SHA-256
245255
```
246256

247-
#### Encrypt
257+
##### Encrypt
248258

249259
```
250260
concealCrypto.obscure(test); // encrypt using facebook conceal
@@ -257,7 +267,7 @@ concealCrypto.obscureFile(File file,boolean deleteOldFile);
257267
```
258268

259269

260-
#### Decrypt
270+
##### Decrypt
261271

262272
```
263273
concealCrypto.deObscure(cipher); // decrypt using facebook conceal

app/src/main/java/com/zeroone/concealexample/BaseActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import android.util.Log;
77

88
import com.zeroone.conceal.ConcealPrefRepository;
9-
import com.zeroone.conceal.OnDataChangeListener;
9+
import com.zeroone.conceal.listener.OnDataChangeListener;
1010
import com.zeroone.conceal.model.CryptoType;
1111

1212
/**

app/src/main/java/com/zeroone/concealexample/MainActivity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13-
import java.util.Map;
1413

1514
public class MainActivity extends BaseActivity {
1615

@@ -32,16 +31,20 @@ protected void onCreate(Bundle savedInstanceState) {
3231
ConcealPrefRepository.applicationInit(getApplication());
3332

3433
concealPrefRepository.clearPrefs();
35-
34+
35+
List<Float> floats = new ArrayList<>();
36+
floats.add(10f);
37+
floats.add(10f);
38+
floats.add(10f);
3639

3740
//FIRST TEST
38-
concealPrefRepository.putString(NAME_KEY, "HAFIQ IQMAL");
39-
concealPrefRepository.putInt(AGE_KEY, 24);
41+
concealPrefRepository.put(NAME_KEY, "HAFIQ IQMAL");
42+
concealPrefRepository.put(AGE_KEY, floats);
4043
concealPrefRepository.putModel(USER_DETAIL, Data.getUser(this));
4144
concealPrefRepository.putModel(TASK_DETAIL, Data.getTaskData(this));
4245

4346
Log.d("FIRST TEST", concealPrefRepository.getString(NAME_KEY));
44-
Log.d("FIRST TEST", concealPrefRepository.getString(AGE_KEY));
47+
Log.d("FIRST TEST", concealPrefRepository.getListFloat(AGE_KEY).toString());
4548
Log.d("FIRST TEST", concealPrefRepository.getModel(USER_DETAIL, User.class).toString());
4649
Log.d("FIRST TEST", concealPrefRepository.getModel(TASK_DETAIL, new TypeToken<ArrayList<Task>>(){}.getType()).toString());
4750
Log.d("FIRST TEST SIZE", ""+concealPrefRepository.getPrefsSize());
@@ -50,9 +53,9 @@ protected void onCreate(Bundle savedInstanceState) {
5053

5154
//SECOND TEST
5255
new ConcealPrefRepository.Editor()
53-
.putString(NAME_KEY, "Hafiq Iqmal")
54-
.putInt(AGE_KEY, 24)
55-
.putString(EMAIL_KEY, "[email protected]")
56+
.put(NAME_KEY, "Hafiq Iqmal")
57+
.put(AGE_KEY, 24)
58+
.put(EMAIL_KEY, "[email protected]")
5659
.apply();
5760

5861
Log.d("SECOND TEST", concealPrefRepository.getString(NAME_KEY));
@@ -99,7 +102,6 @@ protected void onCreate(Bundle savedInstanceState) {
99102
getList();
100103

101104

102-
103105
ConcealCrypto concealCrypto = new ConcealCrypto.CryptoBuilder(this)
104106
.setEnableValueEncryption(true) //default true
105107
.setEnableKeyEncryption(true) // default true
@@ -125,8 +127,6 @@ protected void onCreate(Bundle savedInstanceState) {
125127
Log.d("AES E", cipher);
126128
dec = concealCrypto.aesDecrypt(cipher);
127129
Log.d("AES D", dec);
128-
129-
130130
}
131131

132132

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010
minSdkVersion 16
1111
targetSdkVersion 27
1212
versionCode 1
13-
versionName "1.5.2"
13+
versionName "1.5.5"
1414
}
1515
buildTypes {
1616
release {

library/src/main/java/com/zeroone/conceal/BaseBuilderAbstract.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ String hideValue(String value) {
105105
return this.concealCrypto.obscure(value);
106106
}
107107

108+
public void apply() {
109+
getEditor().apply();
110+
}
108111

109-
protected abstract void apply();
110-
protected abstract void commit();
111-
112+
public void commit() {
113+
getEditor().commit();
114+
}
112115
}

library/src/main/java/com/zeroone/conceal/BaseEditorAbstract.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,36 @@ public abstract class BaseEditorAbstract<T extends BaseEditorAbstract<T>> extend
3131
super(keyPrefix, defaultEmptyValue, sharedPreferences);
3232
}
3333

34-
abstract T putString(@NonNull String key, String value);
35-
abstract void applyString(@NonNull String key, String value);
36-
abstract T putInt(@NonNull String key, int value);
37-
abstract void applyInt(@NonNull String key, int value);
38-
abstract T putLong(@NonNull String key, long value);
39-
abstract void applyLong(@NonNull String key, long value);
40-
abstract T putDouble(@NonNull String key, double value);
41-
abstract void applyDouble(@NonNull String key, double value);
42-
abstract T putFloat(@NonNull String key, float value);
43-
abstract void applyFloat(@NonNull String key, float value);
44-
abstract T putBoolean(@NonNull String key, boolean value);
45-
abstract void applyBoolean(@NonNull String key, boolean value);
46-
abstract T putListString(@NonNull String key, List<String> value);
47-
abstract void applyListString(@NonNull String key, List<String> value);
48-
abstract T putListFloat(@NonNull String key, List<Float> value);
49-
abstract void applyListFloat(@NonNull String key, List<Float> value);
50-
abstract T putListInteger(@NonNull String key, List<Integer> value);
51-
abstract void applyListInteger(@NonNull String key, List<Integer> value);
52-
abstract T putListDouble(@NonNull String key, List<Double> value);
53-
abstract void applyListDouble(@NonNull String key, List<Double> value);
54-
abstract T putListLong(@NonNull String key, List<Long> value);
55-
abstract void applyListLong(@NonNull String key, List<Long> value);
56-
abstract T putListBoolean(@NonNull String key, List<Boolean> value);
57-
abstract void applyListBoolean(@NonNull String key, List<Boolean> value);
58-
abstract T putByte(@NonNull String key, byte[] bytes);
59-
abstract void applyByte(@NonNull String key, byte[] bytes);
34+
abstract T put(@NonNull String key, String value);
35+
abstract void apply(@NonNull String key, String value);
36+
abstract T put(@NonNull String key, int value);
37+
abstract void apply(@NonNull String key, int value);
38+
abstract T put(@NonNull String key, long value);
39+
abstract void apply(@NonNull String key, long value);
40+
abstract T put(@NonNull String key, double value);
41+
abstract void apply(@NonNull String key, double value);
42+
abstract T put(@NonNull String key, float value);
43+
abstract void apply(@NonNull String key, float value);
44+
abstract T put(@NonNull String key, boolean value);
45+
abstract void apply(@NonNull String key, boolean value);
46+
abstract T put(@NonNull String key, List<?> value);
47+
abstract void apply(@NonNull String key, List<?> value);
48+
abstract T put(@NonNull String key, byte[] bytes);
49+
abstract void apply(@NonNull String key, byte[] bytes);
50+
abstract T putModel(@NonNull String key, Object object);
51+
abstract void applyModel(@NonNull String key, Object object);
6052

61-
abstract T putImage(@NonNull String key, @DrawableRes int resId, Context context);
62-
abstract T putImage(@NonNull String key, Bitmap bitmap);
63-
abstract T putImage(@NonNull String key, File file);
64-
abstract T putFile(@NonNull String key,File file,boolean deleteOldFile);
53+
abstract T putDrawable(@NonNull String key, @DrawableRes int resId, Context context);
54+
abstract void applyDrawable(@NonNull String key, @DrawableRes int resId, Context context);
55+
abstract T put(@NonNull String key, Bitmap bitmap);
56+
abstract void apply(@NonNull String key, Bitmap bitmap);
57+
abstract T put(@NonNull String key, File file);
58+
abstract void apply(@NonNull String key, File file);
59+
abstract T put(@NonNull String key,File file,boolean deleteOldFile);
60+
abstract void apply(@NonNull String key,File file,boolean deleteOldFile);
6561

66-
abstract T putMap(@NonNull String key,Map<String,String> values);
62+
abstract T put(@NonNull String key,Map<String,String> values);
63+
abstract void apply(@NonNull String key,Map<String,String> values);
6764

6865
abstract T remove(@NonNull String key);
6966
abstract T clear();

library/src/main/java/com/zeroone/conceal/BasePreferencesBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.SharedPreferences;
55
import android.support.annotation.Nullable;
66

7+
import com.zeroone.conceal.listener.OnDataChangeListener;
78
import com.zeroone.conceal.model.CryptoType;
89

910
/**

library/src/main/java/com/zeroone/conceal/BaseRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55

6-
import com.zeroone.conceal.model.Constant;
6+
import com.zeroone.conceal.listener.OnDataChangeListener;
77

88
/**
99
* Created by hafiq on 28/01/2018.

0 commit comments

Comments
 (0)