1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.development
17 
18 import android.content.Context
19 import android.permission.flags.Flags.sensitiveNotificationAppProtection
20 import android.provider.Settings
21 import android.view.flags.Flags.sensitiveContentAppProtection
22 import androidx.annotation.VisibleForTesting
23 import androidx.preference.Preference
24 import androidx.preference.TwoStatePreference
25 import com.android.server.notification.Flags.screenshareNotificationHiding
26 import com.android.settings.core.PreferenceControllerMixin
27 import com.android.settingslib.development.DeveloperOptionsPreferenceController
28 
29 class SensitiveContentProtectionPreferenceController(val context: Context) :
30     DeveloperOptionsPreferenceController(context),
31     Preference.OnPreferenceChangeListener,
32     PreferenceControllerMixin {
33 
getPreferenceKeynull34     override fun getPreferenceKey(): String =
35         DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS_KEY
36 
37     override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
38         val isEnabled = newValue as Boolean
39         Settings.Global.putInt(
40             mContext.getContentResolver(),
41             Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
42             if (isEnabled) SETTING_VALUE_ON else SETTING_VALUE_OFF
43         )
44         return true
45     }
46 
updateStatenull47     override fun updateState(preference: Preference?) {
48         val mode = Settings.Global.getInt(
49             mContext.getContentResolver(),
50             Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
51             0)
52         (mPreference as TwoStatePreference).isChecked = mode != SETTING_VALUE_OFF
53     }
54 
55     // Overriding as public, kotlin tests can not invoke a protected method
onDeveloperOptionsSwitchDisablednull56     public override fun onDeveloperOptionsSwitchDisabled() {
57         super.onDeveloperOptionsSwitchDisabled()
58         Settings.Global.putInt(
59             mContext.getContentResolver(),
60             Settings.Global.DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
61             SETTING_VALUE_OFF
62         )
63         (mPreference as TwoStatePreference).isChecked = false
64     }
65 
isAvailablenull66     override fun isAvailable(): Boolean {
67         return sensitiveNotificationAppProtection() || screenshareNotificationHiding()
68             || sensitiveContentAppProtection()
69     }
70 
71     companion object {
72         private const val DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS_KEY =
73             "disable_screen_share_protections_for_apps_and_notifications"
74 
75         @VisibleForTesting
76         val SETTING_VALUE_ON = 1
77 
78         @VisibleForTesting
79         val SETTING_VALUE_OFF = 0
80     }
81 }
82