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.location;
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 apps recently accessing location. Driver assistance apps are
34  * also included.
35  */
36 public class LocationRecentAccessViewAllPreferenceController
37         extends PreferenceController<LogicalPreferenceGroup> {
38 
39     private final RecentAppOpsAccess mRecentLocationAccesses;
40     private boolean mShowSystem = false;
41 
LocationRecentAccessViewAllPreferenceController( Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)42     public LocationRecentAccessViewAllPreferenceController(
43             Context context,
44             String preferenceKey,
45             FragmentController fragmentController,
46             CarUxRestrictions uxRestrictions) {
47         this(
48                 context,
49                 preferenceKey,
50                 fragmentController,
51                 uxRestrictions,
52                 RecentAppOpsAccess.createForLocation(context));
53     }
54 
55     @VisibleForTesting
LocationRecentAccessViewAllPreferenceController( Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, RecentAppOpsAccess recentLocationAccesses)56     LocationRecentAccessViewAllPreferenceController(
57             Context context,
58             String preferenceKey,
59             FragmentController fragmentController,
60             CarUxRestrictions uxRestrictions,
61             RecentAppOpsAccess recentLocationAccesses) {
62         super(context, preferenceKey, fragmentController, uxRestrictions);
63         mRecentLocationAccesses = recentLocationAccesses;
64     }
65 
66     @Override
getPreferenceType()67     protected Class<LogicalPreferenceGroup> getPreferenceType() {
68         return LogicalPreferenceGroup.class;
69     }
70 
71     @Override
updateState(LogicalPreferenceGroup preference)72     public void updateState(LogicalPreferenceGroup preference) {
73         super.updateState(preference);
74         List<RecentAppOpsAccess.Access> recentLocationAccesses = loadData();
75         updateUi(recentLocationAccesses);
76     }
77 
78     /**
79      * Rebuilds the preference list to show system applications if {@code showSystem} is true.
80      * System applications will be hidden otherwise.
81      */
setShowSystem(boolean showSystem)82     public void setShowSystem(boolean showSystem) {
83         if (mShowSystem != showSystem) {
84             mShowSystem = showSystem;
85             refreshUi();
86         }
87     }
88 
loadData()89     private List<RecentAppOpsAccess.Access> loadData() {
90         return mRecentLocationAccesses.getAppListSorted(mShowSystem);
91     }
92 
updateUi(List<RecentAppOpsAccess.Access> recentLocationAccesses)93     private void updateUi(List<RecentAppOpsAccess.Access> recentLocationAccesses) {
94         getPreference().removeAll();
95         if (recentLocationAccesses.isEmpty()) {
96             getPreference().addPreference(createNoRecentAccessPreference());
97         } else {
98             for (RecentAppOpsAccess.Access access : recentLocationAccesses) {
99                 CarUiPreference appPreference =
100                         LocationRecentAccessUtil.createAppPreference(getContext(), access);
101                 getPreference().addPreference(appPreference);
102             }
103         }
104     }
105 
createNoRecentAccessPreference()106     private CarUiPreference createNoRecentAccessPreference() {
107         CarUiPreference preference = new CarUiPreference(getContext());
108         preference.setTitle(R.string.location_no_recent_access);
109         preference.setSelectable(false);
110         return preference;
111     }
112 }
113