From a005ddf9005c70b70df7eef5063f08bb0a97d4fa Mon Sep 17 00:00:00 2001 From: Varun Shah Date: Sat, 10 Dec 2022 21:04:35 +0000 Subject: [PATCH] Expose LRJ related APIs. Expose all data transfer jobs and user initiated jobs APIs. Bug: 255371817 Test: atest CtsJobSchedulerTestCases API-Coverage-Bug: 262911363 Change-Id: Icfa3fbdf079a56cdb5d87af173cff16592bb61aa --- .../java/android/app/job/JobInfo.java | 16 ++++++------- .../java/android/app/job/JobParameters.java | 1 - .../java/android/app/job/JobService.java | 24 +++---------------- .../server/job/JobSchedulerService.java | 2 +- core/api/current.txt | 10 ++++++++ 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/apex/jobscheduler/framework/java/android/app/job/JobInfo.java b/apex/jobscheduler/framework/java/android/app/job/JobInfo.java index 19ab5bcc9967d..deb97a5873ab9 100644 --- a/apex/jobscheduler/framework/java/android/app/job/JobInfo.java +++ b/apex/jobscheduler/framework/java/android/app/job/JobInfo.java @@ -739,7 +739,6 @@ public class JobInfo implements Parcelable { /** * @see JobInfo.Builder#setDataTransfer(boolean) - * @hide */ public boolean isDataTransfer() { return (flags & FLAG_DATA_TRANSFER) != 0; @@ -747,7 +746,6 @@ public class JobInfo implements Parcelable { /** * @see JobInfo.Builder#setUserInitiated(boolean) - * @hide */ public boolean isUserInitiated() { return (flags & FLAG_USER_INITIATED) != 0; @@ -1445,6 +1443,7 @@ public class JobInfo implements Parcelable { * reasonable estimates should use the sentinel value * {@link JobInfo#NETWORK_BYTES_UNKNOWN}. * + * TODO(255371817): update documentation to reflect how this data will be used * Note that the system may choose to delay jobs with large network * usage estimates when the device has a poor network connection, in * order to save battery and possible network costs. @@ -1852,11 +1851,6 @@ public class JobInfo implements Parcelable { * being transferred is potentially very large and can take a long time to complete. * *

- * 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. - * - *

* 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 @@ -1873,7 +1867,6 @@ public class JobInfo implements Parcelable { * {@link JobWorkItem JobWorkItems} along with {@link #setDataTransfer(boolean)}. * * @see JobInfo#isDataTransfer() - * @hide */ @NonNull public Builder setDataTransfer(boolean dataTransfer) { @@ -1903,12 +1896,17 @@ public class JobInfo implements Parcelable { * shown in the Task Manager when running. * *

+ * If the app doesn't hold the {@link android.Manifest.permission#RUN_LONG_JOBS} permission + * when scheduling a user-initiated job, JobScheduler will throw a + * {@link SecurityException}. + * + *

* These jobs will not be subject to quotas and will be started immediately once scheduled * if all constraints are met and the device system health allows for additional tasks. * * @see JobInfo#isUserInitiated() - * @hide */ + @RequiresPermission(android.Manifest.permission.RUN_LONG_JOBS) @NonNull public Builder setUserInitiated(boolean userInitiated) { if (userInitiated) { diff --git a/apex/jobscheduler/framework/java/android/app/job/JobParameters.java b/apex/jobscheduler/framework/java/android/app/job/JobParameters.java index 1720534e1183c..a5a7f93137d66 100644 --- a/apex/jobscheduler/framework/java/android/app/job/JobParameters.java +++ b/apex/jobscheduler/framework/java/android/app/job/JobParameters.java @@ -403,7 +403,6 @@ public class JobParameters implements Parcelable { * {@link android.Manifest.permission#RUN_LONG_JOBS} permission. * * @see JobInfo.Builder#setUserInitiated(boolean) - * @hide */ public boolean isUserInitiatedJob() { return mIsUserInitiated; diff --git a/apex/jobscheduler/framework/java/android/app/job/JobService.java b/apex/jobscheduler/framework/java/android/app/job/JobService.java index 898557dfb4f96..449c316c1c65c 100644 --- a/apex/jobscheduler/framework/java/android/app/job/JobService.java +++ b/apex/jobscheduler/framework/java/android/app/job/JobService.java @@ -232,16 +232,8 @@ public abstract class JobService extends Service { public abstract boolean onStopJob(JobParameters params); /** - * Update how much data this job will transfer. This method can - * be called multiple times within the first 30 seconds after - * {@link #onStartJob(JobParameters)} has been called. Only - * one call will be heeded after that time has passed. + * Update the amount of data this job is estimated to transfer after the job has started. * - * This method (or an overload) must be called within the first - * 30 seconds for a data transfer job if a payload size estimate - * was not provided at the time of scheduling. - * - * @hide * @see JobInfo.Builder#setEstimatedNetworkBytes(long, long) */ public final void updateEstimatedNetworkBytes(@NonNull JobParameters params, @@ -250,16 +242,9 @@ public abstract class JobService extends Service { } /** - * Update how much data will transfer for the JobWorkItem. This - * method can be called multiple times within the first 30 seconds - * after {@link #onStartJob(JobParameters)} has been called. - * Only one call will be heeded after that time has passed. + * Update the amount of data this JobWorkItem is estimated to transfer after the job has + * started. * - * This method (or an overload) must be called within the first - * 30 seconds for a data transfer job if a payload size estimate - * was not provided at the time of scheduling. - * - * @hide * @see JobInfo.Builder#setEstimatedNetworkBytes(long, long) */ public final void updateEstimatedNetworkBytes(@NonNull JobParameters params, @@ -270,7 +255,6 @@ public abstract class JobService extends Service { /** * Tell JobScheduler how much data has successfully been transferred for the data transfer job. - * @hide */ public final void updateTransferredNetworkBytes(@NonNull JobParameters params, @BytesLong long transferredDownloadBytes, @BytesLong long transferredUploadBytes) { @@ -281,7 +265,6 @@ public abstract class JobService extends Service { /** * Tell JobScheduler how much data has been transferred for the data transfer * {@link JobWorkItem}. - * @hide */ public final void updateTransferredNetworkBytes(@NonNull JobParameters params, @NonNull JobWorkItem item, @@ -428,7 +411,6 @@ public abstract class JobService extends Service { * Notification)}. * @param notification The notification to be displayed. * @param jobEndNotificationPolicy The policy to apply to the notification when the job stops. - * @hide */ public final void setNotification(@NonNull JobParameters params, int notificationId, @NonNull Notification notification, diff --git a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java index e9b966083851c..c2f71b5038bbd 100644 --- a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java +++ b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java @@ -3885,7 +3885,7 @@ public class JobSchedulerService extends com.android.server.SystemService final int callingUid = Binder.getCallingUid(); if (callingUid != uid && !UserHandle.isCore(callingUid)) { throw new SecurityException("Uid " + callingUid - + " cannot query canRunLongJobs for package " + packageName); + + " cannot query hasRunLongJobsPermission for package " + packageName); } return checkRunLongJobsPermission(uid, packageName); diff --git a/core/api/current.txt b/core/api/current.txt index 1ff2a2a6fa275..ededd831c0ea8 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -8388,6 +8388,7 @@ package android.app.job { method public long getTriggerContentMaxDelay(); method public long getTriggerContentUpdateDelay(); method @Nullable public android.app.job.JobInfo.TriggerContentUri[] getTriggerContentUris(); + method public boolean isDataTransfer(); method public boolean isExpedited(); method public boolean isImportantWhileForeground(); method public boolean isPeriodic(); @@ -8397,6 +8398,7 @@ package android.app.job { method public boolean isRequireCharging(); method public boolean isRequireDeviceIdle(); method public boolean isRequireStorageNotLow(); + method public boolean isUserInitiated(); method public void writeToParcel(android.os.Parcel, int); field public static final int BACKOFF_POLICY_EXPONENTIAL = 1; // 0x1 field public static final int BACKOFF_POLICY_LINEAR = 0; // 0x0 @@ -8423,6 +8425,7 @@ package android.app.job { method public android.app.job.JobInfo build(); method public android.app.job.JobInfo.Builder setBackoffCriteria(long, int); method public android.app.job.JobInfo.Builder setClipData(@Nullable android.content.ClipData, int); + method @NonNull public android.app.job.JobInfo.Builder setDataTransfer(boolean); method public android.app.job.JobInfo.Builder setEstimatedNetworkBytes(long, long); method @NonNull public android.app.job.JobInfo.Builder setExpedited(boolean); method public android.app.job.JobInfo.Builder setExtras(@NonNull android.os.PersistableBundle); @@ -8444,6 +8447,7 @@ package android.app.job { method public android.app.job.JobInfo.Builder setTransientExtras(@NonNull android.os.Bundle); method public android.app.job.JobInfo.Builder setTriggerContentMaxDelay(long); method public android.app.job.JobInfo.Builder setTriggerContentUpdateDelay(long); + method @NonNull @RequiresPermission(android.Manifest.permission.RUN_LONG_JOBS) public android.app.job.JobInfo.Builder setUserInitiated(boolean); } public static final class JobInfo.TriggerContentUri implements android.os.Parcelable { @@ -8471,6 +8475,7 @@ package android.app.job { method @Nullable public android.net.Uri[] getTriggeredContentUris(); method public boolean isExpeditedJob(); method public boolean isOverrideDeadlineExpired(); + method public boolean isUserInitiatedJob(); method public void writeToParcel(android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; field public static final int STOP_REASON_APP_STANDBY = 12; // 0xc @@ -8529,6 +8534,11 @@ package android.app.job { method public final android.os.IBinder onBind(android.content.Intent); method public abstract boolean onStartJob(android.app.job.JobParameters); method public abstract boolean onStopJob(android.app.job.JobParameters); + method public final void setNotification(@NonNull android.app.job.JobParameters, int, @NonNull android.app.Notification, int); + method public final void updateEstimatedNetworkBytes(@NonNull android.app.job.JobParameters, long, long); + method public final void updateEstimatedNetworkBytes(@NonNull android.app.job.JobParameters, @NonNull android.app.job.JobWorkItem, long, long); + method public final void updateTransferredNetworkBytes(@NonNull android.app.job.JobParameters, long, long); + method public final void updateTransferredNetworkBytes(@NonNull android.app.job.JobParameters, @NonNull android.app.job.JobWorkItem, long, long); field public static final String PERMISSION_BIND = "android.permission.BIND_JOB_SERVICE"; }