Home Controls: Add Device Controls settings

Add a setting that would not require authorization for device controls.

Fixes: 216102581
Test: manual
Change-Id: I1fd98a2fbc1cea44d0b2c3612dd1616a053af394
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9d868cb..17aff1a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -13343,6 +13343,10 @@
     <string name="lockscreen_privacy_controls_setting_toggle">Show device controls</string>
     <!-- Device controls summary [CHAR LIMIT=NONE] -->
     <string name="lockscreen_privacy_controls_summary">Access controls when locked</string>
+    <!-- Trivial Device controls toggle name [CHAR LIMIT=60] -->
+    <string name="lockscreen_trivial_controls_setting_toggle">Control from locked device</string>
+    <!-- Trivial Device controls summary [CHAR LIMIT=NONE] -->
+    <string name="lockscreen_trivial_controls_summary">Control external devices without unlocking your phone or tablet if allowed by the device controls app</string>
     <!-- Lockscreen double-line clock summary [CHAR LIMIT=NONE] -->
     <string name="lockscreen_double_line_clock_summary">Show double-line clock when available</string>
     <!-- Lockscreen double-line clock toggle [CHAR LIMIT=60] -->
diff --git a/res/xml/security_lockscreen_settings.xml b/res/xml/security_lockscreen_settings.xml
index 7ecf200..b61f458 100644
--- a/res/xml/security_lockscreen_settings.xml
+++ b/res/xml/security_lockscreen_settings.xml
@@ -29,7 +29,7 @@
             settings:keywords="@string/keywords_lock_screen_notif"/>
 
         <SwitchPreference
-            android:key="security_dispaly_lockscreen_bypass"
+            android:key="security_display_lockscreen_bypass"
             android:title="@string/lockscreen_bypass_title"
             android:summary="@string/lockscreen_bypass_summary"
             settings:searchable="false"
@@ -64,6 +64,12 @@
             settings:controller="com.android.settings.display.ControlsPrivacyPreferenceController" />
 
         <SwitchPreference
+            android:key="lockscreen_trivial_controls_switch"
+            android:title="@string/lockscreen_trivial_controls_setting_toggle"
+            android:summary="@string/lockscreen_trivial_controls_summary"
+            settings:controller="com.android.settings.display.ControlsTrivialPrivacyPreferenceController"/>
+
+        <SwitchPreference
             android:key="lockscreen_double_line_clock_switch"
             android:title="@string/lockscreen_double_line_clock_setting_toggle"
             android:summary="@string/lockscreen_double_line_clock_summary"
diff --git a/src/com/android/settings/display/ControlsPrivacyPreferenceController.java b/src/com/android/settings/display/ControlsPrivacyPreferenceController.java
index 00573a7..6f146d5 100644
--- a/src/com/android/settings/display/ControlsPrivacyPreferenceController.java
+++ b/src/com/android/settings/display/ControlsPrivacyPreferenceController.java
@@ -30,6 +30,9 @@
 
 /**
  * Preference for showing/hiding sensitive device controls content while the device is locked.
+ *
+ * Note that ControlsTrivialPrivacyPreferenceController depends on the preferenceKey
+ * of this controller.
  */
 public class ControlsPrivacyPreferenceController extends TogglePreferenceController {
 
diff --git a/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceController.java b/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceController.java
new file mode 100644
index 0000000..a6c8e03
--- /dev/null
+++ b/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceController.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2022 The Android Open Source 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 android.provider.Settings;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.core.TogglePreferenceController;
+
+/**
+ * Preference for requiring authorization to use home controls for certain devices.
+ */
+public class ControlsTrivialPrivacyPreferenceController extends TogglePreferenceController {
+
+    private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_ALLOW_TRIVIAL_CONTROLS;
+    private static final String DEPENDENCY_SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS;
+
+    public ControlsTrivialPrivacyPreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public boolean isChecked() {
+        return Settings.Secure.getInt(mContext.getContentResolver(), SETTING_KEY, 0) != 0;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        return Settings.Secure.putInt(mContext.getContentResolver(), SETTING_KEY,
+                isChecked ? 1 : 0);
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return mContext.getText(R.string.lockscreen_trivial_controls_summary);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        preference.setEnabled(getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING);
+        refreshSummary(preference);
+    }
+
+    @Override
+    public int getSliceHighlightMenuRes() {
+        return R.string.menu_key_display;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return showDeviceControlsSettingsEnabled() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
+    }
+
+    private boolean showDeviceControlsSettingsEnabled() {
+        return Settings.Secure.getInt(mContext.getContentResolver(), DEPENDENCY_SETTING_KEY, 0)
+                != 0;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        Preference currentPreference = screen.findPreference(getPreferenceKey());
+        currentPreference.setDependency("lockscreen_privacy_controls_switch");
+    }
+}
diff --git a/src/com/android/settings/security/LockscreenDashboardFragment.java b/src/com/android/settings/security/LockscreenDashboardFragment.java
index b3351ac..2e4a1f2 100644
--- a/src/com/android/settings/security/LockscreenDashboardFragment.java
+++ b/src/com/android/settings/security/LockscreenDashboardFragment.java
@@ -21,8 +21,13 @@
 
 import android.app.settings.SettingsEnums;
 import android.content.Context;
+import android.database.ContentObserver;
 import android.hardware.display.AmbientDisplayConfiguration;
+import android.net.Uri;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.provider.Settings;
 
 import androidx.annotation.VisibleForTesting;
 
@@ -68,6 +73,8 @@
 
     private AmbientDisplayConfiguration mConfig;
     private OwnerInfoPreferenceController mOwnerInfoPreferenceController;
+    @VisibleForTesting
+    ContentObserver mControlsContentObserver;
 
     @Override
     public int getMetricsCategory() {
@@ -106,6 +113,27 @@
         use(AmbientDisplayNotificationsPreferenceController.class).setConfig(getConfig(context));
         use(DoubleTapScreenPreferenceController.class).setConfig(getConfig(context));
         use(PickupGesturePreferenceController.class).setConfig(getConfig(context));
+
+        mControlsContentObserver = new ContentObserver(
+                new Handler(Looper.getMainLooper())) {
+            @Override
+            public void onChange(boolean selfChange, Uri uri) {
+                super.onChange(selfChange, uri);
+                updatePreferenceStates();
+            }
+        };
+        context.getContentResolver().registerContentObserver(
+                Settings.Secure.getUriFor(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS),
+                false /* notifyForDescendants */, mControlsContentObserver);
+    }
+
+    @Override
+    public void onDetach() {
+        if (mControlsContentObserver != null) {
+            getContext().getContentResolver().unregisterContentObserver(mControlsContentObserver);
+            mControlsContentObserver = null;
+        }
+        super.onDetach();
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceControllerTest.java
new file mode 100644
index 0000000..2c53d4e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/ControlsTrivialPrivacyPreferenceControllerTest.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2022 The Android Open Source 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.provider.Settings;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+@RunWith(RobolectricTestRunner.class)
+public class ControlsTrivialPrivacyPreferenceControllerTest {
+
+    private static final String TEST_KEY = "test_key";
+    private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_ALLOW_TRIVIAL_CONTROLS;
+    private static final String DEPENDENCY_SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS;
+
+    private Context mContext;
+    private ContentResolver mContentResolver;
+    private ControlsTrivialPrivacyPreferenceController mController;
+
+    @Mock
+    private Preference mPreference;
+
+    @Mock
+    private PreferenceScreen mPreferenceScreen;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = ApplicationProvider.getApplicationContext();
+
+        mContentResolver = mContext.getContentResolver();
+
+        mController = new ControlsTrivialPrivacyPreferenceController(mContext, TEST_KEY);
+    }
+
+    @Test
+    public void isCheckedWhenSettingIsTrue() {
+        Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1);
+
+        assertThat(mController.isChecked()).isTrue();
+    }
+
+    @Test
+    public void isCheckedWhenSettingIsFalse() {
+        Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0);
+
+        assertThat(mController.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isCheckedWhenSettingIsNull() {
+        Settings.Secure.putString(mContentResolver, SETTING_KEY, null);
+
+        assertThat(mController.isChecked()).isFalse();
+    }
+
+    @Test
+    public void checkedMeansSettingIsTrue() {
+        mController.setChecked(true);
+
+        assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0);
+    }
+
+    @Test
+    public void uncheckedMeansSettingIsFalse() {
+        mController.setChecked(false);
+
+        assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0);
+    }
+
+    @Test
+    public void getSummaryRequireDeviceControls() {
+        Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
+
+        assertThat(mController.getSummary().toString()).isEqualTo(
+                mContext.getText(R.string.lockscreen_trivial_controls_summary));
+    }
+
+    @Test
+    public void getSummaryDefault() {
+        Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
+
+        assertThat(mController.getSummary().toString()).isEqualTo(
+                mContext.getText(R.string.lockscreen_trivial_controls_summary));
+    }
+
+    @Test
+    public void updateState() {
+        Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setEnabled(anyBoolean());
+        verify(mPreference, atLeastOnce()).setSummary(mController.getSummary());
+    }
+
+    @Test
+    public void getAvailabilityStatusWithoutDeviceControls() {
+        Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.DISABLED_DEPENDENT_SETTING);
+    }
+
+    @Test
+    public void getAvailabilityStatusWithDeviceControls() {
+        Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
+
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void setDependency() {
+        Mockito.when(mPreferenceScreen
+                .findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        mController.displayPreference(mPreferenceScreen);
+        verify(mPreference).setDependency(anyString());
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/security/LockscreenDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/security/LockscreenDashboardFragmentTest.java
index 5c60001..bf5e957 100644
--- a/tests/robotests/src/com/android/settings/security/LockscreenDashboardFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/security/LockscreenDashboardFragmentTest.java
@@ -67,6 +67,7 @@
                 .thenReturn(mLockPatternUtils);
         mContext = RuntimeEnvironment.application;
         mTestFragment = spy(new TestFragment());
+        doReturn(mContext).when(mTestFragment).getContext();
     }
 
     @Test
@@ -131,6 +132,14 @@
                 .doesNotContain("security_lockscreen_settings_screen");
     }
 
+    @Test
+    public void controlsSettings() {
+        mTestFragment.onAttach(mContext);
+        assertThat(mTestFragment.mControlsContentObserver).isNotNull();
+        mTestFragment.onDetach();
+        assertThat(mTestFragment.mControlsContentObserver).isNull();
+    }
+
     public static class TestFragment extends LockscreenDashboardFragment {
         @Override
         protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {