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 static android.Manifest.permission_group.LOCATION; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.icu.text.RelativeDateTimeFormatter; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 import android.provider.DeviceConfig; 24 import android.provider.Settings; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceCategory; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; 32 import com.android.settings.R; 33 import com.android.settings.dashboard.DashboardFragment; 34 import com.android.settings.dashboard.profileselector.ProfileSelectFragment; 35 import com.android.settingslib.applications.RecentAppOpsAccess; 36 import com.android.settingslib.utils.StringUtil; 37 import com.android.settingslib.widget.AppPreference; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 /** 43 * Preference controller that handles the display of apps that access locations. 44 */ 45 public class RecentLocationAccessPreferenceController extends LocationBasePreferenceController { 46 public static final int MAX_APPS = 3; 47 @VisibleForTesting 48 RecentAppOpsAccess mRecentLocationApps; 49 private PreferenceCategory mCategoryRecentLocationRequests; 50 private int mType = ProfileSelectFragment.ProfileType.ALL; 51 private boolean mShowSystem = false; 52 private boolean mSystemSettingChanged = false; 53 54 private static class PackageEntryClickedListener implements 55 Preference.OnPreferenceClickListener { 56 private final Context mContext; 57 private final String mPackage; 58 private final UserHandle mUserHandle; 59 PackageEntryClickedListener(Context context, String packageName, UserHandle userHandle)60 PackageEntryClickedListener(Context context, String packageName, 61 UserHandle userHandle) { 62 mContext = context; 63 mPackage = packageName; 64 mUserHandle = userHandle; 65 } 66 67 @Override onPreferenceClick(Preference preference)68 public boolean onPreferenceClick(Preference preference) { 69 final Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION); 70 intent.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()); 71 intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, LOCATION); 72 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackage); 73 intent.putExtra(Intent.EXTRA_USER, mUserHandle); 74 mContext.startActivity(intent); 75 return true; 76 } 77 } 78 RecentLocationAccessPreferenceController(Context context, String key)79 public RecentLocationAccessPreferenceController(Context context, String key) { 80 this(context, key, RecentAppOpsAccess.createForLocation(context)); 81 } 82 83 @VisibleForTesting RecentLocationAccessPreferenceController(Context context, String key, RecentAppOpsAccess recentLocationApps)84 public RecentLocationAccessPreferenceController(Context context, String key, 85 RecentAppOpsAccess recentLocationApps) { 86 super(context, key); 87 mRecentLocationApps = recentLocationApps; 88 mShowSystem = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, 89 SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SMALL_ENABLED, false) 90 ? Settings.Secure.getInt(mContext.getContentResolver(), 91 Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1 92 : false; 93 } 94 95 @Override displayPreference(PreferenceScreen screen)96 public void displayPreference(PreferenceScreen screen) { 97 super.displayPreference(screen); 98 mCategoryRecentLocationRequests = screen.findPreference(getPreferenceKey()); 99 mLocationEnabler.refreshLocationMode(); 100 loadRecentAccesses(); 101 } 102 103 @Override updateState(Preference preference)104 public void updateState(Preference preference) { 105 // Only reload the recent accesses in updateState if the system setting has changed. 106 if (mSystemSettingChanged) { 107 loadRecentAccesses(); 108 mSystemSettingChanged = false; 109 } 110 } 111 loadRecentAccesses()112 private void loadRecentAccesses() { 113 mCategoryRecentLocationRequests.removeAll(); 114 final Context prefContext = mCategoryRecentLocationRequests.getContext(); 115 final List<RecentAppOpsAccess.Access> recentLocationAccesses = new ArrayList<>(); 116 final UserManager userManager = UserManager.get(mContext); 117 for (RecentAppOpsAccess.Access access : mRecentLocationApps.getAppListSorted(mShowSystem)) { 118 if (isRequestMatchesProfileType(userManager, access, mType)) { 119 recentLocationAccesses.add(access); 120 if (recentLocationAccesses.size() == MAX_APPS) { 121 break; 122 } 123 } 124 } 125 126 if (recentLocationAccesses.size() > 0) { 127 // Add preferences to container in original order (already sorted by recency). 128 for (RecentAppOpsAccess.Access access : recentLocationAccesses) { 129 mCategoryRecentLocationRequests.addPreference( 130 createAppPreference(prefContext, access, mFragment)); 131 } 132 } else { 133 // If there's no item to display, add a "No recent apps" item. 134 final Preference banner = new AppPreference(prefContext); 135 banner.setTitle(R.string.location_no_recent_accesses); 136 banner.setSelectable(false); 137 mCategoryRecentLocationRequests.addPreference(banner); 138 } 139 } 140 141 @Override onLocationModeChanged(int mode, boolean restricted)142 public void onLocationModeChanged(int mode, boolean restricted) { 143 boolean enabled = mLocationEnabler.isEnabled(mode); 144 mCategoryRecentLocationRequests.setVisible(enabled); 145 } 146 147 /** 148 * Clears the list of apps which recently accessed location from the screen. 149 */ clearPreferenceList()150 public void clearPreferenceList() { 151 if (mCategoryRecentLocationRequests != null) { 152 mCategoryRecentLocationRequests.removeAll(); 153 } 154 } 155 156 /** 157 * Initialize {@link ProfileSelectFragment.ProfileType} of the controller 158 * 159 * @param type {@link ProfileSelectFragment.ProfileType} of the controller. 160 */ setProfileType(@rofileSelectFragment.ProfileType int type)161 public void setProfileType(@ProfileSelectFragment.ProfileType int type) { 162 mType = type; 163 } 164 165 /** 166 * Create a {@link AppPreference} 167 */ createAppPreference(Context prefContext, RecentAppOpsAccess.Access access, DashboardFragment fragment)168 public static AppPreference createAppPreference(Context prefContext, 169 RecentAppOpsAccess.Access access, DashboardFragment fragment) { 170 final AppPreference pref = new AppPreference(prefContext); 171 pref.setIcon(access.icon); 172 pref.setTitle(access.label); 173 pref.setSummary(StringUtil.formatRelativeTime(prefContext, 174 System.currentTimeMillis() - access.accessFinishTime, false, 175 RelativeDateTimeFormatter.Style.LONG)); 176 pref.setOnPreferenceClickListener(new PackageEntryClickedListener( 177 fragment.getContext(), access.packageName, access.userHandle)); 178 return pref; 179 } 180 181 /** 182 * Return if the {@link RecentAppOpsAccess.Access} matches current UI 183 * {@link ProfileSelectFragment.ProfileType} 184 */ isRequestMatchesProfileType(UserManager userManager, RecentAppOpsAccess.Access access, @ProfileSelectFragment.ProfileType int type)185 public static boolean isRequestMatchesProfileType(UserManager userManager, 186 RecentAppOpsAccess.Access access, @ProfileSelectFragment.ProfileType int type) { 187 188 final boolean isWorkProfile = userManager.isManagedProfile( 189 access.userHandle.getIdentifier()); 190 if (isWorkProfile && (type & ProfileSelectFragment.ProfileType.WORK) != 0) { 191 return true; 192 } 193 if (!isWorkProfile && (type & ProfileSelectFragment.ProfileType.PERSONAL) != 0) { 194 return true; 195 } 196 return false; 197 } 198 199 /** 200 * Update the state of the showSystem setting flag and load the new results. 201 */ updateShowSystem()202 void updateShowSystem() { 203 mSystemSettingChanged = true; 204 mShowSystem = !mShowSystem; 205 clearPreferenceList(); 206 loadRecentAccesses(); 207 } 208 } 209