Clear all notification, message mute states, and unread states when the car
recovers from the suspension state.

bug: 130183435
Test: Manual.
Change-Id: I5ba95d5f22c1ee18c9f2c236f0f078aedd8296b8
diff --git a/packages/CarSystemUI/AndroidManifest.xml b/packages/CarSystemUI/AndroidManifest.xml
index 4e8a3a3..195d4fe 100644
--- a/packages/CarSystemUI/AndroidManifest.xml
+++ b/packages/CarSystemUI/AndroidManifest.xml
@@ -19,6 +19,6 @@
         package="com.android.systemui"
         android:sharedUserId="android.uid.systemui"
         coreApp="true">
-
-
+    <!-- This permission is required to monitor car power state. -->
+    <uses-permission android:name="android.car.permission.CAR_POWER" />
 </manifest>
diff --git a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
index febf8b8..835cedf 100644
--- a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
+++ b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
@@ -25,6 +25,7 @@
 import android.car.Car;
 import android.car.drivingstate.CarDrivingStateEvent;
 import android.car.drivingstate.CarUxRestrictionsManager;
+import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
 import android.content.Context;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
@@ -121,8 +122,11 @@
     private boolean mDeviceIsProvisioned = true;
     private HvacController mHvacController;
     private DrivingStateHelper mDrivingStateHelper;
-    private static FlingAnimationUtils sFlingAnimationUtils;
+    private PowerManagerHelper mPowerManagerHelper;
+    private FlingAnimationUtils mFlingAnimationUtils;
     private SwitchToGuestTimer mSwitchToGuestTimer;
+    private NotificationDataManager mNotificationDataManager;
+    private NotificationClickHandlerFactory mNotificationClickHandlerFactory;
 
     // The container for the notifications.
     private CarNotificationView mNotificationView;
@@ -156,6 +160,20 @@
     // If notification shade is being swiped vertically to close.
     private boolean mIsSwipingVerticallyToClose;
 
+    private final CarPowerStateListener mCarPowerStateListener =
+            (int state) -> {
+                // When the car powers on, clear all notifications and mute/unread states.
+                Log.d(TAG, "New car power state: " + state);
+                if (state == CarPowerStateListener.ON) {
+                    if (mNotificationClickHandlerFactory != null) {
+                        mNotificationClickHandlerFactory.clearAllNotifications();
+                    }
+                    if (mNotificationDataManager != null) {
+                        mNotificationDataManager.clearAll();
+                    }
+                }
+            };
+
     @Override
     public void start() {
         // get the provisioned state before calling the parent class since it's that flow that
@@ -172,7 +190,7 @@
                 R.integer.notification_settle_open_percentage);
         mSettleClosePercentage = mContext.getResources().getInteger(
                 R.integer.notification_settle_close_percentage);
-        sFlingAnimationUtils = new FlingAnimationUtils(mContext,
+        mFlingAnimationUtils = new FlingAnimationUtils(mContext,
                 FLING_ANIMATION_MAX_TIME, FLING_SPEED_UP_FACTOR);
 
         createBatteryController();
@@ -204,6 +222,9 @@
         mDrivingStateHelper = new DrivingStateHelper(mContext, this::onDrivingStateChanged);
         mDrivingStateHelper.connectToCarService();
 
+        mPowerManagerHelper = new PowerManagerHelper(mContext, mCarPowerStateListener);
+        mPowerManagerHelper.connectToCarService();
+
         mSwitchToGuestTimer = new SwitchToGuestTimer(mContext);
     }
 
@@ -308,7 +329,6 @@
         }
     }
 
-
     @Override
     protected void makeStatusBarView(@Nullable RegisterStatusBarResult result) {
         super.makeStatusBarView(result);
@@ -407,7 +427,7 @@
                 }
         );
 
-        NotificationClickHandlerFactory clickHandlerFactory = new NotificationClickHandlerFactory(
+        mNotificationClickHandlerFactory = new NotificationClickHandlerFactory(
                 mBarService,
                 launchResult -> {
                     if (launchResult == ActivityManager.START_TASK_TO_FRONT
@@ -422,26 +442,27 @@
         CarUxRestrictionManagerWrapper carUxRestrictionManagerWrapper =
                 new CarUxRestrictionManagerWrapper();
         carUxRestrictionManagerWrapper.setCarUxRestrictionsManager(carUxRestrictionsManager);
-        NotificationDataManager notificationDataManager = new NotificationDataManager();
 
-        notificationDataManager.setOnUnseenCountUpdateListener(
+        mNotificationDataManager = new NotificationDataManager();
+        mNotificationDataManager.setOnUnseenCountUpdateListener(
                 () -> {
                     // TODO: Update Notification Icon based on unseen count
                     Log.d(TAG, "unseen count: " +
-                            notificationDataManager.getUnseenNotificationCount());
+                            mNotificationDataManager.getUnseenNotificationCount());
                 });
 
         CarHeadsUpNotificationManager carHeadsUpNotificationManager =
-                new CarSystemUIHeadsUpNotificationManager(mContext, clickHandlerFactory,
-                        notificationDataManager);
+                new CarSystemUIHeadsUpNotificationManager(mContext,
+                        mNotificationClickHandlerFactory, mNotificationDataManager);
+        mNotificationClickHandlerFactory.setNotificationDataManager(mNotificationDataManager);
 
         carNotificationListener.registerAsSystemService(mContext, carUxRestrictionManagerWrapper,
-                carHeadsUpNotificationManager, notificationDataManager);
+                carHeadsUpNotificationManager, mNotificationDataManager);
 
         mNotificationView = mStatusBarWindow.findViewById(R.id.notification_view);
         View glassPane = mStatusBarWindow.findViewById(R.id.glass_pane);
-        mNotificationView.setClickHandlerFactory(clickHandlerFactory);
-        mNotificationView.setNotificationDataManager(notificationDataManager);
+        mNotificationView.setClickHandlerFactory(mNotificationClickHandlerFactory);
+        mNotificationView.setNotificationDataManager(mNotificationDataManager);
 
         // The glass pane is used to view touch events before passed to the notification list.
         // This allows us to initialize gesture listeners and detect when to close the notifications
@@ -528,7 +549,7 @@
                 PreprocessingManager.getInstance(mContext),
                 carNotificationListener,
                 carUxRestrictionManagerWrapper,
-                notificationDataManager);
+                mNotificationDataManager);
         mNotificationViewController.enable();
     }
 
@@ -639,7 +660,7 @@
                 }
             }
         });
-        sFlingAnimationUtils.apply(animator, from, to, Math.abs(velocity));
+        mFlingAnimationUtils.apply(animator, from, to, Math.abs(velocity));
         animator.start();
     }
 
diff --git a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/PowerManagerHelper.java b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/PowerManagerHelper.java
new file mode 100644
index 0000000..8de1439
--- /dev/null
+++ b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/PowerManagerHelper.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.statusbar.car;
+
+import android.annotation.NonNull;
+import android.car.Car;
+import android.car.CarNotConnectedException;
+import android.car.hardware.power.CarPowerManager;
+import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.util.Log;
+
+/**
+ * Helper class for connecting to the {@link CarPowerManager} and listening for power state changes.
+ */
+public class PowerManagerHelper {
+    public static final String TAG = "PowerManagerHelper";
+
+    private final Context mContext;
+    private final CarPowerStateListener mCarPowerStateListener;
+
+    private Car mCar;
+    private CarPowerManager mCarPowerManager;
+
+    private final ServiceConnection mCarConnectionListener =
+            new ServiceConnection() {
+                public void onServiceConnected(ComponentName name, IBinder service) {
+                    Log.d(TAG, "Car Service connected");
+                    try {
+                        mCarPowerManager = (CarPowerManager) mCar.getCarManager(Car.POWER_SERVICE);
+                        if (mCarPowerManager != null) {
+                            mCarPowerManager.setListener(mCarPowerStateListener);
+                        } else {
+                            Log.e(TAG, "CarPowerManager service not available");
+                        }
+                    } catch (CarNotConnectedException e) {
+                        Log.e(TAG, "Car not connected", e);
+                    }
+                }
+
+                @Override
+                public void onServiceDisconnected(ComponentName name) {
+                    destroyCarPowerManager();
+                }
+            };
+
+    PowerManagerHelper(Context context, @NonNull CarPowerStateListener listener) {
+        mContext = context;
+        mCarPowerStateListener = listener;
+    }
+
+    /**
+     * Connect to Car service.
+     */
+    void connectToCarService() {
+        mCar = Car.createCar(mContext, mCarConnectionListener);
+        if (mCar != null) {
+            mCar.connect();
+        }
+    }
+
+    /**
+     * Disconnects from Car service.
+     */
+    void disconnectFromCarService() {
+        if (mCar != null) {
+            mCar.disconnect();
+        }
+    }
+
+    private void destroyCarPowerManager() {
+        if (mCarPowerManager != null) {
+            mCarPowerManager.clearListener();
+        }
+    }
+}