23
23
import java .io .RandomAccessFile ;
24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .util .ArrayList ;
26
+ import java .util .Collection ;
26
27
import java .util .HashSet ;
27
28
import java .util .List ;
28
29
import java .util .Set ;
35
36
* library.
36
37
*/
37
38
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
+
38
102
private static final String PARSE_APPLICATION_ID = "com.parse.APPLICATION_ID" ;
39
103
private static final String PARSE_CLIENT_KEY = "com.parse.CLIENT_KEY" ;
40
104
@@ -46,6 +110,8 @@ public class Parse {
46
110
private static boolean isLocalDatastoreEnabled ;
47
111
private static OfflineStore offlineStore ;
48
112
113
+ private static Configuration configuration ;
114
+
49
115
/**
50
116
* Enable pinning in your application. This must be called before your application can use
51
117
* pinning. You must invoke {@code enableLocalDatastore(Context)} before
@@ -158,7 +224,12 @@ public static void initialize(Context context) {
158
224
} else {
159
225
throw new RuntimeException ("Can't get Application Metadata" );
160
226
}
161
- initialize (context , applicationId , clientKey );
227
+ initialize (new Configuration .Builder (context )
228
+ .applicationId (applicationId )
229
+ .clientKey (clientKey )
230
+ .setNetworkInterceptors (interceptors )
231
+ .build ()
232
+ );
162
233
}
163
234
164
235
/**
@@ -188,8 +259,19 @@ public static void initialize(Context context) {
188
259
* The client key provided in the Parse dashboard.
189
260
*/
190
261
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 ();
193
275
194
276
ParseHttpClient .setKeepAlive (true );
195
277
ParseHttpClient .setMaxConnections (20 );
@@ -201,9 +283,9 @@ public static void initialize(Context context, String applicationId, String clie
201
283
ParseObject .registerParseSubclasses ();
202
284
203
285
if (isLocalDatastoreEnabled ()) {
204
- offlineStore = new OfflineStore (context );
286
+ offlineStore = new OfflineStore (configuration . context );
205
287
} else {
206
- ParseKeyValueCache .initialize (context );
288
+ ParseKeyValueCache .initialize (configuration . context );
207
289
}
208
290
209
291
// Make sure the data on disk for Parse is for the current
@@ -585,6 +667,7 @@ private Parse() {
585
667
// Initialize all necessary http clients and add interceptors to these http clients
586
668
private static void initializeParseHttpClientsWithParseNetworkInterceptors () {
587
669
// This means developers have not called addInterceptor method so we should do nothing.
670
+ List <ParseNetworkInterceptor > interceptors = configuration .interceptors ;
588
671
if (interceptors == null ) {
589
672
return ;
590
673
}
@@ -605,9 +688,6 @@ private static void initializeParseHttpClientsWithParseNetworkInterceptors() {
605
688
parseHttpClient .addExternalInterceptor (interceptor );
606
689
}
607
690
}
608
-
609
- // Remove interceptors reference since we do not need it anymore
610
- interceptors = null ;
611
691
}
612
692
613
693
0 commit comments