Skip to content

Commit d88b9a0

Browse files
committed
Allow configure persistent store type in runtime.
1 parent 136bc81 commit d88b9a0

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

skygear/src/main/java/io/skygear/skygear/Configuration.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,27 @@ public final class Configuration {
5252
*/
5353
final boolean pubsubConnectAutomatically;
5454

55+
56+
/**
57+
* Boolean indicating whether encrypt current user data saved in SharedPreferences.
58+
*/
59+
final boolean encryptCurrentUserData;
60+
61+
5562
private Configuration(
5663
String endpoint,
5764
String apiKey,
5865
String gcmSenderId,
5966
boolean pubsubHandlerExecutionInBackground,
60-
boolean pubsubConnectAutomatically
67+
boolean pubsubConnectAutomatically,
68+
boolean encryptCurrentUserData
6169
) {
6270
this.endpoint = endpoint;
6371
this.apiKey = apiKey;
6472
this.gcmSenderId = gcmSenderId;
6573
this.pubsubHandlerExecutionInBackground = pubsubHandlerExecutionInBackground;
6674
this.pubsubConnectAutomatically = pubsubConnectAutomatically;
75+
this.encryptCurrentUserData = encryptCurrentUserData;
6776
}
6877

6978
/**
@@ -111,6 +120,15 @@ public boolean isPubsubConnectAutomatically() {
111120
return pubsubConnectAutomatically;
112121
}
113122

123+
/**
124+
* Encrypt current user data boolean.
125+
*
126+
* @return the boolean
127+
*/
128+
public boolean encryptCurrentUserData() {
129+
return encryptCurrentUserData;
130+
}
131+
114132
/**
115133
* Creates an instance of default configuration.
116134
*
@@ -148,6 +166,7 @@ public static final class Builder {
148166
private String gcmSenderId;
149167
private boolean pubsubHandlerExecutionInBackground;
150168
private boolean pubsubConnectAutomatically;
169+
private boolean encryptCurrentUserData;
151170

152171
/**
153172
* Creates an instance of Builder.
@@ -212,6 +231,18 @@ public Builder pubsubConnectAutomatically(boolean automatic) {
212231
return this;
213232
}
214233

234+
/**
235+
* Sets whether encrypt current user data saved in SharedPreferences.
236+
*
237+
* @param enabled the boolean indicating whether encryption Enabled
238+
* automatically.
239+
* @return the builder
240+
*/
241+
public Builder encryptCurrentUserData(boolean enabled) {
242+
this.encryptCurrentUserData = enabled;
243+
return this;
244+
}
245+
215246
/**
216247
* Build a configuration.
217248
*
@@ -231,7 +262,8 @@ public Configuration build() {
231262
this.apiKey,
232263
this.gcmSenderId,
233264
this.pubsubHandlerExecutionInBackground,
234-
this.pubsubConnectAutomatically
265+
this.pubsubConnectAutomatically,
266+
this.encryptCurrentUserData
235267
);
236268
}
237269
}

skygear/src/main/java/io/skygear/skygear/Container.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class Container {
3131
private static final String TAG = "Skygear SDK";
3232
private static Container sharedInstance;
3333

34-
final PersistentStore persistentStore;
34+
PersistentStore persistentStore;
3535
final Context context;
3636
final RequestManager requestManager;
3737
Configuration config;
@@ -61,18 +61,13 @@ public Container(Context context, Configuration config) {
6161
this.context = context.getApplicationContext();
6262
this.config = config;
6363
this.requestManager = new RequestManager(this.context, this.config);
64-
this.persistentStore = new SecurePersistentStore(this.context);
6564

6665
this.auth = new AuthContainer(this);
6766
this.pubsub = new PubsubContainer(this);
6867
this.push = new PushContainer(this);
6968
this.publicDatabase = Database.Factory.publicDatabase(this);
7069
this.privateDatabase = Database.Factory.privateDatabase(this);
71-
this.requestManager.accessToken = this.persistentStore.accessToken;
72-
73-
if (this.persistentStore.defaultAccessControl != null) {
74-
AccessControl.defaultAccessControl = this.persistentStore.defaultAccessControl;
75-
}
70+
this.configPersistentStore(config);
7671
}
7772

7873
/**
@@ -153,6 +148,7 @@ public void configure(Configuration config) {
153148
this.config = config;
154149
this.requestManager.configure(config);
155150
this.pubsub.configure(config);
151+
this.configPersistentStore(config);
156152
}
157153

158154
/**
@@ -263,4 +259,16 @@ public final void onFailure(Error error) {
263259
}
264260
});
265261
}
262+
263+
private void configPersistentStore(Configuration config) {
264+
if (config != null && config.encryptCurrentUserData) {
265+
this.persistentStore = new SecurePersistentStore(this.context);
266+
} else {
267+
this.persistentStore = new PersistentStore(this.context);
268+
}
269+
this.requestManager.accessToken = this.persistentStore.accessToken;
270+
if (this.persistentStore.defaultAccessControl != null) {
271+
AccessControl.defaultAccessControl = this.persistentStore.defaultAccessControl;
272+
}
273+
}
266274
}

skygear/src/main/java/io/skygear/skygear/SkygearApplication.java

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public boolean isPubsubHandlerExecutionInBackground() {
5555
return false;
5656
}
5757

58+
59+
/**
60+
* Gets whether encrypt current user data saved in SharedPreferences.
61+
*
62+
* @return the boolean indicating whether encrypt current user data saved in SharedPreferences
63+
*/
64+
public boolean encryptCurrentUserData() {
65+
return false;
66+
}
67+
5868
@Override
5969
public void onCreate() {
6070
super.onCreate();
@@ -64,6 +74,7 @@ public void onCreate() {
6474
.apiKey(this.getApiKey())
6575
.gcmSenderId(this.getGcmSenderId())
6676
.pubsubHandlerExecutionInBackground(this.isPubsubHandlerExecutionInBackground())
77+
.encryptCurrentUserData(this.encryptCurrentUserData())
6778
.build();
6879

6980
Container.defaultContainer(this).configure(config);

0 commit comments

Comments
 (0)