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.Car.CarServiceLifecycleListener;
21 import android.car.drivingstate.CarDrivingStateEvent;
22 import android.car.drivingstate.CarDrivingStateManager;
23 import android.car.drivingstate.CarDrivingStateManager.CarDrivingStateEventListener;
24 import android.content.Context;
25 import android.util.Log;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.systemui.CarSystemUIFactory;
30 import com.android.systemui.SystemUIFactory;
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 CarDrivingStateEventListener mDrivingStateHandler;
42 
DrivingStateHelper(Context context, @NonNull CarDrivingStateEventListener drivingStateHandler)43     public DrivingStateHelper(Context context,
44             @NonNull CarDrivingStateEventListener drivingStateHandler) {
45         mContext = context;
46         mDrivingStateHandler = drivingStateHandler;
47     }
48 
49     /**
50      * Queries {@link CarDrivingStateManager} for current driving state. Returns {@code true} if car
51      * is idling or moving, {@code false} otherwise.
52      */
isCurrentlyDriving()53     public boolean isCurrentlyDriving() {
54         if (mDrivingStateManager == null) {
55             return false;
56         }
57         CarDrivingStateEvent currentState = mDrivingStateManager.getCurrentCarDrivingState();
58         if (currentState != null) {
59             return currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_IDLING
60                     || currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING;
61         }
62         return false; // Default to false.
63     }
64 
65     /**
66      * Establishes connection with the Car service.
67      */
connectToCarService()68     public void connectToCarService() {
69         ((CarSystemUIFactory) SystemUIFactory.getInstance()).getCarServiceProvider(mContext)
70                 .addListener(mCarServiceLifecycleListener);
71     }
72 
73     private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
74         if (!ready) {
75             return;
76         }
77         logD("Car Service connected");
78         mDrivingStateManager = (CarDrivingStateManager) car.getCarManager(
79                 Car.CAR_DRIVING_STATE_SERVICE);
80         if (mDrivingStateManager != null) {
81             mDrivingStateManager.registerListener(mDrivingStateHandler);
82             mDrivingStateHandler.onDrivingStateChanged(
83                     mDrivingStateManager.getCurrentCarDrivingState());
84         } else {
85             Log.e(TAG, "CarDrivingStateService service not available");
86         }
87     };
88 
logD(String message)89     private void logD(String message) {
90         if (Log.isLoggable(TAG, Log.DEBUG)) {
91             Log.d(TAG, message);
92         }
93     }
94 }
95