Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Listener on task #321

Merged
merged 5 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,14 @@ public String getDatafile(Context context,@RawRes Integer datafileRes){
return datafile;
}
}

if (datafileRes != null) {
return loadRawResource(context, datafileRes);
}else{
logger.error("Invalid datafile resource ID.");
return null;
}
} catch (IOException e) {
logger.error("Unable to load compiled data file", e);
return safeLoadResource(context, datafileRes);
} catch (NullPointerException e){
logger.error("Unable to find compiled data file in raw resource",e);
}
return null;
}


/**
* Starts Optimizely asynchronously
* <p>
Expand Down Expand Up @@ -341,18 +334,32 @@ public void initialize(@NonNull final Context context, @RawRes final Integer dat
datafileHandler.downloadDatafile(context, datafileConfig, getDatafileLoadedListener(context,datafileRes));
}

private String safeLoadResource(Context context, @RawRes final Integer datafileRes) {
String resource = null;
try {
if (datafileRes != null) {
resource = loadRawResource(context, datafileRes);
}
else {
logger.error("Invalid datafile resource ID.");
}
}
catch (IOException exception) {
logger.error("Error parsing resource", exception);
}
return resource;
}

DatafileLoadedListener getDatafileLoadedListener(final Context context, @RawRes final Integer datafileRes) {
return new DatafileLoadedListener() {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void onDatafileLoaded(@Nullable String datafile) {
// App is being used, i.e. in the foreground
if (datafile != null && !datafile.isEmpty()) {
injectOptimizely(context, userProfileService, datafile);
} else {
//if datafile is null than it should be able to take from cache and if not present
//in Cache than should be able to get from raw data file
injectOptimizely(context, userProfileService, getDatafile(context,datafileRes));

injectOptimizely(context, userProfileService, safeLoadResource(context, datafileRes));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.optimizely.ab.android.shared.DatafileConfig;
import com.optimizely.ab.android.shared.OptlyStorage;

import org.json.JSONObject;
import org.slf4j.Logger;

import java.util.concurrent.Executor;
Expand Down Expand Up @@ -130,6 +131,12 @@ protected String doInBackground(Void... params) {
logger.warn("Unable to save new datafile");
}
}
else {
JSONObject jsonFile = datafileCache.load();
if (jsonFile != null) {
dataFile = jsonFile.toString();
}
}

return dataFile;
}
Expand Down