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 com.android.servicestests.apps.jobtestapp;
18 
19 import android.app.Activity;
20 import android.app.job.JobInfo;
21 import android.app.job.JobScheduler;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 public class TestJobActivity extends Activity {
29     private static final String TAG = TestJobActivity.class.getSimpleName();
30     private static final String PACKAGE_NAME = "com.android.servicestests.apps.jobtestapp";
31 
32     public static final String EXTRA_JOB_ID_KEY = PACKAGE_NAME + ".extra.JOB_ID";
33     public static final String ACTION_START_JOB = PACKAGE_NAME + ".action.START_JOB";
34     public static final String ACTION_CANCEL_JOBS = PACKAGE_NAME + ".action.CANCEL_JOBS";
35     public static final int JOB_INITIAL_BACKOFF = 10_000;
36     public static final int JOB_MINIMUM_LATENCY = 5_000;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         ComponentName jobServiceComponent = new ComponentName(this, TestJobService.class);
42         JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
43         final Intent intent = getIntent();
44         switch (intent.getAction()) {
45             case ACTION_CANCEL_JOBS:
46                 jobScheduler.cancelAll();
47                 Log.d(TAG, "Cancelled all jobs for " + getPackageName());
48                 break;
49             case ACTION_START_JOB:
50                 final int jobId = intent.getIntExtra(EXTRA_JOB_ID_KEY, hashCode());
51                 JobInfo.Builder jobBuilder = new JobInfo.Builder(jobId, jobServiceComponent)
52                         .setBackoffCriteria(JOB_INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_LINEAR)
53                         .setMinimumLatency(JOB_MINIMUM_LATENCY)
54                         .setOverrideDeadline(JOB_MINIMUM_LATENCY);
55                 final int result = jobScheduler.schedule(jobBuilder.build());
56                 if (result != JobScheduler.RESULT_SUCCESS) {
57                     Log.e(TAG, "Could not schedule job " + jobId);
58                 } else {
59                     Log.d(TAG, "Successfully scheduled job with id " + jobId);
60                 }
61                 break;
62             default:
63                 Log.e(TAG, "Unknown action " + intent.getAction());
64         }
65         finish();
66     }
67 }