Home | History | Annotate | Download | only in jobtestapp
      1 /*
      2  * Copyright (C) 2018 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 ACTION_SCHEDULE_JOB = PACKAGE_NAME + ".action.SCHEDULE_JOB";
     38     public static final String ACTION_CANCEL_JOBS = PACKAGE_NAME + ".action.CANCEL_JOBS";
     39     public static final int JOB_INITIAL_BACKOFF = 10_000;
     40 
     41     @Override
     42     public void onReceive(Context context, Intent intent) {
     43         final ComponentName jobServiceComponent = new ComponentName(context, TestJobService.class);
     44         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
     45         switch (intent.getAction()) {
     46             case ACTION_CANCEL_JOBS:
     47                 jobScheduler.cancelAll();
     48                 Log.d(TAG, "Cancelled all jobs for " + context.getPackageName());
     49                 break;
     50             case ACTION_SCHEDULE_JOB:
     51                 final int jobId = intent.getIntExtra(EXTRA_JOB_ID_KEY, hashCode());
     52                 final boolean allowInIdle = intent.getBooleanExtra(EXTRA_ALLOW_IN_IDLE, false);
     53                 JobInfo.Builder jobBuilder = new JobInfo.Builder(jobId, jobServiceComponent)
     54                         .setBackoffCriteria(JOB_INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_LINEAR)
     55                         .setOverrideDeadline(0)
     56                         .setImportantWhileForeground(allowInIdle);
     57                 final int result = jobScheduler.schedule(jobBuilder.build());
     58                 if (result != JobScheduler.RESULT_SUCCESS) {
     59                     Log.e(TAG, "Could not schedule job " + jobId);
     60                 } else {
     61                     Log.d(TAG, "Successfully scheduled job with id " + jobId);
     62                 }
     63                 break;
     64             default:
     65                 Log.e(TAG, "Unknown action " + intent.getAction());
     66         }
     67     }
     68 }