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.notification; 18 19 import static com.android.systemui.car.systembar.CarSystemBarController.BOTTOM; 20 import static com.android.systemui.car.systembar.CarSystemBarController.LEFT; 21 import static com.android.systemui.car.systembar.CarSystemBarController.RIGHT; 22 import static com.android.systemui.car.systembar.CarSystemBarController.TOP; 23 24 import android.car.hardware.power.CarPowerManager; 25 import android.content.BroadcastReceiver; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.content.res.Configuration; 30 import android.util.Log; 31 32 import androidx.annotation.CallSuper; 33 import androidx.annotation.NonNull; 34 35 import com.android.systemui.broadcast.BroadcastDispatcher; 36 import com.android.systemui.car.systembar.CarSystemBarController; 37 import com.android.systemui.car.window.OverlayViewMediator; 38 import com.android.systemui.dagger.SysUISingleton; 39 import com.android.systemui.settings.UserTracker; 40 import com.android.systemui.statusbar.policy.ConfigurationController; 41 42 import javax.inject.Inject; 43 44 /** 45 * The view mediator which attaches the view controller to other elements of the system ui. Disables 46 * drag open behavior of the notification panel from any navigation bar. 47 */ 48 @SysUISingleton 49 public class NotificationPanelViewMediator implements OverlayViewMediator, 50 ConfigurationController.ConfigurationListener { 51 52 private static final boolean DEBUG = false; 53 private static final String TAG = "NotificationPanelVM"; 54 55 private final Context mContext; 56 private final CarSystemBarController mCarSystemBarController; 57 private final NotificationPanelViewController mNotificationPanelViewController; 58 private final PowerManagerHelper mPowerManagerHelper; 59 private final BroadcastDispatcher mBroadcastDispatcher; 60 private final UserTracker mUserTracker; 61 private final ConfigurationController mConfigurationController; 62 63 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 64 @Override 65 public void onReceive(Context context, Intent intent) { 66 if (DEBUG) Log.v(TAG, "onReceive: " + intent); 67 String action = intent.getAction(); 68 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { 69 if (mNotificationPanelViewController.isPanelExpanded()) { 70 mNotificationPanelViewController.toggle(); 71 } 72 } 73 } 74 }; 75 76 private final UserTracker.Callback mUserTrackerCallback = new UserTracker.Callback() { 77 @Override 78 public void onUserChanged(int newUser, Context userContext) { 79 mBroadcastDispatcher.unregisterReceiver(mBroadcastReceiver); 80 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, 81 new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS), /* executor= */ null, 82 mUserTracker.getUserHandle()); 83 } 84 85 @Override 86 public void onUserChanging(int newUser, @NonNull Context userContext) { 87 mNotificationPanelViewController.clearCache(); 88 } 89 }; 90 91 private boolean mIsUiModeNight; 92 93 @Inject NotificationPanelViewMediator( Context context, CarSystemBarController carSystemBarController, NotificationPanelViewController notificationPanelViewController, PowerManagerHelper powerManagerHelper, BroadcastDispatcher broadcastDispatcher, UserTracker userTracker, ConfigurationController configurationController)94 public NotificationPanelViewMediator( 95 Context context, 96 CarSystemBarController carSystemBarController, 97 NotificationPanelViewController notificationPanelViewController, 98 PowerManagerHelper powerManagerHelper, 99 BroadcastDispatcher broadcastDispatcher, 100 UserTracker userTracker, 101 ConfigurationController configurationController) { 102 mContext = context; 103 mCarSystemBarController = carSystemBarController; 104 mNotificationPanelViewController = notificationPanelViewController; 105 mPowerManagerHelper = powerManagerHelper; 106 mBroadcastDispatcher = broadcastDispatcher; 107 mUserTracker = userTracker; 108 mConfigurationController = configurationController; 109 } 110 111 @Override 112 @CallSuper registerListeners()113 public void registerListeners() { 114 registerTopBarTouchListener(); 115 registerBottomBarTouchListener(); 116 registerLeftBarTouchListener(); 117 registerRightBarTouchListener(); 118 119 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, 120 new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS), null, 121 mUserTracker.getUserHandle()); 122 mUserTracker.addCallback(mUserTrackerCallback, mContext.getMainExecutor()); 123 } 124 125 @Override setUpOverlayContentViewControllers()126 public void setUpOverlayContentViewControllers() { 127 mPowerManagerHelper.setCarPowerStateListener(state -> { 128 if (state == CarPowerManager.STATE_ON) { 129 mNotificationPanelViewController.onCarPowerStateOn(); 130 } 131 }); 132 mPowerManagerHelper.connectToCarService(); 133 134 mConfigurationController.addCallback(this); 135 } 136 137 @Override onConfigChanged(Configuration newConfig)138 public void onConfigChanged(Configuration newConfig) { 139 boolean isConfigNightMode = newConfig.isNightModeActive(); 140 // Only refresh UI on Night mode changes 141 if (isConfigNightMode != mIsUiModeNight) { 142 mIsUiModeNight = isConfigNightMode; 143 mNotificationPanelViewController.reinflate(); 144 registerListeners(); 145 } 146 } 147 148 @Override onDensityOrFontScaleChanged()149 public void onDensityOrFontScaleChanged() { 150 registerListeners(); 151 } 152 153 @Override onUiModeChanged()154 public void onUiModeChanged() { 155 // No op. 156 } 157 158 @Override onThemeChanged()159 public void onThemeChanged() { 160 // No op. 161 } 162 163 @Override onLocaleListChanged()164 public void onLocaleListChanged() { 165 mNotificationPanelViewController.reinflate(); 166 registerListeners(); 167 } 168 registerTopBarTouchListener()169 protected void registerTopBarTouchListener() { 170 mCarSystemBarController.registerBarTouchListener(TOP, 171 mNotificationPanelViewController.getDragCloseTouchListener()); 172 } 173 registerBottomBarTouchListener()174 protected void registerBottomBarTouchListener() { 175 mCarSystemBarController.registerBarTouchListener(BOTTOM, 176 mNotificationPanelViewController.getDragCloseTouchListener()); 177 } 178 registerLeftBarTouchListener()179 protected void registerLeftBarTouchListener() { 180 mCarSystemBarController.registerBarTouchListener(LEFT, 181 mNotificationPanelViewController.getDragCloseTouchListener()); 182 } 183 registerRightBarTouchListener()184 protected void registerRightBarTouchListener() { 185 mCarSystemBarController.registerBarTouchListener(RIGHT, 186 mNotificationPanelViewController.getDragCloseTouchListener()); 187 } 188 getCarSystemBarController()189 protected final CarSystemBarController getCarSystemBarController() { 190 return mCarSystemBarController; 191 } 192 getNotificationPanelViewController()193 protected final NotificationPanelViewController getNotificationPanelViewController() { 194 return mNotificationPanelViewController; 195 } 196 } 197