• 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 
17 package com.android.systemui.statusbar.car;
18 
19 import android.car.Car;
20 import android.car.CarNotConnectedException;
21 import android.car.drivingstate.CarDrivingStateEvent;
22 import android.car.drivingstate.CarDrivingStateManager;
23 import android.car.drivingstate.CarDrivingStateManager.CarDrivingStateEventListener;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.ServiceConnection;
27 import android.os.IBinder;
28 import android.util.Log;
29 
30 import androidx.annotation.NonNull;
31 
32 /**
33  * Helper class for connecting to the {@link CarDrivingStateManager} and listening for driving state
34  * changes.
35  */
36 public class DrivingStateHelper {
37     public static final String TAG = "DrivingStateHelper";
38 
39     private final Context mContext;
40     private CarDrivingStateManager mDrivingStateManager;
41     private Car mCar;
42     private CarDrivingStateEventListener mDrivingStateHandler;
43 
DrivingStateHelper(Context context, @NonNull CarDrivingStateEventListener drivingStateHandler)44     public DrivingStateHelper(Context context,
45             @NonNull CarDrivingStateEventListener drivingStateHandler) {
46         mContext = context;
47         mDrivingStateHandler = drivingStateHandler;
48     }
49 
50     /**
51      * Queries {@link CarDrivingStateManager} for current driving state. Returns {@code true} if car
52      * is idling or moving, {@code false} otherwise.
53      */
isCurrentlyDriving()54     public boolean isCurrentlyDriving() {
55         if (mDrivingStateManager == null) {
56             return false;
57         }
58         try {
59             CarDrivingStateEvent currentState = mDrivingStateManager.getCurrentCarDrivingState();
60             if (currentState != null) {
61                 return currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_IDLING
62                         || currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING;
63             }
64         } catch (CarNotConnectedException e) {
65             Log.e(TAG, "Cannot determine current driving state. Car not connected", e);
66         }
67 
68         return false; // Default to false.
69     }
70 
71     /**
72      * Establishes connection with the Car service.
73      */
connectToCarService()74     public void connectToCarService() {
75         mCar = Car.createCar(mContext, mCarConnectionListener);
76         if (mCar != null) {
77             mCar.connect();
78         }
79     }
80 
81     /**
82      * Disconnects from Car service and cleans up listeners.
83      */
disconnectFromCarService()84     public void disconnectFromCarService() {
85         if (mCar != null) {
86             mCar.disconnect();
87         }
88     }
89 
90     private final ServiceConnection mCarConnectionListener =
91             new ServiceConnection() {
92                 public void onServiceConnected(ComponentName name, IBinder service) {
93                     logD("Car Service connected");
94                     try {
95                         mDrivingStateManager = (CarDrivingStateManager) mCar.getCarManager(
96                                 Car.CAR_DRIVING_STATE_SERVICE);
97                         if (mDrivingStateManager != null) {
98                             mDrivingStateManager.registerListener(mDrivingStateHandler);
99                             mDrivingStateHandler.onDrivingStateChanged(
100                                     mDrivingStateManager.getCurrentCarDrivingState());
101                         } else {
102                             Log.e(TAG, "CarDrivingStateService service not available");
103                         }
104                     } catch (CarNotConnectedException e) {
105                         Log.e(TAG, "Car not connected", e);
106                     }
107                 }
108 
109                 @Override
110                 public void onServiceDisconnected(ComponentName name) {
111                     destroyDrivingStateManager();
112                 }
113             };
114 
destroyDrivingStateManager()115     private void destroyDrivingStateManager() {
116         try {
117             if (mDrivingStateManager != null) {
118                 mDrivingStateManager.unregisterListener();
119             }
120         } catch (CarNotConnectedException e) {
121             Log.e(TAG, "Error unregistering listeners", e);
122         }
123     }
124 
logD(String message)125     private void logD(String message) {
126         if (Log.isLoggable(TAG, Log.DEBUG)) {
127             Log.d(TAG, message);
128         }
129     }
130 }
131