Settings: Add preference for one shot auto-brightness

Author: Eamon Powell <eamonpowell@outlook.com>
Date:   Sat Aug 28 13:17:21 2021 +1000

    fixup! Settings: Add preference for one shot auto-brightness

    * Strings belong in cm_strings.xml
    * Fix inconsistent capitalization in comments
    * Fix missing & unnecessary newlines

    Change-Id: I9c2c6b8a68ec921ab69a7d3ee19e907c58533823

Author: Eamon Powell <eamonpowell@outlook.com>
Date:   Thu Aug 26 18:22:15 2021 +1000

    Settings: Adjust one shot auto-brightness strings

    The current strings provided are confusing and lack
    clarity regarding what the feature actually does.

    Change-Id: If87fad1e163131e7b9a070f6866f7d564e368cca

Change-Id: I57f11ad4e8fc47b2ff2c771e61920780e359815f
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index 01d7469..9b3dc03 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -107,6 +107,10 @@
     <string name="tethering_allow_vpn_upstreams_title">Allow clients to use VPNs</string>
     <string name="tethering_allow_vpn_upstreams_summary">Permit hotspot clients to use this device\u2019s VPN connections for upstream connectivity</string>
 
+    <!-- One shot automatic brightness -->
+    <string name="auto_brightness_one_shot_title">One shot auto-brightness</string>
+    <string name="auto_brightness_one_shot_summary">Brightness adjustment will only occur at the moment the screen is turned on</string>
+
     <!-- Per-app data restrictions -->
     <string name="data_usage_app_restrict_all">Allow network access</string>
     <string name="data_usage_app_restrict_all_summary">Enable network usage</string>
diff --git a/res/xml/auto_brightness_detail.xml b/res/xml/auto_brightness_detail.xml
index 0584d77..b31808e 100644
--- a/res/xml/auto_brightness_detail.xml
+++ b/res/xml/auto_brightness_detail.xml
@@ -32,6 +32,17 @@
         settings:userRestriction="no_config_brightness"
         settings:controller="com.android.settings.display.AutoBrightnessDetailPreferenceController"/>
 
+    <com.android.settingslib.RestrictedSwitchPreference
+        android:key="auto_brightness_one_shot"
+        android:title="@string/auto_brightness_one_shot_title"
+        android:summary="@string/auto_brightness_one_shot_summary"
+        android:dependency="auto_brightness"
+        settings:keywords="@string/keywords_display_auto_brightness"
+        settings:controller="com.android.settings.display.AutoBrightnessOneShotPreferenceController"
+        settings:useAdminDisabledSummary="true"
+        settings:userRestriction="no_config_brightness"
+        settings:allowDividerAbove="true" />
+
     <com.android.settingslib.widget.FooterPreference
         android:key="auto_brightness_footer"
         android:title="@string/auto_brightness_description"
diff --git a/src/com/android/settings/display/AutoBrightnessOneShotPreferenceController.java b/src/com/android/settings/display/AutoBrightnessOneShotPreferenceController.java
new file mode 100644
index 0000000..aebcd3d
--- /dev/null
+++ b/src/com/android/settings/display/AutoBrightnessOneShotPreferenceController.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2021 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.
+ * 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 lineageos.providers.LineageSettings;
+
+import com.android.settings.R;
+import com.android.settings.core.TogglePreferenceController;
+
+public class AutoBrightnessOneShotPreferenceController extends TogglePreferenceController {
+
+    public AutoBrightnessOneShotPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public boolean isChecked() {
+        return LineageSettings.System.getInt(mContext.getContentResolver(),
+                LineageSettings.System.AUTO_BRIGHTNESS_ONE_SHOT, 0) == 1;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        LineageSettings.System.putInt(mContext.getContentResolver(),
+                LineageSettings.System.AUTO_BRIGHTNESS_ONE_SHOT, isChecked ? 1 : 0);
+        return true;
+    }
+
+    @Override
+    @AvailabilityStatus
+    public int getAvailabilityStatus() {
+        return mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_automatic_brightness_available)
+                ? AVAILABLE_UNSEARCHABLE
+                : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return mContext.getText(isChecked()
+                ? R.string.auto_brightness_summary_on
+                : R.string.auto_brightness_summary_off);
+    }
+
+    @Override
+    public int getSliceHighlightMenuRes() {
+        return R.string.menu_key_display;
+    }
+}