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.CarNotConnectedException;
22 import android.car.drivingstate.CarUxRestrictionsManager;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.ServiceConnection;
26 import android.os.IBinder;
27 import android.os.ServiceManager;
28 import android.util.Log;
29 
30 import com.android.internal.statusbar.IStatusBarService;
31 
32 /**
33  * Application class that makes connections to the car service api so components can share these
34  * objects
35  */
36 public class NotificationApplication extends Application {
37     private static final String TAG = "NotificationApplication";
38     private Car mCar;
39     private NotificationClickHandlerFactory mClickHandlerFactory;
40 
41     private CarUxRestrictionManagerWrapper mCarUxRestrictionManagerWrapper =
42             new CarUxRestrictionManagerWrapper();
43 
44     private ServiceConnection mCarConnectionListener = new ServiceConnection() {
45         @Override
46         public void onServiceConnected(ComponentName name, IBinder service) {
47             Log.d(TAG, "onServiceConnected");
48             try {
49                 CarUxRestrictionsManager carUxRestrictionsManager =
50                         (CarUxRestrictionsManager)
51                                 mCar.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
52                 mCarUxRestrictionManagerWrapper
53                         .setCarUxRestrictionsManager(carUxRestrictionsManager);
54                 PreprocessingManager preprocessingManager = PreprocessingManager.getInstance(
55                         getApplicationContext());
56                 preprocessingManager
57                         .setCarUxRestrictionManagerWrapper(mCarUxRestrictionManagerWrapper);
58             } catch (CarNotConnectedException e) {
59                 Log.e(TAG, "Car not connected in CarConnectionListener", e);
60             }
61         }
62 
63         @Override
64         public void onServiceDisconnected(ComponentName name) {
65         }
66     };
67 
68     /**
69      * Returns the CarUxRestrictionManagerWrapper used to determine visual treatment of 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                 /* callback= */null);
84     }
85 
86     /**
87      * Returns the NotificationClickHandlerFactory used to generate click OnClickListeners
88      * for the notifications
89      */
getClickHandlerFactory()90     public NotificationClickHandlerFactory getClickHandlerFactory() {
91         return mClickHandlerFactory;
92     }
93 }
94