• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
17 package com.android.tv.settings.privacy;
18 
19 import android.annotation.Nullable;
20 import android.app.AppOpsManager;
21 import android.content.Context;
22 import android.hardware.SensorPrivacyManager;
23 import android.hardware.SensorPrivacyManager.Sensors.Sensor;
24 import android.provider.DeviceConfig;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.tv.settings.R;
29 
30 public enum PrivacyToggle {
31     CAMERA_TOGGLE(
32             R.string.camera,
33             R.string.camera_toggle_title,
34             R.string.camera_toggle_info_title,
35             R.string.camera_toggle_info_content,
36             R.string.open_camera_permissions,
37             "android.permission-group.CAMERA",
38             SensorPrivacyManager.Sensors.CAMERA,
39             new int[]{AppOpsManager.OP_CAMERA, AppOpsManager.OP_PHONE_CALL_CAMERA},
40             "camera_toggle_enabled"
41     ),
42 
43     MIC_TOGGLE(
44             R.string.microphone,
45             R.string.mic_toggle_title,
46             R.string.mic_toggle_info_title,
47             R.string.mic_toggle_info_content,
48             R.string.open_mic_permissions,
49             "android.permission-group.MICROPHONE",
50             SensorPrivacyManager.Sensors.MICROPHONE,
51             new int[]{AppOpsManager.OP_RECORD_AUDIO, AppOpsManager.OP_PHONE_CALL_MICROPHONE},
52             "mic_toggle_enabled"
53     );
54 
55     public final int screenTitle;
56     public final int toggleTitle;
57     public final int toggleInfoTitle;
58     public final int toggleInfoText;
59     public final int appPermissionsTitle;
60     public final String permissionsGroupName;
61     @Sensor
62     public final int sensor;
63     public final int[] appOps;
64     public final String deviceConfigName;
65 
PrivacyToggle(int screenTitle, int toggleTitle, int toggleInfoTitle, int toggleInfoText, int appPermissionsTitle, String permissionsGroupName, @Sensor int sensor, int[] appOps, String deviceConfigName)66     PrivacyToggle(int screenTitle, int toggleTitle, int toggleInfoTitle, int toggleInfoText,
67             int appPermissionsTitle, String permissionsGroupName, @Sensor int sensor, int[] appOps,
68             String deviceConfigName) {
69         this.screenTitle = screenTitle;
70         this.toggleTitle = toggleTitle;
71         this.toggleInfoTitle = toggleInfoTitle;
72         this.toggleInfoText = toggleInfoText;
73         this.appPermissionsTitle = appPermissionsTitle;
74         this.permissionsGroupName = permissionsGroupName;
75         this.sensor = sensor;
76         this.appOps = appOps;
77         this.deviceConfigName = deviceConfigName;
78     }
79 
80     /**
81      * Checks if the privacy toggle should be shown.
82      */
isPresentAndEnabled(Context context)83     public boolean isPresentAndEnabled(Context context) {
84         return context.getSystemService(SensorPrivacyManager.class).supportsSensorToggle(
85                 sensor) && DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
86                 deviceConfigName, /* defaultValue= */ true);
87     }
88 
89     /**
90      * Hides the preference if the toggle shouldn't be shown and adds the toggle to the extras so
91      * the SensorFragment knows which sensor is meant.
92      */
preparePreferenceWithSensorFragment(Context context, @Nullable Preference preference, String extrasKey)93     public void preparePreferenceWithSensorFragment(Context context,
94             @Nullable Preference preference, String extrasKey) {
95         if (preference == null) {
96             return;
97         }
98         if (isPresentAndEnabled(context)) {
99             preference.getExtras().putObject(extrasKey, this);
100         } else {
101             preference.setVisible(false);
102         }
103     }
104 }
105