Explain in detail the adaptation of Android Automotive vehicle application to driving mode Safe Drive Mode

  • 2021-12-12 05:43:18
  • OfStack

Preface

Recently, several of the problems encountered on Android Automotive are related to the driving mode applied on Android vehicle-mounted operating system. There is very little data in this field in China. I summarize the relevant knowledge here, mainly including the following aspects:

Differences between Android Automotive and Android Auto Introduction of driving mode of Android Automotive Android Automotive Driving Mode Realization of Several Ways and Code Examples, and Realization Effect

I mainly want to summarize several ways of adapting Android vehicle application to Automotive driving mode (Drive Mode).

Development environment

Android Studio version 4.1. 2

1. The difference between Android Automotive and Android Auto

Android Auto:

Android Auto is an Android end App specially designed for driving environment

It can be used to map some functions on Android devices to the screen of cars through data lines. At that time, the main focus of doing Android Auto was safety. In order to prevent users from picking up their mobile phones during driving, Google added Google Assistant to Android Auto, that is, using voice interaction in the driving environment, so that users can realize the operation of some mobile phones app without changing their physical posture. The disadvantage is that the mobile phone application is mapped to the car through the data line, and the application still runs on the mobile phone, taking the mobile phone as the center, so that the relevant data of the car itself, such as speed, GPS, sensors and driving status, cannot be synchronized to the mobile phone.

Android Automotive is an operating system and open source platform that can be run on vehicle hardware

Our most common Android platform is put into trial operation on mobile phones or tablets. Compared with our common Android operating system, Android Automotive has the same code base, and specially adds specific functions and technical support for automobiles, mainly including the following aspects:

Car App: Includes OEM pre-installed, and app developed by the third party and downloaded to the car through the in-car app store Car API: The OEM factory provides the unique interface to the App of the automobile, including the API related to the dashboard, the API related to the vehicle hardware (cockpit, ventilation, etc.), the multimedia, navigation, on-board system setting interface and the API related to the vehicle sensor Car Service: Car Service is a system service, which provides a series of services related to vehicles. Vehicle Network Service: OEM vendor network services Vehicle HAL: Automotive Hardware Abstraction Layer Description

What Android Auto displays on the car is actually the data on the mobile phone, while Android Automotive should consider the synchronization with the data and account number of app on the mobile phone *

2. Introduction to the driving mode of Android Automotive

As mentioned earlier, Google launched Android Auto at I/O conference in 2014 to better ensure driving safety, and Android Automotive also added driving mode (Drive Mode), aiming to help automobile OEM manufacturers manage applications that may cause driver distraction from the system level.
In driving mode, Android Automotive will make series 1 recommendations for Driving Distraction. OEM manufacturers can also request Activity or Fragment interfaces that may cause distraction to drivers, and register themselves as Distraction Optimized in Manifest File, such as login interface, code scanning interface, song switching interface, video playing interface and game interface that require drivers to operate. While Android Automotive will actively restrict the interface marked Distraction Optimized in driving mode.

3. Several implementations and code examples of Android Automotive driving mode

Mode 1. In the Manifest file, use the metadata meta-data to mark the interface that may cause distraction driving

Android Automotive will identify activities like the following that use metadata to mark distractionOptimized or Fragment as interfaces to be optimized, and disable these interfaces in driving mode, or add a prompt box with higher UI level on top of the current Activity, so as to avoid users' distraction caused by these interfaces during driving.


<activity android:name=".QRCodeScanPage">
	<meta-data android:name="distractionOptimized" android:value="true"/>
</activity>

The above code, which is used to scan the code for users to log in to the interface QRCodeScanPage, will be treated differently by AndroidAutomotive in the driving state. OEM depot can also customize Android Automotive and add cover on the restricted interface.

As you can see, This treatment of distracted interface is simple and rude, Just add Meta-data tags to the elements of the component directly in the Manifest file. The disadvantage is that it is not flexible enough, and all distraction interfaces have the same effect after being covered. In order to be suitable for distraction interfaces of various applications, the whole UI interface is often directly blocked. Even if the UI of QR code is small, it still needs to cover the whole screen, and the user experience is very bad.

Mode 2. Use CarDrivingStateManager class to obtain the current driving state of the car, and apply the occlusion scheme of the distraction interface defined by yourself after obtaining the driving state

The CarDrivingStateManager class of Android Automotive can obtain the current driving state (parking, idling, driving) of the vehicle according to the sensor data provided by the vehicle hardware abstraction layer (VHAL), so that the application can set the CarDrivingStateEventListener listener by:

Guide package:


import android.car.Car;
/* For CarDrivingState */
import android.car.drivingstate.CarDrivingStateEvent;
import android.car.drivingstate.CarDrivingStateManager;

private final CarDrivingStateManager.CarDrivingStateEventListener
mDrivingStateEventListener =
       new CarDrivingStateManager.CarDrivingStateEventListener() {
   @Override
   public void onDrivingStateChanged(CarDrivingStateEvent event) {
       mDrivingStateEvent = event;
       handleDrivingStateChange();
   }
};

Android Automotive defines the following four states for DrivingState:


/**
 * This is when we don't have enough information to infer the car's driving state.
 */
public static final int DRIVING_STATE_UNKNOWN = -1;
/**
 * Car is parked - Gear is in Parked mode.
 */
public static final int DRIVING_STATE_PARKED = 0;
/**
 * Car is idling.  Gear is not in Parked mode and Speed of the vehicle is zero.
 */
public static final int DRIVING_STATE_IDLING = 1;
/**
 * Car is moving.  Gear is not in parked mode and speed of the vehicle is non zero.
 */
public static final int DRIVING_STATE_MOVING = 2;

Code for DrivingStateManager:


mDrivingStateManager = (CarDrivingStateManager) mCar.getCarManager(
       Car.CAR_DRIVING_STATE_SERVICE);
/* Register the listener (implemented below) */
mDrivingStateManager.registerListener(mDrivingStateEventListener);
/* While we wait for a change to be notified, query the current state */
mDrivingStateEvent = mDrivingStateManager.getCurrentCarDrivingState();

In this way, you can get the three driving states of the current vehicle: stop, idle, drive, and may return to UNKNOWN, which needs to be handled by the developer.

In addition, here is a little trick, through the CarUxRestrictions object's isRequiresDistractionOptimization() Method, you can directly obtain whether the current vehicle is in a driving state, 1 indicates that the vehicle is in a driving state, and 0 indicates that it is not in a driving state.

We can see that the above method 2 can actively query the driving status of the current vehicle, and then decide whether to display the distraction interface according to the results returned by DrivingStateEventListener, or write the masking code by itself. Compared with Mode 1, it adds a lot of flexibility.

Mode 3. Use CarUxRestrictionsManager and listen for OnUxRestrictionsChangedListener

Guide package:


import android.car.Car;
/* For CarUxRestrictions */
import android.car.drivingstate.CarUxRestrictions;
import android.car.drivingstate.CarUxRestrictionsManager;

As can be seen from the following CarUxRestrictionManager, OnUxRestrictionsChangedListener provides monitoring for changes in driving mode restriction state:


@Nullable private CarUxRestrictionsManager mCarUxRestrictionsManager;
private CarUxRestrictions mCurrentUxRestrictions;

/* Implement the onUxRestrictionsChangedListener interface */
private CarUxRestrictionsManager.OnUxRestrictionsChangedListener mUxrChangeListener =
            new CarUxRestrictionsManager.OnUxRestrictionsChangedListener()
    {
        @Override
        public void onUxRestrictionsChanged(CarUxRestrictions carUxRestrictions) {
        mCurrentUxRestrictions = carUxRestrictions;
        /* Handle the new restrictions */
        handleUxRestrictionsChanged(carUxRestrictions);
        }
    };

The main application scenarios of this method 3 are: distraction events that are not suitable for listening at startup or distraction interfaces that last for a long time. For example, long-term video playback applications and so on

Conclusion

The above introduces the driving mode of Android Automotive, as well as several implementation methods, advantages and disadvantages of each method and application scenarios. Tomorrow's knee arthroscopic surgery, the network here is not good. When you go back, you can implement the code on the virtual machine of Android vehicle-mounted system. Using the following ADB instruction, you can simulate the current vehicle speed and verify the change of driving state. The last parameter is speed, and the unit is meter/second.


adb shell dumpsys activity service com.android.car inject-vhal-event 0x11600207 40

The above is a detailed explanation of the adaptation of Android Automotive vehicle application to driving mode (Safe Drive Mode). For more information about Android Automotive adaptive driving mode (Safe Drive Mode), please pay attention to other related articles on this site!


Related articles: