Skip to content

Commit c67fa20

Browse files
(fix): Listener on task (#321)
* add a async task around download * load the cached file in the datafile loader to avoid doing it on the postExecute since file io can take some time. * remove unused import * refactor to load from cache if download fails on thread. but, load resource on main thread if all else fails.
1 parent 49cd2c9 commit c67fa20

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,14 @@ public String getDatafile(Context context,@RawRes Integer datafileRes){
292292
return datafile;
293293
}
294294
}
295-
296-
if (datafileRes != null) {
297-
return loadRawResource(context, datafileRes);
298-
}else{
299-
logger.error("Invalid datafile resource ID.");
300-
return null;
301-
}
302-
} catch (IOException e) {
303-
logger.error("Unable to load compiled data file", e);
295+
return safeLoadResource(context, datafileRes);
304296
} catch (NullPointerException e){
305297
logger.error("Unable to find compiled data file in raw resource",e);
306298
}
307299
return null;
308300
}
309301

302+
310303
/**
311304
* Starts Optimizely asynchronously
312305
* <p>
@@ -342,18 +335,32 @@ public void initialize(@NonNull final Context context, @RawRes final Integer dat
342335
datafileHandler.downloadDatafile(context, datafileConfig, getDatafileLoadedListener(context,datafileRes));
343336
}
344337

338+
private String safeLoadResource(Context context, @RawRes final Integer datafileRes) {
339+
String resource = null;
340+
try {
341+
if (datafileRes != null) {
342+
resource = loadRawResource(context, datafileRes);
343+
}
344+
else {
345+
logger.error("Invalid datafile resource ID.");
346+
}
347+
}
348+
catch (IOException exception) {
349+
logger.error("Error parsing resource", exception);
350+
}
351+
return resource;
352+
}
353+
345354
DatafileLoadedListener getDatafileLoadedListener(final Context context, @RawRes final Integer datafileRes) {
346355
return new DatafileLoadedListener() {
347356
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
348357
@Override
349358
public void onDatafileLoaded(@Nullable String datafile) {
350-
// App is being used, i.e. in the foreground
351359
if (datafile != null && !datafile.isEmpty()) {
352360
injectOptimizely(context, userProfileService, datafile);
353361
} else {
354-
//if datafile is null than it should be able to take from cache and if not present
355-
//in Cache than should be able to get from raw data file
356-
injectOptimizely(context, userProfileService, getDatafile(context,datafileRes));
362+
363+
injectOptimizely(context, userProfileService, safeLoadResource(context, datafileRes));
357364
}
358365
}
359366
};

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileLoader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.optimizely.ab.android.shared.DatafileConfig;
2727
import com.optimizely.ab.android.shared.OptlyStorage;
2828

29+
import org.json.JSONObject;
2930
import org.slf4j.Logger;
3031

3132
import java.util.concurrent.Executor;
@@ -130,6 +131,12 @@ protected String doInBackground(Void... params) {
130131
logger.warn("Unable to save new datafile");
131132
}
132133
}
134+
else {
135+
JSONObject jsonFile = datafileCache.load();
136+
if (jsonFile != null) {
137+
dataFile = jsonFile.toString();
138+
}
139+
}
133140

134141
return dataFile;
135142
}

0 commit comments

Comments
 (0)