1 /* 2 * Copyright (C) 2020 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.systemui.car.systembar; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.view.ViewGroup; 22 23 import androidx.annotation.Nullable; 24 25 import com.android.systemui.car.hvac.HvacController; 26 import com.android.systemui.car.statusbar.UserNameViewController; 27 import com.android.systemui.dagger.SysUISingleton; 28 29 import javax.inject.Inject; 30 31 import dagger.Lazy; 32 33 /** A single class which controls the navigation bar views. */ 34 @SysUISingleton 35 public class CarSystemBarController { 36 37 private final Context mContext; 38 private final CarSystemBarViewFactory mCarSystemBarViewFactory; 39 private final ButtonSelectionStateController mButtonSelectionStateController; 40 private final ButtonRoleHolderController mButtonRoleHolderController; 41 private final Lazy<HvacController> mHvacControllerLazy; 42 private final Lazy<UserNameViewController> mUserNameViewControllerLazy; 43 private final Lazy<PrivacyChipViewController> mPrivacyChipViewControllerLazy; 44 45 private boolean mShowTop; 46 private boolean mShowBottom; 47 private boolean mShowLeft; 48 private boolean mShowRight; 49 50 private View.OnTouchListener mTopBarTouchListener; 51 private View.OnTouchListener mBottomBarTouchListener; 52 private View.OnTouchListener mLeftBarTouchListener; 53 private View.OnTouchListener mRightBarTouchListener; 54 private NotificationsShadeController mNotificationsShadeController; 55 56 private CarSystemBarView mTopView; 57 private CarSystemBarView mBottomView; 58 private CarSystemBarView mLeftView; 59 private CarSystemBarView mRightView; 60 61 @Inject CarSystemBarController(Context context, CarSystemBarViewFactory carSystemBarViewFactory, ButtonSelectionStateController buttonSelectionStateController, Lazy<HvacController> hvacControllerLazy, Lazy<UserNameViewController> userNameViewControllerLazy, Lazy<PrivacyChipViewController> privacyChipViewControllerLazy, ButtonRoleHolderController buttonRoleHolderController, SystemBarConfigs systemBarConfigs)62 public CarSystemBarController(Context context, 63 CarSystemBarViewFactory carSystemBarViewFactory, 64 ButtonSelectionStateController buttonSelectionStateController, 65 Lazy<HvacController> hvacControllerLazy, 66 Lazy<UserNameViewController> userNameViewControllerLazy, 67 Lazy<PrivacyChipViewController> privacyChipViewControllerLazy, 68 ButtonRoleHolderController buttonRoleHolderController, 69 SystemBarConfigs systemBarConfigs) { 70 mContext = context; 71 mCarSystemBarViewFactory = carSystemBarViewFactory; 72 mButtonSelectionStateController = buttonSelectionStateController; 73 mHvacControllerLazy = hvacControllerLazy; 74 mUserNameViewControllerLazy = userNameViewControllerLazy; 75 mPrivacyChipViewControllerLazy = privacyChipViewControllerLazy; 76 mButtonRoleHolderController = buttonRoleHolderController; 77 78 // Read configuration. 79 mShowTop = systemBarConfigs.getEnabledStatusBySide(SystemBarConfigs.TOP); 80 mShowBottom = systemBarConfigs.getEnabledStatusBySide(SystemBarConfigs.BOTTOM); 81 mShowLeft = systemBarConfigs.getEnabledStatusBySide(SystemBarConfigs.LEFT); 82 mShowRight = systemBarConfigs.getEnabledStatusBySide(SystemBarConfigs.RIGHT); 83 } 84 85 /** 86 * Hides all system bars. 87 */ hideBars()88 public void hideBars() { 89 setTopWindowVisibility(View.GONE); 90 setBottomWindowVisibility(View.GONE); 91 setLeftWindowVisibility(View.GONE); 92 setRightWindowVisibility(View.GONE); 93 } 94 95 /** 96 * Shows all system bars. 97 */ showBars()98 public void showBars() { 99 setTopWindowVisibility(View.VISIBLE); 100 setBottomWindowVisibility(View.VISIBLE); 101 setLeftWindowVisibility(View.VISIBLE); 102 setRightWindowVisibility(View.VISIBLE); 103 } 104 105 /** Connect to hvac service. */ connectToHvac()106 public void connectToHvac() { 107 mHvacControllerLazy.get().connectToCarService(); 108 } 109 110 /** Clean up */ removeAll()111 public void removeAll() { 112 mHvacControllerLazy.get().removeAllComponents(); 113 mButtonSelectionStateController.removeAll(); 114 mButtonRoleHolderController.removeAll(); 115 mUserNameViewControllerLazy.get().removeAll(); 116 mPrivacyChipViewControllerLazy.get().removeAll(); 117 } 118 119 /** Gets the top window if configured to do so. */ 120 @Nullable getTopWindow()121 public ViewGroup getTopWindow() { 122 return mShowTop ? mCarSystemBarViewFactory.getTopWindow() : null; 123 } 124 125 /** Gets the bottom window if configured to do so. */ 126 @Nullable getBottomWindow()127 public ViewGroup getBottomWindow() { 128 return mShowBottom ? mCarSystemBarViewFactory.getBottomWindow() : null; 129 } 130 131 /** Gets the left window if configured to do so. */ 132 @Nullable getLeftWindow()133 public ViewGroup getLeftWindow() { 134 return mShowLeft ? mCarSystemBarViewFactory.getLeftWindow() : null; 135 } 136 137 /** Gets the right window if configured to do so. */ 138 @Nullable getRightWindow()139 public ViewGroup getRightWindow() { 140 return mShowRight ? mCarSystemBarViewFactory.getRightWindow() : null; 141 } 142 143 /** Toggles the top nav bar visibility. */ setTopWindowVisibility(@iew.Visibility int visibility)144 public boolean setTopWindowVisibility(@View.Visibility int visibility) { 145 return setWindowVisibility(getTopWindow(), visibility); 146 } 147 148 /** Toggles the bottom nav bar visibility. */ setBottomWindowVisibility(@iew.Visibility int visibility)149 public boolean setBottomWindowVisibility(@View.Visibility int visibility) { 150 return setWindowVisibility(getBottomWindow(), visibility); 151 } 152 153 /** Toggles the left nav bar visibility. */ setLeftWindowVisibility(@iew.Visibility int visibility)154 public boolean setLeftWindowVisibility(@View.Visibility int visibility) { 155 return setWindowVisibility(getLeftWindow(), visibility); 156 } 157 158 /** Toggles the right nav bar visibility. */ setRightWindowVisibility(@iew.Visibility int visibility)159 public boolean setRightWindowVisibility(@View.Visibility int visibility) { 160 return setWindowVisibility(getRightWindow(), visibility); 161 } 162 setWindowVisibility(ViewGroup window, @View.Visibility int visibility)163 private boolean setWindowVisibility(ViewGroup window, @View.Visibility int visibility) { 164 if (window == null) { 165 return false; 166 } 167 168 if (window.getVisibility() == visibility) { 169 return false; 170 } 171 172 window.setVisibility(visibility); 173 return true; 174 } 175 176 /** Gets the top navigation bar with the appropriate listeners set. */ 177 @Nullable getTopBar(boolean isSetUp)178 public CarSystemBarView getTopBar(boolean isSetUp) { 179 if (!mShowTop) { 180 return null; 181 } 182 183 mTopView = mCarSystemBarViewFactory.getTopBar(isSetUp); 184 setupBar(mTopView, mTopBarTouchListener, mNotificationsShadeController); 185 return mTopView; 186 } 187 188 /** Gets the bottom navigation bar with the appropriate listeners set. */ 189 @Nullable getBottomBar(boolean isSetUp)190 public CarSystemBarView getBottomBar(boolean isSetUp) { 191 if (!mShowBottom) { 192 return null; 193 } 194 195 mBottomView = mCarSystemBarViewFactory.getBottomBar(isSetUp); 196 setupBar(mBottomView, mBottomBarTouchListener, mNotificationsShadeController); 197 return mBottomView; 198 } 199 200 /** Gets the left navigation bar with the appropriate listeners set. */ 201 @Nullable getLeftBar(boolean isSetUp)202 public CarSystemBarView getLeftBar(boolean isSetUp) { 203 if (!mShowLeft) { 204 return null; 205 } 206 207 mLeftView = mCarSystemBarViewFactory.getLeftBar(isSetUp); 208 setupBar(mLeftView, mLeftBarTouchListener, mNotificationsShadeController); 209 return mLeftView; 210 } 211 212 /** Gets the right navigation bar with the appropriate listeners set. */ 213 @Nullable getRightBar(boolean isSetUp)214 public CarSystemBarView getRightBar(boolean isSetUp) { 215 if (!mShowRight) { 216 return null; 217 } 218 219 mRightView = mCarSystemBarViewFactory.getRightBar(isSetUp); 220 setupBar(mRightView, mRightBarTouchListener, mNotificationsShadeController); 221 return mRightView; 222 } 223 setupBar(CarSystemBarView view, View.OnTouchListener statusBarTouchListener, NotificationsShadeController notifShadeController)224 private void setupBar(CarSystemBarView view, View.OnTouchListener statusBarTouchListener, 225 NotificationsShadeController notifShadeController) { 226 view.setStatusBarWindowTouchListener(statusBarTouchListener); 227 view.setNotificationsPanelController(notifShadeController); 228 mButtonSelectionStateController.addAllButtonsWithSelectionState(view); 229 mButtonRoleHolderController.addAllButtonsWithRoleName(view); 230 mHvacControllerLazy.get().addTemperatureViewToController(view); 231 mUserNameViewControllerLazy.get().addUserNameView(view); 232 mPrivacyChipViewControllerLazy.get().addPrivacyChipView(view); 233 } 234 235 /** Sets a touch listener for the top navigation bar. */ registerTopBarTouchListener(View.OnTouchListener listener)236 public void registerTopBarTouchListener(View.OnTouchListener listener) { 237 mTopBarTouchListener = listener; 238 if (mTopView != null) { 239 mTopView.setStatusBarWindowTouchListener(mTopBarTouchListener); 240 } 241 } 242 243 /** Sets a touch listener for the bottom navigation bar. */ registerBottomBarTouchListener(View.OnTouchListener listener)244 public void registerBottomBarTouchListener(View.OnTouchListener listener) { 245 mBottomBarTouchListener = listener; 246 if (mBottomView != null) { 247 mBottomView.setStatusBarWindowTouchListener(mBottomBarTouchListener); 248 } 249 } 250 251 /** Sets a touch listener for the left navigation bar. */ registerLeftBarTouchListener(View.OnTouchListener listener)252 public void registerLeftBarTouchListener(View.OnTouchListener listener) { 253 mLeftBarTouchListener = listener; 254 if (mLeftView != null) { 255 mLeftView.setStatusBarWindowTouchListener(mLeftBarTouchListener); 256 } 257 } 258 259 /** Sets a touch listener for the right navigation bar. */ registerRightBarTouchListener(View.OnTouchListener listener)260 public void registerRightBarTouchListener(View.OnTouchListener listener) { 261 mRightBarTouchListener = listener; 262 if (mRightView != null) { 263 mRightView.setStatusBarWindowTouchListener(mRightBarTouchListener); 264 } 265 } 266 267 /** Sets a notification controller which toggles the notification panel. */ registerNotificationController( NotificationsShadeController notificationsShadeController)268 public void registerNotificationController( 269 NotificationsShadeController notificationsShadeController) { 270 mNotificationsShadeController = notificationsShadeController; 271 if (mTopView != null) { 272 mTopView.setNotificationsPanelController(mNotificationsShadeController); 273 } 274 if (mBottomView != null) { 275 mBottomView.setNotificationsPanelController(mNotificationsShadeController); 276 } 277 if (mLeftView != null) { 278 mLeftView.setNotificationsPanelController(mNotificationsShadeController); 279 } 280 if (mRightView != null) { 281 mRightView.setNotificationsPanelController(mNotificationsShadeController); 282 } 283 } 284 285 /** 286 * Shows all of the navigation buttons on the valid instances of 287 * {@link CarSystemBarView}. 288 */ showAllNavigationButtons(boolean isSetUp)289 public void showAllNavigationButtons(boolean isSetUp) { 290 checkAllBars(isSetUp); 291 if (mTopView != null) { 292 mTopView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_NAVIGATION); 293 } 294 if (mBottomView != null) { 295 mBottomView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_NAVIGATION); 296 } 297 if (mLeftView != null) { 298 mLeftView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_NAVIGATION); 299 } 300 if (mRightView != null) { 301 mRightView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_NAVIGATION); 302 } 303 } 304 305 /** 306 * Shows all of the keyguard specific buttons on the valid instances of 307 * {@link CarSystemBarView}. 308 */ showAllKeyguardButtons(boolean isSetUp)309 public void showAllKeyguardButtons(boolean isSetUp) { 310 checkAllBars(isSetUp); 311 if (mTopView != null) { 312 mTopView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_KEYGUARD); 313 } 314 if (mBottomView != null) { 315 mBottomView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_KEYGUARD); 316 } 317 if (mLeftView != null) { 318 mLeftView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_KEYGUARD); 319 } 320 if (mRightView != null) { 321 mRightView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_KEYGUARD); 322 } 323 } 324 325 /** 326 * Shows all of the occlusion state buttons on the valid instances of 327 * {@link CarSystemBarView}. 328 */ showAllOcclusionButtons(boolean isSetUp)329 public void showAllOcclusionButtons(boolean isSetUp) { 330 checkAllBars(isSetUp); 331 if (mTopView != null) { 332 mTopView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_OCCLUSION); 333 } 334 if (mBottomView != null) { 335 mBottomView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_OCCLUSION); 336 } 337 if (mLeftView != null) { 338 mLeftView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_OCCLUSION); 339 } 340 if (mRightView != null) { 341 mRightView.showButtonsOfType(CarSystemBarView.BUTTON_TYPE_OCCLUSION); 342 } 343 } 344 345 /** Toggles whether the notifications icon has an unseen indicator or not. */ toggleAllNotificationsUnseenIndicator(boolean isSetUp, boolean hasUnseen)346 public void toggleAllNotificationsUnseenIndicator(boolean isSetUp, boolean hasUnseen) { 347 checkAllBars(isSetUp); 348 if (mTopView != null) { 349 mTopView.toggleNotificationUnseenIndicator(hasUnseen); 350 } 351 if (mBottomView != null) { 352 mBottomView.toggleNotificationUnseenIndicator(hasUnseen); 353 } 354 if (mLeftView != null) { 355 mLeftView.toggleNotificationUnseenIndicator(hasUnseen); 356 } 357 if (mRightView != null) { 358 mRightView.toggleNotificationUnseenIndicator(hasUnseen); 359 } 360 } 361 362 /** Interface for controlling the notifications shade. */ 363 public interface NotificationsShadeController { 364 /** Toggles the visibility of the notifications shade. */ togglePanel()365 void togglePanel(); 366 367 /** Returns {@code true} if the panel is open. */ isNotificationPanelOpen()368 boolean isNotificationPanelOpen(); 369 } 370 checkAllBars(boolean isSetUp)371 private void checkAllBars(boolean isSetUp) { 372 mTopView = getTopBar(isSetUp); 373 mBottomView = getBottomBar(isSetUp); 374 mLeftView = getLeftBar(isSetUp); 375 mRightView = getRightBar(isSetUp); 376 } 377 } 378