Skip to content

Commit

Permalink
Start the virtualdisplay‘s desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjian.scj committed Feb 11, 2025
1 parent 96e1075 commit 887a51e
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.genymobile.scrcpy.video;

import com.genymobile.scrcpy.AndroidVersions;
import com.genymobile.scrcpy.FakeContext;
import com.genymobile.scrcpy.Options;
import com.genymobile.scrcpy.control.PositionMapper;
import com.genymobile.scrcpy.device.DisplayInfo;
Expand All @@ -14,6 +15,10 @@
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.wrappers.ServiceManager;

import android.app.ActivityOptions;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Rect;
import android.hardware.display.VirtualDisplay;
import android.os.Build;
Expand Down Expand Up @@ -166,8 +171,7 @@ public void prepare() {
public void startNew(Surface surface) {
int virtualDisplayId;
try {
int flags = VIRTUAL_DISPLAY_FLAG_PUBLIC
| VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
int flags = VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
| VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH
| VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT;
if (vdDestroyContent) {
Expand All @@ -177,7 +181,8 @@ public void startNew(Surface surface) {
flags |= VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
}
if (Build.VERSION.SDK_INT >= AndroidVersions.API_33_ANDROID_13) {
flags |= VIRTUAL_DISPLAY_FLAG_TRUSTED
flags |= VIRTUAL_DISPLAY_FLAG_PUBLIC
| VIRTUAL_DISPLAY_FLAG_TRUSTED
| VIRTUAL_DISPLAY_FLAG_OWN_DISPLAY_GROUP
| VIRTUAL_DISPLAY_FLAG_ALWAYS_UNLOCKED
| VIRTUAL_DISPLAY_FLAG_TOUCH_FEEDBACK_DISABLED;
Expand All @@ -191,6 +196,10 @@ public void startNew(Surface surface) {
virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
Ln.i("New display: " + displaySize.getWidth() + "x" + displaySize.getHeight() + "/" + dpi + " (id=" + virtualDisplayId + ")");

if (Build.VERSION.SDK_INT < AndroidVersions.API_33_ANDROID_13) {
displayLauncher(virtualDisplayId);
}

displaySizeMonitor.start(virtualDisplayId, this::invalidate);
} catch (Exception e) {
Ln.e("Could not create display", e);
Expand Down Expand Up @@ -258,4 +267,27 @@ private static int scaleDpi(Size initialSize, int initialDpi, Size size) {
public void requestInvalidate() {
invalidate();
}

private void displayLauncher(int virtualDisplayId) {
PackageManager pm = FakeContext.get().getPackageManager();

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo homeResolveInfo = (ResolveInfo) pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);

Intent secondaryHomeIntent = new Intent(Intent.ACTION_MAIN);
secondaryHomeIntent.addCategory(Intent.CATEGORY_SECONDARY_HOME);
ResolveInfo secondaryHomeResolveInfo = (ResolveInfo) pm.resolveActivity(secondaryHomeIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (secondaryHomeResolveInfo.activityInfo.packageName.equals(homeResolveInfo.activityInfo.packageName)) {
Intent launcherIntent = new Intent();
launcherIntent.setClassName(secondaryHomeResolveInfo.activityInfo.packageName, secondaryHomeResolveInfo.activityInfo.name);
launcherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(virtualDisplayId);

ServiceManager.getActivityManager().startActivity(launcherIntent, options.toBundle());
}
}
}

0 comments on commit 887a51e

Please sign in to comment.