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.Manifest;
20 import android.annotation.FlaggedApi;
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.hardware.SensorPrivacyManager;
26 import android.os.Process;
27 import android.os.UserHandle;
28 
29 import com.android.car.settings.Flags;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.Logger;
32 import com.android.car.settings.common.LogicalPreferenceGroup;
33 import com.android.car.ui.preference.CarUiPreference;
34 import com.android.internal.annotations.VisibleForTesting;
35 
36 import java.util.List;
37 
38 @FlaggedApi(Flags.FLAG_MICROPHONE_PRIVACY_UPDATES)
39 public class MicrophoneInfotainmentAppsPreferenceController extends
40         PrivacyBasePreferenceController<LogicalPreferenceGroup> {
41     private static final Logger LOG =
42             new Logger(MicrophoneInfotainmentAppsPreferenceController.class);
43     private final PackageManager mPackageManager;
44     private final Context mContext;
45 
MicrophoneInfotainmentAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46     public MicrophoneInfotainmentAppsPreferenceController(Context context, String preferenceKey,
47             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
48         this(context, preferenceKey, fragmentController, uxRestrictions,
49                 context.getPackageManager(), SensorPrivacyManager.getInstance(context));
50     }
51 
52     @VisibleForTesting
MicrophoneInfotainmentAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, PackageManager packageManager, SensorPrivacyManager sensorPrivacyManager)53     MicrophoneInfotainmentAppsPreferenceController(Context context, String preferenceKey,
54             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
55             PackageManager packageManager, SensorPrivacyManager sensorPrivacyManager) {
56         super(context, preferenceKey, fragmentController, uxRestrictions,
57                 sensorPrivacyManager, SensorPrivacyManager.Sensors.MICROPHONE);
58         mPackageManager = packageManager;
59         mContext = context;
60     }
61 
62     @Override
getPreferenceType()63     protected Class<LogicalPreferenceGroup> getPreferenceType() {
64         return LogicalPreferenceGroup.class;
65     }
66 
67     @Override
updateState(LogicalPreferenceGroup preference)68     protected void updateState(LogicalPreferenceGroup preference) {
69         loadInfotainmentAppsWithMicrophonePermission();
70     }
71 
loadInfotainmentAppsWithMicrophonePermission()72     private void loadInfotainmentAppsWithMicrophonePermission() {
73         getPreference().removeAll();
74 
75         UserHandle userHandle = Process.myUserHandle();
76         List<PackageInfo> packagesWithPermissions = PermissionUtils.getPackagesWithPermissionGroup(
77                 mContext,  Manifest.permission_group.MICROPHONE, userHandle,
78                 /* showSystem= */ false);
79         int sensorPrivacyState = getSensorPrivacyManager().getSensorPrivacyState(
80                 SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
81                 SensorPrivacyManager.Sensors.MICROPHONE);
82         boolean showSummary = sensorPrivacyState == SensorPrivacyManager.StateTypes.DISABLED;
83 
84         for (PackageInfo packageInfo : packagesWithPermissions) {
85             CarUiPreference preference =
86                     RequiredInfotainmentAppsUtils.createInfotainmentAppPreference(
87                             getContext(), mPackageManager, packageInfo.packageName, userHandle,
88                             Manifest.permission_group.MICROPHONE, showSummary);
89 
90             if (preference != null) {
91                 getPreference().addPreference(preference);
92             }
93         }
94     }
95 }
96