1 /*
2  * Copyright (C) 2021 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.app.usage.cts;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobParameters;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobService;
23 import android.content.ComponentName;
24 import android.content.Context;
25 
26 import java.util.function.BooleanSupplier;
27 
28 public final class TestJob extends JobService {
29 
30     public static final int TEST_JOB_ID = 1;
31     public static final String NOTIFICATION_CHANNEL_ID = TestJob.class.getSimpleName();
32     private static boolean sJobStarted;
33     public static BooleanSupplier hasJobStarted = new BooleanSupplier() {
34         @Override
35         public boolean getAsBoolean() {
36             return sJobStarted;
37         }
38     };
39 
40     @Override
onStartJob(JobParameters params)41     public boolean onStartJob(JobParameters params) {
42         sJobStarted = true;
43         return false;
44     }
45 
46     @Override
onStopJob(JobParameters params)47     public boolean onStopJob(JobParameters params) {
48         return false;
49     }
50 
schedule(Context context)51     public static void schedule(Context context) {
52         sJobStarted = false;
53         JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
54         ComponentName componentName = new ComponentName(context, TestJob.class);
55 
56         JobInfo jobInfo = new JobInfo.Builder(TEST_JOB_ID, componentName).build();
57         jobScheduler.schedule(jobInfo);
58     }
59 }
60