1 /*
2  * Copyright (C) 2023 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.portraitlauncher.homeactivities;
18 
19 import static android.car.drivingstate.CarDrivingStateEvent.DRIVING_STATE_MOVING;
20 import static android.car.drivingstate.CarDrivingStateEvent.DRIVING_STATE_UNKNOWN;
21 
22 import android.car.Car;
23 import android.car.drivingstate.CarDrivingStateEvent;
24 import android.car.drivingstate.CarDrivingStateManager;
25 import android.content.Context;
26 import android.os.Build;
27 import android.util.Log;
28 
29 /**
30  * Controls the driving state for {@link CarUiPortraitHomeScreen}
31  */
32 public class CarUiPortraitDriveStateController {
33     private static final boolean DBG = Build.IS_DEBUGGABLE;
34     private static final String TAG = CarUiPortraitDriveStateController.class.getSimpleName();
35 
36     private int mCurrentDrivingState = DRIVING_STATE_UNKNOWN;
37     private final CarDrivingStateManager.CarDrivingStateEventListener mDrivingStateEventListener =
38             this::handleDrivingStateChange;
39 
CarUiPortraitDriveStateController(Context context)40     public CarUiPortraitDriveStateController(Context context) {
41         Car car = Car.createCar(context);
42         if (car != null) {
43             CarDrivingStateManager mDrivingStateManager =
44                     (CarDrivingStateManager) car.getCarManager(Car.CAR_DRIVING_STATE_SERVICE);
45             mDrivingStateManager.registerListener(mDrivingStateEventListener);
46             mDrivingStateEventListener.onDrivingStateChanged(
47                     mDrivingStateManager.getCurrentCarDrivingState());
48         } else {
49             Log.e(TAG, "Failed to initialize car");
50         }
51     }
52 
handleDrivingStateChange(CarDrivingStateEvent carDrivingStateEvent)53     private void handleDrivingStateChange(CarDrivingStateEvent carDrivingStateEvent) {
54         mCurrentDrivingState = carDrivingStateEvent.eventValue;
55     }
56 
57     /**
58      * Returns true if the driving state is moving
59      */
isDrivingStateMoving()60     public boolean isDrivingStateMoving() {
61         logIfDebuggable("Driving state is " + mCurrentDrivingState);
62         return mCurrentDrivingState == DRIVING_STATE_MOVING;
63     }
64 
65     /**
66      * Print debug log in debug build
67      */
logIfDebuggable(String message)68     private static void logIfDebuggable(String message) {
69         if (DBG) {
70             Log.d(TAG, message);
71         }
72     }
73 }
74