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 
17 package com.android.car.settings.privacy;
18 
19 import static com.android.car.settings.enterprise.EnterpriseUtils.getAvailabilityStatusRestricted;
20 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm;
21 import static com.android.car.settings.enterprise.EnterpriseUtils.onClickWhileDisabled;
22 
23 import android.annotation.FlaggedApi;
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.hardware.SensorPrivacyManager;
27 import android.os.UserManager;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.ui.preference.CarUiSwitchPreference;
31 import com.android.internal.annotations.VisibleForTesting;
32 import com.android.internal.camera.flags.Flags;
33 
34 /** Business logic for controlling the mute camera toggle. */
35 @FlaggedApi(Flags.FLAG_CAMERA_PRIVACY_ALLOWLIST)
36 public class CameraInfotainmentAppsTogglePreferenceController
37         extends CameraPrivacyBasePreferenceController<CarUiSwitchPreference> {
38     private boolean mHasCameraPrivacyAllowlist = false;
39 
CameraInfotainmentAppsTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40     public CameraInfotainmentAppsTogglePreferenceController(Context context, String preferenceKey,
41             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
42         this(context, preferenceKey, fragmentController, uxRestrictions,
43                 SensorPrivacyManager.getInstance(context));
44     }
45 
46     @VisibleForTesting
CameraInfotainmentAppsTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, SensorPrivacyManager sensorPrivacyManager)47     CameraInfotainmentAppsTogglePreferenceController(Context context, String preferenceKey,
48             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
49             SensorPrivacyManager sensorPrivacyManager) {
50         super(context, preferenceKey, fragmentController, uxRestrictions, sensorPrivacyManager);
51         mHasCameraPrivacyAllowlist = !sensorPrivacyManager.getCameraPrivacyAllowlist().isEmpty();
52     }
53 
54     @Override
getPreferenceType()55     protected Class<CarUiSwitchPreference> getPreferenceType() {
56         return CarUiSwitchPreference.class;
57     }
58 
isCameraMutedForInfotainmentApps()59     private boolean isCameraMutedForInfotainmentApps() {
60         int state = getSensorPrivacyManager().getSensorPrivacyState(
61                 SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager.Sensors.CAMERA);
62         return (state != SensorPrivacyManager.StateTypes.DISABLED);
63     }
64 
65     @Override
handlePreferenceChanged(CarUiSwitchPreference preference, Object newValue)66     protected boolean handlePreferenceChanged(CarUiSwitchPreference preference,
67             Object newValue) {
68         boolean isChecked = (Boolean) newValue;
69         if (isChecked == isCameraMutedForInfotainmentApps()) {
70             int newState;
71             if (isChecked) {
72                 newState =  SensorPrivacyManager.StateTypes.DISABLED;
73             } else {
74                 newState = mHasCameraPrivacyAllowlist
75                         ? SensorPrivacyManager.StateTypes.ENABLED_EXCEPT_ALLOWLISTED_APPS
76                         : SensorPrivacyManager.StateTypes.ENABLED;
77             }
78 
79             getSensorPrivacyManager().setSensorPrivacyStateForProfileGroup(
80                     SensorPrivacyManager.Sources.SETTINGS,
81                     SensorPrivacyManager.Sensors.CAMERA,
82                     newState);
83         }
84         return true;
85     }
86 
87     @Override
updateState(CarUiSwitchPreference preference)88     protected void updateState(CarUiSwitchPreference preference) {
89         preference.setChecked(!isCameraMutedForInfotainmentApps());
90         if (hasUserRestrictionByDpm(getContext(), UserManager.DISALLOW_CAMERA_TOGGLE)) {
91             setClickableWhileDisabled(preference, /* clickable= */ true, p ->
92                     onClickWhileDisabled(getContext(), getFragmentController(),
93                             UserManager.DISALLOW_CAMERA_TOGGLE));
94         }
95     }
96 
97     @Override
getDefaultAvailabilityStatus()98     protected int getDefaultAvailabilityStatus() {
99         return getAvailabilityStatusRestricted(getContext(), UserManager.DISALLOW_CAMERA_TOGGLE);
100     }
101 }
102