2424import android .content .Context ;
2525import android .content .Intent ;
2626import android .content .ServiceConnection ;
27- import android .content .pm .PackageInfo ;
2827import android .content .res .Resources ;
2928import android .os .AsyncTask ;
3029import android .os .Build ;
4241import com .optimizely .ab .android .shared .Client ;
4342import com .optimizely .ab .android .shared .OptlyStorage ;
4443import 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 ;
4746import com .optimizely .ab .config .parser .ConfigParseException ;
4847import com .optimizely .ab .event .internal .payload .Event ;
4948
6160 * Handles loading the Optimizely data file
6261 */
6362public class OptimizelyManager {
63+
6464 @ NonNull private OptimizelyClient optimizelyClient = new OptimizelyClient (null ,
6565 LoggerFactory .getLogger (OptimizelyClient .class ));
6666 @ NonNull private final String projectId ;
@@ -72,7 +72,7 @@ public class OptimizelyManager {
7272 @ NonNull private final Logger logger ;
7373 @ Nullable private DataFileServiceConnection dataFileServiceConnection ;
7474 @ Nullable private OptimizelyStartListener optimizelyStartListener ;
75- @ Nullable private UserProfile userProfile ;
75+ @ Nullable private UserProfileService userProfileService ;
7676
7777 OptimizelyManager (@ NonNull String projectId ,
7878 @ NonNull Long eventHandlerDispatchInterval ,
@@ -88,7 +88,6 @@ public class OptimizelyManager {
8888 this .dataFileDownloadIntervalTimeUnit = dataFileDownloadIntervalTimeUnit ;
8989 this .executor = executor ;
9090 this .logger = logger ;
91-
9291 }
9392
9493 @ NonNull
@@ -145,14 +144,14 @@ public OptimizelyClient initialize(@NonNull Context context, @NonNull String dat
145144 return optimizelyClient ;
146145 }
147146
148- AndroidUserProfile userProfile =
149- (AndroidUserProfile ) AndroidUserProfile .newInstance (getProjectId (), context );
147+ AndroidUserProfileService userProfileService =
148+ (AndroidUserProfileService ) AndroidUserProfileService .newInstance (getProjectId (), context );
150149 // The User Profile is started on the main thread on an asynchronous start.
151150 // Starting simply creates the file if it doesn't exist so it's not
152151 // terribly expensive. Blocking the UI thread prevents touch input...
153- userProfile .start ();
152+ userProfileService .start ();
154153 try {
155- optimizelyClient = buildOptimizely (context , datafile , userProfile );
154+ optimizelyClient = buildOptimizely (context , datafile , userProfileService );
156155 } catch (ConfigParseException e ) {
157156 logger .error ("Unable to parse compiled data file" , e );
158157 } catch (Exception e ) {
@@ -301,6 +300,7 @@ public void stop(@NonNull Context context) {
301300 */
302301 @ NonNull
303302 public OptimizelyClient getOptimizely () {
303+ // Check version and log warning if version is less than what is required.
304304 isAndroidVersionSupported ();
305305 return optimizelyClient ;
306306 }
@@ -348,24 +348,25 @@ String getProjectId() {
348348 }
349349
350350 @ 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 >() {
353354 @ 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 ;
357358 }
358359
359360 @ Override
360- protected void onPostExecute (UserProfile userProfile ) {
361+ protected void onPostExecute (UserProfileService userProfileService ) {
361362 Intent intent = new Intent (context , DataFileService .class );
362363 intent .putExtra (DataFileService .EXTRA_PROJECT_ID , projectId );
363364 serviceScheduler .schedule (intent , dataFileDownloadIntervalTimeUnit .toMillis (dataFileDownloadInterval ));
364365
365366 try {
366- OptimizelyManager .this .optimizelyClient = buildOptimizely (context , dataFile , userProfile );
367+ OptimizelyManager .this .optimizelyClient = buildOptimizely (context , dataFile , userProfileService );
368+ OptimizelyManager .this .userProfileService = userProfileService ;
367369 optimizelyClient .setDefaultAttributes (OptimizelyDefaultAttributes .buildDefaultAttributesMap (context , logger ));
368- OptimizelyManager .this .userProfile = userProfile ;
369370 logger .info ("Sending Optimizely instance to listener" );
370371
371372 if (optimizelyStartListener != null ) {
@@ -378,38 +379,40 @@ protected void onPostExecute(UserProfile userProfile) {
378379 }
379380 }
380381 };
382+
381383 try {
382384 initUserProfileTask .executeOnExecutor (executor );
383385 } catch (Exception e ) {
384386 logger .error ("Unable to initialize the user profile while injecting Optimizely" , e );
385387 }
386388 }
387389
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 {
389392 OptlyEventHandler eventHandler = OptlyEventHandler .getInstance (context );
390393 eventHandler .setDispatchInterval (eventHandlerDispatchInterval , eventHandlerDispatchIntervalTimeUnit );
391394
392395 Event .ClientEngine clientEngine = OptimizelyClientEngine .getClientEngineFromContext (context );
393396
394397 Optimizely optimizely = Optimizely .builder (dataFile , eventHandler )
395- .withUserProfile ( userProfile )
398+ .withUserProfileService ( userProfileService )
396399 .withClientEngine (clientEngine )
397400 .withClientVersion (BuildConfig .CLIENT_VERSION )
398401 .build ();
399402 return new OptimizelyClient (optimizely , LoggerFactory .getLogger (OptimizelyClient .class ));
400403 }
401404
402405 @ VisibleForTesting
403- public UserProfile getUserProfile () {
404- return userProfile ;
406+ public UserProfileService getUserProfileService () {
407+ return userProfileService ;
405408 }
406409
407410 private boolean isAndroidVersionSupported () {
408411 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
409412 return true ;
410413 } 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 );
413416 return false ;
414417 }
415418 }
@@ -515,7 +518,8 @@ public void onServiceConnected(ComponentName className,
515518 final DataFileService dataFileService = binder .getService ();
516519 if (dataFileService != null ) {
517520 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 )),
519523 LoggerFactory .getLogger (DataFileClient .class ));
520524
521525 DataFileCache dataFileCache = new DataFileCache (
@@ -529,22 +533,29 @@ public void onServiceConnected(ComponentName className,
529533 Executors .newSingleThreadExecutor (),
530534 LoggerFactory .getLogger (DataFileLoader .class ));
531535
532- dataFileService .getDataFile (optimizelyManager .getProjectId (), dataFileLoader , new DataFileLoadedListener () {
536+ dataFileService .getDataFile (optimizelyManager .getProjectId (), dataFileLoader , new
537+ DataFileLoadedListener () {
533538 @ Override
534539 public void onDataFileLoaded (@ Nullable String dataFile ) {
535540 // 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 ));
539547 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 );
543553 } else {
544554 // We should always call the callback even with the dummy
545555 // instances. Devs might gate the rest of their app
546556 // based on the loading of Optimizely
547- OptimizelyStartListener optimizelyStartListener = optimizelyManager .getOptimizelyStartListener ();
557+ OptimizelyStartListener optimizelyStartListener = optimizelyManager
558+ .getOptimizelyStartListener ();
548559 if (optimizelyStartListener != null ) {
549560 optimizelyStartListener .onStart (optimizelyManager .getOptimizely ());
550561 }
0 commit comments