• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.hardware.SensorPrivacyManager;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 
26 import com.android.car.settings.common.FragmentController;
27 import com.android.car.settings.common.PreferenceController;
28 
29 // TODO(b/340624790): combine the base classes for microphone and camera.
30 /**
31  * Abstract PreferenceController that listens to OnSensorPrivacyChangedListener
32  * and will refresh the UI when the sensor privacy changed event happens.
33  *
34  * @param <V> the upper bound on the type of {@link Preference} on which the controller expects
35  *            to operate.
36  */
37 public abstract class PrivacyBasePreferenceController<V extends Preference> extends
38         PreferenceController<V> {
39     private int mSensorType;
40     private final SensorPrivacyManager mSensorPrivacyManager;
41     private final SensorPrivacyManager.OnSensorPrivacyChangedListener mListener =
42             new SensorPrivacyManager.OnSensorPrivacyChangedListener() {
43                 @Override
44                 public void onSensorPrivacyChanged(SensorPrivacyChangedParams params) {
45                     refreshUi();
46                 }
47 
48                 @Override
49                 public void onSensorPrivacyChanged(int sensor, boolean enabled) {
50                     // handled in onSensorPrivacyChanged(SensorPrivacyChangedParams)
51                 }
52             };
53 
PrivacyBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, SensorPrivacyManager sensorPrivacyManager, int sensorType)54     public PrivacyBasePreferenceController(Context context, String preferenceKey,
55             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
56             SensorPrivacyManager sensorPrivacyManager, int sensorType) {
57         super(context, preferenceKey, fragmentController, uxRestrictions);
58         mSensorPrivacyManager = sensorPrivacyManager;
59         mSensorType = sensorType;
60     }
61 
62     @Override
onStartInternal()63     protected void onStartInternal() {
64         super.onStartInternal();
65         mSensorPrivacyManager.addSensorPrivacyListener(mSensorType, mListener);
66     }
67 
68     @Override
onStopInternal()69     protected void onStopInternal() {
70         super.onStopInternal();
71         mSensorPrivacyManager.removeSensorPrivacyListener(mSensorType, mListener);
72     }
73 
getSensorPrivacyManager()74     public SensorPrivacyManager getSensorPrivacyManager() {
75         return mSensorPrivacyManager;
76     }
77 
getPrivacySensorType()78     public int getPrivacySensorType() {
79         return mSensorType;
80     }
81 
82     @VisibleForTesting
getListener()83     SensorPrivacyManager.OnSensorPrivacyChangedListener getListener() {
84         return mListener;
85     }
86 }
87