blob: cd87e78e4be9bf22163b5603c5b227d2e48acaaf [file] [log] [blame]
jovanakedba98c2018-09-14 15:46:24 -07001/*
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
17package com.android.systemui.statusbar.car;
18
19import android.car.Car;
Keun young Park9e395bb2019-10-11 20:00:22 -070020import android.car.Car.CarServiceLifecycleListener;
jovanakedba98c2018-09-14 15:46:24 -070021import android.car.drivingstate.CarDrivingStateEvent;
22import android.car.drivingstate.CarDrivingStateManager;
23import android.car.drivingstate.CarDrivingStateManager.CarDrivingStateEventListener;
jovanakedba98c2018-09-14 15:46:24 -070024import android.content.Context;
jovanakedba98c2018-09-14 15:46:24 -070025import android.util.Log;
26
27import androidx.annotation.NonNull;
28
29/**
30 * Helper class for connecting to the {@link CarDrivingStateManager} and listening for driving state
31 * changes.
32 */
33public class DrivingStateHelper {
34 public static final String TAG = "DrivingStateHelper";
35
36 private final Context mContext;
37 private CarDrivingStateManager mDrivingStateManager;
38 private Car mCar;
39 private CarDrivingStateEventListener mDrivingStateHandler;
40
41 public DrivingStateHelper(Context context,
42 @NonNull CarDrivingStateEventListener drivingStateHandler) {
43 mContext = context;
44 mDrivingStateHandler = drivingStateHandler;
45 }
46
47 /**
48 * Queries {@link CarDrivingStateManager} for current driving state. Returns {@code true} if car
49 * is idling or moving, {@code false} otherwise.
50 */
51 public boolean isCurrentlyDriving() {
Nicholas Saueref8f3942018-11-28 13:00:02 -080052 if (mDrivingStateManager == null) {
53 return false;
54 }
Keun young Park9e395bb2019-10-11 20:00:22 -070055 CarDrivingStateEvent currentState = mDrivingStateManager.getCurrentCarDrivingState();
56 if (currentState != null) {
57 return currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_IDLING
58 || currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING;
jovanakedba98c2018-09-14 15:46:24 -070059 }
jovanakedba98c2018-09-14 15:46:24 -070060 return false; // Default to false.
61 }
62
63 /**
64 * Establishes connection with the Car service.
65 */
66 public void connectToCarService() {
Keun young Park9e395bb2019-10-11 20:00:22 -070067 mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
68 mCarServiceLifecycleListener);
jovanakedba98c2018-09-14 15:46:24 -070069 }
70
Keun young Park9e395bb2019-10-11 20:00:22 -070071 private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
72 if (!ready) {
73 return;
jovanakedba98c2018-09-14 15:46:24 -070074 }
Keun young Park9e395bb2019-10-11 20:00:22 -070075 logD("Car Service connected");
76 mDrivingStateManager = (CarDrivingStateManager) car.getCarManager(
77 Car.CAR_DRIVING_STATE_SERVICE);
78 if (mDrivingStateManager != null) {
79 mDrivingStateManager.registerListener(mDrivingStateHandler);
80 mDrivingStateHandler.onDrivingStateChanged(
81 mDrivingStateManager.getCurrentCarDrivingState());
82 } else {
83 Log.e(TAG, "CarDrivingStateService service not available");
jovanakedba98c2018-09-14 15:46:24 -070084 }
Keun young Park9e395bb2019-10-11 20:00:22 -070085 };
jovanakedba98c2018-09-14 15:46:24 -070086
87 private void logD(String message) {
88 if (Log.isLoggable(TAG, Log.DEBUG)) {
89 Log.d(TAG, message);
90 }
91 }
Brad Stenning8d1a51c2018-11-20 17:34:16 -080092}