@@ -132,73 +132,97 @@ private void notifyStartListener() {
132
132
* @return an {@link OptimizelyClient} instance
133
133
*/
134
134
public OptimizelyClient initialize (@ NonNull Context context , @ NonNull String datafile ) {
135
+ initialize (context , datafile ,true );
136
+ return optimizelyClient ;
137
+ }
138
+
139
+ /**
140
+ * Initialize Optimizely Synchronously using the datafile passed in while downloading the latest datafile in the background from the CDN to cache.
141
+ * It should be noted that even though it initiates a download of the datafile to cache, this method does not use that cached datafile.
142
+ * You can always test if a datafile exists in cache with {@link #isDatafileCached(Context)}.
143
+ * <p>
144
+ * Instantiates and returns an {@link OptimizelyClient} instance. It will also cache the instance
145
+ * for future lookups via getClient
146
+ *
147
+ * @param context any {@link Context} instance
148
+ * @param datafile the datafile used to initialize the OptimizelyClient.
149
+ * @param downloadToCache to check if datafile should get updated in cache after initialization.
150
+ * @return an {@link OptimizelyClient} instance
151
+ */
152
+ protected OptimizelyClient initialize (@ NonNull Context context ,@ Nullable String datafile ,boolean downloadToCache ) {
135
153
if (!isAndroidVersionSupported ()) {
136
154
return optimizelyClient ;
137
155
}
138
-
139
156
try {
140
- optimizelyClient = buildOptimizely (context , datafile );
157
+ if (datafile !=null )
158
+ optimizelyClient = buildOptimizely (context , datafile );
159
+ else
160
+ logger .error ("Invalid datafile" );
141
161
} catch (ConfigParseException e ) {
142
162
logger .error ("Unable to parse compiled data file" , e );
143
163
} catch (Exception e ) {
144
164
logger .error ("Unable to build OptimizelyClient instance" , e );
145
165
} catch (Error e ) {
146
166
logger .error ("Unable to build OptimizelyClient instance" , e );
147
167
}
148
-
149
- datafileHandler .downloadDatafile (context , projectId , null );
168
+ if (downloadToCache ){
169
+ datafileHandler .downloadDatafile (context , projectId , null );
170
+ }
150
171
151
172
return optimizelyClient ;
152
173
}
153
174
154
175
/**
155
176
* Initialize Optimizely Synchronously by loading the resource, use it to initialize Optimizely,
156
177
* and downloading the latest datafile from the CDN in the background to cache.
157
- * It should be noted that even though it initiates a download of the datafile to cache, this method does not use that cached datafile.
158
- * You can always test if a datafile exists in cache with {@link #isDatafileCached(Context)}.
159
178
* <p>
160
- * Instantiates and returns an {@link OptimizelyClient} instance. Will also cache the instance
179
+ * Instantiates and returns an {@link OptimizelyClient} instance using the datafile cached on disk
180
+ * if not available then it will expect that raw data file should exist on given id.
181
+ * and initialize using raw file. Will also cache the instance
161
182
* for future lookups via getClient. The datafile should be stored in res/raw.
162
183
*
163
184
* @param context any {@link Context} instance
164
185
* @param datafileRes the R id that the data file is located under.
165
186
* @return an {@link OptimizelyClient} instance
166
187
*/
167
188
@ NonNull
168
- public OptimizelyClient initialize (@ NonNull Context context , @ RawRes int datafileRes ) {
189
+ public OptimizelyClient initialize (@ NonNull Context context , @ RawRes Integer datafileRes ) {
169
190
try {
170
- String datafile = loadRawResource (context , datafileRes );
171
- return initialize (context , datafile );
172
- } catch (IOException e ) {
173
- logger .error ("Unable to load compiled data file" , e );
191
+
192
+ String datafile ;
193
+ datafile = getDatafile (context , datafileRes );
194
+ optimizelyClient = initialize (context , datafile , true );
195
+ }catch (NullPointerException e ){
196
+ logger .error ("Unable to find compiled data file in raw resource" ,e );
174
197
}
175
198
176
199
// return dummy client if not able to initialize a valid one
177
200
return optimizelyClient ;
178
201
}
179
202
180
- /**
181
- * Initialize Optimizely Synchronously. This one uses the cached datafile and expects it to exist.
182
- * You can use {@link #isDatafileCached(Context)} to see if the datafile exists in cache prior to this call.
183
- * <p>
184
- * Instantiates and returns an {@link OptimizelyClient} instance using the datafile cached on disk
185
- * if not available then it will return a dummy instance.
186
- *
187
- * @param context any {@link Context} instance
188
- * @return an {@link OptimizelyClient} instance
203
+ /** This function will first try to get datafile from Cache, if file is not cached yet
204
+ * than it will read from Raw file
205
+ * @param context
206
+ * @param datafileRes
207
+ * @return datafile
189
208
*/
190
- public OptimizelyClient initialize (@ NonNull Context context ) {
191
-
192
- String datafile = datafileHandler .loadSavedDatafile (context , projectId );
193
-
194
- if (datafile != null ) {
195
- return initialize (context , datafile );
196
- }
197
-
198
- // return dummy client if not able to initialize a valid one
199
- return optimizelyClient ;
209
+ public String getDatafile (Context context ,@ RawRes Integer datafileRes ){
210
+ try {
211
+ if (isDatafileCached (context )) {
212
+ return datafileHandler .loadSavedDatafile (context , projectId );
213
+ } else if (datafileRes !=null ) {
214
+ return loadRawResource (context , datafileRes );
215
+ }else {
216
+ logger .error ("Invalid datafile resource ID." );
217
+ return null ;
218
+ }
219
+ } catch (IOException e ) {
220
+ logger .error ("Unable to load compiled data file" , e );
221
+ }catch (NullPointerException e ){
222
+ logger .error ("Unable to find compiled data file in raw resource" ,e );
223
+ }
224
+ return null ;
200
225
}
201
-
202
226
/**
203
227
* Starts Optimizely asynchronously
204
228
* <p>
@@ -207,53 +231,39 @@ public OptimizelyClient initialize(@NonNull Context context) {
207
231
* once. If there is a cached datafile the returned instance will be built from it. The cached
208
232
* datafile will be updated from network if it is different from the cache. If there is no
209
233
* cached datafile the returned instance will always be built from the remote datafile.
210
- *
211
- * @param activity an Activity, used to automatically unbind {@link DatafileService}
212
- * @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
213
- */
214
- @ TargetApi (Build .VERSION_CODES .ICE_CREAM_SANDWICH )
215
- public void initialize (@ NonNull Activity activity , @ NonNull OptimizelyStartListener optimizelyStartListener ) {
216
- if (!isAndroidVersionSupported ()) {
217
- return ;
218
- }
219
- activity .getApplication ().registerActivityLifecycleCallbacks (new OptlyActivityLifecycleCallbacks (this ));
220
- initialize (activity .getApplicationContext (), optimizelyStartListener );
221
- }
222
-
223
- /**
224
234
* This method does the same thing except it can be used with a generic {@link Context}.
225
235
* @param context any type of context instance
236
+ * @param datafileRes Null is allowed here if user don't want to put datafile in res. Null handling is done in {@link #getDatafile(Context,Integer)}
226
237
* @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
227
- * @see #initialize(Activity , OptimizelyStartListener)
238
+ * @see #initialize(Context, Integer , OptimizelyStartListener)
228
239
*/
229
- public void initialize (@ NonNull Context context , @ NonNull OptimizelyStartListener optimizelyStartListener ) {
240
+ @ TargetApi (Build .VERSION_CODES .ICE_CREAM_SANDWICH )
241
+ public void initialize (@ NonNull final Context context , @ RawRes final Integer datafileRes , @ NonNull OptimizelyStartListener optimizelyStartListener ) {
230
242
if (!isAndroidVersionSupported ()) {
231
243
return ;
232
244
}
233
- this . optimizelyStartListener = optimizelyStartListener ;
234
- datafileHandler .downloadDatafile (context , projectId , getDatafileLoadedListener (context ));
245
+ setOptimizelyStartListener ( optimizelyStartListener ) ;
246
+ datafileHandler .downloadDatafile (context , projectId ,getDatafileLoadedListener (context , datafileRes ));
235
247
}
236
248
237
- DatafileLoadedListener getDatafileLoadedListener (final Context context ) {
249
+ DatafileLoadedListener getDatafileLoadedListener (final Context context , @ RawRes final Integer datafileRes ) {
238
250
return new DatafileLoadedListener () {
239
251
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
240
252
@ Override
241
253
public void onDatafileLoaded (@ Nullable String datafile ) {
242
254
// App is being used, i.e. in the foreground
243
- if (datafile != null ) {
255
+ if (datafile != null && ! datafile . isEmpty () ) {
244
256
injectOptimizely (context , userProfileService , datafile );
245
257
} else {
246
- // We should always call the callback even with the dummy
247
- // instances. Devs might gate the rest of their app
248
- // based on the loading of Optimizely
258
+ //if datafile is null than it should be able to take from cache and if not present
259
+ //in Cache than should be able to get from raw data file
260
+ optimizelyClient = initialize ( context , getDatafile ( context , datafileRes ), false );
249
261
notifyStartListener ();
250
262
}
251
263
}
252
264
253
265
@ Override
254
- public void onStop (Context context ) {
255
-
256
- }
266
+ public void onStop (Context context ) {}
257
267
};
258
268
}
259
269
@@ -281,11 +291,11 @@ public void stop(@NonNull Context context) {
281
291
/**
282
292
* Gets a cached Optimizely instance
283
293
* <p>
284
- * If {@link #initialize(Activity, OptimizelyStartListener)} or {@link #initialize(Context, OptimizelyStartListener )}
294
+ * If {@link #initialize(Context,Integer, OptimizelyStartListener)} or {@link #initialize(Context, Integer )}
285
295
* has not been called yet the returned {@link OptimizelyClient} instance will be a dummy instance
286
296
* that logs warnings in order to prevent {@link NullPointerException}.
287
297
* <p>
288
- * Using {@link #initialize(Activity, OptimizelyStartListener)} or {@link #initialize(Context, OptimizelyStartListener )}
298
+ * Using {@link #initialize(Context,Integer, OptimizelyStartListener)} or {@link #initialize(Context, Integer )}
289
299
* will update the cached instance with a new {@link OptimizelyClient} built from a cached local
290
300
* datafile on disk or a remote datafile on the CDN.
291
301
*
@@ -527,7 +537,6 @@ public static class Builder {
527
537
@ Nullable private EventHandler eventHandler = null ;
528
538
@ Nullable private ErrorHandler errorHandler = null ;
529
539
@ Nullable private UserProfileService userProfileService = null ;
530
-
531
540
Builder (@ NonNull String projectId ) {
532
541
this .projectId = projectId ;
533
542
}
0 commit comments