Skip to content

Commit 08bf693

Browse files
authored
Bring back async init without the datafile. (#211)
1 parent 65fba52 commit 08bf693

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ public void onStart(OptimizelyClient optimizely) {
235235
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
236236

237237
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey) );
238-
239-
240238
}
241239

242240
@Test
@@ -257,8 +255,6 @@ public void onStart(OptimizelyClient optimizely) {
257255
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
258256

259257
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );
260-
261-
262258
}
263259

264260
@Test
@@ -300,6 +296,16 @@ public void initializeWithNullDatafile() {
300296
verify(logger).error(eq("Invalid datafile"));
301297
}
302298

299+
@Test
300+
public void initializeAsyncWithNullDatafile() {
301+
optimizelyManager.initialize(InstrumentationRegistry.getContext(), new OptimizelyStartListener() {
302+
@Override
303+
public void onStart(OptimizelyClient optimizely) {
304+
assertNotNull(optimizely);
305+
}
306+
});
307+
}
308+
303309
@Test
304310
public void load() {
305311
Context context = mock(Context.class);

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

+39-23
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public OptimizelyClient initialize(@NonNull Context context, @NonNull String dat
173173
* @param downloadToCache to check if datafile should get updated in cache after initialization.
174174
* @return an {@link OptimizelyClient} instance
175175
*/
176-
protected OptimizelyClient initialize(@NonNull Context context,@Nullable String datafile,boolean downloadToCache) {
176+
protected OptimizelyClient initialize(@NonNull Context context, @Nullable String datafile, boolean downloadToCache) {
177177
if (!isAndroidVersionSupported()) {
178178
return optimizelyClient;
179179
}
@@ -277,31 +277,44 @@ public void run() {
277277
* @return datafile
278278
*/
279279
public String getDatafile(Context context,@RawRes Integer datafileRes){
280-
try {
281-
if (isDatafileCached(context)) {
282-
return datafileHandler.loadSavedDatafile(context, datafileConfig);
283-
} else if (datafileRes!=null) {
284-
return loadRawResource(context, datafileRes);
285-
}else{
286-
logger.error("Invalid datafile resource ID.");
287-
return null;
288-
}
289-
} catch (IOException e) {
290-
logger.error("Unable to load compiled data file", e);
291-
}catch (NullPointerException e){
292-
logger.error("Unable to find compiled data file in raw resource",e);
293-
}
294-
return null;
280+
try {
281+
if (isDatafileCached(context)) {
282+
return datafileHandler.loadSavedDatafile(context, datafileConfig);
283+
} else if (datafileRes!=null) {
284+
return loadRawResource(context, datafileRes);
285+
}else{
286+
logger.error("Invalid datafile resource ID.");
287+
return null;
288+
}
289+
} catch (IOException e) {
290+
logger.error("Unable to load compiled data file", e);
291+
} catch (NullPointerException e){
292+
logger.error("Unable to find compiled data file in raw resource",e);
293+
}
294+
return null;
295295
}
296+
296297
/**
297298
* Starts Optimizely asynchronously
298299
* <p>
299-
* An {@link OptimizelyClient} instance will be delivered to
300-
* {@link OptimizelyStartListener#onStart(OptimizelyClient)}. The callback will only be hit
301-
* once. If there is a cached datafile the returned instance will be built from it. The cached
302-
* datafile will be updated from network if it is different from the cache. If there is no
303-
* cached datafile the returned instance will always be built from the remote datafile.
304-
* This method does the same thing except it can be used with a generic {@link Context}.
300+
* See {@link #initialize(Context, Integer, OptimizelyStartListener)}
301+
* @param context any type of context instance
302+
* @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
303+
* @deprecated Consider using {@link #initialize(Context, Integer, OptimizelyStartListener)}
304+
*/
305+
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
306+
public void initialize(@NonNull final Context context, @NonNull OptimizelyStartListener optimizelyStartListener) {
307+
initialize(context, null, optimizelyStartListener);
308+
}
309+
310+
/**
311+
* Starts Optimizely asynchronously
312+
* <p>
313+
* * Attempts to fetch the most recent remote datafile and construct an {@link OptimizelyClient}.
314+
* If the datafile has not changed since the SDK last fetched it or if there is an error
315+
* fetching, the SDK will attempt to construct an {@link OptimizelyClient} using a cached datafile.
316+
* If there is no cached datafile, then the SDK will return a dummy, uninitialized {@link OptimizelyClient}.
317+
* Passing in a datafileRes will guarantee the SDK returns an initialized {@link OptimizelyClient}.
305318
* @param context any type of context instance
306319
* @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)}
307320
* @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
@@ -313,7 +326,7 @@ public void initialize(@NonNull final Context context, @RawRes final Integer dat
313326
return;
314327
}
315328
setOptimizelyStartListener(optimizelyStartListener);
316-
datafileHandler.downloadDatafile(context, datafileConfig,getDatafileLoadedListener(context,datafileRes));
329+
datafileHandler.downloadDatafile(context, datafileConfig, getDatafileLoadedListener(context,datafileRes));
317330
}
318331

319332
DatafileLoadedListener getDatafileLoadedListener(final Context context, @RawRes final Integer datafileRes) {
@@ -614,6 +627,9 @@ public static class Builder {
614627
@Nullable private DatafileConfig datafileConfig = null;
615628

616629
@Deprecated
630+
/**
631+
* @deprecated use {@link #Builder()} instead and pass in an SDK Key with {@link #withSDKKey(String)}
632+
*/
617633
Builder(@Nullable String projectId) {
618634
this.projectId = projectId;
619635
}

0 commit comments

Comments
 (0)