1 /* 2 * Copyright (C) 2018 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.car.notification; 18 19 import android.car.CarNotConnectedException; 20 import android.car.drivingstate.CarUxRestrictions; 21 import android.car.drivingstate.CarUxRestrictionsManager; 22 import android.car.drivingstate.CarUxRestrictionsManager.OnUxRestrictionsChangedListener; 23 import android.util.Log; 24 25 /** 26 * Used as a proxy and delegator of Ux Restriction state changes to the 27 * {@link CarUxRestrictionsManager} due to the fact that {@link CarUxRestrictionsManager} can only 28 * have one registered listener. 29 * <p> 30 * This was written not to be a general solution as that responsibility should be in an future api 31 * change of the {@link CarUxRestrictionsManager}. 32 * This class uses setter inject due to the current nature of the asynchronous depenancy creation 33 * when using the Car api 34 */ 35 public class CarUxRestrictionManagerWrapper implements OnUxRestrictionsChangedListener { 36 private static final String TAG = "CarUxRestrictionManager"; 37 38 private CarNotificationView mCarNotificationView; 39 private CarHeadsUpNotificationManager mCarHeadsUpNotificationManager; 40 private CarUxRestrictionsManager mCarUxRestrictionsManager; 41 42 /** 43 * Forwards state change to {@link CarHeadsUpNotificationManager} and 44 * {@link CarNotificationView} if they've been set. 45 * 46 * @param restrictionInfo The latest restiction state 47 */ 48 @Override onUxRestrictionsChanged(CarUxRestrictions restrictionInfo)49 public void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo) { 50 // This is done in this manner (and not a list) to give the next person pause 51 // and check if the implementation of CarUxRestrictionsManager has been updated to handle 52 // multiple listeners and delete this class if so. 53 if (mCarHeadsUpNotificationManager != null) { 54 mCarHeadsUpNotificationManager.onUxRestrictionsChanged(restrictionInfo); 55 } 56 if (mCarNotificationView != null) { 57 mCarNotificationView.onUxRestrictionsChanged(restrictionInfo); 58 } 59 } 60 61 /** 62 * Set the {@link CarNotificationView} that should be notified on ux restriction state changes 63 * 64 * @param carNotificationView {@code null} to turn off notifications 65 */ setCarNotificationView(CarNotificationView carNotificationView)66 public void setCarNotificationView(CarNotificationView carNotificationView) { 67 mCarNotificationView = carNotificationView; 68 } 69 70 /** 71 * set the {@link CarHeadsUpNotificationManager} that should be notified on ux restriction 72 * state changes 73 * 74 * @param carHeadsUpNotificationManager {@code null} to turn off notifications 75 */ setCarHeadsUpNotificationManager( CarHeadsUpNotificationManager carHeadsUpNotificationManager)76 public void setCarHeadsUpNotificationManager( 77 CarHeadsUpNotificationManager carHeadsUpNotificationManager) { 78 mCarHeadsUpNotificationManager = carHeadsUpNotificationManager; 79 } 80 81 /** 82 * Set the {@link CarUxRestrictionsManager} that will be used as the source of data. The 83 * setter self registers as a listener as it's expected to be called from a connection listener 84 * when a connection to the car api is established. 85 * 86 * @param carUxRestrictionsManager The CarUxRestrictionsManager to proxy to 87 */ setCarUxRestrictionsManager( CarUxRestrictionsManager carUxRestrictionsManager)88 public void setCarUxRestrictionsManager( 89 CarUxRestrictionsManager carUxRestrictionsManager) { 90 mCarUxRestrictionsManager = carUxRestrictionsManager; 91 try { 92 mCarUxRestrictionsManager.registerListener(this); 93 } catch (CarNotConnectedException e) { 94 Log.w(TAG, "Failed to register for ux restiction changes ",e); 95 } 96 } 97 98 /** 99 * Proxy to the same call on CarUxRestrictionsManager 100 * 101 * @return CarUxRestrictions The current restictions 102 * @throws CarNotConnectedException Thrown if the Car service is unavailable 103 */ getCurrentCarUxRestrictions()104 public CarUxRestrictions getCurrentCarUxRestrictions() throws CarNotConnectedException { 105 if (mCarUxRestrictionsManager == null) { 106 throw new CarNotConnectedException(); 107 } 108 return mCarUxRestrictionsManager.getCurrentCarUxRestrictions(); 109 } 110 } 111