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.annotation.TargetApi;
20 import android.app.job.JobParameters;
21 import android.app.job.JobService;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 @TargetApi(24)
26 public class TestJobService extends JobService {
27     private static final String TAG = TestJobService.class.getSimpleName();
28     private static final String PACKAGE_NAME = "com.android.servicestests.apps.jobtestapp";
29     public static final String ACTION_JOB_STARTED = PACKAGE_NAME + ".action.JOB_STARTED";
30     public static final String ACTION_JOB_STOPPED = PACKAGE_NAME + ".action.JOB_STOPPED";
31     public static final String JOB_PARAMS_EXTRA_KEY = PACKAGE_NAME + ".extra.JOB_PARAMETERS";
32 
33     @Override
onStartJob(JobParameters params)34     public boolean onStartJob(JobParameters params) {
35         Log.i(TAG, "Test job executing: " + params.getJobId());
36         Intent reportJobStartIntent = new Intent(ACTION_JOB_STARTED);
37         reportJobStartIntent.putExtra(JOB_PARAMS_EXTRA_KEY, params)
38                         .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
39         sendBroadcast(reportJobStartIntent);
40         return true;
41     }
42 
43     @Override
onStopJob(JobParameters params)44     public boolean onStopJob(JobParameters params) {
45         Log.i(TAG, "Test job stopped executing: " + params.getJobId());
46         Intent reportJobStopIntent = new Intent(ACTION_JOB_STOPPED);
47         reportJobStopIntent.putExtra(JOB_PARAMS_EXTRA_KEY, params)
48                         .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
49         sendBroadcast(reportJobStopIntent);
50         // Deadline constraint is dropped on reschedule, so it's more reliable to use a new job.
51         return false;
52     }
53 }
54