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.R;
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 import com.android.internal.camera.flags.Flags;
36 
37 import java.util.List;
38 
39 /**
40  * Displays a list of infotainment apps which holds camera permission.
41  */
42 @FlaggedApi(Flags.FLAG_CAMERA_PRIVACY_ALLOWLIST)
43 public final class CameraInfotainmentAppsPreferenceController
44         extends CameraPrivacyBasePreferenceController<LogicalPreferenceGroup> {
45     private static final Logger LOG = new Logger(CameraInfotainmentAppsPreferenceController.class);
46     private final PackageManager mPackageManager;
47     private List<String> mCameraPrivacyAllowlist;
48 
CameraInfotainmentAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49     public CameraInfotainmentAppsPreferenceController(Context context, String preferenceKey,
50             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
51         this(context, preferenceKey, fragmentController, uxRestrictions,
52                 context.getPackageManager(), SensorPrivacyManager.getInstance(context));
53     }
54 
55     @VisibleForTesting
CameraInfotainmentAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, PackageManager packageManager, SensorPrivacyManager sensorPrivacyManager)56     CameraInfotainmentAppsPreferenceController(Context context, String preferenceKey,
57             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
58             PackageManager packageManager, SensorPrivacyManager sensorPrivacyManager) {
59         super(context, preferenceKey, fragmentController, uxRestrictions, sensorPrivacyManager);
60         mPackageManager = packageManager;
61         mCameraPrivacyAllowlist = sensorPrivacyManager.getCameraPrivacyAllowlist();
62     }
63 
64     @Override
getPreferenceType()65     protected Class<LogicalPreferenceGroup> getPreferenceType() {
66         return LogicalPreferenceGroup.class;
67     }
68 
69     @Override
updateState(LogicalPreferenceGroup preference)70     protected void updateState(LogicalPreferenceGroup preference) {
71         loadInfotainmentAppsWithCameraPermission();
72     }
73 
loadInfotainmentAppsWithCameraPermission()74     private void loadInfotainmentAppsWithCameraPermission() {
75         LogicalPreferenceGroup infotainmentAppsPref = getPreference().findPreference(getContext()
76                 .getString(R.string.pk_camera_infotainment_apps_list));
77         infotainmentAppsPref.removeAll();
78 
79         UserHandle userHandle = Process.myUserHandle();
80         List<PackageInfo> packagesWithPermissions = PermissionUtils.getPackagesWithPermissionGroup(
81                 getContext(), Manifest.permission_group.CAMERA, userHandle,
82                 /* showSystem= */ false);
83         int sensorPrivacyState = getSensorPrivacyManager().getSensorPrivacyState(
84                 SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
85                 SensorPrivacyManager.Sensors.CAMERA);
86 
87         for (PackageInfo packageInfo : packagesWithPermissions) {
88             if (mCameraPrivacyAllowlist.contains(packageInfo.packageName)) {
89                 continue;
90             }
91 
92             boolean showSummary = sensorPrivacyState == SensorPrivacyManager.StateTypes.DISABLED;
93             CarUiPreference preference =
94                     RequiredInfotainmentAppsUtils.createInfotainmentAppPreference(
95                             getContext(), mPackageManager, packageInfo.packageName, userHandle,
96                             Manifest.permission_group.CAMERA, showSummary);
97 
98             if (preference != null) {
99                 infotainmentAppsPref.addPreference(preference);
100             }
101         }
102     }
103 }
104