1 /*
2  * Copyright 2021 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 package com.android.settings.location;
17 
18 import static com.android.settings.location.RecentLocationAccessPreferenceController.createAppPreference;
19 import static com.android.settings.location.RecentLocationAccessPreferenceController.isRequestMatchesProfileType;
20 
21 import android.content.Context;
22 import android.os.UserManager;
23 import android.provider.DeviceConfig;
24 import android.provider.Settings;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
30 import com.android.settings.R;
31 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
32 import com.android.settings.overlay.FeatureFactory;
33 import com.android.settingslib.applications.RecentAppOpsAccess;
34 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
35 import com.android.settingslib.widget.AppPreference;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /** Preference controller for preference category displaying all recent location access (apps). */
41 public class RecentLocationAccessSeeAllPreferenceController
42         extends LocationBasePreferenceController {
43 
44     private final RecentAppOpsAccess mRecentLocationAccesses;
45 
46     private PreferenceScreen mCategoryAllRecentLocationAccess;
47     private MetricsFeatureProvider mMetricsFeatureProvider;
48     private boolean mShowSystem = false;
49     private Preference mPreference;
50 
RecentLocationAccessSeeAllPreferenceController(Context context, String key)51     public RecentLocationAccessSeeAllPreferenceController(Context context, String key) {
52         super(context, key);
53         mShowSystem = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
54             SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SMALL_ENABLED, false)
55             ? Settings.Secure.getInt(mContext.getContentResolver(),
56             Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1
57             : false;
58 
59         mRecentLocationAccesses = RecentAppOpsAccess.createForLocation(context);
60         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
61     }
62 
63     @Override
onLocationModeChanged(int mode, boolean restricted)64     public void onLocationModeChanged(int mode, boolean restricted) {
65         mCategoryAllRecentLocationAccess.setEnabled(mLocationEnabler.isEnabled(mode));
66     }
67 
68     @Override
displayPreference(PreferenceScreen screen)69     public void displayPreference(PreferenceScreen screen) {
70         super.displayPreference(screen);
71         mCategoryAllRecentLocationAccess = screen.findPreference(getPreferenceKey());
72     }
73 
74     @Override
updateState(Preference preference)75     public void updateState(Preference preference) {
76         mCategoryAllRecentLocationAccess.removeAll();
77         mPreference = preference;
78 
79         final UserManager userManager = UserManager.get(mContext);
80 
81         final List<RecentAppOpsAccess.Access> recentLocationAccesses = new ArrayList<>();
82         for (RecentAppOpsAccess.Access access : mRecentLocationAccesses.getAppListSorted(
83                 mShowSystem)) {
84             if (isRequestMatchesProfileType(
85                     userManager, access, ProfileSelectFragment.ProfileType.ALL)) {
86                 recentLocationAccesses.add(access);
87             }
88         }
89 
90         if (recentLocationAccesses.isEmpty()) {
91             // If there's no item to display, add a "No recent apps" item.
92             final Preference banner = new AppPreference(mContext);
93             banner.setTitle(R.string.location_no_recent_apps);
94             banner.setSelectable(false);
95             mCategoryAllRecentLocationAccess.addPreference(banner);
96         } else {
97             for (RecentAppOpsAccess.Access request : recentLocationAccesses) {
98                 final Preference appPreference = createAppPreference(
99                         preference.getContext(),
100                         request, mFragment);
101                 mCategoryAllRecentLocationAccess.addPreference(appPreference);
102             }
103         }
104     }
105 
106     /**
107      * Set the value of {@link #mShowSystem}.
108      */
setShowSystem(boolean showSystem)109     public void setShowSystem(boolean showSystem) {
110         mShowSystem = showSystem;
111         if (mPreference != null) {
112             updateState(mPreference);
113             mMetricsFeatureProvider.logClickedPreference(mPreference, getMetricsCategory());
114         }
115     }
116 }
117