浏览代码

android safe area

/siyaoH-1.17-PlatformMessage
siyao 4 年前
当前提交
88d5673e
共有 18 个文件被更改,包括 531 次插入11 次删除
  1. 1
      Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/GalleryMain.cs
  2. 20
      com.unity.uiwidgets/Runtime/engine2/AndroidPlatformUtil.cs
  3. 86
      com.unity.uiwidgets/Runtime/engine2/DisplayMetrics.cs
  4. 6
      com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs
  5. 2
      com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanelWrapper.cs
  6. 1
      com.unity.uiwidgets/Runtime/rendering/binding.cs
  7. 5
      com.unity.uiwidgets/Runtime/ui2/window.cs
  8. 2
      com.unity.uiwidgets/Runtime/widgets/binding.cs
  9. 44
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java
  10. 32
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java.meta
  11. 216
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java
  12. 32
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java.meta
  13. 13
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java
  14. 32
      com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java.meta
  15. 18
      com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java
  16. 32
      com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java.meta

1
Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/GalleryMain.cs


protected override void onEnable()
{
AndroidPlatformUtil.SetStatusBarValue(true);
AddFont("Material Icons", new List<string> {"MaterialIcons-Regular.ttf"}, new List<int> {0});
AddFont("CupertinoIcons", new List<string> {"CupertinoIcons.ttf"}, new List<int> {0});
AddFont("GalleryIcons", new List<string> {"gallery/GalleryIcons.ttf"}, new List<int> {0});

20
com.unity.uiwidgets/Runtime/engine2/AndroidPlatformUtil.cs


#if !UNITY_EDITOR && UNITY_ANDROID
#if UNITY_ANDROID
using System;
namespace Unity.UIWidgets.engine2 {
public static class AndroidPlatformUtil {
[DllImport(NativeBindings.dllName)]

var dir = Application.temporaryCachePath + "/";
if (!File.Exists(dir + file)) {
var path = "jar:file://" + Application.dataPath + "!/assets/" + file;
using(var unpackerWWW = UnityWebRequest.Get(path)) {
using (var unpackerWWW = UnityWebRequest.Get(path)) {
File.WriteAllBytes(fileInfo.FullName, data);
File.WriteAllBytes(fileInfo.FullName, data);
[DllImport(NativeBindings.dllName)]
static extern System.IntPtr GetUnityContextEventFunc();

}
public static void SetStatusBarValue(bool value) {
#if !UNITY_EDITOR
using (var util = new AndroidJavaClass("com.unity.uiwidgets.plugin.Utils")) {
util.CallStatic("SetStatusBarState", value);
}
#endif
}
}
}

86
com.unity.uiwidgets/Runtime/engine2/DisplayMetrics.cs


using System.Runtime.InteropServices;
using Unity.UIWidgets.ui;
[StructLayout(LayoutKind.Sequential)]
public struct viewMetrics {
public float insets_top;
public float insets_bottom;
public float insets_left;
public float insets_right;
public float padding_top;
public float padding_bottom;
public float padding_left;
public float padding_right;
}
public class DisplayMetrics {
float _devicePixelRatioByDefault;

return _devicePixelRatioByDefault;
}
#if UNITY_ANDROID
_devicePixelRatioByDefault = AndroidDevicePixelRatio();
#endif

}
}
public WindowPadding viewPadding {
get {
return new WindowPadding(viewMetrics.padding_left,
viewMetrics.padding_top,
viewMetrics.padding_right,
viewMetrics.padding_bottom);
}
}
public WindowPadding viewInsets {
get {
return new WindowPadding(viewMetrics.insets_left,
viewMetrics.insets_top,
viewMetrics.insets_right,
viewMetrics.insets_bottom);
}
}
private viewMetrics? _viewMetrics;
public viewMetrics viewMetrics {
get {
if (_viewMetrics != null) {
return _viewMetrics.Value;
}
#if UNITY_ANDROID && !UNITY_EDITOR
using (
AndroidJavaClass viewController =
new AndroidJavaClass("com.unity.uiwidgets.plugin.UIWidgetsViewController")
) {
AndroidJavaObject metrics = viewController.CallStatic<AndroidJavaObject>("getMetrics");
float insets_bottom = metrics.Get<float>("insets_bottom");
float insets_top = metrics.Get<float>("insets_top");
float insets_left = metrics.Get<float>("insets_left");
float insets_right = metrics.Get<float>("insets_right");
float padding_bottom = metrics.Get<float>("padding_bottom");
float padding_top = metrics.Get<float>("padding_top");
float padding_left = metrics.Get<float>("padding_left");
float padding_right = metrics.Get<float>("padding_right");
_viewMetrics = new viewMetrics {
insets_bottom = insets_bottom,
insets_left = insets_left,
insets_right = insets_right,
insets_top = insets_top,
padding_left = padding_left,
padding_top = padding_top,
padding_right = padding_right,
padding_bottom = padding_bottom
};
}
#else
this._viewMetrics = new viewMetrics {
insets_bottom = 0,
insets_left = 0,
insets_right = 0,
insets_top = 0,
padding_left = 0,
padding_top = 0,
padding_right = 0,
padding_bottom = 0
};
#endif
return _viewMetrics.Value;
}
}
#if UNITY_ANDROID
static float AndroidDevicePixelRatio() {
using (

}
}
#endif
}
}

6
com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs


public static List<UIWidgetsPanel> panels = new List<UIWidgetsPanel>();
static bool _ShowDebugLog;
DisplayMetrics _displayMetrics = new DisplayMetrics();
float _devicePixelRatioOverride;
public bool hardwareAntiAliasing;

float _currentDevicePixelRatio {
get {
#if !UNITY_EDITOR
return _displayMetrics.DevicePixelRatioByDefault;
return _wrapper.displayMetrics.DevicePixelRatioByDefault;
#endif
var currentDpi = Screen.dpi;
if (currentDpi == 0) {

2
com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanelWrapper.cs


public Isolate isolate { get; private set; }
public float devicePixelRatio { get; private set; }
public DisplayMetrics displayMetrics = new DisplayMetrics();
public void Initiate(IUIWidgetsWindow host, int width, int height, float dpr, Configurations _configurations) {
D.assert(renderTexture == null);

1
com.unity.uiwidgets/Runtime/rendering/binding.cs


}
void _handlePersistentFrameCallback(TimeSpan timeStamp) {
window.updateSafeArea();
drawFrame();
_mouseTracker.schedulePostFrameCheck();
}

5
com.unity.uiwidgets/Runtime/ui2/window.cs


}
}
public void updateSafeArea() {
padding = _panel.displayMetrics.viewPadding;
viewInsets = _panel.displayMetrics.viewInsets;
}
public void scheduleFrame() {
Window_scheduleFrame(_ptr);
_panel.window.onNewFrameScheduled();

2
com.unity.uiwidgets/Runtime/widgets/binding.cs


public void attachRootWidget(Widget rootWidget) {
_readyToProduceFrames = true;
window.updateSafeArea();
_renderViewElement = new RenderObjectToWidgetAdapter<RenderBox>(
container: renderView,
debugShortDescription: "[root]",

44
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java


package com.unity.uiwidgets.plugin;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class UIWidgetsMessageManager {
public static final String TAG = "UIWidgets";
private static UIWidgetsMessageManager _instance;
private String gameObjectName;
public static UIWidgetsMessageManager getInstance() {
if (_instance == null) {
_instance = new UIWidgetsMessageManager();
}
return _instance;
}
public void SetObjectName(String name) {
gameObjectName = name;
}
public void UIWidgetsMethodMessage(String channel, String method, List<Object> args) {
JSONObject object = new JSONObject();
try {
object.put("channel", channel);
object.put("method", method);
object.put("args", new JSONArray(args));
UnityPlayer.UnitySendMessage(gameObjectName, "OnUIWidgetsMethodMessage", object.toString());
} catch (JSONException e) {
Log.e(TAG, "error parse json", e);
}
}
}

32
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java.meta


fileFormatVersion: 2
guid: fbff29dbbe1ce46a5bd2b95fef473852
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

216
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java


package com.unity.uiwidgets.plugin;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.view.Surface;
import java.util.Arrays;
import android.view.WindowManager;
import android.os.Handler;
import java.lang.reflect.Method;
public class UIWidgetsViewController {
private static UIWidgetsViewController _instance;
public static UIWidgetsViewController getInstance() {
if (_instance == null) {
_instance = new UIWidgetsViewController();
_instance.setup();
}
return _instance;
}
private UIWidgetsViewMetrics viewMetrics;
private boolean keyboardOpen;
private float statusHeight;
private float navigationBarHeight;
private void setup() {
//Log.i("tag", "On Setup 2");
keyboardOpen = false;
viewMetrics = new UIWidgetsViewMetrics();
setupHeights();
updateViewMetrics();
setupViewMetricsChangedListener();
}
private void setupHeights() {
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0);
Rect rect = new Rect();
unityView.getWindowVisibleDisplayFrame(rect);
statusHeight = rect.top;
navigationBarHeight = unityView.getRootView().getHeight() - rect.bottom;
}
public static UIWidgetsViewMetrics getMetrics() {
UIWidgetsViewController controller = getInstance();
return controller.viewMetrics;
}
enum ZeroSides { NONE, LEFT, RIGHT, BOTH }
ZeroSides calculateShouldZeroSides(View unityView) {
Activity activity = UnityPlayer.currentActivity;
int orientation = activity.getResources().getConfiguration().orientation;
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (rotation == Surface.ROTATION_90) {
return ZeroSides.RIGHT;
}
else if (rotation == Surface.ROTATION_270) {
return Build.VERSION.SDK_INT >= 23 ? ZeroSides.LEFT : ZeroSides.RIGHT;
} else if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
return ZeroSides.BOTH;
}
}
return ZeroSides.NONE;
}
public boolean lastStatusBarHidden = true;
private int calculateBottomKeyboardInset(Rect insets) {
if (keyboardOpen) {
return insets.bottom;
} else {
return 0;
}
}
private int getNavigationBarHeight() {
Resources resources = UnityPlayer.currentActivity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0)
{
return hasNavigationBar() ? resources.getDimensionPixelSize(resourceId) : 0;
}
return 0;
}
private boolean hasNavigationBar() {
boolean hasBar = false;
Resources resources = UnityPlayer.currentActivity.getResources();
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasBar = resources.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasBar = false;
} else if ("0".equals(navBarOverride)) {
hasBar = true;
}
} catch (Exception e) {
e.printStackTrace();
}
//Log.i("UIWidgetsDebug", " hasBar: " + hasBar);
return hasBar;
}
public void updateViewMetrics() {
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0);
Rect rect = new Rect();
unityView.getWindowVisibleDisplayFrame(rect);
//Log.i("UIWidgetsDebug", "calculation: " + unityView.getRootView().getHeight() + " " + rect.bottom + " " + rect.top);
rect.bottom = unityView.getRootView().getHeight() - (rect.bottom - rect.top) - rect.top;
rect.right = unityView.getRootView().getWidth() - (rect.right - rect.left) - rect.left;
boolean statusBarHidden = (View.SYSTEM_UI_FLAG_FULLSCREEN & unityView.getWindowSystemUiVisibility()) != 0;
boolean navigationBarHidden = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION & unityView.getWindowSystemUiVisibility()) != 0;
ZeroSides zeroSides = ZeroSides.NONE;
if (navigationBarHidden) {
zeroSides = calculateShouldZeroSides(unityView);
}
viewMetrics.padding_top = rect.top;
viewMetrics.padding_right = zeroSides == ZeroSides.RIGHT || zeroSides == ZeroSides.BOTH ? 0 : rect.right;
viewMetrics.padding_bottom = 0;
viewMetrics.padding_left = zeroSides == ZeroSides.LEFT || zeroSides == ZeroSides.BOTH ? 0 : rect.left;
viewMetrics.insets_top = 0;
viewMetrics.insets_right = 0;
viewMetrics.insets_bottom = navigationBarHidden? calculateBottomKeyboardInset(rect) : rect.bottom;
viewMetrics.insets_left = 0;
//adjust
viewMetrics.insets_bottom -= navigationBarHeight;
viewMetrics.padding_top -= statusHeight;
//Log.i("UIWidgetsDebug", "checks: " + navigationBarHidden + " " + rect.bottom);
//Log.i("UIWidgetsDebug", " padding: " + viewMetrics.padding_top + " " + viewMetrics.padding_right + " " + viewMetrics.padding_bottom + " " + viewMetrics.padding_left);
//Log.i("UIWidgetsDebug", " insets: " + viewMetrics.insets_top + " " + viewMetrics.insets_right + " " + viewMetrics.insets_bottom + " " + viewMetrics.insets_left);
}
public void setupViewMetricsChangedListener() {
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0);
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
unityView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(final int visibility){
boolean statusBarHidden = (View.SYSTEM_UI_FLAG_FULLSCREEN & visibility) != 0;
unityView.setSystemUiVisibility(visibility);
UIWidgetsViewController controller = getInstance();
if (statusBarHidden != controller.lastStatusBarHidden) {
controller.onViewMetricsChanged();
}
}
});
}
});
unityView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final int defaultKeyboardHeightDP = 100;
private final int estimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
private final Rect rect = new Rect();
@Override
public void onGlobalLayout() {
int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, estimatedKeyboardDP, unityView.getResources().getDisplayMetrics());
unityView.getWindowVisibleDisplayFrame(rect);
int heightDiff = unityView.getRootView().getHeight() - (rect.bottom - rect.top);
boolean isShown = heightDiff >= estimatedKeyboardHeight;
if (keyboardOpen == isShown) {
return;
}
keyboardOpen = isShown;
onViewMetricsChanged();
}
}
);
}
public void onViewMetricsChanged() {
updateViewMetrics();
UIWidgetsMessageManager.getInstance().UIWidgetsMethodMessage("ViewportMatricsChanged", "UIWidgetViewController.keyboardChanged",
Arrays.asList(""));
}
}

32
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java.meta


fileFormatVersion: 2
guid: dd01a28ebf279449dace0dc8580a9ece
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

13
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java


package com.unity.uiwidgets.plugin;
public class UIWidgetsViewMetrics {
public float insets_top;
public float insets_bottom;
public float insets_left;
public float insets_right;
public float padding_top;
public float padding_bottom;
public float padding_left;
public float padding_right;
}

32
com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java.meta


fileFormatVersion: 2
guid: efb8c112055a04b7dae8566ccf299bc7
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

18
com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java


package com.unity.uiwidgets.plugin;
import android.view.WindowManager;
import com.unity3d.player.UnityPlayer;
public class Utils {
public static void SetStatusBarState(final boolean show) {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
if (show) {
UnityPlayer.currentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
UnityPlayer.currentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
});
}
}

32
com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java.meta


fileFormatVersion: 2
guid: 418c6c9d61a804a9e817e77aa7f83f67
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存