• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.car.notification;
17 
18 import android.app.Activity;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ServiceConnection;
23 import android.os.Bundle;
24 import android.os.IBinder;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.util.Log;
28 
29 import com.android.internal.statusbar.IStatusBarService;
30 
31 /**
32  * Displays all undismissed notifications.
33  */
34 public class CarNotificationCenterActivity extends Activity {
35 
36     private static final String TAG = "CarNotificationActivity";
37 
38     private CarNotificationListener mNotificationListener;
39     private PreprocessingManager mPreprocessingManager;
40     private CarNotificationView mCarNotificationView;
41     private NotificationViewController mNotificationViewController;
42     private IStatusBarService mStatusBarService;
43     private NotificationDataManager mNotificationDataManager;
44     private CarNotificationVisibilityLogger mNotificationVisibilityLogger;
45 
46     private ServiceConnection mNotificationListenerConnectionListener = new ServiceConnection() {
47         public void onServiceConnected(ComponentName className, IBinder binder) {
48             Log.d(TAG, "onServiceConnected");
49             mNotificationListener = ((CarNotificationListener.LocalBinder) binder).getService();
50             NotificationApplication app = (NotificationApplication) getApplication();
51             mNotificationViewController =
52                     new NotificationViewController(mCarNotificationView,
53                             mPreprocessingManager,
54                             mNotificationListener,
55                             app.getCarUxRestrictionWrapper());
56             mNotificationViewController.enable();
57             getApplicationContext().getMainExecutor().execute(() -> {
58                 if (isResumed()) {
59                     notifyVisibilityChanged(/* isVisible= */ true);
60                     mCarNotificationView.setVisibleNotificationsAsSeen();
61                 }
62             });
63         }
64 
65         public void onServiceDisconnected(ComponentName className) {
66             Log.d(TAG, "onServiceDisconnected");
67             mNotificationViewController.disable();
68             mNotificationViewController = null;
69             mNotificationListener = null;
70         }
71     };
72 
73     @Override
onCreate(Bundle savedInstanceState)74     protected void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         mPreprocessingManager = PreprocessingManager.getInstance(getApplicationContext());
77         NotificationApplication app = (NotificationApplication) getApplication();
78         setContentView(R.layout.notification_center_activity);
79         mCarNotificationView = findViewById(R.id.notification_view);
80         mCarNotificationView.setClickHandlerFactory(app.getClickHandlerFactory());
81         findViewById(R.id.exit_button_container).setOnClickListener(v -> finish());
82         mNotificationDataManager = NotificationDataManager.getInstance();
83         mStatusBarService = IStatusBarService.Stub.asInterface(
84                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
85         mNotificationVisibilityLogger = new CarNotificationVisibilityLogger(mStatusBarService,
86                 mNotificationDataManager);
87 
88         mNotificationDataManager.setOnUnseenCountUpdateListener(() -> {
89             mNotificationListener.setNotificationsShown(
90                     NotificationDataManager.getInstance().getSeenNotifications());
91             mNotificationVisibilityLogger.notifyVisibilityChanged(isResumed());
92         });
93     }
94 
95     @Override
onStart()96     protected void onStart() {
97         super.onStart();
98         // Bind notification listener
99         Intent intent = new Intent(this, CarNotificationListener.class);
100         intent.setAction(CarNotificationListener.ACTION_LOCAL_BINDING);
101         bindService(intent, mNotificationListenerConnectionListener, Context.BIND_AUTO_CREATE);
102         if (mNotificationViewController != null) {
103             mNotificationViewController.onVisibilityChanged(/* isVisible= */ true);
104         }
105     }
106 
107     @Override
onStop()108     protected void onStop() {
109         super.onStop();
110         notifyVisibilityChanged(/* isVisible= */ false);
111 
112         // Unbind notification listener
113         if (mNotificationViewController != null) {
114             mNotificationViewController.disable();
115             mNotificationViewController = null;
116         }
117 
118         if (mNotificationListenerConnectionListener != null) {
119             unbindService(mNotificationListenerConnectionListener);
120             mNotificationListener = null;
121         }
122     }
123 
notifyVisibilityChanged(boolean isVisible)124     private void notifyVisibilityChanged(boolean isVisible) {
125         if (mNotificationViewController != null) {
126             mNotificationViewController.onVisibilityChanged(isVisible);
127         }
128         try {
129             if (isVisible) {
130                 mStatusBarService.onPanelRevealed(/* clearNotificationEffects= */ true,
131                         mNotificationDataManager.getVisibleNotifications().size());
132             } else {
133                 mStatusBarService.onPanelHidden();
134             }
135         } catch (RemoteException ex) {
136             Log.e(TAG, "Unable to report notification visibility changes", ex);
137         }
138     }
139 }
140