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 
22 import com.android.car.settings.R;
23 import com.android.car.settings.common.FragmentController;
24 import com.android.car.settings.common.LogicalPreferenceGroup;
25 import com.android.car.settings.common.PreferenceController;
26 import com.android.car.ui.preference.CarUiPreference;
27 import com.android.internal.annotations.VisibleForTesting;
28 import com.android.settingslib.applications.RecentAppOpsAccess;
29 
30 import java.util.List;
31 
32 /**
33  * This controller displays a list of recently used apps.
34  */
35 public class CameraRecentAccessViewAllPreferenceController extends
36         PreferenceController<LogicalPreferenceGroup> {
37 
38     private final RecentAppOpsAccess mRecentCameraAccesses;
39     private boolean mShowSystem = false;
40 
CameraRecentAccessViewAllPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public CameraRecentAccessViewAllPreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
43         this(context, preferenceKey, fragmentController, uxRestrictions,
44                 RecentAppOpsAccess.createForCamera(context));
45     }
46 
47     @VisibleForTesting
CameraRecentAccessViewAllPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, RecentAppOpsAccess recentCameraAccesses)48     CameraRecentAccessViewAllPreferenceController(Context context, String preferenceKey,
49             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
50             RecentAppOpsAccess recentCameraAccesses) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52         mRecentCameraAccesses = recentCameraAccesses;
53     }
54 
55     @Override
getPreferenceType()56     protected Class<LogicalPreferenceGroup> getPreferenceType() {
57         return LogicalPreferenceGroup.class;
58     }
59 
60     @Override
updateState(LogicalPreferenceGroup preference)61     public void updateState(LogicalPreferenceGroup preference) {
62         super.updateState(preference);
63         List<RecentAppOpsAccess.Access> recentCameraAccesses = loadData();
64         updateUi(recentCameraAccesses);
65     }
66 
67     /**
68      * Rebuilds the preference list to show system applications if {@code showSystem} is true.
69      * System applications will be hidden otherwise.
70      */
setShowSystem(boolean showSystem)71     public void setShowSystem(boolean showSystem) {
72         if (mShowSystem != showSystem) {
73             mShowSystem = showSystem;
74             refreshUi();
75         }
76     }
77 
loadData()78     private List<RecentAppOpsAccess.Access> loadData() {
79         return mRecentCameraAccesses.getAppListSorted(mShowSystem);
80     }
81 
updateUi(List<RecentAppOpsAccess.Access> recentCameraAccesses)82     private void updateUi(List<RecentAppOpsAccess.Access> recentCameraAccesses) {
83         getPreference().removeAll();
84         if (recentCameraAccesses.isEmpty()) {
85             getPreference().addPreference(createNoRecentAccessPreference());
86         } else {
87             for (RecentAppOpsAccess.Access request : recentCameraAccesses) {
88                 CarUiPreference appPreference = CameraRecentAccessUtil.createAppPreference(
89                         getContext(),
90                         request);
91                 getPreference().addPreference(appPreference);
92             }
93         }
94     }
95 
createNoRecentAccessPreference()96     private CarUiPreference createNoRecentAccessPreference() {
97         CarUiPreference preference = new CarUiPreference(getContext());
98         preference.setTitle(R.string.camera_no_recent_access);
99         preference.setSelectable(false);
100         return preference;
101     }
102 }
103 
104