1 /*
2  * Copyright (C) 2015 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.job;
18 
19 import android.annotation.TargetApi;
20 import android.app.job.JobParameters;
21 import android.app.job.JobService;
22 import android.util.Log;
23 
24 import java.util.ArrayList;
25 
26 @TargetApi(24)
27 public class MockPriorityJobService extends JobService {
28     private static final String TAG = "MockPriorityJobService";
29 
30     @Override
onCreate()31     public void onCreate() {
32         super.onCreate();
33         Log.e(TAG, "Created test service.");
34     }
35 
36     @Override
onStartJob(JobParameters params)37     public boolean onStartJob(JobParameters params) {
38         Log.i(TAG, "Test job executing: " + params.getJobId());
39         TestEnvironment.getTestEnvironment().executedEvents.add(
40                 new TestEnvironment.Event(TestEnvironment.EVENT_START_JOB, params.getJobId()));
41         return true;  // Job not finished
42     }
43 
44     @Override
onStopJob(JobParameters params)45     public boolean onStopJob(JobParameters params) {
46         Log.i(TAG, "Test job stop executing: " + params.getJobId());
47         int reason = params.getStopReason();
48         int event = TestEnvironment.EVENT_STOP_JOB;
49         Log.d(TAG, "stop reason: " + String.valueOf(reason));
50         if (reason == JobParameters.REASON_PREEMPT) {
51             event = TestEnvironment.EVENT_PREEMPT_JOB;
52             Log.d(TAG, "preempted " + String.valueOf(params.getJobId()));
53         }
54         TestEnvironment.getTestEnvironment().executedEvents
55                 .add(new TestEnvironment.Event(event, params.getJobId()));
56         return false;  // Do not reschedule
57     }
58 
59     public static class TestEnvironment {
60 
61         public static final int EVENT_START_JOB = 0;
62         public static final int EVENT_PREEMPT_JOB = 1;
63         public static final int EVENT_STOP_JOB = 2;
64 
65         private static TestEnvironment kTestEnvironment;
66 
67         private ArrayList<Event> executedEvents = new ArrayList<Event>();
68 
getTestEnvironment()69         public static TestEnvironment getTestEnvironment() {
70             if (kTestEnvironment == null) {
71                 kTestEnvironment = new TestEnvironment();
72             }
73             return kTestEnvironment;
74         }
75 
76         public static class Event {
77             public int event;
78             public int jobId;
79 
Event()80             public Event() {
81             }
82 
Event(int event, int jobId)83             public Event(int event, int jobId) {
84                 this.event = event;
85                 this.jobId = jobId;
86             }
87 
88             @Override
equals(Object other)89             public boolean equals(Object other) {
90                 if (other instanceof Event) {
91                     Event otherEvent = (Event) other;
92                     return otherEvent.event == event && otherEvent.jobId == jobId;
93                 }
94                 return false;
95             }
96         }
97 
setUp()98         public void setUp() {
99             executedEvents.clear();
100         }
101 
getExecutedEvents()102         public ArrayList<Event> getExecutedEvents() {
103             return executedEvents;
104         }
105     }
106 }
107