1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.jobscheduler.cts.jobtestapp;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobScheduler;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.util.Log;
26 
27 /**
28  * Schedules jobs for this package but does not, by itself, occupy a foreground uid state
29  * while doing so.
30  */
31 public class TestJobSchedulerReceiver extends BroadcastReceiver {
32     private static final String TAG = TestJobSchedulerReceiver.class.getSimpleName();
33     private static final String PACKAGE_NAME = "android.jobscheduler.cts.jobtestapp";
34 
35     public static final String EXTRA_JOB_ID_KEY = PACKAGE_NAME + ".extra.JOB_ID";
36     public static final String EXTRA_ALLOW_IN_IDLE = PACKAGE_NAME + ".extra.ALLOW_IN_IDLE";
37     public static final String EXTRA_REQUIRE_NETWORK_ANY = PACKAGE_NAME
38             + ".extra.REQUIRE_NETWORK_ANY";
39     public static final String ACTION_SCHEDULE_JOB = PACKAGE_NAME + ".action.SCHEDULE_JOB";
40     public static final String ACTION_CANCEL_JOBS = PACKAGE_NAME + ".action.CANCEL_JOBS";
41     public static final int JOB_INITIAL_BACKOFF = 10_000;
42 
43     @Override
onReceive(Context context, Intent intent)44     public void onReceive(Context context, Intent intent) {
45         final ComponentName jobServiceComponent = new ComponentName(context, TestJobService.class);
46         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
47         switch (intent.getAction()) {
48             case ACTION_CANCEL_JOBS:
49                 jobScheduler.cancelAll();
50                 Log.d(TAG, "Cancelled all jobs for " + context.getPackageName());
51                 break;
52             case ACTION_SCHEDULE_JOB:
53                 final int jobId = intent.getIntExtra(EXTRA_JOB_ID_KEY, hashCode());
54                 final boolean allowInIdle = intent.getBooleanExtra(EXTRA_ALLOW_IN_IDLE, false);
55                 final boolean network = intent.getBooleanExtra(EXTRA_REQUIRE_NETWORK_ANY, false);
56                 JobInfo.Builder jobBuilder = new JobInfo.Builder(jobId, jobServiceComponent)
57                         .setBackoffCriteria(JOB_INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_LINEAR)
58                         .setOverrideDeadline(0)
59                         .setImportantWhileForeground(allowInIdle);
60                 if (network) {
61                     jobBuilder = jobBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
62                 }
63                 final int result = jobScheduler.schedule(jobBuilder.build());
64                 if (result != JobScheduler.RESULT_SUCCESS) {
65                     Log.e(TAG, "Could not schedule job " + jobId);
66                 } else {
67                     Log.d(TAG, "Successfully scheduled job with id " + jobId);
68                 }
69                 break;
70             default:
71                 Log.e(TAG, "Unknown action " + intent.getAction());
72         }
73     }
74 }