24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .util .ArrayList ;
26
26
import java .util .Collection ;
27
+ import java .util .Collections ;
27
28
import java .util .HashSet ;
28
29
import java .util .List ;
29
30
import java .util .Set ;
36
37
* library.
37
38
*/
38
39
public class Parse {
40
+ /**
41
+ * Represents an opaque configuration for the {@code Parse} SDK configuration.
42
+ */
39
43
public static final class Configuration {
44
+ /**
45
+ * Allows for simple constructing of a {@code Configuration} object.
46
+ */
40
47
public static final class Builder {
41
48
private Context context ;
42
49
private String applicationId ;
43
50
private String clientKey ;
44
51
private boolean localDataStoreEnabled ;
45
52
private List <ParseNetworkInterceptor > interceptors ;
46
53
54
+ /**
55
+ * Initialize a bulider with a given context.
56
+ *
57
+ * This context will then be passed through to the rest of the Parse SDK for use during
58
+ * initialization.
59
+ *
60
+ * <p/>
61
+ * You may define {@code com.parse.APPLICATION_ID} and {@code com.parse.CLIENT_KEY}
62
+ * {@code meta-data} in your {@code AndroidManifest.xml}:
63
+ * <pre>
64
+ * <manifest ...>
65
+ *
66
+ * ...
67
+ *
68
+ * <application ...>
69
+ * <meta-data
70
+ * android:name="com.parse.APPLICATION_ID"
71
+ * android:value="@string/parse_app_id" />
72
+ * <meta-data
73
+ * android:name="com.parse.CLIENT_KEY"
74
+ * android:value="@string/parse_client_key" />
75
+ *
76
+ * ...
77
+ *
78
+ * </application>
79
+ * </manifest>
80
+ * </pre>
81
+ * <p/>
82
+ *
83
+ * This will cause the values for {@code applicationId} and {@code clientKey} to be set to
84
+ * those defined in your manifest.
85
+ *
86
+ * @param context The active {@link Context} for your application. Cannot be null.
87
+ */
47
88
public Builder (Context context ) {
48
89
this .context = context ;
90
+
91
+ // Yes, our public API states we cannot be null. But for unit tests, it's easier just to
92
+ // support null here.
93
+ if (context != null ) {
94
+ Context applicationContext = context .getApplicationContext ();
95
+ Bundle metaData = ManifestInfo .getApplicationMetadata (applicationContext );
96
+ if (metaData != null ) {
97
+ applicationId = metaData .getString (PARSE_APPLICATION_ID );
98
+ clientKey = metaData .getString (PARSE_CLIENT_KEY );
99
+ }
100
+ }
49
101
}
50
102
103
+ /**
104
+ * Set the application id to be used by Parse.
105
+ *
106
+ * This method is only required if you intend to use a different {@code applicationId} than
107
+ * is defined by {@code com.parse.APPLICATION_ID} in your {@code AndroidManifest.xml}.
108
+ *
109
+ * @param applicationId The application id to set.
110
+ * @return The same builder, for easy chaining.
111
+ */
51
112
public Builder applicationId (String applicationId ) {
52
113
this .applicationId = applicationId ;
53
114
return this ;
54
115
}
55
116
117
+ /**
118
+ * Set the client key to be used by Parse.
119
+ *
120
+ * This method is only required if you intend to use a different {@code clientKey} than
121
+ * is defined by {@code com.parse.CLIENT_KEY} in your {@code AndroidManifest.xml}.
122
+ *
123
+ * @param clientKey The client key to set.
124
+ * @return The same builder, for easy chaining.
125
+ */
56
126
public Builder clientKey (String clientKey ) {
57
127
this .clientKey = clientKey ;
58
128
return this ;
59
129
}
60
130
131
+ /**
132
+ * Add a {@link ParseNetworkInterceptor}.
133
+ *
134
+ * @param interceptor The interceptor to add.
135
+ * @return The same builder, for easy chaining.
136
+ */
61
137
public Builder addNetworkInterceptor (ParseNetworkInterceptor interceptor ) {
62
138
if (interceptors == null ) {
63
139
interceptors = new ArrayList <>();
@@ -66,36 +142,52 @@ public Builder addNetworkInterceptor(ParseNetworkInterceptor interceptor) {
66
142
return this ;
67
143
}
68
144
145
+ /**
146
+ * Enable pinning in your application. This must be called before your application can use
147
+ * pinning.
148
+ * @return The same builder, for easy chaining.
149
+ */
69
150
public Builder enableLocalDataStore () {
70
151
localDataStoreEnabled = true ;
71
152
return this ;
72
153
}
73
154
74
155
private Builder setNetworkInterceptors (Collection <ParseNetworkInterceptor > interceptors ) {
75
156
if (interceptors != null ) {
76
- interceptors .clear ();
77
- interceptors .addAll (interceptors );
157
+ this . interceptors .clear ();
158
+ this . interceptors .addAll (interceptors );
78
159
}
79
160
return this ;
80
161
}
81
162
163
+ private Builder setLocalDatastoreEnabled (boolean enabled ) {
164
+ localDataStoreEnabled = enabled ;
165
+ return this ;
166
+ }
167
+
168
+ /**
169
+ * Construct this builder into a concrete {@code Configuration} instance.
170
+ * @return A constructed {@code Configuration} object.
171
+ */
82
172
public Configuration build () {
83
173
return new Configuration (this );
84
174
}
85
175
}
86
176
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 ;
177
+ /* package for tests */ final Context context ;
178
+ /* package for tests */ final String applicationId ;
179
+ /* package for tests */ final String clientKey ;
180
+ /* package for tests */ final boolean localDataStoreEnabled ;
181
+ /* package for tests */ final List <ParseNetworkInterceptor > interceptors ;
92
182
93
183
private Configuration (Builder builder ) {
94
184
this .context = builder .context ;
95
185
this .applicationId = builder .applicationId ;
96
186
this .clientKey = builder .clientKey ;
97
187
this .localDataStoreEnabled = builder .localDataStoreEnabled ;
98
- this .interceptors = builder .interceptors ;
188
+ this .interceptors = builder .interceptors != null ?
189
+ Collections .unmodifiableList (new ArrayList <>(builder .interceptors )) :
190
+ null ;
99
191
}
100
192
}
101
193
@@ -110,8 +202,6 @@ private Configuration(Builder builder) {
110
202
private static boolean isLocalDatastoreEnabled ;
111
203
private static OfflineStore offlineStore ;
112
204
113
- private static Configuration configuration ;
114
-
115
205
/**
116
206
* Enable pinning in your application. This must be called before your application can use
117
207
* pinning. You must invoke {@code enableLocalDatastore(Context)} before
@@ -199,36 +289,23 @@ public static void enableLocalDatastore(Context context) {
199
289
* The active {@link Context} for your application.
200
290
*/
201
291
public static void initialize (Context context ) {
202
- Context applicationContext = context .getApplicationContext ();
203
- String applicationId ;
204
- String clientKey ;
205
- Bundle metaData = ManifestInfo .getApplicationMetadata (applicationContext );
206
- if (metaData != null ) {
207
- applicationId = metaData .getString (PARSE_APPLICATION_ID );
208
- clientKey = metaData .getString (PARSE_CLIENT_KEY );
209
-
210
- if (applicationId == null ) {
211
- throw new RuntimeException ("ApplicationId not defined. " +
212
- "You must provide ApplicationId in AndroidManifest.xml.\n " +
213
- "<meta-data\n " +
214
- " android:name=\" com.parse.APPLICATION_ID\" \n " +
215
- " android:value=\" <Your Application Id>\" />" );
216
- }
217
- if (clientKey == null ) {
218
- throw new RuntimeException ("ClientKey not defined. " +
219
- "You must provide ClientKey in AndroidManifest.xml.\n " +
220
- "<meta-data\n " +
221
- " android:name=\" com.parse.CLIENT_KEY\" \n " +
222
- " android:value=\" <Your Client Key>\" />" );
223
- }
224
- } else {
225
- throw new RuntimeException ("Can't get Application Metadata" );
226
- }
227
- initialize (new Configuration .Builder (context )
228
- .applicationId (applicationId )
229
- .clientKey (clientKey )
230
- .setNetworkInterceptors (interceptors )
231
- .build ()
292
+ Configuration .Builder builder = new Configuration .Builder (context );
293
+ if (builder .applicationId == null ) {
294
+ throw new RuntimeException ("ApplicationId not defined. " +
295
+ "You must provide ApplicationId in AndroidManifest.xml.\n " +
296
+ "<meta-data\n " +
297
+ " android:name=\" com.parse.APPLICATION_ID\" \n " +
298
+ " android:value=\" <Your Application Id>\" />" );
299
+ } if (builder .clientKey == null ) {
300
+ throw new RuntimeException ("ClientKey not defined. " +
301
+ "You must provide ClientKey in AndroidManifest.xml.\n " +
302
+ "<meta-data\n " +
303
+ " android:name=\" com.parse.CLIENT_KEY\" \n " +
304
+ " android:value=\" <Your Client Key>\" />" );
305
+ }
306
+ initialize (builder .setNetworkInterceptors (interceptors )
307
+ .setLocalDatastoreEnabled (isLocalDatastoreEnabled )
308
+ .build ()
232
309
);
233
310
}
234
311
@@ -263,26 +340,29 @@ public static void initialize(Context context, String applicationId, String clie
263
340
.applicationId (applicationId )
264
341
.clientKey (clientKey )
265
342
.setNetworkInterceptors (interceptors )
343
+ .setLocalDatastoreEnabled (isLocalDatastoreEnabled )
266
344
.build ()
267
345
);
268
346
}
269
347
270
348
public static void initialize (Configuration configuration ) {
271
- Parse .configuration = configuration ;
349
+ // NOTE (richardross): We will need this here, as ParsePlugins uses the return value of
350
+ // isLocalDataStoreEnabled() to perform additional behavior.
351
+ isLocalDatastoreEnabled = configuration .localDataStoreEnabled ;
272
352
273
353
ParsePlugins .Android .initialize (configuration .context , configuration .applicationId , configuration .clientKey );
274
354
Context applicationContext = configuration .context .getApplicationContext ();
275
355
276
356
ParseHttpClient .setKeepAlive (true );
277
357
ParseHttpClient .setMaxConnections (20 );
278
358
// If we have interceptors in list, we have to initialize all http clients and add interceptors
279
- if (interceptors != null ) {
280
- initializeParseHttpClientsWithParseNetworkInterceptors ();
359
+ if (configuration . interceptors != null ) {
360
+ initializeParseHttpClientsWithParseNetworkInterceptors (configuration . interceptors );
281
361
}
282
362
283
363
ParseObject .registerParseSubclasses ();
284
364
285
- if (isLocalDatastoreEnabled () ) {
365
+ if (configuration . localDataStoreEnabled ) {
286
366
offlineStore = new OfflineStore (configuration .context );
287
367
} else {
288
368
ParseKeyValueCache .initialize (configuration .context );
@@ -665,9 +745,8 @@ private Parse() {
665
745
private static List <ParseNetworkInterceptor > interceptors ;
666
746
667
747
// Initialize all necessary http clients and add interceptors to these http clients
668
- private static void initializeParseHttpClientsWithParseNetworkInterceptors () {
748
+ private static void initializeParseHttpClientsWithParseNetworkInterceptors (List < ParseNetworkInterceptor > interceptors ) {
669
749
// This means developers have not called addInterceptor method so we should do nothing.
670
- List <ParseNetworkInterceptor > interceptors = configuration .interceptors ;
671
750
if (interceptors == null ) {
672
751
return ;
673
752
}
0 commit comments