From 6f7ec7f723d3e479ef1ee6e1f619432bac7f2ee0 Mon Sep 17 00:00:00 2001
From: Fredia Huya-Kouadio <fhuyakou@gmail.com>
Date: Tue, 31 May 2022 23:26:03 -0700
Subject: Misc editor tweaks and polishes: - Using a bucketized approach to
 select the editor scale in order to avoid too high values - Add default app
 dimensions: used on Android devices with free floating app windows to set the
 default app frame - Add ability to launch the Game window in an adjacent
 frame when in multi window mode

---
 platform/android/java/app/config.gradle            |  2 +-
 platform/android/java/editor/build.gradle          |  2 ++
 .../java/editor/src/main/AndroidManifest.xml       |  7 ++++++
 .../java/org/godotengine/editor/GodotEditor.java   | 29 +++++++++++++++++++++-
 .../java/editor/src/main/res/values/dimens.xml     |  5 ++++
 .../lib/src/org/godotengine/godot/GodotIO.java     | 24 +++++++++++++++---
 6 files changed, 64 insertions(+), 5 deletions(-)
 create mode 100644 platform/android/java/editor/src/main/res/values/dimens.xml

(limited to 'platform/android/java')

diff --git a/platform/android/java/app/config.gradle b/platform/android/java/app/config.gradle
index 1b2976e715..1ee3baea03 100644
--- a/platform/android/java/app/config.gradle
+++ b/platform/android/java/app/config.gradle
@@ -1,6 +1,6 @@
 ext.versions = [
     androidGradlePlugin: '7.0.3',
-    compileSdk         : 30,
+    compileSdk         : 31,
     minSdk             : 19, // Also update 'platform/android/java/lib/AndroidManifest.xml#minSdkVersion' & 'platform/android/export/export_plugin.cpp#DEFAULT_MIN_SDK_VERSION'
     targetSdk          : 30, // Also update 'platform/android/java/lib/AndroidManifest.xml#targetSdkVersion' & 'platform/android/export/export_plugin.cpp#DEFAULT_TARGET_SDK_VERSION'
     buildTools         : '30.0.3',
diff --git a/platform/android/java/editor/build.gradle b/platform/android/java/editor/build.gradle
index 3312f61ad3..940ae67073 100644
--- a/platform/android/java/editor/build.gradle
+++ b/platform/android/java/editor/build.gradle
@@ -5,6 +5,8 @@ dependencies {
     implementation libraries.kotlinStdLib
     implementation libraries.androidxFragment
     implementation project(":lib")
+
+    implementation "androidx.window:window:1.0.0"
 }
 
 android {
diff --git a/platform/android/java/editor/src/main/AndroidManifest.xml b/platform/android/java/editor/src/main/AndroidManifest.xml
index bae075d929..659caf7ab4 100644
--- a/platform/android/java/editor/src/main/AndroidManifest.xml
+++ b/platform/android/java/editor/src/main/AndroidManifest.xml
@@ -34,6 +34,9 @@
             android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
             android:process=":GodotProjectManager">
 
+            <layout android:defaultHeight="@dimen/editor_default_window_height"
+                android:defaultWidth="@dimen/editor_default_window_width" />
+
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
@@ -47,6 +50,8 @@
             android:launchMode="singleTask"
             android:screenOrientation="userLandscape"
             android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
+            <layout android:defaultHeight="@dimen/editor_default_window_height"
+                android:defaultWidth="@dimen/editor_default_window_width" />
         </activity>
 
         <activity
@@ -57,6 +62,8 @@
             android:launchMode="singleTask"
             android:screenOrientation="userLandscape"
             android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
+            <layout android:defaultHeight="@dimen/editor_default_window_height"
+                android:defaultWidth="@dimen/editor_default_window_width" />
         </activity>
 
     </application>
diff --git a/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.java b/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.java
index 8a6bf88267..c05ed965ca 100644
--- a/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.java
+++ b/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.java
@@ -34,10 +34,13 @@ import org.godotengine.godot.FullScreenGodotApp;
 import org.godotengine.godot.utils.PermissionsUtil;
 
 import android.content.Intent;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Debug;
 
 import androidx.annotation.Nullable;
+import androidx.window.layout.WindowMetrics;
+import androidx.window.layout.WindowMetricsCalculator;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -91,23 +94,47 @@ public class GodotEditor extends FullScreenGodotApp {
 	public void onNewGodotInstanceRequested(String[] args) {
 		// Parse the arguments to figure out which activity to start.
 		Class<?> targetClass = GodotGame.class;
+		// Whether we should launch the new godot instance in an adjacent window
+		// https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_LAUNCH_ADJACENT
+		boolean launchAdjacent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (isInMultiWindowMode() || isLargeScreen());
+
 		for (String arg : args) {
 			if (EDITOR_ARG.equals(arg)) {
 				targetClass = GodotEditor.class;
+				launchAdjacent = false;
 				break;
 			}
 
 			if (PROJECT_MANAGER_ARG.equals(arg)) {
 				targetClass = GodotProjectManager.class;
+				launchAdjacent = false;
 				break;
 			}
 		}
 
 		// Launch a new activity
-		Intent newInstance = new Intent(this, targetClass).putExtra(COMMAND_LINE_PARAMS, args);
+		Intent newInstance = new Intent(this, targetClass)
+									 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+									 .putExtra(COMMAND_LINE_PARAMS, args);
+		if (launchAdjacent) {
+			newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
+		}
 		startActivity(newInstance);
 	}
 
+	protected boolean isLargeScreen() {
+		WindowMetrics metrics =
+				WindowMetricsCalculator.getOrCreate().computeMaximumWindowMetrics(this);
+
+		// Get the screen's density scale
+		float scale = getResources().getDisplayMetrics().density;
+
+		// Get the minimum window size
+		float minSize = Math.min(metrics.getBounds().width(), metrics.getBounds().height());
+		float minSizeDp = minSize / scale;
+		return minSizeDp >= 840f; // Correspond to the EXPANDED window size class.
+	}
+
 	@Override
 	public void setRequestedOrientation(int requestedOrientation) {
 		if (!overrideOrientationRequest()) {
diff --git a/platform/android/java/editor/src/main/res/values/dimens.xml b/platform/android/java/editor/src/main/res/values/dimens.xml
new file mode 100644
index 0000000000..03fb6184d2
--- /dev/null
+++ b/platform/android/java/editor/src/main/res/values/dimens.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+	<dimen name="editor_default_window_height">600dp</dimen>
+	<dimen name="editor_default_window_width">800dp</dimen>
+</resources>
diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
index b69d25dd8b..a8e3669ac6 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
+++ b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
@@ -224,12 +224,30 @@ public class GodotIO {
 	}
 
 	public int getScreenDPI() {
-		DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
-		return (int)(metrics.density * 160f);
+		return activity.getResources().getDisplayMetrics().densityDpi;
 	}
 
+	/**
+	 * Returns bucketized density values.
+	 */
 	public float getScaledDensity() {
-		return activity.getResources().getDisplayMetrics().scaledDensity;
+		int densityDpi = activity.getResources().getDisplayMetrics().densityDpi;
+		float selectedScaledDensity;
+		if (densityDpi >= DisplayMetrics.DENSITY_XXXHIGH) {
+			selectedScaledDensity = 4.0f;
+		} else if (densityDpi >= DisplayMetrics.DENSITY_XXHIGH) {
+			selectedScaledDensity = 3.0f;
+		} else if (densityDpi >= DisplayMetrics.DENSITY_XHIGH) {
+			selectedScaledDensity = 2.0f;
+		} else if (densityDpi >= DisplayMetrics.DENSITY_HIGH) {
+			selectedScaledDensity = 1.5f;
+		} else if (densityDpi >= DisplayMetrics.DENSITY_MEDIUM) {
+			selectedScaledDensity = 1.0f;
+		} else {
+			selectedScaledDensity = 0.75f;
+		}
+		Log.d(TAG, "Selected scaled density: " + selectedScaledDensity);
+		return selectedScaledDensity;
 	}
 
 	public double getScreenRefreshRate(double fallback) {
-- 
cgit v1.2.3