android.app.job.JobInfo Java Examples

The following examples show how to use android.app.job.JobInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EpgSyncJobService.java    From androidtv-sample-inputs with Apache License 2.0 6 votes vote down vote up
/**
 * Manually requests a job to run now.
 *
 * <p>To check the current status of the sync, register a {@link
 * android.content.BroadcastReceiver} with an {@link android.content.IntentFilter} which checks
 * for the action {@link #ACTION_SYNC_STATUS_CHANGED}.
 *
 * <p>The sync status is an extra parameter in the {@link Intent} with key {@link #SYNC_STATUS}.
 * The sync status is either {@link #SYNC_STARTED} or {@link #SYNC_FINISHED}.
 *
 * <p>Check that the value of {@link #BUNDLE_KEY_INPUT_ID} matches your {@link
 * android.media.tv.TvInputService}. If you're calling this from your setup activity, you can
 * get the extra parameter {@link TvInputInfo#EXTRA_INPUT_ID}.
 *
 * <p>
 *
 * @param context Application's context.
 * @param inputId Component name for the app's TvInputService. This can be received through an
 *     Intent extra parameter {@link TvInputInfo#EXTRA_INPUT_ID}.
 * @param syncDuration The duration of EPG content to fetch in milliseconds. For a manual sync,
 *     this should be relatively short. For a background sync this should be long.
 * @param jobServiceComponent The {@link EpgSyncJobService} class that will run.
 */
public static void requestImmediateSync(
        Context context, String inputId, long syncDuration, ComponentName jobServiceComponent) {
    if (jobServiceComponent.getClass().isAssignableFrom(EpgSyncJobService.class)) {
        throw new IllegalArgumentException("This class does not extend EpgSyncJobService");
    }
    PersistableBundle persistableBundle = new PersistableBundle();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        persistableBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        persistableBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    }
    persistableBundle.putString(EpgSyncJobService.BUNDLE_KEY_INPUT_ID, inputId);
    persistableBundle.putLong(EpgSyncJobService.BUNDLE_KEY_SYNC_PERIOD, syncDuration);
    JobInfo.Builder builder = new JobInfo.Builder(REQUEST_SYNC_JOB_ID, jobServiceComponent);
    JobInfo jobInfo =
            builder.setExtras(persistableBundle)
                    .setOverrideDeadline(EpgSyncJobService.OVERRIDE_DEADLINE_MILLIS)
                    .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                    .build();
    scheduleJob(context, jobInfo);
    if (DEBUG) {
        Log.d(TAG, "Single job scheduled");
    }
}
 
Example #2
Source File: BrightnessIdleJob.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static void scheduleJob(Context context) {
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);

    JobInfo pending = jobScheduler.getPendingJob(JOB_ID);
    JobInfo jobInfo =
            new JobInfo.Builder(JOB_ID, new ComponentName(context, BrightnessIdleJob.class))
                    .setRequiresDeviceIdle(true)
                    .setRequiresCharging(true)
                    .setPeriodic(TimeUnit.HOURS.toMillis(24)).build();

    if (pending != null && !pending.equals(jobInfo)) {
        jobScheduler.cancel(JOB_ID);
        pending = null;
    }

    if (pending == null) {
        jobScheduler.schedule(jobInfo);
    }
}
 
Example #3
Source File: SplashScreen.java    From leafpicrevived with GNU General Public License v3.0 6 votes vote down vote up
private void startLookingForMedia() {

        new Thread(() -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /* TODO  && (has included folders) */) {

                JobInfo job = new JobInfo.Builder(0, new ComponentName(getApplicationContext(), LookForMediaJob.class))
                        .setPeriodic(1000)
                        .setRequiresDeviceIdle(true)
                        .build();

                JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
                if (scheduler.getAllPendingJobs().size() == 0)
                    Log.wtf(TAG, scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS
                            ? "LookForMediaJob scheduled successfully!" : "LookForMediaJob scheduled failed!");

            }
        }).start();
    }
 
Example #4
Source File: JobInfoSchedulerTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void schedule_whenExtrasEvailable_transmitsExtras() {
  String extras = "e1";
  TransportContext transportContext =
      TransportContext.builder()
          .setBackendName("backend1")
          .setExtras(extras.getBytes(Charset.defaultCharset()))
          .build();
  store.recordNextCallTime(transportContext, 1000000);
  scheduler.schedule(transportContext, 1);
  JobInfo jobInfo = jobScheduler.getAllPendingJobs().get(0);
  PersistableBundle bundle = jobInfo.getExtras();
  assertThat(bundle.get(JobInfoScheduler.EXTRAS))
      .isEqualTo(
          Base64.encodeToString(extras.getBytes(Charset.defaultCharset()), Base64.DEFAULT));
}
 
Example #5
Source File: BackgroundDownloadService.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
private static JobInfo getJobInfo(boolean requireUnmetered, boolean allowRoaming,
                                  boolean requireCharging) {
    JobInfo.Builder builder = new JobInfo.Builder(
            JobSchedulerId.BACKGROUND_DOWNLOAD.id(),
            new ComponentName("com.totsp.crossword.shortyz",
                    BackgroundDownloadService.class.getName()));

    builder.setPeriodic(TimeUnit.HOURS.toMillis(1))
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setRequiresCharging(requireCharging)
            .setPersisted(true);

    if (!requireUnmetered) {
        if (allowRoaming) {
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        } else {
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING);
        }
    }

    return builder.build();
}
 
Example #6
Source File: BackgroundDownloadService.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
private static void scheduleJob(Context context) {
    JobScheduler scheduler =
            (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    JobInfo info = getJobInfo(
            preferences.getBoolean("backgroundDownloadRequireUnmetered", true),
            preferences.getBoolean("backgroundDownloadAllowRoaming", false),
            preferences.getBoolean("backgroundDownloadRequireCharging", false));


    LOGGER.info("Scheduling background download job: " + info);

    int result = scheduler.schedule(info);

    if (result == JobScheduler.RESULT_SUCCESS) {
        LOGGER.info("Successfully scheduled background downloads");
    } else {
        LOGGER.log(Level.WARNING, "Unable to schedule background downloads");
    }
}
 
Example #7
Source File: WidgetHelper.java    From materialistic with Apache License 2.0 6 votes vote down vote up
private void scheduleUpdate(int appWidgetId) {
    String frequency = getConfig(appWidgetId, R.string.pref_widget_frequency);
    long frequencyHourMillis = DateUtils.HOUR_IN_MILLIS * (TextUtils.isEmpty(frequency) ?
            DEFAULT_FREQUENCY_HOUR : Integer.valueOf(frequency));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getJobScheduler().schedule(new JobInfo.Builder(appWidgetId,
                new ComponentName(mContext.getPackageName(), WidgetRefreshJobService.class.getName()))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPeriodic(frequencyHourMillis)
                .build());
    } else {
        mAlarmManager.setInexactRepeating(AlarmManager.RTC,
                System.currentTimeMillis() + frequencyHourMillis,
                frequencyHourMillis,
                createRefreshPendingIntent(appWidgetId));
    }

}
 
Example #8
Source File: BackgroundDexOptService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static void schedule(Context context) {
    if (isBackgroundDexoptDisabled()) {
        return;
    }

    JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Schedule a one-off job which scans installed packages and updates
    // out-of-date oat files.
    js.schedule(new JobInfo.Builder(JOB_POST_BOOT_UPDATE, sDexoptServiceName)
                .setMinimumLatency(TimeUnit.MINUTES.toMillis(1))
                .setOverrideDeadline(TimeUnit.MINUTES.toMillis(1))
                .build());

    // Schedule a daily job which scans installed packages and compiles
    // those with fresh profiling data.
    js.schedule(new JobInfo.Builder(JOB_IDLE_OPTIMIZE, sDexoptServiceName)
                .setRequiresDeviceIdle(true)
                .setRequiresCharging(true)
                .setPeriodic(IDLE_OPTIMIZATION_PERIOD)
                .build());

    if (DEBUG_DEXOPT) {
        Log.i(TAG, "Jobs scheduled");
    }
}
 
Example #9
Source File: AndroidJobSchedulerUtils.java    From android_job_scheduler with Apache License 2.0 6 votes vote down vote up
public static JobInfo persistableBundleToJobInfo(PersistableBundle bundle) {
    JobInfo.Builder builder = new JobInfo.Builder(bundle.getInt(B_KEY_ID),
            new ComponentName(bundle.getString(B_KEY_COMPONENT_PKG), bundle.getString(B_KEY_COMPONENT_NAME)))
                .setMinimumLatency(bundle.getInt(B_KEY_INTERVAL))
                .setExtras(bundle);

    if (bundle.containsKey(B_KEY_PERSISTENT_ACROSS_REBOOTS)) {
        builder.setPersisted(true);
    }
    if (bundle.containsKey(B_KEY_REQUIRES_CHARGING)) {
        builder.setRequiresCharging(true);
    }
    if (bundle.containsKey(B_KEY_BACKOFF_CRITERIA)) {
        builder.setBackoffCriteria(
                bundle.getPersistableBundle(B_KEY_BACKOFF_CRITERIA).getInt(B_INNER_BACKOFF_MILLIS),
                bundle.getPersistableBundle(B_KEY_BACKOFF_CRITERIA).getInt(B_INNER_BACKOFF_POLICY)
        );
    }
    if (bundle.containsKey(B_KEY_NETWORK_TYPE)) {
        builder.setRequiredNetworkType(
                bundle.getPersistableBundle(B_KEY_NETWORK_TYPE).getInt(B_INNER_REQUIRED_NETWORK)
        );
    }

    return builder.build();
}
 
Example #10
Source File: QiscusNetworkCheckerJobService.java    From qiscus-sdk-android with Apache License 2.0 6 votes vote down vote up
public static void scheduleJob(Context context) {
    QiscusLogger.print(TAG, "scheduleJob: ");
    ComponentName componentName = new ComponentName(context, QiscusNetworkCheckerJobService.class);
    JobInfo jobInfo = new JobInfo.Builder(STATIC_JOB_ID, componentName)
            .setMinimumLatency(TimeUnit.SECONDS.toMillis(5))
            .setOverrideDeadline(TimeUnit.SECONDS.toMillis(10))
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setPersisted(true)
            .build();

    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    if (jobScheduler != null) {
        jobScheduler.schedule(jobInfo);
    }

}
 
Example #11
Source File: JobSchedulerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public int enqueue(JobInfo job, JobWorkItem work) throws RemoteException {
    if (DEBUG) {
        Slog.d(TAG, "Enqueueing job: " + job.toString() + " work: " + work);
    }
    final int uid = Binder.getCallingUid();
    final int userId = UserHandle.getUserId(uid);

    enforceValidJobRequest(uid, job);
    if (job.isPersisted()) {
        throw new IllegalArgumentException("Can't enqueue work for persisted jobs");
    }
    if (work == null) {
        throw new NullPointerException("work is null");
    }

    validateJobFlags(job, uid);

    long ident = Binder.clearCallingIdentity();
    try {
        return JobSchedulerService.this.scheduleAsPackage(job, work, uid, null, userId,
                null);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #12
Source File: ServiceSchedulerTest.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleWithDurationExtra() {
    final Intent intent = new Intent(context, EventIntentService.class);
    when(pendingIntentFactory.hasPendingIntent(intent)).thenReturn(false);
    PendingIntent pendingIntent = getPendingIntent();
    when(pendingIntentFactory.getPendingIntent(intent)).thenReturn(pendingIntent);
    when(optlyStorage.getLong(EventIntentService.EXTRA_INTERVAL, AlarmManager.INTERVAL_HOUR)).thenReturn(AlarmManager.INTERVAL_HOUR);

    long duration = AlarmManager.INTERVAL_DAY;
    intent.putExtra(EventIntentService.EXTRA_INTERVAL, duration);
    serviceScheduler.schedule(intent, duration);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ArgumentCaptor<JobInfo> jobInfoArgumentCaptor = ArgumentCaptor.forClass(JobInfo.class);

        verify(jobScheduler).schedule(jobInfoArgumentCaptor.capture());

        assertEquals(jobInfoArgumentCaptor.getValue().getIntervalMillis(), duration );
    }
    else {
        verify(alarmManager).setInexactRepeating(AlarmManager.ELAPSED_REALTIME, duration, duration, pendingIntent);
    }

    verify(logger).info("Scheduled {}", intent.getComponent().toShortString());
    pendingIntent.cancel();
}
 
Example #13
Source File: UpdateWeatherService.java    From your-local-weather with GNU General Public License v3.0 6 votes vote down vote up
private void resendTheIntentInSeveralSeconds(int seconds) {
    appendLog(getBaseContext(), TAG, "resendTheIntentInSeveralSeconds:SDK:", Build.VERSION.SDK_INT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        ComponentName serviceComponent = new ComponentName(this, UpdateWeatherResendJob.class);
        JobInfo.Builder builder = new JobInfo.Builder(UpdateWeatherResendJob.JOB_ID, serviceComponent);

        builder.setMinimumLatency(seconds * 1000); // wait at least
        builder.setOverrideDeadline((3 + seconds) * 1000); // maximum delay
        JobScheduler jobScheduler = getSystemService(JobScheduler.class);
        jobScheduler.schedule(builder.build());
        appendLog(getBaseContext(), TAG, "resendTheIntentInSeveralSeconds: sent");
    } else {
        AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
                0,
                new Intent(getBaseContext(), UpdateWeatherService.class),
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + (1000 * seconds), pendingIntent);
    }
}
 
Example #14
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * UI onclick listener to schedule a new job. 
 */
public void scheduleJob(View v) {
	JobInfo.Builder builder = new JobInfo.Builder(kJobId++,mServiceComponent);
	String delay = mDelayEditText.getText().toString();
	if (delay != null && !TextUtils.isEmpty(delay)) {
		builder.setMinimumLatency(Long.valueOf(delay) * 1000);
	}
	String deadline = mDeadlineEditText.getText().toString();
	if (deadline != null && !TextUtils.isEmpty(deadline)) {
		builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
	}
	boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
	boolean requiresAnyConnectivity = mAnyConnectivityRadioButton
			.isChecked();
	if (requiresUnmetered) {
		builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
	} else if (requiresAnyConnectivity) {
		builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
	}
	builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
	builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
	JobScheduler jobScheduler =
	        (JobScheduler) getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE);
	
	jobScheduler.schedule(builder.build());
}
 
Example #15
Source File: ContentObserverController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
void detachLocked() {
    final int N = mMyObservers.size();
    for (int i=0; i<N; i++) {
        final ObserverInstance obs = mMyObservers.get(i);
        obs.mJobs.remove(this);
        if (obs.mJobs.size() == 0) {
            if (DEBUG) {
                Slog.i(TAG, "Unregistering observer " + obs + " for " + obs.mUri.getUri());
            }
            mContext.getContentResolver().unregisterContentObserver(obs);
            ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observerOfUser =
                    mObservers.get(obs.mUserId);
            if (observerOfUser !=  null) {
                observerOfUser.remove(obs.mUri);
            }
        }
    }
}
 
Example #16
Source File: JobProxy21.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Override
public void plantOneOff(JobRequest request) {
    long startMs = Common.getStartMs(request);
    long endMs = Common.getEndMs(request, true);

    JobInfo jobInfo = createBuilderOneOff(createBaseBuilder(request, true), startMs, endMs).build();
    int scheduleResult = schedule(jobInfo);

    if (scheduleResult == ERROR_BOOT_PERMISSION) {
        jobInfo = createBuilderOneOff(createBaseBuilder(request, false), startMs, endMs).build();
        scheduleResult = schedule(jobInfo);
    }

    mCat.d("Schedule one-off jobInfo %s, %s, start %s, end %s (from now), reschedule count %d", scheduleResultToString(scheduleResult),
            request, JobUtil.timeToString(startMs), JobUtil.timeToString(Common.getEndMs(request, false)), Common.getRescheduleCount(request));
}
 
Example #17
Source File: JobProxy21.java    From android-job with Apache License 2.0 6 votes vote down vote up
protected int convertNetworkType(@NonNull JobRequest.NetworkType networkType) {
    switch (networkType) {
        case ANY:
            return JobInfo.NETWORK_TYPE_NONE;
        case CONNECTED:
            return JobInfo.NETWORK_TYPE_ANY;
        case UNMETERED:
            return JobInfo.NETWORK_TYPE_UNMETERED;
        case NOT_ROAMING:
            return JobInfo.NETWORK_TYPE_UNMETERED; // use unmetered here, is overwritten in v24
        case METERED:
            return JobInfo.NETWORK_TYPE_ANY; // use any here as fallback
        default:
            throw new IllegalStateException("not implemented");
    }
}
 
Example #18
Source File: AddWatchNextService.java    From leanback-homescreen-channels with Apache License 2.0 6 votes vote down vote up
public static void scheduleAddWatchNextRequest(Context context, ClipData clipData) {
    JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);

    PersistableBundle bundle = new PersistableBundle();
    bundle.putString(ID_KEY, clipData.getClipId());
    bundle.putString(CONTENT_ID_KEY, clipData.getContentId());
    bundle.putLong(DURATION_KEY, clipData.getDuration());
    bundle.putLong(PROGRESS_KEY, clipData.getProgress());
    bundle.putString(TITLE_KEY, clipData.getTitle());
    bundle.putString(DESCRIPTION_KEY, clipData.getDescription());
    bundle.putString(CARD_IMAGE_URL_KEY, clipData.getCardImageUrl());

    scheduler.schedule(new JobInfo.Builder(1,
            new ComponentName(context, AddWatchNextService.class))
            .setExtras(bundle)
            .build());
}
 
Example #19
Source File: JobStatus.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * @return Whether or not this job is ready to run, based on its requirements. This is true if
 * the constraints are satisfied <strong>or</strong> the deadline on the job has expired.
 * TODO: This function is called a *lot*.  We should probably just have it check an
 * already-computed boolean, which we updated whenever we see one of the states it depends
 * on here change.
 */
public boolean isReady() {
    // Deadline constraint trumps other constraints (except for periodic jobs where deadline
    // is an implementation detail. A periodic job should only run if its constraints are
    // satisfied).
    // AppNotIdle implicit constraint must be satisfied
    // DeviceNotDozing implicit constraint must be satisfied
    // NotRestrictedInBackground implicit constraint must be satisfied
    final boolean deadlineSatisfied = (!job.isPeriodic() && hasDeadlineConstraint()
            && (satisfiedConstraints & CONSTRAINT_DEADLINE) != 0);
    final boolean notDozing = (satisfiedConstraints & CONSTRAINT_DEVICE_NOT_DOZING) != 0
            || (job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0;
    final boolean notRestrictedInBg =
            (satisfiedConstraints & CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0;
    return (isConstraintsSatisfied() || deadlineSatisfied) && notDozing && notRestrictedInBg;
}
 
Example #20
Source File: ServiceSchedulerTest.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleWithNoDurationExtra() {
    final Intent intent = new Intent(context, EventIntentService.class);
    when(pendingIntentFactory.hasPendingIntent(intent)).thenReturn(false);
    PendingIntent pendingIntent = getPendingIntent();
    when(pendingIntentFactory.getPendingIntent(intent)).thenReturn(pendingIntent);
    when(optlyStorage.getLong(EventIntentService.EXTRA_INTERVAL, AlarmManager.INTERVAL_HOUR)).thenReturn(AlarmManager.INTERVAL_HOUR);

    serviceScheduler.schedule(intent, AlarmManager.INTERVAL_HOUR);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ArgumentCaptor<JobInfo> jobInfoArgumentCaptor = ArgumentCaptor.forClass(JobInfo.class);

        verify(jobScheduler).schedule(jobInfoArgumentCaptor.capture());

        assertEquals(jobInfoArgumentCaptor.getValue().getIntervalMillis(), AlarmManager.INTERVAL_HOUR );

    }
    else {
        verify(alarmManager).setInexactRepeating(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_HOUR, AlarmManager.INTERVAL_HOUR, pendingIntent);
    }
    verify(logger).info("Scheduled {}", intent.getComponent().toShortString());
    pendingIntent.cancel();
}
 
Example #21
Source File: GcmJobService.java    From ti.goosh with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
static void scheduleJob(Context context, Bundle extras) {
	ComponentName jobComponentName = new ComponentName(context.getPackageName(), GcmJobService.class.getName());
	JobScheduler mJobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
	JobInfo existingInfo = mJobScheduler.getPendingJob(JOB_ID);
	if (existingInfo != null) {
		mJobScheduler.cancel(JOB_ID);
	}

	JobInfo.Builder jobBuilder = new JobInfo.Builder(JOB_ID, jobComponentName)
			.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).setTransientExtras(extras);
	int result = mJobScheduler.schedule(jobBuilder.build());
	if (result != JobScheduler.RESULT_SUCCESS) {
		Log.e(LCAT, "Could not start job, error code: " + result);
	}
}
 
Example #22
Source File: JobStatus.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new JobStatus that was loaded from disk. We ignore the provided
 * {@link android.app.job.JobInfo} time criteria because we can load a persisted periodic job
 * from the {@link com.android.server.job.JobStore} and still want to respect its
 * wallclock runtime rather than resetting it on every boot.
 * We consider a freshly loaded job to no longer be in back-off, and the associated
 * standby bucket is whatever the OS thinks it should be at this moment.
 */
public JobStatus(JobInfo job, int callingUid, String sourcePkgName, int sourceUserId,
        int standbyBucket, long baseHeartbeat, String sourceTag,
        long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis,
        long lastSuccessfulRunTime, long lastFailedRunTime,
        Pair<Long, Long> persistedExecutionTimesUTC,
        int innerFlags) {
    this(job, callingUid, resolveTargetSdkVersion(job), sourcePkgName, sourceUserId,
            standbyBucket, baseHeartbeat,
            sourceTag, 0,
            earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis,
            lastSuccessfulRunTime, lastFailedRunTime, innerFlags);

    // Only during initial inflation do we record the UTC-timebase execution bounds
    // read from the persistent store.  If we ever have to recreate the JobStatus on
    // the fly, it means we're rescheduling the job; and this means that the calculated
    // elapsed timebase bounds intrinsically become correct.
    this.mPersistedUtcTimes = persistedExecutionTimesUTC;
    if (persistedExecutionTimesUTC != null) {
        if (DEBUG) {
            Slog.i(TAG, "+ restored job with RTC times because of bad boot clock");
        }
    }
}
 
Example #23
Source File: TimeZoneUpdateIdler.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules the TimeZoneUpdateIdler job service to run once.
 *
 * @param context Context to use to get a job scheduler.
 */
public static void schedule(Context context, long minimumDelayMillis) {
    // Request that the JobScheduler tell us when the device falls idle.
    JobScheduler jobScheduler =
            (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // The TimeZoneUpdateIdler will send an intent that will trigger the Receiver.
    ComponentName idlerJobServiceName =
            new ComponentName(context, TimeZoneUpdateIdler.class);

    // We require the device is idle, but also that it is charging to be as non-invasive as
    // we can.
    JobInfo.Builder jobInfoBuilder =
            new JobInfo.Builder(TIME_ZONE_UPDATE_IDLE_JOB_ID, idlerJobServiceName)
                    .setRequiresDeviceIdle(true)
                    .setRequiresCharging(true)
                    .setMinimumLatency(minimumDelayMillis);

    Slog.d(TAG, "schedule() called: minimumDelayMillis=" + minimumDelayMillis);
    jobScheduler.schedule(jobInfoBuilder.build());
}
 
Example #24
Source File: PhotosContentJob.java    From proofmode with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isScheduled(Context context) {
    JobScheduler js = context.getSystemService(JobScheduler.class);
    List<JobInfo> jobs = js.getAllPendingJobs();
    if (jobs == null) {
        return false;
    }
    for (int i=0; i<jobs.size(); i++) {
        if (jobs.get(i).getId() == PHOTOS_CONTENT_JOB) {
            return true;
        }
    }
    return false;
}
 
Example #25
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
public void scheduleJob(View view) {
    int selectedNetworkID = networkOptions.getCheckedRadioButtonId();
    int selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
    int seekBarInteger = mSeekBar.getProgress();
    boolean seekBarSet = seekBarInteger > 0;
    switch (selectedNetworkID) {
        case R.id.noNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
            break;
        case R.id.anyNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_ANY;
            break;
        case R.id.wifiNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_UNMETERED;
            break;
    }

    mScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(getPackageName(), SleeperJobScheduler.class.getName());
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
    builder.setRequiredNetworkType(selectedNetworkOption)
            .setRequiresDeviceIdle(mDeviceIdleSwitch.isChecked())
            .setRequiresCharging(mDeviceChargingSwitch.isChecked());
    if (seekBarSet){
        builder.setOverrideDeadline(seekBarInteger * 1000);
    }
    boolean constraintSet = (selectedNetworkOption != JobInfo.NETWORK_TYPE_NONE) || mDeviceChargingSwitch.isChecked() || mDeviceIdleSwitch.isChecked() || seekBarSet;
    if (constraintSet) {
        JobInfo myJobInfo = builder.build();
        mScheduler.schedule(myJobInfo);
        Toast.makeText(this, "Job Scheduled, job will run when " + "the constraints are met.", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Please set at least one constraint", Toast.LENGTH_SHORT).show();
    }
}
 
Example #26
Source File: BootCompleteReceiver.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
private void startRefreshTorUnlockIPs(Context context) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP || refreshPeriodHours == 0) {
        return;
    }
    ComponentName jobService = new ComponentName(context, GetIPsJobService.class);
    JobInfo.Builder getIPsJobBuilder = new JobInfo.Builder(mJobId, jobService);
    getIPsJobBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    getIPsJobBuilder.setPeriodic(refreshPeriodHours * 60 * 60 * 1000);

    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    if (jobScheduler != null) {
        jobScheduler.schedule(getIPsJobBuilder.build());
    }
}
 
Example #27
Source File: JobStore.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the back-off policy out of the params tag. These attributes may not exist, depending
 * on whether the back-off was set when the job was first scheduled.
 */
private void maybeBuildBackoffPolicyFromXml(JobInfo.Builder jobBuilder, XmlPullParser parser) {
    String val = parser.getAttributeValue(null, "initial-backoff");
    if (val != null) {
        long initialBackoff = Long.parseLong(val);
        val = parser.getAttributeValue(null, "backoff-policy");
        int backoffPolicy = Integer.parseInt(val);  // Will throw NFE which we catch higher up.
        jobBuilder.setBackoffCriteria(initialBackoff, backoffPolicy);
    }
}
 
Example #28
Source File: MainActivity.java    From Easy_xkcd with Apache License 2.0 5 votes vote down vote up
@Override
public void onPostExecute(Void dummy) {
    super.onPostExecute(dummy);
    if (!fromOnRestart && savedInstanceState == null && prefHelper.launchToOverview()) {
        currentFragment = CurrentFragment.Overview;
        showOverview(false);
    } else if (getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG) == null || newComicFound) {
        Timber.d("Creating a new Fragment...");
        switch (currentFragment) {
            case Browser:
                showBrowserFragment(false);
                break;
            case Favorites:
                showFavoritesFragment(false);
                break;
            case Overview:
                showOverview(false);
                break;
        }
    }
    updateToolbarTitle();
    unlockRotation();
    //Setup the notifications in case the device was restarted
    Timber.d("interval: %d", prefHelper.getNotificationInterval());
    if (!fromOnRestart && savedInstanceState == null && prefHelper.getNotificationInterval() != 0) {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(new JobInfo.Builder(UPDATE_JOB_ID, new ComponentName(MainActivity.this, ComicNotifierJob.class))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPeriodic(prefHelper.getNotificationInterval())
                .setPersisted(true)
                .build()
        );
        Timber.d("job scheduled...");
    }
    updateTaskRunning = false;
}
 
Example #29
Source File: JobIntentService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
JobWorkEnqueuer(Context context, ComponentName cn, int jobId) {
    super(context, cn);
    ensureJobId(jobId);
    JobInfo.Builder b = new JobInfo.Builder(jobId, mComponentName);
    mJobInfo = b.setOverrideDeadline(0).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build();
    mJobScheduler = (JobScheduler) context.getApplicationContext().getSystemService(
            Context.JOB_SCHEDULER_SERVICE);
}
 
Example #30
Source File: JobInfoScheduler.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
private boolean isJobServiceOn(JobScheduler scheduler, int jobId, int attemptNumber) {
  for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
    int existingAttemptNumber = jobInfo.getExtras().getInt(ATTEMPT_NUMBER);
    if (jobInfo.getId() == jobId) {
      return existingAttemptNumber >= attemptNumber;
    }
  }
  return false;
}