Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b07077b2 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a new API in JobInfo.Builder for Data Transfer jobs."

parents ebd0a052 6d4947c2
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -16,11 +16,13 @@

package android.app;

import android.annotation.NonNull;
import android.app.job.IJobScheduler;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.app.job.JobSnapshot;
import android.app.job.JobWorkItem;
import android.content.Context;
import android.os.RemoteException;

import java.util.List;
@@ -36,8 +38,10 @@ import java.util.List;
 */
public class JobSchedulerImpl extends JobScheduler {
    IJobScheduler mBinder;
    private final Context mContext;

    public JobSchedulerImpl(IJobScheduler binder) {
    public JobSchedulerImpl(@NonNull Context context, IJobScheduler binder) {
        mContext = context;
        mBinder = binder;
    }

@@ -102,6 +106,15 @@ public class JobSchedulerImpl extends JobScheduler {
        }
    }

    @Override
    public boolean canRunLongJobs() {
        try {
            return mBinder.canRunLongJobs(mContext.getOpPackageName());
        } catch (RemoteException e) {
            return false;
        }
    }

    @Override
    public boolean hasRunLongJobsPermission(String packageName, int userId) {
        try {
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ interface IJobScheduler {
    void cancelAll();
    ParceledListSlice getAllPendingJobs();
    JobInfo getPendingJob(int jobId);
    boolean canRunLongJobs(String packageName);
    boolean hasRunLongJobsPermission(String packageName, int userId);
    List<JobInfo> getStartedJobs();
    ParceledListSlice getAllJobSnapshots();
+82 −2
Original line number Diff line number Diff line
@@ -395,6 +395,13 @@ public class JobInfo implements Parcelable {
     */
    public static final int FLAG_EXPEDITED = 1 << 4;

    /**
     * Whether it's a data transfer job or not.
     *
     * @hide
     */
    public static final int FLAG_DATA_TRANSFER = 1 << 5;

    /**
     * @hide
     */
@@ -722,6 +729,14 @@ public class JobInfo implements Parcelable {
        return (flags & FLAG_EXPEDITED) != 0;
    }

    /**
     * @see JobInfo.Builder#setDataTransfer(boolean)
     * @hide
     */
    public boolean isDataTransfer() {
        return (flags & FLAG_DATA_TRANSFER) != 0;
    }

    /**
     * @see JobInfo.Builder#setImportantWhileForeground(boolean)
     */
@@ -1815,6 +1830,52 @@ public class JobInfo implements Parcelable {
            return this;
        }

        /**
         * Indicates that this job will be used to transfer data to or from a remote server. The
         * system could attempt to run a data transfer job longer than a regular job if the data
         * being transferred is potentially very large and can take a long time to complete.
         *
         * <p>
         * The app must hold the {@link android.Manifest.permission#RUN_LONG_JOBS} permission to
         * use this API. JobScheduler will throw a {@link SecurityException} if an app without the
         * permission granted attempts to schedule a data transfer job.
         *
         * <p>
         * You must provide an estimate of the payload size via
         * {@link #setEstimatedNetworkBytes(long, long)} when scheduling the job or use
         * {@link JobService#updateEstimatedNetworkBytes(JobParameters, long, long)} or
         * {@link JobService#updateEstimatedNetworkBytes(JobParameters, JobWorkItem, long, long)}
         * shortly after the job starts.
         *
         * <p>
         * For user-initiated transfers that must be started immediately, call
         * {@link #setExpedited(boolean) setExpedited(true)}. Otherwise, the system may defer the
         * job to a more opportune time. Using {@link #setExpedited(boolean) setExpedited(true)}
         * with this API will only be allowed for foreground apps and when the user has clearly
         * interacted with the app. {@link #setExpedited(boolean) setExpedited(true)} will return
         * {@link JobScheduler#RESULT_FAILURE} for a data transfer job if the app is in the
         * background. Apps that successfully schedule data transfer jobs with
         * {@link #setExpedited(boolean) setExpedited(true)} will not have quotas applied to them,
         * though they may still be stopped for system health or constraint reasons. The system will
         * also give a user the ability to stop a data transfer job via the Task Manager.
         *
         * <p>
         * If you want to perform more than one data transfer job, consider enqueuing multiple
         * {@link JobWorkItem JobWorkItems} along with {@link #setDataTransfer(boolean)}.
         *
         * @see JobInfo#isDataTransfer()
         * @hide
         */
        @NonNull
        public Builder setDataTransfer(boolean dataTransfer) {
            if (dataTransfer) {
                mFlags |= FLAG_DATA_TRANSFER;
            } else {
                mFlags &= (~FLAG_DATA_TRANSFER);
            }
            return this;
        }

        /**
         * Setting this to true indicates that this job is important while the scheduling app
         * is in the foreground or on the temporary whitelist for background restrictions.
@@ -2062,8 +2123,9 @@ public class JobInfo implements Parcelable {
                        "An expedited job must be high or max priority. Don't use expedited jobs"
                                + " for unimportant tasks.");
            }
            if ((constraintFlags & ~CONSTRAINT_FLAG_STORAGE_NOT_LOW) != 0
                    || (flags & ~(FLAG_EXPEDITED | FLAG_EXEMPT_FROM_APP_STANDBY)) != 0) {
            if (((constraintFlags & ~CONSTRAINT_FLAG_STORAGE_NOT_LOW) != 0
                    || (flags & ~(FLAG_EXPEDITED | FLAG_EXEMPT_FROM_APP_STANDBY
                                    | FLAG_DATA_TRANSFER)) != 0)) {
                throw new IllegalArgumentException(
                        "An expedited job can only have network and storage-not-low constraints");
            }
@@ -2072,6 +2134,24 @@ public class JobInfo implements Parcelable {
                        "Can't call addTriggerContentUri() on an expedited job");
            }
        }

        if ((flags & FLAG_DATA_TRANSFER) != 0) {
            if (backoffPolicy == BACKOFF_POLICY_LINEAR) {
                throw new IllegalArgumentException(
                        "A data transfer job cannot have a linear backoff policy.");
            }
            if (hasLateConstraint) {
                throw new IllegalArgumentException("A data transfer job cannot have a deadline");
            }
            if ((flags & FLAG_PREFETCH) != 0) {
                throw new IllegalArgumentException(
                        "A data transfer job cannot also be a prefetch job");
            }
            if (networkRequest == null) {
                throw new IllegalArgumentException(
                        "A data transfer job must specify a valid network type");
            }
        }
    }

    /**
+8 −0
Original line number Diff line number Diff line
@@ -249,6 +249,14 @@ public abstract class JobScheduler {
     */
    public abstract @Nullable JobInfo getPendingJob(int jobId);

    /**
     * Returns {@code true} if the calling app currently holds the
     * {@link android.Manifest.permission#RUN_LONG_JOBS} permission, allowing it to run long jobs.
     */
    public boolean canRunLongJobs() {
        return false;
    }

    /**
     * Returns {@code true} if the app currently holds the
     * {@link android.Manifest.permission#RUN_LONG_JOBS} permission, allowing it to run long jobs.
+2 −2
Original line number Diff line number Diff line
@@ -44,9 +44,9 @@ public class JobSchedulerFrameworkInitializer {
     * <p>If this is called from other places, it throws a {@link IllegalStateException).
     */
    public static void registerServiceWrappers() {
        SystemServiceRegistry.registerStaticService(
        SystemServiceRegistry.registerContextAwareService(
                Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
                (b) -> new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)));
                (context, b) -> new JobSchedulerImpl(context, IJobScheduler.Stub.asInterface(b)));
        SystemServiceRegistry.registerContextAwareService(
                Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
                (context, b) -> new DeviceIdleManager(
Loading