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 static android.car.drivingstate.CarUxRestrictions.UX_RESTRICTIONS_BASELINE;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.car.drivingstate.CarUxRestrictionsConfiguration;
23 import android.car.drivingstate.ICarUxRestrictionsChangeListener;
24 import android.car.drivingstate.ICarUxRestrictionsManager;
25 import android.os.IRemoteCallback;
26 import android.os.RemoteException;
27 import android.os.SystemClock;
28 
29 import com.android.internal.annotations.GuardedBy;
30 
31 import java.util.Collections;
32 import java.util.List;
33 
34 /**
35  * A fake implementation of {@link ICarUxRestrictionsManager.Stub} to facilitate the use of {@link
36  * android.car.drivingstate.CarUxRestrictionsManager} in external unit tests.
37  *
38  * @hide
39  */
40 public class FakeCarUxRestrictionsService extends ICarUxRestrictionsManager.Stub implements
41         CarUxRestrictionsController {
42     private final Object mLock = new Object();
43 
44     @GuardedBy("mLock")
45     private CarUxRestrictions mCarUxRestrictions;
46     @GuardedBy("mLock")
47     private ICarUxRestrictionsChangeListener mListener;
48 
49     @GuardedBy("mLock")
50     private String mMode = "baseline";
51 
createCarUxRestrictions(int activeRestrictions)52     private static CarUxRestrictions createCarUxRestrictions(int activeRestrictions) {
53         return new CarUxRestrictions.Builder(
54                 false, /* requires driving distraction optimization */
55                 activeRestrictions,
56                 SystemClock.elapsedRealtimeNanos())
57                 .build();
58     }
59 
FakeCarUxRestrictionsService()60     FakeCarUxRestrictionsService() {
61         synchronized (mLock) {
62             mCarUxRestrictions = createCarUxRestrictions(UX_RESTRICTIONS_BASELINE);
63         }
64     }
65 
66     @Override
registerUxRestrictionsChangeListener( ICarUxRestrictionsChangeListener listener, int displayId)67     public void registerUxRestrictionsChangeListener(
68             ICarUxRestrictionsChangeListener listener, int displayId) {
69         synchronized (mLock) {
70             this.mListener = listener;
71         }
72     }
73 
74     @Override
unregisterUxRestrictionsChangeListener(ICarUxRestrictionsChangeListener listener)75     public void unregisterUxRestrictionsChangeListener(ICarUxRestrictionsChangeListener listener) {
76         synchronized (mLock) {
77             this.mListener = null;
78         }
79     }
80 
81     @Override
getCurrentUxRestrictions(int displayId)82     public CarUxRestrictions getCurrentUxRestrictions(int displayId) {
83         synchronized (mLock) {
84             return mCarUxRestrictions;
85         }
86     }
87 
88     @Override
getStagedConfigs()89     public List<CarUxRestrictionsConfiguration> getStagedConfigs() {
90         return Collections.emptyList();
91     }
92 
93     @Override
getConfigs()94     public List<CarUxRestrictionsConfiguration> getConfigs() {
95         return Collections.emptyList();
96     }
97 
98     @Override
setRestrictionMode(String mode)99     public boolean setRestrictionMode(String mode) throws RemoteException {
100         synchronized (mLock) {
101             mMode = mode;
102         }
103         return true;
104     }
105 
106     @Override
getRestrictionMode()107     public String getRestrictionMode() throws RemoteException {
108         synchronized (mLock) {
109             return mMode;
110         }
111     }
112 
113     @Override
reportVirtualDisplayToPhysicalDisplay(IRemoteCallback binder, int virtualDisplayId, int physicalDisplayId)114     public void reportVirtualDisplayToPhysicalDisplay(IRemoteCallback binder, int virtualDisplayId,
115             int physicalDisplayId) throws RemoteException {
116 
117     }
118 
119     @Override
getMappedPhysicalDisplayOfVirtualDisplay(int displayId)120     public int getMappedPhysicalDisplayOfVirtualDisplay(int displayId) throws RemoteException {
121         return 0;
122     }
123 
124     @Override
saveUxRestrictionsConfigurationForNextBoot( List<CarUxRestrictionsConfiguration> config)125     public boolean saveUxRestrictionsConfigurationForNextBoot(
126             List<CarUxRestrictionsConfiguration> config) {
127         return true;
128     }
129 
130     /**************************** CarUxRestrictionsController impl ********************************/
131 
132     @Override
setUxRestrictions(int restrictions)133     public void setUxRestrictions(int restrictions) throws RemoteException {
134         synchronized (mLock) {
135             mCarUxRestrictions = createCarUxRestrictions(restrictions);
136             if (isListenerRegistered()) {
137                 mListener.onUxRestrictionsChanged(mCarUxRestrictions);
138             }
139         }
140     }
141 
142     @Override
clearUxRestrictions()143     public void clearUxRestrictions() throws RemoteException {
144         setUxRestrictions(0);
145     }
146 
147     @Override
isListenerRegistered()148     public boolean isListenerRegistered() {
149         synchronized (mLock) {
150             return mListener != null;
151         }
152     }
153 
154 }
155