Search in sources :

Example 1 with ServiceScheduler

use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.

the class EventRescheduler method onReceive.

/**
 * Called when intent filter has kicked in.
 * @param context current context
 * @param intent broadcast intent received.  Try and reschedule.
 * @see BroadcastReceiver#onReceive(Context, Intent)
 */
@Override
public void onReceive(Context context, Intent intent) {
    if (context != null && intent != null) {
        ServiceScheduler serviceScheduler = new ServiceScheduler(context, new ServiceScheduler.PendingIntentFactory(context), LoggerFactory.getLogger(ServiceScheduler.class));
        Intent eventServiceIntent = new Intent(context, EventIntentService.class);
        reschedule(context, intent, eventServiceIntent, serviceScheduler);
    } else {
        logger.warn("Received invalid broadcast to event rescheduler");
    }
}
Also used : Intent(android.content.Intent) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler)

Example 2 with ServiceScheduler

use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.

the class EventReschedulerTest method flushOnWifiConnectionIfScheduled.

@Test
public void flushOnWifiConnectionIfScheduled() {
    final Intent eventServiceIntent = mock(Intent.class);
    ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
    when(intent.getAction()).thenReturn(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    when(intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)).thenReturn(true);
    when(serviceScheduler.isScheduled(eventServiceIntent)).thenReturn(true);
    rescheduler.reschedule(context, intent, eventServiceIntent, serviceScheduler);
    verify(context).startService(eventServiceIntent);
    verify(logger).info("Preemptively flushing events since wifi became available");
}
Also used : Intent(android.content.Intent) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler) Test(org.junit.Test)

Example 3 with ServiceScheduler

use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.

the class OptimizelyManagerTest method injectOptimizelyNullListener.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void injectOptimizelyNullListener() {
    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();
    }
    UserProfileService userProfileService = mock(UserProfileService.class);
    ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
    ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
    ArgumentCaptor<DefaultUserProfileService.StartCallback> callbackArgumentCaptor = ArgumentCaptor.forClass(DefaultUserProfileService.StartCallback.class);
    optimizelyManager.setOptimizelyStartListener(null);
    optimizelyManager.injectOptimizely(context, userProfileService, minDatafile);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler.PendingIntentFactory(context);
    Intent intent = new Intent(context, DatafileService.class);
    intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, optimizelyManager.getDatafileConfig().toJSONString());
    serviceScheduler.schedule(intent, optimizelyManager.getDatafileDownloadInterval() * 1000);
    try {
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("Timed out");
    }
    verify(logger).info("No listener to send Optimizely to");
    verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));
    Intent intent2 = captor.getValue();
    assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
    assertEquals(optimizelyManager.getDatafileConfig().toJSONString(), intent2.getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG));
}
Also used : Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) Intent(android.content.Intent) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler) PackageManager(android.content.pm.PackageManager) AlarmManager(android.app.AlarmManager) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) Test(org.junit.Test) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 4 with ServiceScheduler

use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.

the class ServiceSchedulerTest method setup.

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Before
public void setup() {
    context = mock(Context.class);
    optlyStorage = mock(OptlyStorage.class);
    alarmManager = mock(AlarmManager.class);
    jobScheduler = mock(JobScheduler.class);
    when(context.getApplicationContext()).thenReturn(context);
    when(context.getSystemService(Context.ALARM_SERVICE)).thenReturn(alarmManager);
    when(context.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(jobScheduler);
    pendingIntentFactory = mock(ServiceScheduler.PendingIntentFactory.class);
    logger = mock(Logger.class);
    serviceScheduler = new ServiceScheduler(context, pendingIntentFactory, logger);
}
Also used : Context(android.content.Context) JobScheduler(android.app.job.JobScheduler) OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) AlarmManager(android.app.AlarmManager) Logger(org.slf4j.Logger) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler) Before(org.junit.Before) RequiresApi(androidx.annotation.RequiresApi)

Example 5 with ServiceScheduler

use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.

the class DatafileServiceTest method testIntentExtraData.

@Test
@Ignore
public void testIntentExtraData() {
    Context context = mock(Context.class);
    when(context.getPackageName()).thenReturn("com.optly");
    ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
    ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler.PendingIntentFactory(context);
    Intent intent = new Intent(context, DatafileService.class);
    intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, new DatafileConfig("1", null).toJSONString());
    serviceScheduler.schedule(intent, TimeUnit.HOURS.toMillis(1L));
    try {
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("Timed out");
    }
    verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));
    Intent intent2 = captor.getValue();
    assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
}
Also used : Context(android.content.Context) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ServiceScheduler (com.optimizely.ab.android.shared.ServiceScheduler)6 Intent (android.content.Intent)4 AlarmManager (android.app.AlarmManager)3 Context (android.content.Context)3 Test (org.junit.Test)3 OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)2 JobScheduler (android.app.job.JobScheduler)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 RequiresApi (androidx.annotation.RequiresApi)1 SdkSuppress (androidx.test.filters.SdkSuppress)1 Client (com.optimizely.ab.android.shared.Client)1 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)1 DefaultUserProfileService (com.optimizely.ab.android.user_profile.DefaultUserProfileService)1 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 Logger (org.slf4j.Logger)1