@Override public void onReceive(Context context, Intent intent) { /* * Create the job manager. We may need to reschedule jobs and some applications aren't initializing the * manager in Application.onCreate(). It may happen that some jobs can't be created if the JobCreator * wasn't registered, yet. Apps / Libraries need to figure out how to solve this themselves. */ try { JobManager.create(context); } catch (JobManagerCreateException ignored) { } } }
@Override public final void onReceive(Context context, Intent intent) { if (context == null || intent == null || !ACTION_ADD_JOB_CREATOR.equals(intent.getAction())) { return; } try { addJobCreator(context, JobManager.create(context)); } catch (JobManagerCreateException e) { } }
public Common(@NonNull Context context, JobCat cat, int jobId) { mContext = context; mJobId = jobId; mCat = cat; JobManager manager; try { manager = JobManager.create(context); } catch (JobManagerCreateException e) { mCat.e(e); manager = null; } mJobManager = manager; }
@Override public void onCreate() { super.onCreate(); JobManager .create(this) .addJobCreator(new DemoUnifiedJobCreator()); } }
@Override public void onInitializeTasks() { super.onInitializeTasks(); /* * When the app is being updated, then all jobs are cleared in the GcmNetworkManager. The manager * calls this method to reschedule. Let's initialize the JobManager here, which will reschedule * jobs manually. */ try { JobManager.create(getApplicationContext()); } catch (JobManagerCreateException ignored) { } } }
protected final JobManager createManager() { Context mockContext = mock(MockContext.class); when(mockContext.getApplicationContext()).thenReturn(mContext); return JobManager.create(mockContext); }
@Override protected void onHandleWork(@NonNull Intent intent) { /* * Delay this slightly. This avoids a race condition if the app was launched by the * AlarmManager. Then the alarm was already removed, but the JobRequest might still * be available in the storage. We still catch this case, because we never execute * a job with the same ID twice. Nonetheless, add the delay to save resources. */ try { CAT.d("Reschedule service started"); SystemClock.sleep(JobConfig.getJobReschedulePause()); JobManager manager; try { manager = JobManager.create(this); } catch (Exception e) { return; } Set<JobRequest> requests = manager.getAllJobRequests(null, true, true); int rescheduledCount = rescheduleJobs(manager, requests); CAT.d("Reschedule %d jobs of %d jobs", rescheduledCount, requests.size()); } finally { if (latch != null) { // latch can be null, if the service was restarted after a process death latch.countDown(); } } }
@Test public void verifyForceAllowApi14() { JobConfig.setForceAllowApi14(true); Context context = mockContext(); PackageManager packageManager = mock(PackageManager.class); when(context.getPackageManager()).thenReturn(packageManager); JobManager.create(context); }
@Test(expected = JobManagerCreateException.class) public void verifyJobManagerCrashesWithoutSupportedApi() { JobManager.create(mockContext()); }
@Test public void verifyCreateSuccessful() { PackageManager packageManager = mock(PackageManager.class); when(packageManager.queryIntentServices(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo())); when(packageManager.queryBroadcastReceivers(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo())); Context context = mockContext(); when(context.getPackageManager()).thenReturn(packageManager); JobManager.create(context); }
@Override public boolean onStopJob(JobParameters params) { Job job = JobManager.create(this).getJob(params.getJobId()); if (job != null) { job.cancel(); CAT.d("Called onStopJob for %s", job); } else { CAT.d("Called onStopJob, job %d not found", params.getJobId()); } // do not reschedule return false; }
@Override public void onStopped() { int jobId = getJobId(); Job job = JobManager.create(getApplicationContext()).getJob(jobId); if (job != null) { job.cancel(); CAT.d("Called onStopped for %s", job); } else { CAT.d("Called onStopped, job %d not found", jobId); } }
@Override protected void before() throws Throwable { JobConfig.setJobReschedulePause(0, TimeUnit.MILLISECONDS); JobConfig.setSkipJobReschedule(true); JobConfig.setApiEnabled(JobApi.WORK_MANAGER, false); mManager = JobManager.create(InstrumentationRegistry.getTargetContext()); mManager.cancelAll(); }
@Override protected void before() throws Throwable { JobIntentServiceReset.reset(); JobConfig.addLogger(TestLogger.INSTANCE); JobConfig.setSkipJobReschedule(true); mManager = JobManager.create(mContext); mManager.addJobCreator(mJobCreator); }
@Override public void onCreate() { super.onCreate(); Stetho.initializeWithDefaults(this); if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); } JobManager.create(this).addJobCreator(new DemoJobCreator()); } }
@Override protected void before() { Context context = InstrumentationRegistry.getTargetContext(); Executor executor = new Executor() { @Override public void execute(@NonNull Runnable command) { command.run(); } }; WorkManagerTestInitHelper.initializeTestWorkManager(context, new Configuration.Builder().setExecutor(executor).build()); JobConfig.setJobReschedulePause(0, TimeUnit.MILLISECONDS); JobConfig.setSkipJobReschedule(true); JobConfig.forceApi(JobApi.WORK_MANAGER); mManager = JobManager.create(context); mManager.cancelAll(); }
@Override public void onReceive(Context context, Intent intent) { /* * Create the job manager. We may need to reschedule jobs and some applications aren't initializing the * manager in Application.onCreate(). It may happen that some jobs can't be created if the JobCreator * wasn't registered, yet. Apps / Libraries need to figure out how to solve this themselves. */ try { JobManager.create(context); } catch (JobManagerCreateException ignored) { } } }
@Override public final void onReceive(Context context, Intent intent) { if (context == null || intent == null || !ACTION_ADD_JOB_CREATOR.equals(intent.getAction())) { return; } try { addJobCreator(context, JobManager.create(context)); } catch (JobManagerCreateException e) { } }
/** * {@inheritDoc} */ @Override public void init(Context context) { JobManager.create(context).addJobCreator(new JobCreator()); }
@Override public boolean onStopJob(JobParameters params) { Job job = JobManager.create(this).getJob(params.getJobId()); if (job != null) { job.cancel(); CAT.d("Called onStopJob for %s", job); } else { CAT.d("Called onStopJob, job %d not found", params.getJobId()); } // do not reschedule return false; }