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 android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.hardware.SensorPrivacyManager; 22 23 import com.android.car.settings.common.ColoredSwitchPreference; 24 import com.android.car.settings.common.FragmentController; 25 import com.android.car.settings.common.PreferenceController; 26 import com.android.internal.annotations.VisibleForTesting; 27 28 /** Business logic for controlling the mute camera toggle. */ 29 public class CameraTogglePreferenceController 30 extends PreferenceController<ColoredSwitchPreference> { 31 32 private final SensorPrivacyManager mSensorPrivacyManager; 33 private final SensorPrivacyManager.OnSensorPrivacyChangedListener mListener = 34 (sensor, enabled) -> refreshUi(); 35 CameraTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)36 public CameraTogglePreferenceController(Context context, String preferenceKey, 37 FragmentController fragmentController, 38 CarUxRestrictions uxRestrictions) { 39 this(context, preferenceKey, fragmentController, uxRestrictions, 40 SensorPrivacyManager.getInstance(context)); 41 } 42 43 @VisibleForTesting CameraTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, SensorPrivacyManager sensorPrivacyManager)44 CameraTogglePreferenceController(Context context, String preferenceKey, 45 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 46 SensorPrivacyManager sensorPrivacyManager) { 47 super(context, preferenceKey, fragmentController, uxRestrictions); 48 mSensorPrivacyManager = sensorPrivacyManager; 49 } 50 51 @Override getPreferenceType()52 protected Class<ColoredSwitchPreference> getPreferenceType() { 53 return ColoredSwitchPreference.class; 54 } 55 56 @Override onStartInternal()57 protected void onStartInternal() { 58 mSensorPrivacyManager.addSensorPrivacyListener( 59 SensorPrivacyManager.Sensors.CAMERA, mListener); 60 } 61 62 @Override onStopInternal()63 protected void onStopInternal() { 64 mSensorPrivacyManager.removeSensorPrivacyListener(SensorPrivacyManager.Sensors.CAMERA, 65 mListener); 66 } 67 68 @Override handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)69 protected boolean handlePreferenceChanged(ColoredSwitchPreference preference, 70 Object newValue) { 71 boolean isChecked = (Boolean) newValue; 72 boolean isCameraMuted = mSensorPrivacyManager.isSensorPrivacyEnabled( 73 SensorPrivacyManager.Sensors.CAMERA); 74 if (isChecked == isCameraMuted) { 75 mSensorPrivacyManager.setSensorPrivacyForProfileGroup( 76 SensorPrivacyManager.Sources.SETTINGS, 77 SensorPrivacyManager.Sensors.CAMERA, 78 !isChecked); 79 } 80 return true; 81 } 82 83 @Override getAvailabilityStatus()84 protected int getAvailabilityStatus() { 85 boolean hasFeatureCameraToggle = mSensorPrivacyManager.supportsSensorToggle( 86 SensorPrivacyManager.Sensors.CAMERA); 87 return hasFeatureCameraToggle ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 88 } 89 90 @Override updateState(ColoredSwitchPreference preference)91 protected void updateState(ColoredSwitchPreference preference) { 92 preference.setChecked(!mSensorPrivacyManager.isSensorPrivacyEnabled( 93 SensorPrivacyManager.Sensors.CAMERA)); 94 } 95 } 96