Skip to content

Commit ffa9ac5

Browse files
committed
integrate with gson.
1 parent f9b33e8 commit ffa9ac5

File tree

10 files changed

+289
-25
lines changed

10 files changed

+289
-25
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Gradle
1010
```gradle
1111
dependencies {
1212
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.3.1'
13+
14+
//or
15+
16+
compile 'com.github.afiqiqmal:ConcealSharedPreference-Android:1.3.1' {
17+
exclude group: 'com.google.code.gson', module: 'gson'
18+
}
1319
}
1420
```
1521

@@ -71,7 +77,10 @@ concealPrefRepository.putString(KEY,"Hello");
7177
concealPrefRepository.putInt(KEY,1000000);
7278
concealPrefRepository.putDouble(KEY,100.00);
7379
concealPrefRepository.putbyte(KEY,new byte[]);
74-
concealPrefRepository.putMap(KEY,new Map<String,String>())
80+
concealPrefRepository.putMap(KEY,new Map<String,String>());
81+
//using gson
82+
concealPrefRepository.putModel(KEY, Data.getUser(this));
83+
concealPrefRepository.putModel(KEY, Data.getTaskData(this));
7584

7685
// Files and Images will be encrypted
7786
// prefix of this encrypted images and files start with "conceal_enc_";
@@ -126,6 +135,10 @@ concealPrefRepository.getInt(KEY);
126135
concealPrefRepository.getInt(KEY,DEFAULT_VALUE);
127136
concealPrefRepository.getDouble(KEY);
128137
concealPrefRepository.getDouble(KEY,DEFAULT_VALUE);
138+
139+
//using gson
140+
concealPrefRepository.getModel(KEY, User.class).toString();
141+
concealPrefRepository.getModel(KEY, new TypeToken<ArrayList<Task>>(){}.getType()).toString();
129142
.....
130143

131144
Bitmap bitmap = concealPrefRepository.getImage(KEY); //return String path

app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ dependencies {
4242
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.1', {
4343
exclude group: 'com.android.support', module: 'support-annotations'
4444
})
45-
compile project(':library')
45+
46+
compile (project(':library')) {
47+
exclude group: 'com.google.code.gson', module: 'gson'
48+
}
49+
50+
compile 'com.google.code.gson:gson:2.8.2'
4651
}

app/src/main/assets/task.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"id": 269,
4+
"project_id": 7,
5+
"user_id": 417,
6+
"title": "Hellow World",
7+
"description": "Testing description lorem upsum heloo world"
8+
},
9+
{
10+
"id": 256,
11+
"project_id": 7,
12+
"user_id": 443,
13+
"title": "I suppose it.",
14+
"description": "I beat him when he sneezes; For he can thoroughly enjoy The pepper when he sneezes; For he can thoroughly enjoy The pepper when he sneezes: He only does it to annoy, Because he knows it teases.'."
15+
},
16+
{
17+
"id": 257,
18+
"project_id": 6,
19+
"user_id": 5,
20+
"title": "For instance,.",
21+
"description": "I ever was at in all directions, 'just like a writing-desk?' 'Come, we shall get on better.' 'I'd rather not,' the Cat again, sitting on a little pattering of feet in the back. At last the."
22+
}
23+
]

app/src/main/assets/users.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 417,
3+
"name": "Hafiq",
4+
"email": "[email protected]"
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.zeroone.concealexample;
2+
3+
import android.content.Context;
4+
5+
import com.google.gson.Gson;
6+
import com.google.gson.reflect.TypeToken;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* Created by hafiq on 15/12/2017.
15+
*/
16+
17+
public class Data {
18+
19+
public static List<Task> getTaskData(Context context) {
20+
return new Gson().fromJson(loadJSONFromAsset(context, "task.json"), new TypeToken<ArrayList<Task>>(){}.getType());
21+
}
22+
23+
public static User getUser(Context context) {
24+
return new Gson().fromJson(loadJSONFromAsset(context, "users.json"), User.class);
25+
}
26+
27+
public static String loadJSONFromAsset(Context context, String filename) {
28+
String json = null;
29+
try {
30+
InputStream is = context.getAssets().open(filename);
31+
int size = is.available();
32+
byte[] buffer = new byte[size];
33+
is.read(buffer);
34+
is.close();
35+
json = new String(buffer, "UTF-8");
36+
} catch (IOException ex) {
37+
ex.printStackTrace();
38+
return null;
39+
}
40+
return json;
41+
}
42+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import android.util.Log;
1010

1111
import com.facebook.crypto.CryptoConfig;
12+
import com.google.gson.reflect.TypeToken;
1213
import com.zeroone.conceal.ConcealCrypto;
1314
import com.zeroone.conceal.ConcealPrefRepository;
1415
import com.zeroone.conceal.model.CryptoFile;
1516

1617
import java.io.File;
18+
import java.util.ArrayList;
1719
import java.util.List;
1820
import java.util.Map;
1921

@@ -22,6 +24,8 @@ public class MainActivity extends BaseActivity {
2224
String NAME_KEY = "user_name";
2325
String AGE_KEY = "user_age";
2426
String EMAIL_KEY = "user_email";
27+
String USER_DETAIL = "user_detail";
28+
String TASK_DETAIL = "task_detail";
2529
String IMAGE_KEY = "user_image";
2630
String FILE_KEY = "user_file";
2731

@@ -38,9 +42,13 @@ protected void onCreate(Bundle savedInstanceState) {
3842
//FIRST TEST
3943
concealPrefRepository.putString(NAME_KEY, "HAFIQ IQMAL");
4044
concealPrefRepository.putInt(AGE_KEY, 24);
45+
concealPrefRepository.putModel(USER_DETAIL, Data.getUser(this));
46+
concealPrefRepository.putModel(TASK_DETAIL, Data.getTaskData(this));
4147

4248
Log.d("FIRST TEST", concealPrefRepository.getString(NAME_KEY));
43-
Log.d("FIRST TEST",concealPrefRepository.getString(AGE_KEY));
49+
Log.d("FIRST TEST", concealPrefRepository.getString(AGE_KEY));
50+
Log.d("FIRST TEST", concealPrefRepository.getModel(USER_DETAIL, User.class).toString());
51+
Log.d("FIRST TEST", concealPrefRepository.getModel(TASK_DETAIL, new TypeToken<ArrayList<Task>>(){}.getType()).toString());
4452
Log.d("FIRST TEST SIZE", ""+concealPrefRepository.getPrefsSize());
4553

4654
concealPrefRepository.clearPrefs();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.zeroone.concealexample;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author : hafiq on 22/09/2017.
7+
*/
8+
9+
public class Task {
10+
11+
private int id;
12+
private int project_id;
13+
private int user_id;
14+
private String title;
15+
private String description;
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public int getProject_id() {
26+
return project_id;
27+
}
28+
29+
public void setProject_id(int project_id) {
30+
this.project_id = project_id;
31+
}
32+
33+
public int getUser_id() {
34+
return user_id;
35+
}
36+
37+
public void setUser_id(int user_id) {
38+
this.user_id = user_id;
39+
}
40+
41+
public String getTitle() {
42+
return title;
43+
}
44+
45+
public void setTitle(String title) {
46+
this.title = title;
47+
}
48+
49+
public String getDescription() {
50+
return description;
51+
}
52+
53+
public void setDescription(String description) {
54+
this.description = description;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "Task{" +
60+
"id=" + id +
61+
", project_id=" + project_id +
62+
", user_id=" + user_id +
63+
", title='" + title + '\'' +
64+
", description='" + description + '\'' +
65+
'}';
66+
}
67+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.zeroone.concealexample;
2+
3+
/**
4+
* @author : hafiq on 22/09/2017.
5+
*/
6+
7+
public class User {
8+
9+
private int id;
10+
private String name;
11+
private String email;
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public void setId(int id) {
18+
this.id = id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public String getEmail() {
30+
return email;
31+
}
32+
33+
public void setEmail(String email) {
34+
this.email = email;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "User{" +
40+
"id=" + id +
41+
", name='" + name + '\'' +
42+
", email='" + email + '\'' +
43+
'}';
44+
}
45+
}

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ dependencies {
2424
compile fileTree(dir: 'libs', include: ['*.jar'])
2525
compile 'com.facebook.conceal:conceal:2.0.1@aar'
2626
compile 'com.android.support:support-annotations:27.0.2'
27+
compile 'com.google.code.gson:gson:2.8.2'
2728
}

0 commit comments

Comments
 (0)