• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.Context;
19 
20 import androidx.annotation.VisibleForTesting;
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceCategory;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settingslib.core.lifecycle.Lifecycle;
26 import com.android.settingslib.location.RecentLocationApps;
27 import com.android.settingslib.widget.apppreference.AppPreference;
28 
29 import java.util.List;
30 
31 import com.android.settings.R;
32 
33 /** Preference controller for preference category displaying all recent location requests. */
34 public class RecentLocationRequestSeeAllPreferenceController
35         extends LocationBasePreferenceController {
36     /** Key for preference category "All recent location requests" */
37     private static final String KEY_ALL_RECENT_LOCATION_REQUESTS = "all_recent_location_requests";
38     private final RecentLocationRequestSeeAllFragment mFragment;
39     private PreferenceCategory mCategoryAllRecentLocationRequests;
40     private RecentLocationApps mRecentLocationApps;
41     private boolean mShowSystem = false;
42     private Preference mPreference;
43 
RecentLocationRequestSeeAllPreferenceController( Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment)44     public RecentLocationRequestSeeAllPreferenceController(
45             Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
46         this(context, lifecycle, fragment, new RecentLocationApps(context));
47     }
48 
49     @VisibleForTesting
RecentLocationRequestSeeAllPreferenceController( Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment, RecentLocationApps recentLocationApps)50     RecentLocationRequestSeeAllPreferenceController(
51             Context context,
52             Lifecycle lifecycle,
53             RecentLocationRequestSeeAllFragment fragment,
54             RecentLocationApps recentLocationApps) {
55         super(context, lifecycle);
56         mFragment = fragment;
57         mRecentLocationApps = recentLocationApps;
58     }
59 
60     @Override
getPreferenceKey()61     public String getPreferenceKey() {
62         return KEY_ALL_RECENT_LOCATION_REQUESTS;
63     }
64 
65     @Override
onLocationModeChanged(int mode, boolean restricted)66     public void onLocationModeChanged(int mode, boolean restricted) {
67         mCategoryAllRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode));
68     }
69 
70     @Override
displayPreference(PreferenceScreen screen)71     public void displayPreference(PreferenceScreen screen) {
72         super.displayPreference(screen);
73         mCategoryAllRecentLocationRequests =
74                 (PreferenceCategory) screen.findPreference(KEY_ALL_RECENT_LOCATION_REQUESTS);
75     }
76 
77     @Override
updateState(Preference preference)78     public void updateState(Preference preference) {
79         mCategoryAllRecentLocationRequests.removeAll();
80         mPreference = preference;
81         List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppListSorted(
82                 mShowSystem);
83         if (requests.isEmpty()) {
84             // If there's no item to display, add a "No recent apps" item.
85             final Preference banner = new AppPreference(mContext);
86             banner.setTitle(R.string.location_no_recent_apps);
87             banner.setSelectable(false);
88             mCategoryAllRecentLocationRequests.addPreference(banner);
89         } else {
90             for (RecentLocationApps.Request request : requests) {
91                 Preference appPreference = createAppPreference(preference.getContext(), request);
92                 mCategoryAllRecentLocationRequests.addPreference(appPreference);
93             }
94         }
95     }
96 
97     @VisibleForTesting
createAppPreference( Context prefContext, RecentLocationApps.Request request)98     AppPreference createAppPreference(
99             Context prefContext, RecentLocationApps.Request request) {
100         final AppPreference pref = new AppPreference(prefContext);
101         pref.setSummary(request.contentDescription);
102         pref.setIcon(request.icon);
103         pref.setTitle(request.label);
104         pref.setOnPreferenceClickListener(
105                 new RecentLocationRequestPreferenceController.PackageEntryClickedListener(
106                         mFragment, request.packageName, request.userHandle));
107         return pref;
108     }
109 
setShowSystem(boolean showSystem)110     public void setShowSystem(boolean showSystem) {
111         mShowSystem = showSystem;
112         if (mPreference != null) {
113             updateState(mPreference);
114         }
115     }
116 }
117