• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.location;
15 
16 import android.content.Context;
17 import android.os.Bundle;
18 import android.os.UserHandle;
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.settings.R;
26 import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
27 import com.android.settings.core.SubSettingLauncher;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 import com.android.settingslib.location.RecentLocationApps;
31 import com.android.settingslib.widget.apppreference.AppPreference;
32 
33 import java.util.List;
34 
35 public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController {
36     /** Key for preference category "Recent location requests" */
37     private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests";
38     @VisibleForTesting
39     static final String KEY_SEE_ALL_BUTTON = "recent_location_requests_see_all_button";
40     private final LocationSettings mFragment;
41     private final RecentLocationApps mRecentLocationApps;
42     private PreferenceCategory mCategoryRecentLocationRequests;
43 
44     /** Used in this class and {@link RecentLocationRequestSeeAllPreferenceController} */
45     static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener {
46         private final DashboardFragment mFragment;
47         private final String mPackage;
48         private final UserHandle mUserHandle;
49 
PackageEntryClickedListener(DashboardFragment fragment, String packageName, UserHandle userHandle)50         public PackageEntryClickedListener(DashboardFragment fragment, String packageName,
51                 UserHandle userHandle) {
52             mFragment = fragment;
53             mPackage = packageName;
54             mUserHandle = userHandle;
55         }
56 
57         @Override
onPreferenceClick(Preference preference)58         public boolean onPreferenceClick(Preference preference) {
59             // start new fragment to display extended information
60             final Bundle args = new Bundle();
61             args.putString(AppInfoDashboardFragment.ARG_PACKAGE_NAME, mPackage);
62             new SubSettingLauncher(mFragment.getContext())
63                     .setDestination(AppInfoDashboardFragment.class.getName())
64                     .setArguments(args)
65                     .setTitleRes(R.string.application_info_label)
66                     .setUserHandle(mUserHandle)
67                     .setSourceMetricsCategory(mFragment.getMetricsCategory())
68                     .launch();
69             return true;
70         }
71     }
72 
RecentLocationRequestPreferenceController(Context context, LocationSettings fragment, Lifecycle lifecycle)73     public RecentLocationRequestPreferenceController(Context context, LocationSettings fragment,
74             Lifecycle lifecycle) {
75         this(context, fragment, lifecycle, new RecentLocationApps(context));
76     }
77 
78     @VisibleForTesting
RecentLocationRequestPreferenceController(Context context, LocationSettings fragment, Lifecycle lifecycle, RecentLocationApps recentApps)79     RecentLocationRequestPreferenceController(Context context, LocationSettings fragment,
80             Lifecycle lifecycle, RecentLocationApps recentApps) {
81         super(context, lifecycle);
82         mFragment = fragment;
83         mRecentLocationApps = recentApps;
84     }
85 
86     @Override
getPreferenceKey()87     public String getPreferenceKey() {
88         return KEY_RECENT_LOCATION_REQUESTS;
89     }
90 
91     @Override
displayPreference(PreferenceScreen screen)92     public void displayPreference(PreferenceScreen screen) {
93         super.displayPreference(screen);
94         mCategoryRecentLocationRequests =
95                 (PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS);
96     }
97 
98     @Override
updateState(Preference preference)99     public void updateState(Preference preference) {
100         mCategoryRecentLocationRequests.removeAll();
101         final Context prefContext = preference.getContext();
102         final List<RecentLocationApps.Request> recentLocationRequests =
103                 mRecentLocationApps.getAppListSorted(false);
104         if (recentLocationRequests.size() > 3) {
105             // Display the top 3 preferences to container in original order.
106             for (int i = 0; i < 3; i++) {
107                 mCategoryRecentLocationRequests.addPreference(
108                         createAppPreference(prefContext, recentLocationRequests.get(i)));
109             }
110         } else if (recentLocationRequests.size() > 0) {
111             // Add preferences to container in original order (already sorted by recency).
112             for (RecentLocationApps.Request request : recentLocationRequests) {
113                 mCategoryRecentLocationRequests.addPreference(
114                         createAppPreference(prefContext, request));
115             }
116         } else {
117             // If there's no item to display, add a "No recent apps" item.
118             final Preference banner = createAppPreference(prefContext);
119             banner.setTitle(R.string.location_no_recent_apps);
120             banner.setSelectable(false);
121             mCategoryRecentLocationRequests.addPreference(banner);
122         }
123     }
124 
125     @Override
onLocationModeChanged(int mode, boolean restricted)126     public void onLocationModeChanged(int mode, boolean restricted) {
127         mCategoryRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode));
128     }
129 
130     @VisibleForTesting
createAppPreference(Context prefContext)131     AppPreference createAppPreference(Context prefContext) {
132         return new AppPreference(prefContext);
133     }
134 
135     @VisibleForTesting
createAppPreference(Context prefContext, RecentLocationApps.Request request)136     AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) {
137         final AppPreference pref = createAppPreference(prefContext);
138         pref.setSummary(request.contentDescription);
139         pref.setIcon(request.icon);
140         pref.setTitle(request.label);
141         pref.setOnPreferenceClickListener(new PackageEntryClickedListener(
142                 mFragment, request.packageName, request.userHandle));
143         return pref;
144     }
145 }
146