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 import android.support.annotation.VisibleForTesting; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceCategory; 22 import android.support.v7.preference.PreferenceScreen; 23 import com.android.settings.R; 24 import com.android.settings.applications.appinfo.AppInfoDashboardFragment; 25 import com.android.settings.core.SubSettingLauncher; 26 import com.android.settings.dashboard.DashboardFragment; 27 import com.android.settings.widget.AppPreference; 28 import com.android.settingslib.core.lifecycle.Lifecycle; 29 import com.android.settingslib.location.RecentLocationApps; 30 import java.util.List; 31 32 public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController { 33 34 /** Key for preference category "Recent location requests" */ 35 private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests"; 36 @VisibleForTesting 37 static final String KEY_SEE_ALL_BUTTON = "recent_location_requests_see_all_button"; 38 private final LocationSettings mFragment; 39 private final RecentLocationApps mRecentLocationApps; 40 private PreferenceCategory mCategoryRecentLocationRequests; 41 private Preference mSeeAllButton; 42 43 /** Used in this class and {@link RecentLocationRequestSeeAllPreferenceController}*/ 44 static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener { 45 private final DashboardFragment mFragment; 46 private final String mPackage; 47 private final UserHandle mUserHandle; 48 49 public PackageEntryClickedListener(DashboardFragment fragment, String packageName, 50 UserHandle userHandle) { 51 mFragment = fragment; 52 mPackage = packageName; 53 mUserHandle = userHandle; 54 } 55 56 @Override 57 public boolean onPreferenceClick(Preference preference) { 58 // start new fragment to display extended information 59 final Bundle args = new Bundle(); 60 args.putString(AppInfoDashboardFragment.ARG_PACKAGE_NAME, mPackage); 61 62 new SubSettingLauncher(mFragment.getContext()) 63 .setDestination(AppInfoDashboardFragment.class.getName()) 64 .setArguments(args) 65 .setTitle(R.string.application_info_label) 66 .setUserHandle(mUserHandle) 67 .setSourceMetricsCategory(mFragment.getMetricsCategory()) 68 .launch(); 69 return true; 70 } 71 } 72 73 public RecentLocationRequestPreferenceController(Context context, LocationSettings fragment, 74 Lifecycle lifecycle) { 75 this(context, fragment, lifecycle, new RecentLocationApps(context)); 76 } 77 78 @VisibleForTesting 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 87 public String getPreferenceKey() { 88 return KEY_RECENT_LOCATION_REQUESTS; 89 } 90 91 @Override 92 public void displayPreference(PreferenceScreen screen) { 93 super.displayPreference(screen); 94 mCategoryRecentLocationRequests = 95 (PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS); 96 mSeeAllButton = screen.findPreference(KEY_SEE_ALL_BUTTON); 97 98 } 99 100 @Override 101 public void updateState(Preference preference) { 102 mCategoryRecentLocationRequests.removeAll(); 103 mSeeAllButton.setVisible(false); 104 105 final Context prefContext = preference.getContext(); 106 final List<RecentLocationApps.Request> recentLocationRequests = 107 mRecentLocationApps.getAppListSorted(); 108 109 if (recentLocationRequests.size() > 3) { 110 // Display the top 3 preferences to container in original order. 111 for (int i = 0; i < 3; i ++) { 112 mCategoryRecentLocationRequests.addPreference( 113 createAppPreference(prefContext, recentLocationRequests.get(i))); 114 } 115 // Display a button to list all requests 116 mSeeAllButton.setVisible(true); 117 } else if (recentLocationRequests.size() > 0) { 118 // Add preferences to container in original order (already sorted by recency). 119 for (RecentLocationApps.Request request : recentLocationRequests) { 120 mCategoryRecentLocationRequests.addPreference( 121 createAppPreference(prefContext, request)); 122 } 123 } else { 124 // If there's no item to display, add a "No recent apps" item. 125 final Preference banner = createAppPreference(prefContext); 126 banner.setTitle(R.string.location_no_recent_apps); 127 banner.setSelectable(false); 128 mCategoryRecentLocationRequests.addPreference(banner); 129 } 130 } 131 132 @Override 133 public void onLocationModeChanged(int mode, boolean restricted) { 134 mCategoryRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode)); 135 } 136 137 @VisibleForTesting 138 AppPreference createAppPreference(Context prefContext) { 139 return new AppPreference(prefContext); 140 } 141 142 @VisibleForTesting 143 AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) { 144 final AppPreference pref = createAppPreference(prefContext); 145 pref.setSummary(request.contentDescription); 146 pref.setIcon(request.icon); 147 pref.setTitle(request.label); 148 pref.setOnPreferenceClickListener(new PackageEntryClickedListener( 149 mFragment, request.packageName, request.userHandle)); 150 return pref; 151 } 152 } 153