From 87cc9a3fc60a81d4c2b32ce9223b8e4aa5e3a06a Mon Sep 17 00:00:00 2001 From: mopsicus Date: Tue, 27 Aug 2024 15:48:50 +0300 Subject: [PATCH] fix: methods for more flexible keyboard height control #114 --- .../com/mopsicus/umi/KeyboardProvider.java | 17 ++++++++- .../main/java/com/mopsicus/umi/Plugin.java | 37 ++++++++++++++++--- CHANGELOG.md | 7 ++++ Documentation~/index.md | 9 +++++ README.md | 1 + README.ru.md | 1 + Runtime/MobileInput.cs | 34 ++++++++++++++--- 7 files changed, 94 insertions(+), 12 deletions(-) diff --git a/Android~/plugin/src/main/java/com/mopsicus/umi/KeyboardProvider.java b/Android~/plugin/src/main/java/com/mopsicus/umi/KeyboardProvider.java index f414404..ffdc3c1 100644 --- a/Android~/plugin/src/main/java/com/mopsicus/umi/KeyboardProvider.java +++ b/Android~/plugin/src/main/java/com/mopsicus/umi/KeyboardProvider.java @@ -129,6 +129,21 @@ private int getNavigationBarHeight() { */ } + /** + * Get height of navigation bar for custom behaviour + */ + public int getNavBarHeight() { + if (!checkSoftKeys() || getNavBarNavigationType() == 2) { + return 0; + } + Resources resources = activity.getResources(); + int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (resourceId > 0) { + return resources.getDimensionPixelSize(resourceId); + } + return 0; + } + /** * Return type of screen navigation * 0 : Navigation is displaying with 3 buttons @@ -138,7 +153,7 @@ private int getNavigationBarHeight() { * @return int of type */ @SuppressWarnings("unused") - private int getNavBarNavigationType() { + public int getNavBarNavigationType() { Resources resources = activity.getResources(); @SuppressLint("DiscouragedApi") int resourceId = resources.getIdentifier("config_navBarInteractionMode", "integer", "android"); if (resourceId > 0) { diff --git a/Android~/plugin/src/main/java/com/mopsicus/umi/Plugin.java b/Android~/plugin/src/main/java/com/mopsicus/umi/Plugin.java index c420fe2..efa243f 100644 --- a/Android~/plugin/src/main/java/com/mopsicus/umi/Plugin.java +++ b/Android~/plugin/src/main/java/com/mopsicus/umi/Plugin.java @@ -8,6 +8,7 @@ import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; + import com.unity3d.player.UnityPlayer; import org.json.JSONException; @@ -57,12 +58,13 @@ public class Plugin { /** * Get view recursive + * * @param view View to start search * @return Last view */ private static View getLeafView(View view) { if (view instanceof ViewGroup) { - ViewGroup viewGroup = (ViewGroup)view; + ViewGroup viewGroup = (ViewGroup) view; for (int i = 0; i < viewGroup.getChildCount(); ++i) { View result = getLeafView(viewGroup.getChildAt(i)); if (result != null) { @@ -70,8 +72,7 @@ private static View getLeafView(View view) { } } return null; - } - else { + } else { return view; } } @@ -98,7 +99,7 @@ public static void init(final String data) { if (layout != null) { group.removeView(layout); } - ViewGroup rootView = activity.findViewById (android.R.id.content); + ViewGroup rootView = activity.findViewById(android.R.id.content); View topMostView = getLeafView(rootView); group = (ViewGroup) topMostView.getParent(); layout = new RelativeLayout(activity); @@ -110,6 +111,29 @@ public static void init(final String data) { }); } + /** + * Get height of navigation bar + * + * @return int of value + */ + @SuppressWarnings("unused") + public static int getBarHeight() { + return keyboardProvider.getNavBarHeight(); + } + + /** + * Return type of screen navigation + * 0 : Navigation is displaying with 3 buttons + * 1 : Navigation is displaying with 2 button(Android P navigation mode) + * 2 : Full screen gesture(Gesture on android Q) + * + * @return int of type + */ + @SuppressWarnings("unused") + public static int getBarType() { + return keyboardProvider.getNavBarNavigationType(); + } + /** * Destroy plugin, remove layout */ @@ -128,7 +152,8 @@ public static void destroy() { /** * Send data to MobileInput - * @param id Input id + * + * @param id Input id * @param data Data to process */ @SuppressWarnings("unused") @@ -140,7 +165,7 @@ public static void execute(final int id, final String data) { * Check device rotate locking */ @SuppressWarnings("unused") - public static boolean checkIsRotateLocked () { + public static boolean checkIsRotateLocked() { int val = Settings.System.getInt(UnityPlayer.currentActivity.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0); return (val != 1); } diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f5ee16..f66d2d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. +## [2.0.2] - 2024-08-27 +- ### Added +- Custom caret/cursor/handles color +- Methods for more flexible keyboard height control #114 +- ### Fixed +- Prevent double field initialization + ## [2.0.1] - 2024-05-02 - ### Fixed - Keyboard autohide on tap #110 diff --git a/Documentation~/index.md b/Documentation~/index.md index f5dfe2b..2c9ed6f 100644 --- a/Documentation~/index.md +++ b/Documentation~/index.md @@ -40,6 +40,12 @@ This script manages the interaction between the Unity app and native part. It co `UpdateFonts()` – update fonts in app folder when changing fonts list +`IsRotationLocked()` – check is screen rotation locked (Android) + +`GetBarType()` – get navigation bar type (Android) + +`GetBarHeight()` – get navigation bar height, if exist (Android) + `Action OnKeyboardAction` and `Action OnOrientationChange` – two events that you can subscribe to and detect keyboard appearance and orientation changes ```csharp @@ -120,6 +126,7 @@ Many options from `TMP Input Field` will be applied to the native field. You can - text color - placeholder text - placeholder text color +- cursor/caret color - character limit - font size - text align @@ -171,6 +178,8 @@ Different native input types are used for multiline and singleline options: `UIT When you get the keyboard height from `OnKeyboardAction`, you get the "true" height. To convert it to Unity UI height, you need to divide it by the screen aspect ratio. Screen aspect ratio is the ratio of the height of the screen to the height of the UI canvas and to the screen scale (for iOS). +For Android builds, you can use the `GetBarType()` and `GetBarHeight()` methods to determine the screen device parameters for more flexible control. + See [demo app](../Samples~/Demo/) with chat screen. diff --git a/README.md b/README.md index 6274137..f85c936 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ In the inspector, you can edit several options that will be applied to the nativ - text color - placeholder text - placeholder text color +- cursor/caret color - character limit - font size - text align diff --git a/README.ru.md b/README.ru.md index 163de70..0415918 100644 --- a/README.ru.md +++ b/README.ru.md @@ -83,6 +83,7 @@ public class Bootstrap : MonoBehaviour { - цвет текста - цвет текста подсказки +- цвет курсора/каретки - текст подсказки - лимит символов - размер шрифта diff --git a/Runtime/MobileInput.cs b/Runtime/MobileInput.cs index eb5ca9b..c37f36b 100644 --- a/Runtime/MobileInput.cs +++ b/Runtime/MobileInput.cs @@ -312,11 +312,35 @@ void OnDataReceive(string data) { /// /// Check if screen rotation locked /// - bool IsRotatationLocked() { + public static bool IsRotationLocked() { using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) { return plugin.CallStatic("checkIsRotateLocked"); } } + + /// + /// Return type of screen navigation + /// + /// 0 : Navigation is displaying with 3 buttons + /// 1 : Navigation is displaying with 2 button(Android P navigation mode) + /// 2 : Full screen gesture(Gesture on android Q) + /// + /// Type of navigation bar + public static int GetBarType() { + using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) { + return plugin.CallStatic("getBarType"); + } + } + + /// + /// Get height of navigation bar + /// + /// Height of navbar + public static int GetBarHeight() { + using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) { + return plugin.CallStatic("getBarHeight"); + } + } #endif /// @@ -427,12 +451,12 @@ public static void Destroy() { /// Check screen scale factor (iOS) /// public static float GetScreenScale() { -#if UNITY_ANDROID - return 1f; -#elif UNITY_IOS +#if UNITY_IOS return scaleFactor(); +#else + return 1f; #endif - } + } /// /// Update fonts