1 /*
2  * Copyright (C) 2022 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.car.drivingstate.CarUxRestrictions;
24 import android.content.Context;
25 import android.hardware.SensorPrivacyManager;
26 import android.os.UserManager;
27 
28 import com.android.car.settings.common.ColoredSwitchPreference;
29 import com.android.car.settings.common.FragmentController;
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.internal.camera.flags.Flags;
32 
33 /** Business logic for controlling the mute camera toggle. */
34 public class CameraTogglePreferenceController
35         extends CameraPrivacyBasePreferenceController<ColoredSwitchPreference> {
36 
CameraTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public CameraTogglePreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController,
39             CarUxRestrictions uxRestrictions) {
40         this(context, preferenceKey, fragmentController, uxRestrictions,
41                 SensorPrivacyManager.getInstance(context));
42     }
43 
44     @VisibleForTesting
CameraTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, SensorPrivacyManager sensorPrivacyManager)45     CameraTogglePreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
47             SensorPrivacyManager sensorPrivacyManager) {
48         super(context, preferenceKey, fragmentController, uxRestrictions, sensorPrivacyManager);
49     }
50 
51     @Override
getPreferenceType()52     protected Class<ColoredSwitchPreference> getPreferenceType() {
53         return ColoredSwitchPreference.class;
54     }
55 
56     @Override
handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)57     protected boolean handlePreferenceChanged(ColoredSwitchPreference preference,
58             Object newValue) {
59         boolean isChecked = (Boolean) newValue;
60         boolean isCameraMuted = getSensorPrivacyManager().isSensorPrivacyEnabled(
61                 SensorPrivacyManager.Sensors.CAMERA);
62         if (isChecked == isCameraMuted) {
63             getSensorPrivacyManager().setSensorPrivacyForProfileGroup(
64                     SensorPrivacyManager.Sources.SETTINGS,
65                     SensorPrivacyManager.Sensors.CAMERA,
66                     !isChecked);
67         }
68         return true;
69     }
70 
71     @Override
getDefaultAvailabilityStatus()72     protected int getDefaultAvailabilityStatus() {
73         if (Flags.cameraPrivacyAllowlist() || !getSensorPrivacyManager()
74                 .supportsSensorToggle(SensorPrivacyManager.Sensors.CAMERA)) {
75             // Hide preference if feature flag is enabled or system doesn't have a camera
76             return UNSUPPORTED_ON_DEVICE;
77         } else {
78             return getAvailabilityStatusRestricted(getContext(),
79                     UserManager.DISALLOW_CAMERA_TOGGLE);
80         }
81     }
82 
83     @Override
updateState(ColoredSwitchPreference preference)84     protected void updateState(ColoredSwitchPreference preference) {
85         preference.setChecked(!getSensorPrivacyManager().isSensorPrivacyEnabled(
86                 SensorPrivacyManager.Sensors.CAMERA));
87         if (hasUserRestrictionByDpm(getContext(), UserManager.DISALLOW_CAMERA_TOGGLE)) {
88             setClickableWhileDisabled(preference, /* clickable= */ true, p ->
89                     onClickWhileDisabled(getContext(), getFragmentController(),
90                             UserManager.DISALLOW_CAMERA_TOGGLE));
91         }
92     }
93 }
94