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