LineageParts: Use Display.getCutout() to check if we have a center notch
Inspired by SystemUI's currentRotationHasCornerCutout() ^.^
Change-Id: I8e08aa96e0f202212fadcfd8d7925492bb970727
diff --git a/src/org/lineageos/lineageparts/statusbar/StatusBarSettings.java b/src/org/lineageos/lineageparts/statusbar/StatusBarSettings.java
index ae373fd..7078dca 100644
--- a/src/org/lineageos/lineageparts/statusbar/StatusBarSettings.java
+++ b/src/org/lineageos/lineageparts/statusbar/StatusBarSettings.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2015 The CyanogenMod Project
- * 2017-2021 The LineageOS Project
+ * 2017-2022 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@
private PreferenceCategory mStatusBarClockCategory;
private Preference mNetworkTrafficPref;
- private boolean mHasNotch;
+ private boolean mHasCenteredCutout;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -80,8 +80,8 @@
mNetworkTrafficPref = findPreference(NETWORK_TRAFFIC_SETTINGS);
- mHasNotch = DeviceUtils.hasNotch(getActivity());
- if (mHasNotch) {
+ mHasCenteredCutout = DeviceUtils.hasCenteredCutout(getActivity());
+ if (mHasCenteredCutout) {
getPreferenceScreen().removePreference(mNetworkTrafficPref);
}
@@ -127,7 +127,7 @@
mStatusBarAmPm.setSummary(R.string.status_bar_am_pm_info);
}
- final boolean disallowCenteredClock = mHasNotch || getNetworkTrafficStatus() != 0;
+ final boolean disallowCenteredClock = mHasCenteredCutout || getNetworkTrafficStatus() != 0;
// Adjust status bar preferences for RTL
if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
@@ -195,7 +195,7 @@
}
private void updateNetworkTrafficStatus(int clockPosition) {
- if (mHasNotch) {
+ if (mHasCenteredCutout) {
// Unconditional no network traffic for you
return;
}
@@ -225,7 +225,7 @@
public Set<String> getNonIndexableKeys(Context context) {
final Set<String> result = new ArraySet<String>();
- if (DeviceUtils.hasNotch(context)) {
+ if (DeviceUtils.hasCenteredCutout(context)) {
result.add(NETWORK_TRAFFIC_SETTINGS);
}
return result;
diff --git a/src/org/lineageos/lineageparts/utils/DeviceUtils.java b/src/org/lineageos/lineageparts/utils/DeviceUtils.java
index 12ff417..f37b7bf 100644
--- a/src/org/lineageos/lineageparts/utils/DeviceUtils.java
+++ b/src/org/lineageos/lineageparts/utils/DeviceUtils.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016 The CyanogenMod project
- * 2017-2020 The LineageOS project
+ * 2017-2022 The LineageOS project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
+import android.graphics.Point;
+import android.graphics.Rect;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
@@ -38,6 +40,8 @@
import android.telephony.TelephonyManager;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
+import android.view.Display;
+import android.view.DisplayCutout;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.Surface;
@@ -46,10 +50,34 @@
public class DeviceUtils {
- /* returns whether the device has a notch or not. */
- public static boolean hasNotch(Context context) {
- return context.getResources().getBoolean(
- org.lineageos.platform.internal.R.bool.config_haveNotch);
+ /* returns whether the device has a centered display cutout or not. */
+ public static boolean hasCenteredCutout(Context context) {
+ Display display = context.getDisplay();
+ DisplayCutout cutout = display.getCutout();
+ if (cutout != null) {
+ Point realSize = new Point();
+ display.getRealSize(realSize);
+
+ switch (display.getRotation()) {
+ case Surface.ROTATION_0: {
+ Rect rect = cutout.getBoundingRectTop();
+ return !(rect.left <= 0 || rect.right >= realSize.x);
+ }
+ case Surface.ROTATION_90: {
+ Rect rect = cutout.getBoundingRectLeft();
+ return !(rect.top <= 0 || rect.bottom >= realSize.y);
+ }
+ case Surface.ROTATION_180: {
+ Rect rect = cutout.getBoundingRectBottom();
+ return !(rect.left <= 0 || rect.right >= realSize.x);
+ }
+ case Surface.ROTATION_270: {
+ Rect rect = cutout.getBoundingRectRight();
+ return !(rect.top <= 0 || rect.bottom >= realSize.y);
+ }
+ }
+ }
+ return false;
}
public static int getDeviceKeys(Context context) {