Skip to content

Commit 9a3aa6c

Browse files
committed
use abstract remove redundant method. update gradle
1 parent 0d85faf commit 9a3aa6c

File tree

14 files changed

+685
-279
lines changed

14 files changed

+685
-279
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ Permission need to use in your project. Please Allow it first, or it will affect
5151
SharedPreferences initialize
5252
```java
5353
ConcealPrefRepository concealPrefRepository = new ConcealPrefRepository.PreferencesBuilder(this)
54-
.useDefaultPrefStorage()
5554
//.useThisPrefStorage("Android_Prefs")
56-
.sharedPrefsBackedKeyChain(CryptoConfig.KEY_256) //CryptoConfig.KEY_256 or CryptoConfig.KEY_128
55+
.sharedPrefsBackedKeyChain(CryptoType.KEY_256) //CryptoType.KEY_256 or CryptoType.KEY_128
5756
.enableCrypto(true,true) //param 1 - enable value encryption , param 2 - enable key encryption
5857
.createPassword("Android") //default value - BuildConfig.APPLICATION_ID
5958
.setFolderName("testing") //create Folder for data stored: default is - "conceal_path"
@@ -220,7 +219,7 @@ new ConcealPrefRepository.UserPref("KEY PREFIX").setLastName("Firstname").apply(
220219
### Extra Usage for Conceal Encryption and Decryption
221220

222221
```java
223-
ConcealCrypto concealCrypto = new ConcealCrypto(this,CryptoConfig.KEY_256); // CryptoConfig.KEY_256 or CryptoConfig.KEY_128
222+
ConcealCrypto concealCrypto = new ConcealCrypto(this,CryptoType.KEY_256); // CryptoType.KEY_256 or CryptoType.KEY_128
224223
concealCrypto.setEnableCrypto(true); //default true
225224
concealCrypto.setmEntityPassword("Android");
226225
concealCrypto.setmEntityPassword(Entity.create("Android"));
@@ -240,7 +239,7 @@ OR
240239
```java
241240
ConcealCrypto concealCrypto1 = new ConcealCrypto.CryptoBuilder(this)
242241
.setEnableCrypto(true) //default true
243-
.setKeyChain(CryptoConfig.KEY_256) // CryptoConfig.KEY_256 or CryptoConfig.KEY_128
242+
.setKeyChain(CryptoType.KEY_256) // CryptoType.KEY_256 or CryptoType.KEY_128
244243
.createPassword("Mac OSX")
245244
.create();
246245

app/build.gradle

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 27
5-
buildToolsVersion "27.0.1"
5+
buildToolsVersion "27.0.3"
66
defaultConfig {
77
applicationId "com.zeroone.concealexample"
88
minSdkVersion 16
@@ -31,19 +31,22 @@ android {
3131
}
3232

3333
dependencies {
34-
compile fileTree(include: ['*.jar'], dir: 'libs')
35-
compile 'com.android.support:appcompat-v7:27.0.2'
36-
androidTestCompile 'com.android.support:support-annotations:27.0.2'
37-
androidTestCompile 'com.android.support.test:runner:1.0.1'
38-
androidTestCompile 'com.android.support.test:rules:1.0.1'
34+
implementation fileTree(include: ['*.jar'], dir: 'libs')
35+
implementation 'com.android.support:appcompat-v7:27.0.2'
36+
androidTestImplementation 'junit:junit:4.12'
37+
androidTestImplementation 'com.android.support:support-annotations:27.0.2'
38+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
39+
androidTestImplementation 'com.android.support.test:rules:1.0.1'
40+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
41+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
3942
// Optional -- Hamcrest library
40-
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
43+
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
4144

42-
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.1', {
45+
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
4346
exclude group: 'com.android.support', module: 'support-annotations'
4447
})
4548

46-
compile (project(':library')) {
49+
api (project(':library')) {
4750
exclude group: 'com.google.code.gson', module: 'gson'
4851
}
4952

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import android.support.annotation.Nullable;
55
import android.support.v7.app.AppCompatActivity;
66

7-
import com.facebook.crypto.CryptoConfig;
87
import com.zeroone.conceal.ConcealPrefRepository;
98
import com.zeroone.conceal.OnDataChangeListener;
9+
import com.zeroone.conceal.model.CryptoType;
1010

1111
/**
1212
* @author : hafiq on 27/03/2017.
@@ -21,13 +21,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2121
super.onCreate(savedInstanceState);
2222

2323
concealPrefRepository = new ConcealPrefRepository.PreferencesBuilder(this)
24-
.useDefaultPrefStorage()
25-
.sharedPrefsBackedKeyChain(CryptoConfig.KEY_256)
24+
.sharedPrefsBackedKeyChain(CryptoType.KEY_256)
2625
.enableCrypto(false,true)
2726
.createPassword("Password@123")
2827
.setPrefListener(this)
2928
.create();
30-
3129
}
3230

3331
@Override

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import android.os.Bundle;
44
import android.util.Log;
55

6-
import com.facebook.crypto.CryptoConfig;
76
import com.google.gson.reflect.TypeToken;
87
import com.zeroone.conceal.ConcealCrypto;
98
import com.zeroone.conceal.ConcealPrefRepository;
9+
import com.zeroone.conceal.model.CryptoType;
1010

1111
import java.util.ArrayList;
12+
import java.util.List;
1213
import java.util.Map;
1314

1415
public class MainActivity extends BaseActivity {
@@ -20,6 +21,8 @@ public class MainActivity extends BaseActivity {
2021
String TASK_DETAIL = "task_detail";
2122
String IMAGE_KEY = "user_image";
2223
String FILE_KEY = "user_file";
24+
String PREFIX = "testing";
25+
2326

2427
@Override
2528
protected void onCreate(Bundle savedInstanceState) {
@@ -29,7 +32,7 @@ protected void onCreate(Bundle savedInstanceState) {
2932
ConcealPrefRepository.applicationInit(getApplication());
3033

3134
concealPrefRepository.clearPrefs();
32-
35+
3336

3437
//FIRST TEST
3538
concealPrefRepository.putString(NAME_KEY, "HAFIQ IQMAL");
@@ -56,25 +59,28 @@ protected void onCreate(Bundle savedInstanceState) {
5659
Log.d("SECOND TEST",concealPrefRepository.getString(AGE_KEY));
5760
Log.d("SECOND TEST SIZE", ""+concealPrefRepository.getPrefsSize());
5861

62+
getList();
5963

6064

6165
concealPrefRepository.clearPrefs();
6266

6367
//add user details preferences
64-
new ConcealPrefRepository.UserPref("PREFIX").setFirstName("Firstname").setLastName("Lasname").setEmail("[email protected]").apply();
68+
new ConcealPrefRepository.UserPref(PREFIX).setFirstName("Firstname").setLastName("Lasname").setEmail("[email protected]").apply();
69+
70+
getList();
6571

6672
//get user details
67-
Log.d("THIRD_TEST",new ConcealPrefRepository.UserPref("PREFIX").getFirstName());
68-
Log.d("THIRD_TEST",new ConcealPrefRepository.UserPref().setDefault("No Data").getLastName());
69-
Log.d("THIRD_TEST",new ConcealPrefRepository.UserPref("PREFIX").setDefault("No Data").getEmail());
73+
Log.d("THIRD_TEST", new ConcealPrefRepository.UserPref(PREFIX).getFirstName());
74+
Log.d("THIRD_TEST", new ConcealPrefRepository.UserPref().setDefault("No Data").getLastName());
75+
Log.d("THIRD_TEST", new ConcealPrefRepository.UserPref(PREFIX).setDefault("No Data").getEmail());
7076
Log.d("THIRD_TEST TEST SIZE", ""+concealPrefRepository.getPrefsSize());
7177

7278

7379

7480
concealPrefRepository.clearPrefs();
7581

7682

77-
ConcealPrefRepository.UserPref userPref = new ConcealPrefRepository.UserPref("PREFIX", "No Data");
83+
ConcealPrefRepository.UserPref userPref = new ConcealPrefRepository.UserPref(PREFIX, "No Data");
7884
userPref.setUserName("afiqiqmal");
7985
userPref.setEmail("[email protected]");
8086
userPref.apply();
@@ -86,7 +92,7 @@ protected void onCreate(Bundle savedInstanceState) {
8692

8793

8894

89-
ConcealPrefRepository.DevicePref devicePref = new ConcealPrefRepository.DevicePref("PREFIX", "No Data");
95+
ConcealPrefRepository.DevicePref devicePref = new ConcealPrefRepository.DevicePref(PREFIX, "No Data");
9096
devicePref.setDeviceId("ABC123123123");
9197
devicePref.setDeviceOS("android");
9298
devicePref.apply();
@@ -107,12 +113,14 @@ protected void onCreate(Bundle savedInstanceState) {
107113
e.printStackTrace();
108114
}
109115
}
110-
Log.d("VIEW ALL SIZE", ""+concealPrefRepository.getPrefsSize());
116+
117+
118+
getList();
111119

112120

113121
ConcealCrypto concealCrypto = new ConcealCrypto.CryptoBuilder(this)
114122
.setEnableCrypto(true) //default true
115-
.setKeyChain(CryptoConfig.KEY_256) // CryptoConfig.KEY_256 or CryptoConfig.KEY_128
123+
.setKeyChain(CryptoType.KEY_256) // CryptoType.KEY_256 or CryptoType.KEY_128
116124
.createPassword("Mac OSX")
117125
.create();
118126

@@ -137,4 +145,19 @@ protected void onCreate(Bundle savedInstanceState) {
137145

138146

139147
}
148+
149+
150+
private void getList() {
151+
List<String> mapList = concealPrefRepository.getAllSharedPrefDataToString();
152+
for(String s: mapList){
153+
try {
154+
Log.d("VIEW_LIST", s);
155+
}
156+
catch (Exception e){
157+
e.printStackTrace();
158+
}
159+
}
160+
161+
Log.d("VIEW ALL SIZE", ""+concealPrefRepository.getPrefsSize());
162+
}
140163
}

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ buildscript {
77
}
88
dependencies {
99
classpath 'com.android.tools.build:gradle:3.0.1'
10-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
11-
1210
// NOTE: Do not place your application dependencies here; they belong
1311
// in the individual module build.gradle files
1412
}

library/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ group='com.github.afiqiqmal'
44

55
android {
66
compileSdkVersion 27
7-
buildToolsVersion "27.0.1"
7+
buildToolsVersion "27.0.3"
88

99
defaultConfig {
1010
minSdkVersion 16
@@ -21,8 +21,8 @@ android {
2121
}
2222

2323
dependencies {
24-
compile fileTree(dir: 'libs', include: ['*.jar'])
25-
compile 'com.facebook.conceal:conceal:2.0.1@aar'
26-
compile 'com.android.support:support-annotations:27.0.2'
27-
compile 'com.google.code.gson:gson:2.8.2'
24+
implementation fileTree(dir: 'libs', include: ['*.jar'])
25+
implementation 'com.facebook.conceal:conceal:2.0.2@aar'
26+
implementation 'com.android.support:support-annotations:27.0.2'
27+
implementation 'com.google.code.gson:gson:2.8.2'
2828
}

library/proguard-rules.pro

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424
# hide the original source file name.
2525
#-renamesourcefileattribute SourceFile
2626

27-
-renamesourcefileattribute SourceFile
28-
-repackageclasses ''
29-
-flattenpackagehierarchy
30-
-dontskipnonpubliclibraryclasses
31-
-forceprocessing
32-
33-
#########--------Android Support--------#########
34-
-keep class android.support.v4.app.** { *; }
35-
-keep interface android.support.v4.app.** { *; }
36-
-dontwarn android.support.**
37-
38-
#########--------Remove Log--------#########
39-
-assumenosideeffects class android.util.Log { *; }
40-
41-
42-
-keep class com.zeroone.conceal.model.Constant { *; }
27+
#-renamesourcefileattribute SourceFile
28+
#-repackageclasses ''
29+
#-flattenpackagehierarchy
30+
#-dontskipnonpubliclibraryclasses
31+
#-forceprocessing
32+
#
33+
##########--------Android Support--------#########
34+
#-keep class android.support.v4.app.** { *; }
35+
#-keep interface android.support.v4.app.** { *; }
36+
#-dontwarn android.support.**
37+
#
38+
##########--------Remove Log--------#########
39+
#-assumenosideeffects class android.util.Log { *; }
40+
#
41+
#
42+
#-keep class com.zeroone.conceal.model.Constant { *; }
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.zeroone.conceal;
2+
3+
import android.annotation.SuppressLint;
4+
import android.content.SharedPreferences;
5+
import android.support.annotation.Nullable;
6+
7+
/**
8+
* Created by hafiq on 28/01/2018.
9+
*/
10+
11+
@SuppressLint("CommitPrefEdits")
12+
abstract class BaseBuilderAbstract {
13+
14+
private String PREFIX = "conceal";
15+
private String DEFAULT_VALUE = null;
16+
private String SEPARATOR = "_";
17+
private SharedPreferences sharedPreferences;
18+
private SharedPreferences.Editor editor;
19+
private ConcealCrypto concealCrypto;
20+
21+
BaseBuilderAbstract(SharedPreferences sharedPreferences) {
22+
this.PREFIX = null;
23+
24+
if (sharedPreferences == null){
25+
throw new IllegalArgumentException("Need to initialize ConcealPrefRepository.PreferencesBuilder first");
26+
}
27+
28+
this.sharedPreferences = sharedPreferences;
29+
this.editor = sharedPreferences.edit();
30+
}
31+
32+
BaseBuilderAbstract(@Nullable String keyPrefix, SharedPreferences sharedPreferences) {
33+
if (keyPrefix != null)
34+
this.PREFIX = keyPrefix+this.SEPARATOR;
35+
36+
if (sharedPreferences == null){
37+
throw new IllegalArgumentException("Need to initialize ConcealPrefRepository.PreferencesBuilder first");
38+
}
39+
40+
this.sharedPreferences = sharedPreferences;
41+
this.editor = sharedPreferences.edit();
42+
43+
}
44+
45+
BaseBuilderAbstract(@Nullable String keyPrefix, @Nullable String defaultEmptyValue, SharedPreferences sharedPreferences) {
46+
if (defaultEmptyValue != null) {
47+
this.DEFAULT_VALUE = defaultEmptyValue;
48+
}
49+
50+
if (keyPrefix != null)
51+
this.PREFIX = keyPrefix+this.SEPARATOR;
52+
53+
if (sharedPreferences == null){
54+
throw new IllegalArgumentException("Need to initialize ConcealPrefRepository.PreferencesBuilder first");
55+
}
56+
57+
this.sharedPreferences = sharedPreferences;
58+
this.editor = sharedPreferences.edit();
59+
60+
}
61+
62+
void setConcealCrypto(ConcealCrypto concealCrypto) {
63+
this.concealCrypto = concealCrypto;
64+
}
65+
66+
void setDefaultValue(String defaultValue) {
67+
if (defaultValue != null) {
68+
this.DEFAULT_VALUE = defaultValue;
69+
}
70+
}
71+
72+
SharedPreferences.Editor getEditor() {
73+
if (editor == null){
74+
throw new IllegalArgumentException("Need to initialize ConcealPrefRepository.PreferencesBuilder first");
75+
}
76+
77+
return editor;
78+
}
79+
80+
81+
String returnValue(String KEY){
82+
String value = concealCrypto.deObscure(sharedPreferences.getString(setHashKey(KEY), null));
83+
if (value == null)
84+
return this.DEFAULT_VALUE;
85+
86+
return value;
87+
}
88+
89+
String setHashKey(String key) {
90+
return concealCrypto.hashKey(PREFIX+key);
91+
}
92+
93+
String hideValue(String value) {
94+
return concealCrypto.obscure(value);
95+
}
96+
97+
protected abstract void apply();
98+
99+
protected abstract void commit();
100+
101+
}

0 commit comments

Comments
 (0)