Search in sources :

Example 1 with DefaultUserProfileService

use of com.optimizely.ab.android.user_profile.DefaultUserProfileService in project android-sdk by optimizely.

the class OptimizelyManager method initialize.

/**
 * Initialize Optimizely Synchronously using the datafile passed in.
 * It should be noted that even though it initiates a download of the datafile to cache, this method does not use that cached datafile.
 * You can always test if a datafile exists in cache with {@link #isDatafileCached(Context)}.
 * <p>
 * Instantiates and returns an {@link OptimizelyClient} instance. It will also cache the instance
 * for future lookups via getClient
 *
 * @param context  any {@link Context} instance
 * @param datafile the datafile used to initialize the OptimizelyClient.
 * @param downloadToCache to check if datafile should get updated in cache after initialization.
 * @param updateConfigOnNewDatafile When a new datafile is fetched from the server in the background thread, the SDK will be updated with the new datafile immediately if this value is set to true. When it's set to false (default), the new datafile is cached and will be used when the SDK is started again.
 * @return an {@link OptimizelyClient} instance
 */
public OptimizelyClient initialize(@NonNull Context context, @Nullable String datafile, boolean downloadToCache, boolean updateConfigOnNewDatafile) {
    if (!isAndroidVersionSupported()) {
        return optimizelyClient;
    }
    try {
        if (datafile != null) {
            if (getUserProfileService() instanceof DefaultUserProfileService) {
                DefaultUserProfileService defaultUserProfileService = (DefaultUserProfileService) getUserProfileService();
                defaultUserProfileService.start();
            }
            optimizelyClient = buildOptimizely(context, datafile);
            startDatafileHandler(context);
        } else {
            logger.error("Invalid datafile");
        }
    } catch (ConfigParseException e) {
        logger.error("Unable to parse compiled data file", e);
    } catch (Exception e) {
        logger.error("Unable to build OptimizelyClient instance", e);
    } catch (Error e) {
        logger.error("Unable to build OptimizelyClient instance", e);
    }
    if (downloadToCache) {
        datafileHandler.downloadDatafileToCache(context, datafileConfig, updateConfigOnNewDatafile);
    }
    return optimizelyClient;
}
Also used : ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException) IOException(java.io.IOException)

Example 2 with DefaultUserProfileService

use of com.optimizely.ab.android.user_profile.DefaultUserProfileService in project android-sdk by optimizely.

the class OptimizelyManager method cleanupUserProfileCache.

private void cleanupUserProfileCache(UserProfileService userProfileService) {
    final DefaultUserProfileService defaultUserProfileService;
    if (userProfileService instanceof DefaultUserProfileService) {
        defaultUserProfileService = (DefaultUserProfileService) userProfileService;
    } else {
        return;
    }
    final ProjectConfig config = optimizelyClient.getProjectConfig();
    if (config == null) {
        return;
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Set<String> experimentIds = config.getExperimentIdMapping().keySet();
                defaultUserProfileService.removeInvalidExperiments(experimentIds);
            } catch (Exception e) {
                logger.error("Error removing invalid experiments from default user profile service.", e);
            }
        }
    }).start();
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException) IOException(java.io.IOException)

Example 3 with DefaultUserProfileService

use of com.optimizely.ab.android.user_profile.DefaultUserProfileService in project android-sdk by optimizely.

the class OptimizelyManagerBuilderTest method testBuildWithUserProfileService.

@Test
public void testBuildWithUserProfileService() {
    Context appContext = mock(Context.class);
    when(appContext.getApplicationContext()).thenReturn(appContext);
    DefaultUserProfileService ups = mock(DefaultUserProfileService.class);
    OptimizelyManager manager = OptimizelyManager.builder().withSDKKey(testSdkKey).withDatafileDownloadInterval(61L, TimeUnit.SECONDS).withUserProfileService(ups).build(appContext);
    manager.initialize(appContext, minDatafile);
    assertEquals(manager.getUserProfileService(), ups);
}
Also used : Context(android.content.Context) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) Test(org.junit.Test)

Example 4 with DefaultUserProfileService

use of com.optimizely.ab.android.user_profile.DefaultUserProfileService in project android-sdk by optimizely.

the class OptimizelyManagerTest method injectOptimizelyHandlesInvalidDatafile.

@Test
public void injectOptimizelyHandlesInvalidDatafile() {
    Context context = mock(Context.class);
    PackageManager packageManager = mock(PackageManager.class);
    when(context.getPackageName()).thenReturn("com.optly");
    when(context.getApplicationContext()).thenReturn(context);
    when(context.getApplicationContext().getPackageManager()).thenReturn(packageManager);
    try {
        when(packageManager.getPackageInfo("com.optly", 0)).thenReturn(mock(PackageInfo.class));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    DefaultUserProfileService userProfileService = mock(DefaultUserProfileService.class);
    ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
    ArgumentCaptor<DefaultUserProfileService.StartCallback> callbackArgumentCaptor = ArgumentCaptor.forClass(DefaultUserProfileService.StartCallback.class);
    when(defaultDatafileHandler.getConfig()).thenReturn(null);
    optimizelyManager.setOptimizelyStartListener(null);
    optimizelyManager.injectOptimizely(context, userProfileService, "{}");
    try {
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("Timed out");
    }
    assertFalse(optimizelyManager.getOptimizely().isValid());
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) Intent(android.content.Intent) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) Test(org.junit.Test)

Aggregations

DefaultUserProfileService (com.optimizely.ab.android.user_profile.DefaultUserProfileService)4 Context (android.content.Context)2 ConfigParseException (com.optimizely.ab.config.parser.ConfigParseException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 Intent (android.content.Intent)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 ProjectConfig (com.optimizely.ab.config.ProjectConfig)1