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 
17 package com.android.car.notification;
18 
19 import android.app.Application;
20 import android.car.Car;
21 import android.car.drivingstate.CarUxRestrictionsManager;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.ServiceConnection;
25 import android.os.IBinder;
26 import android.os.ServiceManager;
27 import android.util.Log;
28 
29 import com.android.internal.statusbar.IStatusBarService;
30 
31 /**
32  * Application class that makes connections to the car service api so components can share these
33  * objects
34  */
35 public class NotificationApplication extends Application {
36     private static final String TAG = "NotificationApplication";
37     private Car mCar;
38     private NotificationClickHandlerFactory mClickHandlerFactory;
39 
40     private CarUxRestrictionManagerWrapper mCarUxRestrictionManagerWrapper =
41             new CarUxRestrictionManagerWrapper();
42 
43     private ServiceConnection mCarConnectionListener = new ServiceConnection() {
44         @Override
45         public void onServiceConnected(ComponentName name, IBinder service) {
46             Log.d(TAG, "onServiceConnected");
47             try {
48                 CarUxRestrictionsManager carUxRestrictionsManager =
49                         (CarUxRestrictionsManager)
50                                 mCar.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
51                 mCarUxRestrictionManagerWrapper
52                         .setCarUxRestrictionsManager(carUxRestrictionsManager);
53                 PreprocessingManager preprocessingManager = PreprocessingManager.getInstance(
54                         getApplicationContext());
55                 preprocessingManager
56                         .setCarUxRestrictionManagerWrapper(mCarUxRestrictionManagerWrapper);
57             } catch (RuntimeException e) {
58                 Log.e(TAG, "Car not connected in CarConnectionListener", e);
59             }
60         }
61 
62         @Override
63         public void onServiceDisconnected(ComponentName name) {
64         }
65     };
66 
67     /**
68      * Returns the CarUxRestrictionManagerWrapper used to determine visual treatment of
69      * notifications.
70      */
getCarUxRestrictionWrapper()71     public CarUxRestrictionManagerWrapper getCarUxRestrictionWrapper() {
72         return mCarUxRestrictionManagerWrapper;
73     }
74 
75     @Override
onCreate()76     public void onCreate() {
77         super.onCreate();
78         mCar = Car.createCar(this, mCarConnectionListener);
79         mCar.connect();
80         mClickHandlerFactory = new NotificationClickHandlerFactory(
81                 IStatusBarService.Stub.asInterface(
82                         ServiceManager.getService(Context.STATUS_BAR_SERVICE)));
83     }
84 
85     @Override
onTerminate()86     public void onTerminate() {
87         super.onTerminate();
88         if (mCar != null) {
89             mCar.disconnect();
90         }
91     }
92 
93     /**
94      * Returns the NotificationClickHandlerFactory used to generate click OnClickListeners
95      * for the notifications
96      */
getClickHandlerFactory()97     public NotificationClickHandlerFactory getClickHandlerFactory() {
98         return mClickHandlerFactory;
99     }
100 }
101