Skip to content

Commit f3e78cc

Browse files
Add better, more flexible APIs for SDK initialization.
This PR hopes to unify some of these initialization settings int o a single 'configuration' class that can be easily used to intialize parse in a much cleaner way, especially as we add even more initialization options.
1 parent 652ede3 commit f3e78cc

File tree

1 file changed

+88
-8
lines changed

1 file changed

+88
-8
lines changed

Parse/src/main/java/com/parse/Parse.java

+88-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.RandomAccessFile;
2424
import java.io.UnsupportedEncodingException;
2525
import java.util.ArrayList;
26+
import java.util.Collection;
2627
import java.util.HashSet;
2728
import java.util.List;
2829
import java.util.Set;
@@ -35,6 +36,69 @@
3536
* library.
3637
*/
3738
public class Parse {
39+
public static final class Configuration {
40+
public static final class Builder {
41+
private Context context;
42+
private String applicationId;
43+
private String clientKey;
44+
private boolean localDataStoreEnabled;
45+
private List<ParseNetworkInterceptor> interceptors;
46+
47+
public Builder(Context context) {
48+
this.context = context;
49+
}
50+
51+
public Builder applicationId(String applicationId) {
52+
this.applicationId = applicationId;
53+
return this;
54+
}
55+
56+
public Builder clientKey(String clientKey) {
57+
this.clientKey = clientKey;
58+
return this;
59+
}
60+
61+
public Builder addNetworkInterceptor(ParseNetworkInterceptor interceptor) {
62+
if (interceptors == null) {
63+
interceptors = new ArrayList<>();
64+
}
65+
interceptors.add(interceptor);
66+
return this;
67+
}
68+
69+
public Builder enableLocalDataStore() {
70+
localDataStoreEnabled = true;
71+
return this;
72+
}
73+
74+
private Builder setNetworkInterceptors(Collection<ParseNetworkInterceptor> interceptors) {
75+
if (interceptors != null) {
76+
interceptors.clear();
77+
interceptors.addAll(interceptors);
78+
}
79+
return this;
80+
}
81+
82+
public Configuration build() {
83+
return new Configuration(this);
84+
}
85+
}
86+
87+
private final Context context;
88+
private final String applicationId;
89+
private final String clientKey;
90+
private final boolean localDataStoreEnabled;
91+
private final List<ParseNetworkInterceptor> interceptors;
92+
93+
private Configuration(Builder builder) {
94+
this.context = builder.context;
95+
this.applicationId = builder.applicationId;
96+
this.clientKey = builder.clientKey;
97+
this.localDataStoreEnabled = builder.localDataStoreEnabled;
98+
this.interceptors = builder.interceptors;
99+
}
100+
}
101+
38102
private static final String PARSE_APPLICATION_ID = "com.parse.APPLICATION_ID";
39103
private static final String PARSE_CLIENT_KEY = "com.parse.CLIENT_KEY";
40104

@@ -46,6 +110,8 @@ public class Parse {
46110
private static boolean isLocalDatastoreEnabled;
47111
private static OfflineStore offlineStore;
48112

113+
private static Configuration configuration;
114+
49115
/**
50116
* Enable pinning in your application. This must be called before your application can use
51117
* pinning. You must invoke {@code enableLocalDatastore(Context)} before
@@ -158,7 +224,12 @@ public static void initialize(Context context) {
158224
} else {
159225
throw new RuntimeException("Can't get Application Metadata");
160226
}
161-
initialize(context, applicationId, clientKey);
227+
initialize(new Configuration.Builder(context)
228+
.applicationId(applicationId)
229+
.clientKey(clientKey)
230+
.setNetworkInterceptors(interceptors)
231+
.build()
232+
);
162233
}
163234

164235
/**
@@ -188,8 +259,19 @@ public static void initialize(Context context) {
188259
* The client key provided in the Parse dashboard.
189260
*/
190261
public static void initialize(Context context, String applicationId, String clientKey) {
191-
ParsePlugins.Android.initialize(context, applicationId, clientKey);
192-
Context applicationContext = context.getApplicationContext();
262+
initialize(new Configuration.Builder(context)
263+
.applicationId(applicationId)
264+
.clientKey(clientKey)
265+
.setNetworkInterceptors(interceptors)
266+
.build()
267+
);
268+
}
269+
270+
public static void initialize(Configuration configuration) {
271+
Parse.configuration = configuration;
272+
273+
ParsePlugins.Android.initialize(configuration.context, configuration.applicationId, configuration.clientKey);
274+
Context applicationContext = configuration.context.getApplicationContext();
193275

194276
ParseHttpClient.setKeepAlive(true);
195277
ParseHttpClient.setMaxConnections(20);
@@ -201,9 +283,9 @@ public static void initialize(Context context, String applicationId, String clie
201283
ParseObject.registerParseSubclasses();
202284

203285
if (isLocalDatastoreEnabled()) {
204-
offlineStore = new OfflineStore(context);
286+
offlineStore = new OfflineStore(configuration.context);
205287
} else {
206-
ParseKeyValueCache.initialize(context);
288+
ParseKeyValueCache.initialize(configuration.context);
207289
}
208290

209291
// Make sure the data on disk for Parse is for the current
@@ -585,6 +667,7 @@ private Parse() {
585667
// Initialize all necessary http clients and add interceptors to these http clients
586668
private static void initializeParseHttpClientsWithParseNetworkInterceptors() {
587669
// This means developers have not called addInterceptor method so we should do nothing.
670+
List<ParseNetworkInterceptor> interceptors = configuration.interceptors;
588671
if (interceptors == null) {
589672
return;
590673
}
@@ -605,9 +688,6 @@ private static void initializeParseHttpClientsWithParseNetworkInterceptors() {
605688
parseHttpClient.addExternalInterceptor(interceptor);
606689
}
607690
}
608-
609-
// Remove interceptors reference since we do not need it anymore
610-
interceptors = null;
611691
}
612692

613693

0 commit comments

Comments
 (0)