24
24
import android .content .Context ;
25
25
import android .content .Intent ;
26
26
import android .content .ServiceConnection ;
27
- import android .content .pm .PackageInfo ;
28
27
import android .content .res .Resources ;
29
28
import android .os .AsyncTask ;
30
29
import android .os .Build ;
42
41
import com .optimizely .ab .android .shared .Client ;
43
42
import com .optimizely .ab .android .shared .OptlyStorage ;
44
43
import com .optimizely .ab .android .shared .ServiceScheduler ;
45
- import com .optimizely .ab .android .user_profile .AndroidUserProfile ;
46
- import com .optimizely .ab .bucketing .UserProfile ;
44
+ import com .optimizely .ab .android .user_profile .AndroidUserProfileService ;
45
+ import com .optimizely .ab .bucketing .UserProfileService ;
47
46
import com .optimizely .ab .config .parser .ConfigParseException ;
48
47
import com .optimizely .ab .event .internal .payload .Event ;
49
48
61
60
* Handles loading the Optimizely data file
62
61
*/
63
62
public class OptimizelyManager {
63
+
64
64
@ NonNull private OptimizelyClient optimizelyClient = new OptimizelyClient (null ,
65
65
LoggerFactory .getLogger (OptimizelyClient .class ));
66
66
@ NonNull private final String projectId ;
@@ -72,7 +72,7 @@ public class OptimizelyManager {
72
72
@ NonNull private final Logger logger ;
73
73
@ Nullable private DataFileServiceConnection dataFileServiceConnection ;
74
74
@ Nullable private OptimizelyStartListener optimizelyStartListener ;
75
- @ Nullable private UserProfile userProfile ;
75
+ @ Nullable private UserProfileService userProfileService ;
76
76
77
77
OptimizelyManager (@ NonNull String projectId ,
78
78
@ NonNull Long eventHandlerDispatchInterval ,
@@ -88,7 +88,6 @@ public class OptimizelyManager {
88
88
this .dataFileDownloadIntervalTimeUnit = dataFileDownloadIntervalTimeUnit ;
89
89
this .executor = executor ;
90
90
this .logger = logger ;
91
-
92
91
}
93
92
94
93
@ NonNull
@@ -145,14 +144,14 @@ public OptimizelyClient initialize(@NonNull Context context, @NonNull String dat
145
144
return optimizelyClient ;
146
145
}
147
146
148
- AndroidUserProfile userProfile =
149
- (AndroidUserProfile ) AndroidUserProfile .newInstance (getProjectId (), context );
147
+ AndroidUserProfileService userProfileService =
148
+ (AndroidUserProfileService ) AndroidUserProfileService .newInstance (getProjectId (), context );
150
149
// The User Profile is started on the main thread on an asynchronous start.
151
150
// Starting simply creates the file if it doesn't exist so it's not
152
151
// terribly expensive. Blocking the UI thread prevents touch input...
153
- userProfile .start ();
152
+ userProfileService .start ();
154
153
try {
155
- optimizelyClient = buildOptimizely (context , datafile , userProfile );
154
+ optimizelyClient = buildOptimizely (context , datafile , userProfileService );
156
155
} catch (ConfigParseException e ) {
157
156
logger .error ("Unable to parse compiled data file" , e );
158
157
} catch (Exception e ) {
@@ -301,6 +300,7 @@ public void stop(@NonNull Context context) {
301
300
*/
302
301
@ NonNull
303
302
public OptimizelyClient getOptimizely () {
303
+ // Check version and log warning if version is less than what is required.
304
304
isAndroidVersionSupported ();
305
305
return optimizelyClient ;
306
306
}
@@ -348,24 +348,25 @@ String getProjectId() {
348
348
}
349
349
350
350
@ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
351
- void injectOptimizely (@ NonNull final Context context , final @ NonNull AndroidUserProfile userProfile , @ NonNull final ServiceScheduler serviceScheduler , @ NonNull final String dataFile ) {
352
- AsyncTask <Void , Void , UserProfile > initUserProfileTask = new AsyncTask <Void , Void , UserProfile >() {
351
+ void injectOptimizely (@ NonNull final Context context , final @ NonNull AndroidUserProfileService userProfileService ,
352
+ @ NonNull final ServiceScheduler serviceScheduler , @ NonNull final String dataFile ) {
353
+ AsyncTask <Void , Void , UserProfileService > initUserProfileTask = new AsyncTask <Void , Void , UserProfileService >() {
353
354
@ Override
354
- protected UserProfile doInBackground (Void [] params ) {
355
- userProfile .start ();
356
- return userProfile ;
355
+ protected UserProfileService doInBackground (Void [] params ) {
356
+ userProfileService .start ();
357
+ return userProfileService ;
357
358
}
358
359
359
360
@ Override
360
- protected void onPostExecute (UserProfile userProfile ) {
361
+ protected void onPostExecute (UserProfileService userProfileService ) {
361
362
Intent intent = new Intent (context , DataFileService .class );
362
363
intent .putExtra (DataFileService .EXTRA_PROJECT_ID , projectId );
363
364
serviceScheduler .schedule (intent , dataFileDownloadIntervalTimeUnit .toMillis (dataFileDownloadInterval ));
364
365
365
366
try {
366
- OptimizelyManager .this .optimizelyClient = buildOptimizely (context , dataFile , userProfile );
367
+ OptimizelyManager .this .optimizelyClient = buildOptimizely (context , dataFile , userProfileService );
368
+ OptimizelyManager .this .userProfileService = userProfileService ;
367
369
optimizelyClient .setDefaultAttributes (OptimizelyDefaultAttributes .buildDefaultAttributesMap (context , logger ));
368
- OptimizelyManager .this .userProfile = userProfile ;
369
370
logger .info ("Sending Optimizely instance to listener" );
370
371
371
372
if (optimizelyStartListener != null ) {
@@ -378,38 +379,40 @@ protected void onPostExecute(UserProfile userProfile) {
378
379
}
379
380
}
380
381
};
382
+
381
383
try {
382
384
initUserProfileTask .executeOnExecutor (executor );
383
385
} catch (Exception e ) {
384
386
logger .error ("Unable to initialize the user profile while injecting Optimizely" , e );
385
387
}
386
388
}
387
389
388
- private OptimizelyClient buildOptimizely (@ NonNull Context context , @ NonNull String dataFile , @ NonNull UserProfile userProfile ) throws ConfigParseException {
390
+ private OptimizelyClient buildOptimizely (@ NonNull Context context , @ NonNull String dataFile , @ NonNull
391
+ UserProfileService userProfileService ) throws ConfigParseException {
389
392
OptlyEventHandler eventHandler = OptlyEventHandler .getInstance (context );
390
393
eventHandler .setDispatchInterval (eventHandlerDispatchInterval , eventHandlerDispatchIntervalTimeUnit );
391
394
392
395
Event .ClientEngine clientEngine = OptimizelyClientEngine .getClientEngineFromContext (context );
393
396
394
397
Optimizely optimizely = Optimizely .builder (dataFile , eventHandler )
395
- .withUserProfile ( userProfile )
398
+ .withUserProfileService ( userProfileService )
396
399
.withClientEngine (clientEngine )
397
400
.withClientVersion (BuildConfig .CLIENT_VERSION )
398
401
.build ();
399
402
return new OptimizelyClient (optimizely , LoggerFactory .getLogger (OptimizelyClient .class ));
400
403
}
401
404
402
405
@ VisibleForTesting
403
- public UserProfile getUserProfile () {
404
- return userProfile ;
406
+ public UserProfileService getUserProfileService () {
407
+ return userProfileService ;
405
408
}
406
409
407
410
private boolean isAndroidVersionSupported () {
408
411
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
409
412
return true ;
410
413
} else {
411
- logger .warn ("Optimizely will not work on this phone. It's Android version {} is less the minimum supported " +
412
- "version {}" , Build .VERSION .SDK_INT , Build .VERSION_CODES .ICE_CREAM_SANDWICH );
414
+ logger .warn ("Optimizely will not work on this phone. It's Android version {} is less the minimum " +
415
+ "supported version {}" , Build .VERSION .SDK_INT , Build .VERSION_CODES .ICE_CREAM_SANDWICH );
413
416
return false ;
414
417
}
415
418
}
@@ -515,7 +518,8 @@ public void onServiceConnected(ComponentName className,
515
518
final DataFileService dataFileService = binder .getService ();
516
519
if (dataFileService != null ) {
517
520
DataFileClient dataFileClient = new DataFileClient (
518
- new Client (new OptlyStorage (dataFileService .getApplicationContext ()), LoggerFactory .getLogger (OptlyStorage .class )),
521
+ new Client (new OptlyStorage (dataFileService .getApplicationContext ()),
522
+ LoggerFactory .getLogger (OptlyStorage .class )),
519
523
LoggerFactory .getLogger (DataFileClient .class ));
520
524
521
525
DataFileCache dataFileCache = new DataFileCache (
@@ -529,22 +533,29 @@ public void onServiceConnected(ComponentName className,
529
533
Executors .newSingleThreadExecutor (),
530
534
LoggerFactory .getLogger (DataFileLoader .class ));
531
535
532
- dataFileService .getDataFile (optimizelyManager .getProjectId (), dataFileLoader , new DataFileLoadedListener () {
536
+ dataFileService .getDataFile (optimizelyManager .getProjectId (), dataFileLoader , new
537
+ DataFileLoadedListener () {
533
538
@ Override
534
539
public void onDataFileLoaded (@ Nullable String dataFile ) {
535
540
// App is being used, i.e. in the foreground
536
- AlarmManager alarmManager = (AlarmManager ) dataFileService .getApplicationContext ().getSystemService (Context .ALARM_SERVICE );
537
- ServiceScheduler .PendingIntentFactory pendingIntentFactory = new ServiceScheduler .PendingIntentFactory (dataFileService .getApplicationContext ());
538
- ServiceScheduler serviceScheduler = new ServiceScheduler (alarmManager , pendingIntentFactory , LoggerFactory .getLogger (ServiceScheduler .class ));
541
+ AlarmManager alarmManager = (AlarmManager ) dataFileService .getApplicationContext ()
542
+ .getSystemService (Context .ALARM_SERVICE );
543
+ ServiceScheduler .PendingIntentFactory pendingIntentFactory = new ServiceScheduler
544
+ .PendingIntentFactory (dataFileService .getApplicationContext ());
545
+ ServiceScheduler serviceScheduler = new ServiceScheduler (alarmManager , pendingIntentFactory ,
546
+ LoggerFactory .getLogger (ServiceScheduler .class ));
539
547
if (dataFile != null ) {
540
- AndroidUserProfile userProfile =
541
- (AndroidUserProfile ) AndroidUserProfile .newInstance (optimizelyManager .getProjectId (), dataFileService .getApplicationContext ());
542
- optimizelyManager .injectOptimizely (dataFileService .getApplicationContext (), userProfile , serviceScheduler , dataFile );
548
+ AndroidUserProfileService userProfileService = (AndroidUserProfileService )
549
+ AndroidUserProfileService .newInstance (optimizelyManager .getProjectId (),
550
+ dataFileService .getApplicationContext ());
551
+ optimizelyManager .injectOptimizely (dataFileService .getApplicationContext (),
552
+ userProfileService , serviceScheduler , dataFile );
543
553
} else {
544
554
// We should always call the callback even with the dummy
545
555
// instances. Devs might gate the rest of their app
546
556
// based on the loading of Optimizely
547
- OptimizelyStartListener optimizelyStartListener = optimizelyManager .getOptimizelyStartListener ();
557
+ OptimizelyStartListener optimizelyStartListener = optimizelyManager
558
+ .getOptimizelyStartListener ();
548
559
if (optimizelyStartListener != null ) {
549
560
optimizelyStartListener .onStart (optimizelyManager .getOptimizely ());
550
561
}
0 commit comments