Commit eb96794b authored by Lijin Shen's avatar Lijin Shen Committed by Commit Bot

Add a multi-display related method to ApiCompatibilityUtils

Move some multi-display related function to api compatibility packages.
1. Guarded by android version check
2. So that can be accessed on other base package

Bug: 1081894
Change-Id: I087c846758a0a894fbf03e7fbdcaa49c3811569a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2204679
Commit-Queue: Lijin Shen <lazzzis@google.com>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770663}
parent 64a26245
......@@ -25,6 +25,7 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
......@@ -37,6 +38,7 @@ import android.provider.Settings;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
......@@ -63,6 +65,8 @@ import org.chromium.base.annotations.VerifiesOnQ;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class to use new APIs that were added after KitKat (API level 19).
......@@ -80,6 +84,25 @@ public class ApiCompatibilityUtils {
static boolean isRunningInUserTestHarness() {
return ActivityManager.isRunningInUserTestHarness();
}
static List<Integer> getTargetableDisplayIds(@Nullable Activity activity) {
List<Integer> displayList = new ArrayList<>();
if (activity == null) return displayList;
DisplayManager displayManager =
(DisplayManager) activity.getSystemService(Context.DISPLAY_SERVICE);
if (displayManager == null) return displayList;
Display[] displays = displayManager.getDisplays();
ActivityManager am =
(ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
for (Display display : displays) {
if (display.getState() == Display.STATE_ON
&& am.isActivityStartAllowedOnDisplay(activity, display.getDisplayId(),
new Intent(activity, activity.getClass()))) {
displayList.add(display.getDisplayId());
}
}
return displayList;
}
}
@VerifiesOnP
......@@ -601,6 +624,22 @@ public class ApiCompatibilityUtils {
return false;
}
/**
* Get a list of ids of targetable displays, including the default display for the
* current activity. A set of targetable displays can only be determined on Q+. An empty list
* is returned if called on prior Q.
* @param activity The {@link Activity} to check.
* @return A list of display ids. Empty if there is none or version is less than Q, or
* windowAndroid does not contain an activity.
*/
@NonNull
public static List<Integer> getTargetableDisplayIds(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return ApisQ.getTargetableDisplayIds(activity);
}
return new ArrayList<>();
}
/**
* Disables the Smart Select {@link TextClassifier} for the given {@link TextView} instance.
* @param textView The {@link TextView} that should have its classifier disabled.
......
......@@ -11,14 +11,12 @@ import android.app.ActivityManager.AppTask;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Browser;
import android.text.TextUtils;
import android.view.Display;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
......@@ -38,7 +36,6 @@ import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -76,13 +73,12 @@ public class MultiWindowUtils implements ActivityStateListener {
/**
* @param activity The {@link Activity} to check.
* @return Whether the system currently supports multiple displays.
* @return Whether the system currently supports multiple displays, requiring Android Q+.
*/
public boolean isInMultiDisplayMode(Activity activity) {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.ANDROID_MULTIPLE_DISPLAY)) return false;
// TODO(crbug.com/824954): Consider supporting more displays. Update function signature
// to pass in WindowAndroid instead.
return getTargetableDisplayIds(activity).size() == 1;
// TODO(crbug.com/824954): Consider supporting more displays.
return ApiCompatibilityUtils.getTargetableDisplayIds(activity).size() == 2;
}
@VisibleForTesting
......@@ -120,36 +116,6 @@ public class MultiWindowUtils implements ActivityStateListener {
}
}
/**
* Get a list of ids of targetable displays, excluding the default display for the
* current activity. A set of targetable displays can only be determined on Q+. An empty list
* is returned if called on prior Q.
* @param activity The {@link Activity} to check.
* @return A list of display ids. Empty if there is none or version is less than Q, or
* windowAndroid does not contain an activity.
*/
@NonNull
public static List<Integer> getTargetableDisplayIds(Activity activity) {
List<Integer> displayList = new ArrayList<>();
if (activity == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return displayList;
DisplayManager displayManager =
(DisplayManager) activity.getSystemService(Context.DISPLAY_SERVICE);
if (displayManager == null) return displayList;
Display[] displays = displayManager.getDisplays();
if (displays == null) return displayList;
Display defaultDisplay = DisplayAndroidManager.getDefaultDisplayForContext(activity);
ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
for (Display display : displays) {
if (defaultDisplay.getDisplayId() != display.getDisplayId()
&& display.getState() == Display.STATE_ON
&& am.isActivityStartAllowedOnDisplay(activity, display.getDisplayId(),
new Intent(activity, activity.getClass()))) {
displayList.add(display.getDisplayId());
}
}
return displayList;
}
/**
* Sets extras on the intent used when handling "open in other window" or
* "move to other window". Specifically, sets the class, adds the launch adjacent flag, and
......@@ -207,13 +173,18 @@ public class MultiWindowUtils implements ActivityStateListener {
* @return The targetable secondary display. {@code Display.INVALID_DISPLAY} if not found.
*/
public static int getDisplayIdForTargetableSecondaryDisplay(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
|| !ChromeFeatureList.isEnabled(ChromeFeatureList.ANDROID_MULTIPLE_DISPLAY)) {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.ANDROID_MULTIPLE_DISPLAY)) {
return Display.INVALID_DISPLAY;
}
// TODO(crbug.com/824954): Update function signature to pass in WindowAndroid instead.
List<Integer> displays = getTargetableDisplayIds(activity);
if (displays.size() != 0) return displays.get(0);
List<Integer> displays = ApiCompatibilityUtils.getTargetableDisplayIds(activity);
Display defaultDisplay = DisplayAndroidManager.getDefaultDisplayForContext(activity);
if (displays.size() != 0) {
for (int id : displays) {
if (id != defaultDisplay.getDisplayId()) {
return id;
}
}
}
return Display.INVALID_DISPLAY;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment