AlwaysLoveme / capacitor-plugin-safe-area

capacitor plugin to get safeArea info on Android and IOS, support Capacitor6
MIT License
66 stars 14 forks source link

Android bottom inset is 0 when using windowTranslucentNavigation #3

Closed digaus closed 2 years ago

digaus commented 2 years ago

Expected bottom offset to correctly calculate when using <item name="android:windowTranslucentNavigation">true</item> in the styles.xml, this is not the case :/

AlwaysLoveme commented 2 years ago

Expected bottom offset to correctly calculate when using <item name="android:windowTranslucentNavigation">true</item> in the styles.xml, this is not the case :/

OK,I will fix it

digaus commented 2 years ago
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

            final WindowInsets cutout = window.getDecorView().getRootWindowInsets();
            final float density = activity.getResources().getDisplayMetrics().density;
            JSObject json = new JSObject();
            json.put("top", cutout.getStableInsetTop() / density);
            json.put("bottom", cutout.getStableInsetBottom() / density);
            json.put("left", cutout.getStableInsetLeft() / density);
            json.put("right", cutout.getStableInsetRight() / density);
            ret.put("insets", json);
  }

This is how I was able to get correct values

AlwaysLoveme commented 2 years ago
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

            final WindowInsets cutout = window.getDecorView().getRootWindowInsets();
            final float density = activity.getResources().getDisplayMetrics().density;
            JSObject json = new JSObject();
            json.put("top", cutout.getStableInsetTop() / density);
            json.put("bottom", cutout.getStableInsetBottom() / density);
            json.put("left", cutout.getStableInsetLeft() / density);
            json.put("right", cutout.getStableInsetRight() / density);
            ret.put("insets", json);
  }

This is how I was able to get correct values

Fixed in verison V0.0.6 , please update the npm package 😊

digaus commented 2 years ago

Now android tablet does not return the safe areas anymore. Fixed it this way:


       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            Log.i(SafeAreaPlugin.class.toString(), String.format("Requires at least %d+", Build.VERSION_CODES.P));
            return this.result(0, 0, 0, 0);
        }

        WindowInsets windowInsets = bridge.getActivity().getWindow().getDecorView().getRootWindowInsets();
        if (windowInsets == null) {
            Log.i(SafeAreaPlugin.class.toString(), "WindowInsets is not available.");
            return this.result(0, 0, 0, 0);
        }
        float density = this.getDensity(bridge);

        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        if (displayCutout == null) {
            Log.i(SafeAreaPlugin.class.toString(), "DisplayCutout is not available.");
            int top = Math.round(windowInsets.getStableInsetTop() / density);
            int left = Math.round(windowInsets.getStableInsetLeft() / density);
            int right = Math.round(windowInsets.getStableInsetRight() / density);
            int bottom = Math.round(windowInsets.getStableInsetBottom() / density);

            return this.result(top, left, right, bottom);
        }

        int top = Math.round(displayCutout.getSafeInsetTop() / density);
        int left = Math.round(displayCutout.getSafeInsetLeft() / density);
        int right = Math.round(displayCutout.getSafeInsetRight() / density);
        int bottom = Math.round(displayCutout.getSafeInsetBottom() / density);

        return this.result(top, left, right, bottom);
AlwaysLoveme commented 2 years ago

Now android tablet does not return the safe areas anymore. Fixed it this way:


       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            Log.i(SafeAreaPlugin.class.toString(), String.format("Requires at least %d+", Build.VERSION_CODES.P));
            return this.result(0, 0, 0, 0);
        }

        WindowInsets windowInsets = bridge.getActivity().getWindow().getDecorView().getRootWindowInsets();
        if (windowInsets == null) {
            Log.i(SafeAreaPlugin.class.toString(), "WindowInsets is not available.");
            return this.result(0, 0, 0, 0);
        }
        float density = this.getDensity(bridge);

        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        if (displayCutout == null) {
            Log.i(SafeAreaPlugin.class.toString(), "DisplayCutout is not available.");
            int top = Math.round(windowInsets.getStableInsetTop() / density);
            int left = Math.round(windowInsets.getStableInsetLeft() / density);
            int right = Math.round(windowInsets.getStableInsetRight() / density);
            int bottom = Math.round(windowInsets.getStableInsetBottom() / density);

            return this.result(top, left, right, bottom);
        }

        int top = Math.round(displayCutout.getSafeInsetTop() / density);
        int left = Math.round(displayCutout.getSafeInsetLeft() / density);
        int right = Math.round(displayCutout.getSafeInsetRight() / density);
        int bottom = Math.round(displayCutout.getSafeInsetBottom() / density);

        return this.result(top, left, right, bottom);

fixed in lastest version

digaus commented 2 years ago

displayCutout.getSafeInsetBottom does not return correct value on Samsung S10e device. I think we should always use windowInsets.getStableInsetBottom() and not use displayCutout at all. So far windowInsets.getStableInsetBottom() always worked correctly for me.

AlwaysLoveme commented 2 years ago

Samsung S10e device

I have no Samsung S10e device ,but i think you may right, it will deprecated displayCutout in version 0.0.10

digaus commented 2 years ago

Samsung S10e device

I have no Samsung S10e device ,but i think you may right, it will deprecated displayCutout in version 0.0.10

I am currently also testing on an older device and you should allow all version newer than M

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Log.i(SafeAreaPlugin.class.toString(), String.format("Requires at least %d+", Build.VERSION_CODES.P));
            return this.result(0, 0, 0, 0);
        }
digaus commented 1 year ago

@AlwaysLoveme can you change the version to Build.VERSION_CODES.M instead of Build.VERSION_CODES.P? Tested on Android 6.0.1 Galaxy A3