Search in sources :

Example 56 with JobStatus

use of com.android.server.job.controllers.JobStatus in project android_frameworks_base by AOSPA.

the class JobSchedulerService method onJobCompleted.

// JobCompletedListener implementations.
/**
     * A job just finished executing. We fetch the
     * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
     * whether we want to reschedule we readd it to the controllers.
     * @param jobStatus Completed job.
     * @param needsReschedule Whether the implementing class should reschedule this job.
     */
@Override
public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
    if (DEBUG) {
        Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
    }
    // shuts down before it is added back.
    if (!stopTrackingJob(jobStatus, null, !jobStatus.getJob().isPeriodic())) {
        if (DEBUG) {
            Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
        }
        // We still want to check for jobs to execute, because this job may have
        // scheduled a new job under the same job id, and now we can run it.
        mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
        return;
    }
    // that may cause ordering problems if the app removes jobStatus while in here.
    if (needsReschedule) {
        JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
        startTrackingJob(rescheduled, jobStatus);
    } else if (jobStatus.getJob().isPeriodic()) {
        JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
        startTrackingJob(rescheduledPeriodic, jobStatus);
    }
    reportActive();
    mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
}
Also used : JobStatus(com.android.server.job.controllers.JobStatus)

Example 57 with JobStatus

use of com.android.server.job.controllers.JobStatus in project platform_frameworks_base by android.

the class JobSchedulerService method scheduleAsPackage.

public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId, String tag) {
    JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag);
    try {
        if (ActivityManagerNative.getDefault().getAppStartMode(uId, job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) {
            Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() + " -- package not allowed to start");
            return JobScheduler.RESULT_FAILURE;
        }
    } catch (RemoteException e) {
    }
    if (DEBUG)
        Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
    JobStatus toCancel;
    synchronized (mLock) {
        // Jobs on behalf of others don't apply to the per-app job cap
        if (ENFORCE_MAX_JOBS && packageName == null) {
            if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) {
                Slog.w(TAG, "Too many jobs for uid " + uId);
                throw new IllegalStateException("Apps may not schedule more than " + MAX_JOBS_PER_APP + " distinct jobs");
            }
        }
        toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());
        if (toCancel != null) {
            cancelJobImpl(toCancel, jobStatus);
        }
        startTrackingJob(jobStatus, toCancel);
    }
    mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
    return JobScheduler.RESULT_SUCCESS;
}
Also used : JobStatus(com.android.server.job.controllers.JobStatus) RemoteException(android.os.RemoteException)

Example 58 with JobStatus

use of com.android.server.job.controllers.JobStatus in project platform_frameworks_base by android.

the class JobSchedulerService method getRescheduleJobForFailure.

/**
     * Reschedules the given job based on the job's backoff policy. It doesn't make sense to
     * specify an override deadline on a failed job (the failed job will run even though it's not
     * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any
     * ready job with {@link JobStatus#numFailures} > 0 will be executed.
     *
     * @param failureToReschedule Provided job status that we will reschedule.
     * @return A newly instantiated JobStatus with the same constraints as the last job except
     * with adjusted timing constraints.
     *
     * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH
     */
private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
    final long elapsedNowMillis = SystemClock.elapsedRealtime();
    final JobInfo job = failureToReschedule.getJob();
    final long initialBackoffMillis = job.getInitialBackoffMillis();
    final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
    long delayMillis;
    switch(job.getBackoffPolicy()) {
        case JobInfo.BACKOFF_POLICY_LINEAR:
            delayMillis = initialBackoffMillis * backoffAttempts;
            break;
        default:
            if (DEBUG) {
                Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
            }
        case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
            delayMillis = (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
            break;
    }
    delayMillis = Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
    JobStatus newJob = new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis, JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
    for (int ic = 0; ic < mControllers.size(); ic++) {
        StateController controller = mControllers.get(ic);
        controller.rescheduleForFailure(newJob, failureToReschedule);
    }
    return newJob;
}
Also used : JobStatus(com.android.server.job.controllers.JobStatus) JobInfo(android.app.job.JobInfo) StateController(com.android.server.job.controllers.StateController)

Example 59 with JobStatus

use of com.android.server.job.controllers.JobStatus in project android_frameworks_base by crdroidandroid.

the class JobSchedulerService method scheduleAsPackage.

public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId, String tag) {
    JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag);
    try {
        if (ActivityManagerNative.getDefault().getAppStartMode(uId, job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) {
            Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() + " -- package not allowed to start");
            return JobScheduler.RESULT_FAILURE;
        }
    } catch (RemoteException e) {
    }
    if (DEBUG)
        Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
    JobStatus toCancel;
    synchronized (mLock) {
        // Jobs on behalf of others don't apply to the per-app job cap
        if (ENFORCE_MAX_JOBS && packageName == null) {
            if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) {
                Slog.w(TAG, "Too many jobs for uid " + uId);
                throw new IllegalStateException("Apps may not schedule more than " + MAX_JOBS_PER_APP + " distinct jobs");
            }
        }
        toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());
        if (toCancel != null) {
            cancelJobImpl(toCancel, jobStatus);
        }
        startTrackingJob(jobStatus, toCancel);
    }
    mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
    return JobScheduler.RESULT_SUCCESS;
}
Also used : JobStatus(com.android.server.job.controllers.JobStatus) RemoteException(android.os.RemoteException)

Example 60 with JobStatus

use of com.android.server.job.controllers.JobStatus in project android_frameworks_base by crdroidandroid.

the class JobSchedulerService method onBootPhase.

@Override
public void onBootPhase(int phase) {
    if (PHASE_SYSTEM_SERVICES_READY == phase) {
        mConstants.start(getContext().getContentResolver());
        // Register br for package removals and user removals.
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
        filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
        filter.addDataScheme("package");
        getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
        final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
        getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
        mPowerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
        try {
            ActivityManagerNative.getDefault().registerUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE | ActivityManager.UID_OBSERVER_IDLE);
        } catch (RemoteException e) {
        // ignored; both services live in system_server
        }
    } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
        synchronized (mLock) {
            // Let's go!
            mReadyToRock = true;
            mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME));
            mLocalDeviceIdleController = LocalServices.getService(DeviceIdleController.LocalService.class);
            // Create the "runners".
            for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
                mActiveServices.add(new JobServiceContext(this, mBatteryStats, mJobPackageTracker, getContext().getMainLooper()));
            }
            // Attach jobs to their controllers.
            mJobs.forEachJob(new JobStatusFunctor() {

                @Override
                public void process(JobStatus job) {
                    for (int controller = 0; controller < mControllers.size(); controller++) {
                        final StateController sc = mControllers.get(controller);
                        sc.maybeStartTrackingJobLocked(job, null);
                    }
                }
            });
            // GO GO GO!
            mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
        }
    }
}
Also used : JobStatus(com.android.server.job.controllers.JobStatus) IntentFilter(android.content.IntentFilter) DeviceIdleController(com.android.server.DeviceIdleController) JobStatusFunctor(com.android.server.job.JobStore.JobStatusFunctor) RemoteException(android.os.RemoteException) StateController(com.android.server.job.controllers.StateController)

Aggregations

JobStatus (com.android.server.job.controllers.JobStatus)122 JobInfo (android.app.job.JobInfo)42 Builder (android.app.job.JobInfo.Builder)32 JobSet (com.android.server.job.JobStore.JobSet)32 RemoteException (android.os.RemoteException)25 StateController (com.android.server.job.controllers.StateController)10 IntentFilter (android.content.IntentFilter)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 PowerManager (android.os.PowerManager)5 WorkSource (android.os.WorkSource)5 DeviceIdleController (com.android.server.DeviceIdleController)5 JobStatusFunctor (com.android.server.job.JobStore.JobStatusFunctor)5 ArrayList (java.util.ArrayList)5 PersistableBundle (android.os.PersistableBundle)4