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