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.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 mic toggle. */
29 public class MicTogglePreferenceController extends PreferenceController<ColoredSwitchPreference> {
30 
31     private final SensorPrivacyManager mSensorPrivacyManager;
32     private final SensorPrivacyManager.OnSensorPrivacyChangedListener mListener =
33             (sensor, enabled) -> refreshUi();
34 
MicTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)35     public MicTogglePreferenceController(Context context, String preferenceKey,
36             FragmentController fragmentController,
37             CarUxRestrictions uxRestrictions) {
38         this(context, preferenceKey, fragmentController, uxRestrictions,
39                 SensorPrivacyManager.getInstance(context));
40     }
41 
42     @VisibleForTesting
MicTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, SensorPrivacyManager sensorPrivacyManager)43     MicTogglePreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
45             SensorPrivacyManager sensorPrivacyManager) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mSensorPrivacyManager = sensorPrivacyManager;
48     }
49 
50     @Override
getPreferenceType()51     protected Class<ColoredSwitchPreference> getPreferenceType() {
52         return ColoredSwitchPreference.class;
53     }
54 
55     @Override
onStartInternal()56     protected void onStartInternal() {
57         mSensorPrivacyManager.addSensorPrivacyListener(
58                 SensorPrivacyManager.Sensors.MICROPHONE, mListener);
59     }
60 
61     @Override
onStopInternal()62     protected void onStopInternal() {
63         mSensorPrivacyManager.removeSensorPrivacyListener(SensorPrivacyManager.Sensors.MICROPHONE,
64                 mListener);
65     }
66 
67     @Override
handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)68     protected boolean handlePreferenceChanged(ColoredSwitchPreference preference,
69             Object newValue) {
70         boolean isChecked = (Boolean) newValue;
71         // Settings UX currently shows "checked means mic is enabled", but the underlying API is
72         // inversely written around "is mic muted?" So we must be careful when doing
73         // comparisons.
74         boolean isMicMuted = mSensorPrivacyManager.isSensorPrivacyEnabled(
75                 SensorPrivacyManager.Sensors.MICROPHONE);
76         if (isChecked == isMicMuted) {
77             // UX and underlying API state for mic do not match, so update sensor privacy
78             mSensorPrivacyManager.setSensorPrivacyForProfileGroup(
79                     SensorPrivacyManager.Sources.SETTINGS,
80                     SensorPrivacyManager.Sensors.MICROPHONE,
81                     !isChecked);
82         }
83         return true;
84     }
85 
86     @Override
getAvailabilityStatus()87     protected int getAvailabilityStatus() {
88         boolean hasFeatureMicToggle = mSensorPrivacyManager.supportsSensorToggle(
89                 SensorPrivacyManager.Sensors.MICROPHONE);
90         return hasFeatureMicToggle ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
91     }
92 
93     @Override
updateState(ColoredSwitchPreference preference)94     protected void updateState(ColoredSwitchPreference preference) {
95         preference.setChecked(!mSensorPrivacyManager.isSensorPrivacyEnabled(
96                 SensorPrivacyManager.Sensors.MICROPHONE));
97     }
98 }
99