jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 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; |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 20 | import android.car.Car.CarServiceLifecycleListener; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 21 | import android.car.drivingstate.CarDrivingStateEvent; |
| 22 | import android.car.drivingstate.CarDrivingStateManager; |
| 23 | import android.car.drivingstate.CarDrivingStateManager.CarDrivingStateEventListener; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 24 | import android.content.Context; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import androidx.annotation.NonNull; |
| 28 | |
| 29 | /** |
| 30 | * Helper class for connecting to the {@link CarDrivingStateManager} and listening for driving state |
| 31 | * changes. |
| 32 | */ |
| 33 | public 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 Sauer | ef8f394 | 2018-11-28 13:00:02 -0800 | [diff] [blame] | 52 | if (mDrivingStateManager == null) { |
| 53 | return false; |
| 54 | } |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 55 | CarDrivingStateEvent currentState = mDrivingStateManager.getCurrentCarDrivingState(); |
| 56 | if (currentState != null) { |
| 57 | return currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_IDLING |
| 58 | || currentState.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 59 | } |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 60 | return false; // Default to false. |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Establishes connection with the Car service. |
| 65 | */ |
| 66 | public void connectToCarService() { |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 67 | mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT, |
| 68 | mCarServiceLifecycleListener); |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 71 | private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> { |
| 72 | if (!ready) { |
| 73 | return; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 74 | } |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 75 | 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"); |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 84 | } |
Keun young Park | 9e395bb | 2019-10-11 20:00:22 -0700 | [diff] [blame] | 85 | }; |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 86 | |
| 87 | private void logD(String message) { |
| 88 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 89 | Log.d(TAG, message); |
| 90 | } |
| 91 | } |
Brad Stenning | 8d1a51c | 2018-11-20 17:34:16 -0800 | [diff] [blame] | 92 | } |