前言
车载系统开发中大屏需要在主界面增加一些特色画面,例如在主界面划分多块区域显示,其中一个显示地图应用或者其他应用。android9.0之后的版本提供一个车载开发相关的Car模块,包含了车辆控制交互以及UI界面。其中CarLauncher为车载设计的一款主界面,配合CarSystemUI实现车载专属的UI效果。
预实现效果
CarLauncher中使用了android系统隐藏API–android.app.ActivityView来进行多Activity窗口显示。
在原生CarLauncher的布局中,ActivityView嵌套在activity的布局中,用法如下:
<androidx.cardview.widget.CardView
style="@style/CardViewStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/main_screen_widget_margin"
app:layout_constraintBottom_toTopOf="@+id/divider_horizontal"
app:layout_constraintRight_toLeftOf="@+id/end_edge"
app:layout_constraintLeft_toRightOf="@+id/start_edge"
android:layoutDirection="locale"
app:layout_constraintTop_toBottomOf="@+id/top_edge">
<android.car.app.CarActivityView
android:id="@+id/maps"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.cardview.widget.CardView>
CarActivityView继承ActivityView,CarActivityView主要处理一些业务逻辑。
在主Activity中,继承了FragmentActivity以达到多个碎片Fragment的加载,使布局更加灵活。
public class CarLauncher extends FragmentActivity {
...代码省略
}
CarActivityView需要设置生命周期的监听。
mActivityView = findViewById(R.id.maps);
if (mActivityView != null) {
mActivityView.setCallback(mActivityViewCallback);
}
当收到onActivityViewReady创建成功的回调时再去启动其他进程的应用。
private final ActivityView.StateCallback mActivityViewCallback =
new ActivityView.StateCallback() {
@Override
public void onActivityViewReady(ActivityView view) {
if (DEBUG) Log.d(TAG, "onActivityViewReady(" + getUserId() + ")");
mActivityViewReady = true;
startMapsInActivityView();
maybeLogReady();
}
@Override
public void onActivityViewDestroyed(ActivityView view) {
if (DEBUG) Log.d(TAG, "onActivityViewDestroyed(" + getUserId() + ")");
mActivityViewReady = false;
}
@Override
public void onTaskMovedToFront(int taskId) {
if (DEBUG) {
Log.d(TAG, "onTaskMovedToFront(" + getUserId() + "): started="
+ mIsStarted);
}
try {
if (mIsStarted) {
ActivityManager am =
(ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.moveTaskToFront(CarLauncher.this.getTaskId(), /* flags= */ 0);
}
} catch (RuntimeException e) {
Log.w(TAG, "Failed to move CarLauncher to front.");
}
}
};
启动其他进程的应用使用如下:
private void startMapsInActivityView() {
if (mActivityView == null || !mActivityViewReady) {
return;
}
// If we happen to be be resurfaced into a multi display mode we skip launching content
// in the activity view as we will get recreated anyway.
if (isInMultiWindowMode() || isInPictureInPictureMode()) {
return;
}
// Don't start Maps when the display is off for ActivityVisibilityTests.
if (getDisplay().getState() != Display.STATE_ON) {
return;
}
try {
mActivityView.startActivity(getMapsIntent());
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Maps activity not found", e);
}
}
private Intent getMapsIntent() {
return Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MAPS);
}
通过ActivityView.startActivity
去启动其他进程的Activity,Intent.makeMainSelectorActivity
创建意图。
CarLauncher源码地址:
/packages/apps/Car/Launcher/src/com/android/car/carlauncher/
原理
ActivityView
创建时会初始化一个VirtualDisplay
。
private void initVirtualDisplay() {
if (this.mVirtualDisplay != null) {
throw new IllegalStateException("Trying to initialize for the second time.");
} else {
int width = this.mSurfaceView.getWidth();
int height = this.mSurfaceView.getHeight();
DisplayManager displayManager = (DisplayManager)this.mContext.getSystemService(DisplayManager.class);
this.mVirtualDisplay = displayManager.createVirtualDisplay("ActivityViewVirtualDisplay@" + System.identityHashCode(this), width, height, this.getBaseDisplayDensity(), this.mSurface, 9);
if (this.mVirtualDisplay == null) {
Log.e("ActivityView", "Failed to initialize ActivityView");
} else {
int displayId = this.mVirtualDisplay.getDisplay().getDisplayId();
IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
try {
wm.dontOverrideDisplayInfo(displayId);
} catch (RemoteException var8) {
var8.rethrowAsRuntimeException();
}
this.mInputForwarder = InputManager.getInstance().createInputForwarder(displayId);
this.mTaskStackListener = new ActivityView.TaskStackListenerImpl();
try {
this.mActivityManager.registerTaskStackListener(this.mTaskStackListener);
} catch (RemoteException var7) {
Log.e("ActivityView", "Failed to register task stack listener", var7);
}
}
}
}
MediaProjection
内部通过VirtualDisplay
来实现屏幕录制,ActivityView
本质上是一个虚拟屏幕VirtualDisplay
。
接着主要调用ActivityView.startActivity
来启动Activity。
public void startActivity(Intent intent) {
ActivityOptions options = this.prepareActivityOptions();
this.getContext().startActivity(intent, options.toBundle());
}
ActivityOptions
为Activity的配置项,包含了虚拟屏幕的displayId。
ActivityOptions.java
源码地址:
/frameworks/base/core/java/android/app/ActivityOptions.java
通过setLaunchDisplayId
设置对应虚拟屏幕。
private ActivityOptions prepareActivityOptions() {
if (this.mVirtualDisplay == null) {
throw new IllegalStateException("Trying to start activity before ActivityView is ready.");
} else {
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(this.mVirtualDisplay.getDisplay().getDisplayId());
return options;
}
}
setLaunchDisplayId
源码入下:
/**
* Sets the id of the display where activity should be launched.
* An app can launch activities on public displays or private displays that are owned by the app
* or where an app already has activities. Otherwise, trying to launch on a private display
* or providing an invalid display id will result in an exception.
* <p>
* Setting launch display id will be ignored on devices that don't have
* {@link android.content.pm.PackageManager#FEATURE_ACTIVITIES_ON_SECONDARY_DISPLAYS}.
* @param launchDisplayId The id of the display where the activity should be launched.
* @return {@code this} {@link ActivityOptions} instance.
*/
public ActivityOptions setLaunchDisplayId(int launchDisplayId) {
mLaunchDisplayId = launchDisplayId;
return this;
}
ActivityOptions
以KEY_LAUNCH_DISPLAY_ID
最为key值传入displayId。
```java
private static final String KEY_LAUNCH_DISPLAY_ID = "android.activity.launchDisplayId";
public Bundle toBundle() {
...
if (mLaunchDisplayId != INVALID_DISPLAY) {
b.putInt(KEY_LAUNCH_DISPLAY_ID, mLaunchDisplayId);
}
...
}
ActivityOptions
最终SafeActivityOptions
在框架中应用。
public static SafeActivityOptions fromBundle(Bundle bOptions) {
return bOptions != null
? new SafeActivityOptions(ActivityOptions.fromBundle(bOptions))
: null;
}
在Context.startActivity
的流程中通过RootActivityContainer
进行处理。
RootActivityContainer
源码地址:
frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
RootActivityContainer
创建ActivityDisplay
,每个ActivityDisplay
对应一个VirtualDisplay
。
ActivityDisplay
源码地址:
/frameworks/base/services/core/java/com/android/server/wm/ActivityDisplay.java
ActivityDisplay
与WMS交互来控制窗口显示。