• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
29 import com.android.settingslib.location.RecentLocationAccesses;
30 import com.android.settingslib.widget.AppPreference;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /** Preference controller for preference category displaying all recent location access (apps). */
36 public class RecentLocationAccessSeeAllPreferenceController
37         extends LocationBasePreferenceController {
38 
39     private PreferenceScreen mCategoryAllRecentLocationAccess;
40     private final RecentLocationAccesses mRecentLocationAccesses;
41     private boolean mShowSystem = false;
42     private Preference mPreference;
43     private int mType = ProfileSelectFragment.ProfileType.ALL;
44 
RecentLocationAccessSeeAllPreferenceController(Context context, String key)45     public RecentLocationAccessSeeAllPreferenceController(Context context, String key) {
46         super(context, key);
47         mRecentLocationAccesses = new RecentLocationAccesses(context);
48     }
49 
50     @Override
onLocationModeChanged(int mode, boolean restricted)51     public void onLocationModeChanged(int mode, boolean restricted) {
52         mCategoryAllRecentLocationAccess.setEnabled(mLocationEnabler.isEnabled(mode));
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         super.displayPreference(screen);
58         mCategoryAllRecentLocationAccess = screen.findPreference(getPreferenceKey());
59     }
60 
61     @Override
updateState(Preference preference)62     public void updateState(Preference preference) {
63         mCategoryAllRecentLocationAccess.removeAll();
64         mPreference = preference;
65 
66         final UserManager userManager = UserManager.get(mContext);
67 
68         final List<RecentLocationAccesses.Access> recentLocationAccesses = new ArrayList<>();
69         for (RecentLocationAccesses.Access access : mRecentLocationAccesses.getAppListSorted(
70                 mShowSystem)) {
71             if (isRequestMatchesProfileType(userManager, access, mType)) {
72                 recentLocationAccesses.add(access);
73             }
74         }
75 
76         if (recentLocationAccesses.isEmpty()) {
77             // If there's no item to display, add a "No recent apps" item.
78             final Preference banner = new AppPreference(mContext);
79             banner.setTitle(R.string.location_no_recent_apps);
80             banner.setSelectable(false);
81             mCategoryAllRecentLocationAccess.addPreference(banner);
82         } else {
83             for (RecentLocationAccesses.Access request : recentLocationAccesses) {
84                 final Preference appPreference = createAppPreference(
85                         preference.getContext(),
86                         request, mFragment);
87                 mCategoryAllRecentLocationAccess.addPreference(appPreference);
88             }
89         }
90     }
91 
92     /**
93      * Initialize {@link ProfileSelectFragment.ProfileType} of the controller
94      *
95      * @param type {@link ProfileSelectFragment.ProfileType} of the controller.
96      */
setProfileType(@rofileSelectFragment.ProfileType int type)97     public void setProfileType(@ProfileSelectFragment.ProfileType int type) {
98         mType = type;
99     }
100 
101     /**
102      * Set the value of {@link #mShowSystem}.
103      */
setShowSystem(boolean showSystem)104     public void setShowSystem(boolean showSystem) {
105         mShowSystem = showSystem;
106         if (mPreference != null) {
107             updateState(mPreference);
108         }
109     }
110 }
111