Settings: Support custom device doze settings

* Like https://review.lineageos.org/c/LineageOS/android_packages_apps_Settings/+/318590
  but in an AOSP compatible way

Change-Id: I694987dc05cfb6fee53329252031d805bd52c012
diff --git a/res/xml/security_lockscreen_settings.xml b/res/xml/security_lockscreen_settings.xml
index cb1ce44..34b9e1a 100644
--- a/res/xml/security_lockscreen_settings.xml
+++ b/res/xml/security_lockscreen_settings.xml
@@ -122,6 +122,13 @@
             android:summary="@string/doze_summary"
             settings:controller="com.android.settings.display.AmbientDisplayNotificationsPreferenceController" />
 
+        <Preference
+            android:key="doze_device_settings"
+            android:title="@string/ambient_display_screen_title"
+            settings:controller="com.android.settings.display.LineageDozePreferenceController">
+            <intent android:action="org.lineageos.settings.device.DOZE_SETTINGS" />
+        </Preference>
+
     </PreferenceCategory>
 
     <!-- Work profile settings are at the bottom with high order value to avoid users thinking that
diff --git a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
index 2458034..beedea4 100644
--- a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
+++ b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
@@ -29,6 +29,8 @@
 import com.android.settings.R;
 import com.android.settings.core.TogglePreferenceController;
 
+import org.leafos.settings.util.PackageManagerUtils;
+
 public class AmbientDisplayAlwaysOnPreferenceController extends TogglePreferenceController {
 
     private final int ON = 1;
@@ -47,7 +49,8 @@
     @Override
     public int getAvailabilityStatus() {
         return isAvailable(getConfig())
-                && !SystemProperties.getBoolean(PROP_AWARE_AVAILABLE, false) ?
+                && !SystemProperties.getBoolean(PROP_AWARE_AVAILABLE, false)
+                && !PackageManagerUtils.isCustomDozePresent(mContext.getPackageManager()) ?
                 AVAILABLE : UNSUPPORTED_ON_DEVICE;
     }
 
diff --git a/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java b/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
index 71d431a..91f745b 100644
--- a/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
+++ b/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
@@ -30,6 +30,8 @@
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 
+import org.leafos.settings.util.PackageManagerUtils;
+
 public class AmbientDisplayNotificationsPreferenceController extends
         TogglePreferenceController implements Preference.OnPreferenceChangeListener {
 
@@ -81,6 +83,7 @@
     @Override
     public int getAvailabilityStatus() {
         return getAmbientConfig().pulseOnNotificationAvailable()
+                && !PackageManagerUtils.isCustomDozePresent(mContext.getPackageManager())
                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
     }
 
diff --git a/src/com/android/settings/display/LineageDozePreferenceController.java b/src/com/android/settings/display/LineageDozePreferenceController.java
new file mode 100644
index 0000000..6c69e29
--- /dev/null
+++ b/src/com/android/settings/display/LineageDozePreferenceController.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2022 The LeafOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.display;
+
+import android.content.Context;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.leafos.settings.util.PackageManagerUtils;
+
+public class LineageDozePreferenceController extends BasePreferenceController {
+
+    public LineageDozePreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return PackageManagerUtils.isCustomDozePresent(mContext.getPackageManager()) ?
+                AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return "";
+    }
+}
diff --git a/src/org/leafos/settings/util/PackageManagerUtils.java b/src/org/leafos/settings/util/PackageManagerUtils.java
new file mode 100644
index 0000000..1449031
--- /dev/null
+++ b/src/org/leafos/settings/util/PackageManagerUtils.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2022 The LeafOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.leafos.settings.util;
+
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.UserHandle;
+
+public class PackageManagerUtils {
+
+    public static String ACTION_DOZE = "org.lineageos.settings.device.DOZE_SETTINGS";
+
+    public static boolean isIntentPresent(PackageManager pm, String name) {
+        Intent intent = new Intent(name);
+        for (ResolveInfo info : pm.queryIntentActivitiesAsUser(intent,
+                PackageManager.MATCH_SYSTEM_ONLY, UserHandle.myUserId())) {
+            if (info.activityInfo != null) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public static boolean isCustomDozePresent(PackageManager pm) {
+        return isIntentPresent(pm, ACTION_DOZE);
+    }
+
+}