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 android.car.hardware.power.CarPowerManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.res.Configuration;
25 import android.os.UserHandle;
26 import android.util.Log;
27 
28 import androidx.annotation.CallSuper;
29 
30 import com.android.systemui.broadcast.BroadcastDispatcher;
31 import com.android.systemui.car.CarDeviceProvisionedController;
32 import com.android.systemui.car.systembar.CarSystemBarController;
33 import com.android.systemui.car.window.OverlayViewMediator;
34 import com.android.systemui.dagger.SysUISingleton;
35 import com.android.systemui.statusbar.policy.ConfigurationController;
36 
37 import javax.inject.Inject;
38 
39 /**
40  * The view mediator which attaches the view controller to other elements of the system ui. Disables
41  * drag open behavior of the notification panel from any navigation bar.
42  */
43 @SysUISingleton
44 public class NotificationPanelViewMediator implements OverlayViewMediator,
45         ConfigurationController.ConfigurationListener {
46 
47     private static final boolean DEBUG = false;
48     private static final String TAG = "NotificationPanelVM";
49 
50     private final CarSystemBarController mCarSystemBarController;
51     private final NotificationPanelViewController mNotificationPanelViewController;
52     private final PowerManagerHelper mPowerManagerHelper;
53     private final BroadcastDispatcher mBroadcastDispatcher;
54     private final CarDeviceProvisionedController mCarDeviceProvisionedController;
55     private final ConfigurationController mConfigurationController;
56 
57     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
58         @Override
59         public void onReceive(Context context, Intent intent) {
60             if (DEBUG) Log.v(TAG, "onReceive: " + intent);
61             String action = intent.getAction();
62             if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
63                 if (mNotificationPanelViewController.isPanelExpanded()) {
64                     mNotificationPanelViewController.toggle();
65                 }
66             }
67         }
68     };
69 
70     @Inject
NotificationPanelViewMediator( CarSystemBarController carSystemBarController, NotificationPanelViewController notificationPanelViewController, PowerManagerHelper powerManagerHelper, BroadcastDispatcher broadcastDispatcher, CarDeviceProvisionedController carDeviceProvisionedController, ConfigurationController configurationController )71     public NotificationPanelViewMediator(
72             CarSystemBarController carSystemBarController,
73             NotificationPanelViewController notificationPanelViewController,
74 
75             PowerManagerHelper powerManagerHelper,
76             BroadcastDispatcher broadcastDispatcher,
77 
78             CarDeviceProvisionedController carDeviceProvisionedController,
79             ConfigurationController configurationController
80     ) {
81         mCarSystemBarController = carSystemBarController;
82         mNotificationPanelViewController = notificationPanelViewController;
83         mPowerManagerHelper = powerManagerHelper;
84         mBroadcastDispatcher = broadcastDispatcher;
85         mCarDeviceProvisionedController = carDeviceProvisionedController;
86         mConfigurationController = configurationController;
87     }
88 
89     @Override
90     @CallSuper
registerListeners()91     public void registerListeners() {
92         mCarSystemBarController.registerTopBarTouchListener(
93                 mNotificationPanelViewController.getDragCloseTouchListener());
94         mCarSystemBarController.registerBottomBarTouchListener(
95                 mNotificationPanelViewController.getDragCloseTouchListener());
96         mCarSystemBarController.registerLeftBarTouchListener(
97                 mNotificationPanelViewController.getDragCloseTouchListener());
98         mCarSystemBarController.registerRightBarTouchListener(
99                 mNotificationPanelViewController.getDragCloseTouchListener());
100 
101         mCarSystemBarController.registerNotificationController(
102                 new CarSystemBarController.NotificationsShadeController() {
103                     @Override
104                     public void togglePanel() {
105                         mNotificationPanelViewController.toggle();
106                     }
107 
108                     @Override
109                     public boolean isNotificationPanelOpen() {
110                         return mNotificationPanelViewController.isPanelExpanded();
111                     }
112                 });
113 
114         mBroadcastDispatcher.registerReceiver(mBroadcastReceiver,
115                 new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS), null, UserHandle.ALL);
116     }
117 
118     @Override
setUpOverlayContentViewControllers()119     public void setUpOverlayContentViewControllers() {
120         mNotificationPanelViewController.setOnUnseenCountUpdateListener(unseenNotificationCount -> {
121             boolean hasUnseen = unseenNotificationCount > 0;
122             mCarSystemBarController.toggleAllNotificationsUnseenIndicator(
123                     mCarDeviceProvisionedController.isCurrentUserFullySetup(), hasUnseen);
124         });
125 
126         mPowerManagerHelper.setCarPowerStateListener(state -> {
127             if (state == CarPowerManager.CarPowerStateListener.ON) {
128                 mNotificationPanelViewController.onCarPowerStateOn();
129             }
130         });
131         mPowerManagerHelper.connectToCarService();
132 
133         mConfigurationController.addCallback(this);
134     }
135 
136     @Override
onConfigChanged(Configuration newConfig)137     public void onConfigChanged(Configuration newConfig) {
138         // No op.
139     }
140 
141     @Override
onDensityOrFontScaleChanged()142     public void onDensityOrFontScaleChanged() {
143         registerListeners();
144     }
145 
146     @Override
onUiModeChanged()147     public void onUiModeChanged() {
148         // No op.
149     }
150 
151     @Override
onThemeChanged()152     public void onThemeChanged() {
153         // No op.
154     }
155 
156     @Override
onLocaleListChanged()157     public void onLocaleListChanged() {
158         mNotificationPanelViewController.reinflate();
159         registerListeners();
160     }
161 
getCarSystemBarController()162     protected final CarSystemBarController getCarSystemBarController() {
163         return mCarSystemBarController;
164     }
165 
getNotificationPanelViewController()166     protected final NotificationPanelViewController getNotificationPanelViewController() {
167         return mNotificationPanelViewController;
168     }
169 }
170