Java Code Examples for android.view.WindowInsets#getDisplayCutout()

The following examples show how to use android.view.WindowInsets#getDisplayCutout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatingViewManager.java    From dingo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Find the safe area of DisplayCutout.
 *
 * @param activity {@link Activity} (Portrait and `windowLayoutInDisplayCutoutMode` != never)
 * @return Safe cutout insets.
 */
public static Rect findCutoutSafeArea(@NonNull Activity activity) {
    final Rect safeInsetRect = new Rect();
    // TODO:Rewrite with android-x
    // TODO:Consider alternatives
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
        return safeInsetRect;
    }

    // Fix: getDisplayCutout() on a null object reference (issue #110)
    final WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
    if (windowInsets == null) {
        return safeInsetRect;
    }

    // set safeInsetRect
    final DisplayCutout displayCutout = windowInsets.getDisplayCutout();
    if (displayCutout != null) {
        safeInsetRect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
    }

    return safeInsetRect;
}
 
Example 2
Source File: CutoutUtil.java    From DKVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 是否为允许全屏界面显示内容到刘海区域的刘海屏机型(与AndroidManifest中配置对应)
 */
public static boolean allowDisplayToCutout(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        // 9.0系统全屏界面默认会保留黑边,不允许显示内容到刘海区域
        Window window = activity.getWindow();
        WindowInsets windowInsets = window.getDecorView().getRootWindowInsets();
        if (windowInsets == null) {
            return false;
        }
        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        if (displayCutout == null) {
            return false;
        }
        List<Rect> boundingRects = displayCutout.getBoundingRects();
        return boundingRects.size() > 0;
    } else {
        return hasCutoutHuawei(activity)
                || hasCutoutOPPO(activity)
                || hasCutoutVIVO(activity)
                || hasCutoutXIAOMI(activity);
    }
}
 
Example 3
Source File: FloatingViewManager.java    From FloatingView with Apache License 2.0 6 votes vote down vote up
/**
 * Find the safe area of DisplayCutout.
 *
 * @param activity {@link Activity} (Portrait and `windowLayoutInDisplayCutoutMode` != never)
 * @return Safe cutout insets.
 */
public static Rect findCutoutSafeArea(@NonNull Activity activity) {
    final Rect safeInsetRect = new Rect();
    // TODO:Rewrite with android-x
    // TODO:Consider alternatives
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
        return safeInsetRect;
    }

    // Fix: getDisplayCutout() on a null object reference (issue #110)
    final WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
    if (windowInsets == null) {
        return safeInsetRect;
    }

    // set safeInsetRect
    final DisplayCutout displayCutout = windowInsets.getDisplayCutout();
    if (displayCutout != null) {
        safeInsetRect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
    }

    return safeInsetRect;
}
 
Example 4
Source File: StreamFragment.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.P)
private DisplayCutout getDisplayCutout() {
    Activity activity = getActivity();
    if (activity != null) {
        WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
        if (windowInsets != null) {
            return windowInsets.getDisplayCutout();
        }
    }

    return null;
}
 
Example 5
Source File: AppUtils.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@TargetApi(28)
private static boolean attachHasOfficialNotch(View view) {
    WindowInsets windowInsets = view.getRootWindowInsets();
    if (windowInsets != null) {
        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        return displayCutout != null;
    } else {
        throw new IllegalStateException("activity has not yet attach to window, you must call `isCutout` after `Activity#onAttachedToWindow` is called.");
    }
}
 
Example 6
Source File: DeviceUtil.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
/**
 * 判断全面屏
 *
 * @param activity Activity
 * @return boolean
 */
public static boolean isFullScreenDisplay(@NonNull Activity activity) {
    if (isSupportP()) {
        // 谷歌官方提供
        Window window = activity.getWindow();
        if (window != null) {
            WindowInsets windowInsets = window.getDecorView().getRootWindowInsets();
            if (windowInsets != null) {
                DisplayCutout displayCutout = windowInsets.getDisplayCutout();
                if (displayCutout != null) {
                    List<Rect> rects = displayCutout.getBoundingRects();
                    // 一般为刘海屏即为全面屏
                    if (rects != null && rects.size() > 0) {
                        return true;
                    }
                }
            }
        }
    }

    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    if (wm != null) {
        Display defaultDisplay = wm.getDefaultDisplay();
        if (defaultDisplay != null) {
            DisplayMetrics dm = new DisplayMetrics();
            defaultDisplay.getRealMetrics(dm);
            float heightPixels = dm.heightPixels;
            float widthPixels = dm.widthPixels;
            // 通过分比率比例去判断
            return heightPixels / widthPixels >= 1.96f;
        }
    }
    return false;
}
 
Example 7
Source File: NotchUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
/**
 * @param view
 * @return false indicates the failure to get the result
 */
@TargetApi(28)
private static boolean attachHasOfficialNotch(View view) {
    WindowInsets windowInsets = view.getRootWindowInsets();
    if (windowInsets != null) {
        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        sHasNotch = displayCutout != null;
        return true;
    } else {
        // view not attached, do nothing
        return false;
    }
}
 
Example 8
Source File: NotchUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
@TargetApi(28)
private static void getOfficialSafeInsetRect(View view, Rect out) {
    if (view == null) {
        return;
    }
    WindowInsets rootWindowInsets = view.getRootWindowInsets();
    if (rootWindowInsets == null) {
        return;
    }
    DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
    if (displayCutout != null) {
        out.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(),
                displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
    }
}
 
Example 9
Source File: SkinNotchUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
/**
 *
 * @param view
 * @return false indicates the failure to get the result
 */
@TargetApi(28)
private static boolean attachHasOfficialNotch(View view){
    WindowInsets windowInsets = view.getRootWindowInsets();
    if(windowInsets != null){
        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        sHasNotch = displayCutout != null;
        return true;
    }else{
        // view not attached, do nothing
        return false;
    }
}
 
Example 10
Source File: SkinNotchUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
@TargetApi(28)
private static void getOfficialSafeInsetRect(View view, Rect out) {
    if(view == null){
        return;
    }
    WindowInsets rootWindowInsets = view.getRootWindowInsets();
    if(rootWindowInsets == null){
        return;
    }
    DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
    if(displayCutout != null){
        out.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(),
                displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
    }
}
 
Example 11
Source File: NotchUtils.java    From ImmersionBar with Apache License 2.0 5 votes vote down vote up
/**
 * 获得DisplayCutout
 * Gets display cutout.
 *
 * @param activity the activity
 * @return the display cutout
 */
private static DisplayCutout getDisplayCutout(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (activity != null) {
            Window window = activity.getWindow();
            if (window != null) {
                WindowInsets windowInsets = window.getDecorView().getRootWindowInsets();
                if (windowInsets != null) {
                    return windowInsets.getDisplayCutout();
                }
            }
        }
    }
    return null;
}
 
Example 12
Source File: NotchUtils.java    From ImmersionBar with Apache License 2.0 5 votes vote down vote up
private static DisplayCutout getDisplayCutout(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (view != null) {
            WindowInsets windowInsets = view.getRootWindowInsets();
            if (windowInsets != null) {
                return windowInsets.getDisplayCutout();
            }
        }
    }
    return null;
}
 
Example 13
Source File: NotchUtils.java    From MNImageBrowser with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获得DisplayCutout
 * Gets display cutout.
 *
 * @param activity the activity
 * @return the display cutout
 */
private static DisplayCutout getDisplayCutout(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (activity != null) {
            Window window = activity.getWindow();
            if (window != null) {
                WindowInsets windowInsets = window.getDecorView().getRootWindowInsets();
                if (windowInsets != null) {
                    return windowInsets.getDisplayCutout();
                }
            }
        }
    }
    return null;
}
 
Example 14
Source File: NotchUtils.java    From MNImageBrowser with GNU General Public License v3.0 5 votes vote down vote up
private static DisplayCutout getDisplayCutout(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (view != null) {
            WindowInsets windowInsets = view.getRootWindowInsets();
            if (windowInsets != null) {
                return windowInsets.getDisplayCutout();
            }
        }
    }
    return null;
}