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.server.cts.device.statsd;
18 
19 import android.annotation.TargetApi;
20 import android.app.job.JobInfo;
21 import android.app.job.JobParameters;
22 import android.app.job.JobScheduler;
23 import android.app.job.JobService;
24 import android.content.Context;
25 import android.os.Handler;
26 import android.util.Log;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * Handles callback from the framework {@link android.app.job.JobScheduler}.
33  * Runs a job for 0.5 seconds. Provides a countdown latch to wait on, by the test that schedules it.
34  */
35 @TargetApi(21)
36 public class StatsdJobService extends JobService {
37   private static final String TAG = "AtomTestsJobService";
38 
39   JobInfo mRunningJobInfo;
40   JobParameters mRunningParams;
41   private static CountDownLatch sLatch;
42 
43   final Handler mHandler = new Handler();
44   final Runnable mWorker = new Runnable() {
45     @Override public void run() {
46       try {
47         Thread.sleep(500);
48       } catch (InterruptedException e) {
49       }
50       jobFinished(mRunningParams, false);
51       if (sLatch != null) {
52         sLatch.countDown();
53       }
54     }
55   };
56 
resetCountDownLatch()57   public static synchronized CountDownLatch resetCountDownLatch() {
58     sLatch = new CountDownLatch(1);
59     return sLatch;
60   }
61 
62   @Override
onCreate()63   public void onCreate() {
64     super.onCreate();
65   }
66 
67   @Override
onStartJob(JobParameters params)68   public boolean onStartJob(JobParameters params) {
69     mRunningParams = params;
70     mHandler.post(mWorker);
71     return true;
72   }
73 
74   @Override
onStopJob(JobParameters params)75   public boolean onStopJob(JobParameters params) {
76     return false;
77   }
78 }