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