LineageParts: Add device controls global action

Change-Id: I67ac94740b4d0acbc8dd9ae98123e5c0f8aaf0bb
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a192925..9fe4181 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -264,6 +264,7 @@
     <string name="power_menu_bug_report_devoptions_unavailable">Bug reporting is disabled as development settings aren\'t enabled</string>
     <string name="power_menu_bug_report_unavailable_for_user">Bug reporting is disabled for non-primary users.</string>
     <string name="power_menu_emergency_title">Emergency</string>
+    <string name="power_menu_devicecontrols_title">Device controls</string>
     <string name="power_menu_advanced_restart_title">Advanced restart</string>
     <string name="power_menu_advanced_restart_summary">When unlocked, include options in the power menu for restarting into recovery or bootloader</string>
 
diff --git a/res/xml/power_menu_settings.xml b/res/xml/power_menu_settings.xml
index 175d3f1..4c3c9b3 100644
--- a/res/xml/power_menu_settings.xml
+++ b/res/xml/power_menu_settings.xml
@@ -49,6 +49,12 @@
             android:title="@string/power_menu_emergency_title"
             android:defaultValue="false" />
 
+        <CheckBoxPreference
+            android:key="devicecontrols"
+            android:title="@string/power_menu_devicecontrols_title"
+            android:defaultValue="false"
+            android:enabled="false" />
+
     </PreferenceCategory>
 
     <PreferenceCategory
diff --git a/src/org/lineageos/lineageparts/input/PowerMenuActions.java b/src/org/lineageos/lineageparts/input/PowerMenuActions.java
index 2d0f5ce..38814e3 100644
--- a/src/org/lineageos/lineageparts/input/PowerMenuActions.java
+++ b/src/org/lineageos/lineageparts/input/PowerMenuActions.java
@@ -17,18 +17,21 @@
 
 package org.lineageos.lineageparts.input;
 
+import android.Manifest;
 import android.content.Context;
 import android.content.pm.UserInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
+import android.service.controls.ControlsProviderService;
 
 import androidx.preference.CheckBoxPreference;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
 
 import com.android.internal.widget.LockPatternUtils;
+import com.android.settingslib.applications.ServiceListing;
 
 import org.lineageos.internal.util.PowerMenuConstants;
 import org.lineageos.lineageparts.R;
@@ -55,6 +58,7 @@
     private CheckBoxPreference mUsersPref;
     private CheckBoxPreference mBugReportPref;
     private CheckBoxPreference mEmergencyPref;
+    private CheckBoxPreference mDeviceControlsPref;
 
     private LineageGlobalActions mLineageGlobalActions;
 
@@ -87,6 +91,8 @@
                 mBugReportPref = findPreference(GLOBAL_ACTION_KEY_BUGREPORT);
             } else if (action.equals(GLOBAL_ACTION_KEY_EMERGENCY)) {
                 mEmergencyPref = findPreference(GLOBAL_ACTION_KEY_EMERGENCY);
+            } else if (action.equals(GLOBAL_ACTION_KEY_DEVICECONTROLS)) {
+                mDeviceControlsPref = findPreference(GLOBAL_ACTION_KEY_DEVICECONTROLS);
             }
         }
 
@@ -135,6 +141,23 @@
                     GLOBAL_ACTION_KEY_EMERGENCY));
         }
 
+        if (mDeviceControlsPref != null) {
+            mDeviceControlsPref.setChecked(mLineageGlobalActions.userConfigContains(
+                    GLOBAL_ACTION_KEY_DEVICECONTROLS));
+
+            // Enable preference if any device control app is installed
+            ServiceListing serviceListing = new ServiceListing.Builder(mContext)
+                    .setIntentAction(ControlsProviderService.SERVICE_CONTROLS)
+                    .setPermission(Manifest.permission.BIND_CONTROLS)
+                    .setNoun("Controls Provider")
+                    .setSetting("controls_providers")
+                    .setTag("controls_providers")
+                    .build();
+            serviceListing.addCallback(
+                    services -> mDeviceControlsPref.setEnabled(!services.isEmpty()));
+            serviceListing.reload();
+        }
+
         updatePreferences();
     }
 
@@ -170,6 +193,10 @@
             value = mEmergencyPref.isChecked();
             mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_EMERGENCY);
 
+        } else if (preference == mDeviceControlsPref) {
+            value = mDeviceControlsPref.isChecked();
+            mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_DEVICECONTROLS);
+
         } else {
             return super.onPreferenceTreeClick(preference);
         }