1 /*
2  * Copyright (C) 2019 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.car.testapi;
18 
19 import android.content.Context;
20 
21 import com.android.car.SystemActivityMonitoringService;
22 
23 /**
24  * This is fake implementation of the SystemActivityMonitoringService which is used by the
25  * {@link com.android.car.AppFocusService}. A faked version is needed for offline unit tests so that
26  * the foreground app ID can be changed manually.
27  *
28  * @hide
29  */
30 public class FakeSystemActivityMonitoringService extends SystemActivityMonitoringService {
31     private static final int DEFAULT_FOREGROUND_ID = -1;
32 
33     private int mForegroundPid = DEFAULT_FOREGROUND_ID;
34     private int mForegroundUid = DEFAULT_FOREGROUND_ID;
35 
FakeSystemActivityMonitoringService(Context context)36     public FakeSystemActivityMonitoringService(Context context) {
37         super(context);
38     }
39 
FakeSystemActivityMonitoringService()40     public FakeSystemActivityMonitoringService() {
41         this(/*context=*/ null);
42     }
43 
44     @Override
isInForeground(int pid, int uid)45     public boolean isInForeground(int pid, int uid) {
46         return (mForegroundPid == DEFAULT_FOREGROUND_ID || mForegroundPid == pid)
47                 && (mForegroundUid == DEFAULT_FOREGROUND_ID || mForegroundUid == uid);
48     }
49 
setForegroundPid(int pid)50     void setForegroundPid(int pid) {
51         mForegroundPid = pid;
52     }
53 
setForegroundUid(int uid)54     void setForegroundUid(int uid) {
55         mForegroundUid = uid;
56     }
57 
resetForegroundPid()58     void resetForegroundPid() {
59         mForegroundPid = DEFAULT_FOREGROUND_ID;
60     }
61 
resetForegroundUid()62     void resetForegroundUid() {
63         mForegroundUid = DEFAULT_FOREGROUND_ID;
64     }
65 }
66