@@ -292,21 +292,14 @@ public String getDatafile(Context context,@RawRes Integer datafileRes){
292
292
return datafile ;
293
293
}
294
294
}
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 );
304
296
} catch (NullPointerException e ){
305
297
logger .error ("Unable to find compiled data file in raw resource" ,e );
306
298
}
307
299
return null ;
308
300
}
309
301
302
+
310
303
/**
311
304
* Starts Optimizely asynchronously
312
305
* <p>
@@ -342,18 +335,32 @@ public void initialize(@NonNull final Context context, @RawRes final Integer dat
342
335
datafileHandler .downloadDatafile (context , datafileConfig , getDatafileLoadedListener (context ,datafileRes ));
343
336
}
344
337
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
+
345
354
DatafileLoadedListener getDatafileLoadedListener (final Context context , @ RawRes final Integer datafileRes ) {
346
355
return new DatafileLoadedListener () {
347
356
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
348
357
@ Override
349
358
public void onDatafileLoaded (@ Nullable String datafile ) {
350
- // App is being used, i.e. in the foreground
351
359
if (datafile != null && !datafile .isEmpty ()) {
352
360
injectOptimizely (context , userProfileService , datafile );
353
361
} 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 ));
357
364
}
358
365
}
359
366
};
0 commit comments