Merge "Add developer option for screenshare protections" into main
diff --git a/res/values/strings.xml b/res/values/strings.xml
index dc2c416..9762f83 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -11979,6 +11979,11 @@
<!-- Developer settings: Summary for allowing mock modem service. [CHAR LIMIT=NONE]-->
<string name="allow_mock_modem_summary">Allow this device to run Mock Modem service for instrumentation testing. Do not enable this during normal usage of the phone</string>
+ <!-- Developer settings: Title for disable app and notification screen share protections [CHAR LIMIT=50] -->
+ <string name="disable_screen_share_protections_for_apps_and_notifications">Disable screen share protections</string>
+ <!-- Developer settings: Summary for disable app and notification screen share protections summary [CHAR LIMIT=150] -->
+ <string name="disable_screen_share_protections_for_apps_and_notifications_summary">Disables system applied app and notifications protections during screen sharing</string>
+
<!-- Title for media control settings [CHAR LIMIT=50]-->
<string name="media_controls_title">Media</string>
<!-- Title of toggle to enable or disable the media resumption feature in quick settings [CHAR LIMIT=50]-->
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index fb5e280..c0b6560 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -705,6 +705,11 @@
android:title="@string/show_notification_channel_warnings"
android:summary="@string/show_notification_channel_warnings_summary" />
+ <SwitchPreferenceCompat
+ android:key="disable_screen_share_protections_for_apps_and_notifications"
+ android:title="@string/disable_screen_share_protections_for_apps_and_notifications"
+ android:summary="@string/disable_screen_share_protections_for_apps_and_notifications_summary" />
+
<Preference
android:key="asst_importance_reset"
android:title="@string/asst_importance_reset_title"
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 504eda8..6b38b28 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -763,6 +763,7 @@
context, context.getSystemService(UiModeManager.class)));
controllers.add(new ForceEnableNotesRolePreferenceController(context));
controllers.add(new GrammaticalGenderPreferenceController(context));
+ controllers.add(new SensitiveContentProtectionPreferenceController(context));
return controllers;
}
diff --git a/src/com/android/settings/development/OWNERS b/src/com/android/settings/development/OWNERS
index 09a4914..92c8cfa 100644
--- a/src/com/android/settings/development/OWNERS
+++ b/src/com/android/settings/development/OWNERS
@@ -1,6 +1,9 @@
# GameDefaultFrameRatePreferenceController
per-file GameDefaultFrameRatePreferenceController.java=file:platform/frameworks/base:/GAME_MANAGER_OWNERS
+# SensitiveContentProtectionPreferenceController
+per-file SensitiveContentProtectionPreferenceController.kt=file:platform/frameworks/base:/core/java/android/permission/OWNERS
+
# ShowHdrSdrRatioPreferenceController
per-file ShowHdrSdrRatioPreferenceController.java=file:platform/frameworks/native:/services/surfaceflinger/OWNERS
diff --git a/src/com/android/settings/development/SensitiveContentProtectionPreferenceController.kt b/src/com/android/settings/development/SensitiveContentProtectionPreferenceController.kt
new file mode 100644
index 0000000..6fe56c8
--- /dev/null
+++ b/src/com/android/settings/development/SensitiveContentProtectionPreferenceController.kt
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2024 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.development
+
+import android.content.Context
+import android.provider.Settings
+import androidx.annotation.VisibleForTesting
+import androidx.preference.Preference
+import androidx.preference.TwoStatePreference
+import com.android.server.notification.Flags.sensitiveNotificationAppProtection
+import com.android.server.notification.Flags.screenshareNotificationHiding
+import com.android.settings.core.PreferenceControllerMixin
+import com.android.settingslib.development.DeveloperOptionsPreferenceController
+
+class SensitiveContentProtectionPreferenceController(val context: Context) :
+ DeveloperOptionsPreferenceController(context),
+ Preference.OnPreferenceChangeListener,
+ PreferenceControllerMixin {
+
+ override fun getPreferenceKey(): String =
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS_KEY
+
+ override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
+ val isEnabled = newValue as Boolean
+ Settings.Global.putInt(
+ mContext.getContentResolver(),
+ Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ if (isEnabled) SETTING_VALUE_ON else SETTING_VALUE_OFF
+ )
+ return true
+ }
+
+ override fun updateState(preference: Preference?) {
+ val mode = Settings.Global.getInt(
+ mContext.getContentResolver(),
+ Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ 0)
+ (mPreference as TwoStatePreference).isChecked = mode != SETTING_VALUE_OFF
+ }
+
+ // Overriding as public, kotlin tests can not invoke a protected method
+ public override fun onDeveloperOptionsSwitchDisabled() {
+ super.onDeveloperOptionsSwitchDisabled()
+ Settings.Global.putInt(
+ mContext.getContentResolver(),
+ Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ SETTING_VALUE_OFF
+ )
+ (mPreference as TwoStatePreference).isChecked = false
+ }
+
+ override fun isAvailable(): Boolean {
+ return sensitiveNotificationAppProtection() || screenshareNotificationHiding()
+ }
+
+ companion object {
+ private const val DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS_KEY =
+ "disable_screen_share_protections_for_apps_and_notifications"
+
+ @VisibleForTesting
+ val SETTING_VALUE_ON = 1
+
+ @VisibleForTesting
+ val SETTING_VALUE_OFF = 0
+ }
+}
diff --git a/tests/spa_unit/src/com/android/settings/development/SensitiveContentProtectionPreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/development/SensitiveContentProtectionPreferenceControllerTest.kt
new file mode 100644
index 0000000..bb30619
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/development/SensitiveContentProtectionPreferenceControllerTest.kt
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2024 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.development
+
+import android.content.Context
+import android.platform.test.annotations.RequiresFlagsDisabled
+import android.platform.test.annotations.RequiresFlagsEnabled
+import android.platform.test.flag.junit.DeviceFlagsValueProvider
+import android.provider.Settings
+import android.provider.Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS
+import androidx.preference.Preference
+import androidx.preference.PreferenceScreen
+import androidx.preference.SwitchPreference
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import com.android.server.notification.Flags.FLAG_SCREENSHARE_NOTIFICATION_HIDING
+import com.android.server.notification.Flags.FLAG_SENSITIVE_NOTIFICATION_APP_PROTECTION
+import com.android.settings.development.SensitiveContentProtectionPreferenceController.Companion.SETTING_VALUE_OFF
+import com.android.settings.development.SensitiveContentProtectionPreferenceController.Companion.SETTING_VALUE_ON
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.Mock
+import org.mockito.Mockito.verify
+import org.mockito.junit.MockitoJUnit
+import org.mockito.Mockito.`when` as whenever
+
+@RunWith(AndroidJUnit4::class)
+class SensitiveContentProtectionPreferenceControllerTest {
+ @get:Rule
+ val mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
+
+ @get:Rule
+ val mocks = MockitoJUnit.rule()
+
+ @Mock
+ private lateinit var preference: SwitchPreference
+
+ @Mock
+ private lateinit var screen: PreferenceScreen
+
+ private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
+ private lateinit var controller: SensitiveContentProtectionPreferenceController
+
+ @Before
+ fun setUp() {
+ controller = SensitiveContentProtectionPreferenceController(context)
+ whenever(screen.findPreference<Preference>(controller.getPreferenceKey()))
+ .thenReturn(preference)
+ controller.displayPreference(screen)
+ }
+
+ @Test
+ fun onPreferenceChange_settingEnabled_shouldDisableSensitiveContentProtection() {
+ controller.onPreferenceChange(preference, true /* new value */)
+ val mode = Settings.Global.getInt(
+ context.contentResolver,
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ -1 /* default */
+ )
+
+ assertEquals(mode, SETTING_VALUE_ON)
+ }
+
+ @Test
+ fun onPreferenceChange_settingDisabled_shouldEnableSensitiveContentProtection() {
+ controller.onPreferenceChange(preference, false /* new value */)
+ val mode = Settings.Global.getInt(
+ context.contentResolver,
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ -1 /* default */
+ )
+
+ assertEquals(mode, SETTING_VALUE_OFF)
+ }
+
+ @Test
+ fun updateState_settingEnabled_preferenceShouldBeChecked() {
+ Settings.Global.putInt(
+ context.contentResolver,
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ SETTING_VALUE_ON
+ )
+ controller.updateState(preference)
+
+ verify(preference).isChecked = true
+ }
+
+ @Test
+ fun updateState_settingDisabled_preferenceShouldNotBeChecked() {
+ Settings.Global.putInt(
+ context.contentResolver,
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ SETTING_VALUE_OFF
+ )
+ controller.updateState(preference)
+
+ verify(preference).isChecked = false
+ }
+
+ @Test
+ fun onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() {
+ controller.onDeveloperOptionsSwitchDisabled()
+ val mode = Settings.Global.getInt(
+ context.contentResolver,
+ DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
+ -1 /* default */
+ )
+
+ assertEquals(mode, SETTING_VALUE_OFF)
+ verify(preference).isChecked = false
+ verify(preference).isEnabled = false
+ }
+
+ @Test
+ @RequiresFlagsDisabled(
+ FLAG_SENSITIVE_NOTIFICATION_APP_PROTECTION,
+ FLAG_SCREENSHARE_NOTIFICATION_HIDING)
+ fun isAvailable_flagsDisabled_returnFalse() {
+ assertFalse(controller.isAvailable)
+ }
+
+ @Test
+ @RequiresFlagsEnabled(FLAG_SENSITIVE_NOTIFICATION_APP_PROTECTION)
+ fun isAvailable_sensitiveNotificationAppProtectionEnabled_returnTrue() {
+ assertTrue(controller.isAvailable)
+ }
+
+ @Test
+ @RequiresFlagsEnabled(FLAG_SCREENSHARE_NOTIFICATION_HIDING)
+ fun isAvailable_screenshareNotificationHidingEnabled_returnTrue() {
+ assertTrue(controller.isAvailable)
+ }
+}
\ No newline at end of file