Update Location Settings for Enterprise

Find packages of the corporate user and show them
with badged icon on the "Recent location requests" list.

Bug: 15467321
Change-Id: Ib0fdb9f0c811307875296c37bab6d1fbab919022
This commit is contained in:
Zoltan Szatmary-Ban
2014-06-16 13:08:24 +01:00
parent 5427b9a799
commit ca153c55dd

View File

@@ -16,15 +16,18 @@
package com.android.settings.location; package com.android.settings.location;
import android.app.ActivityManager; import android.app.AppGlobals;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.IPackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager;
import android.preference.Preference; import android.preference.Preference;
import android.util.Log; import android.util.Log;
@@ -89,33 +92,37 @@ public class RecentLocationApps {
} }
/** /**
* Fills a list of applications which queried location recently within * Fills a list of applications which queried location recently within specified time.
* specified time.
*/ */
public List<Preference> getAppList() { public List<Preference> getAppList() {
// Retrieve a location usage list from AppOps // Retrieve a location usage list from AppOps
AppOpsManager aoManager = AppOpsManager aoManager =
(AppOpsManager) mActivity.getSystemService(Context.APP_OPS_SERVICE); (AppOpsManager) mActivity.getSystemService(Context.APP_OPS_SERVICE);
List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps( List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps(new int[] {
new int[] { AppOpsManager.OP_MONITOR_LOCATION, AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION, });
AppOpsManager.OP_MONITOR_LOCATION,
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION,
});
// Process the AppOps list and generate a preference list. // Process the AppOps list and generate a preference list.
ArrayList<Preference> prefs = new ArrayList<Preference>(); ArrayList<Preference> prefs = new ArrayList<Preference>();
long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
for (AppOpsManager.PackageOps ops : appOps) { final UserManager um = (UserManager) mActivity.getSystemService(Context.USER_SERVICE);
final List<UserHandle> profiles = um.getUserProfiles();
final int appOpsN = appOps.size();
for (int i = 0; i < appOpsN; ++i) {
AppOpsManager.PackageOps ops = appOps.get(i);
// Don't show the Android System in the list - it's not actionable for the user. // Don't show the Android System in the list - it's not actionable for the user.
// Also don't show apps belonging to background users. // Also don't show apps belonging to background users except managed users.
String packageName = ops.getPackageName();
int uid = ops.getUid(); int uid = ops.getUid();
boolean isAndroidOs = (uid == Process.SYSTEM_UID) int userId = UserHandle.getUserId(uid);
&& ANDROID_SYSTEM_PACKAGE_NAME.equals(ops.getPackageName()); boolean isAndroidOs =
if (!isAndroidOs && ActivityManager.getCurrentUser() == UserHandle.getUserId(uid)) { (uid == Process.SYSTEM_UID) && ANDROID_SYSTEM_PACKAGE_NAME.equals(packageName);
Preference pref = getPreferenceFromOps(now, ops); if (isAndroidOs || !profiles.contains(UserHandle.fromUserId(userId))) {
if (pref != null) { continue;
prefs.add(pref); }
} Preference preference = getPreferenceFromOps(um, now, ops);
if (preference != null) {
prefs.add(preference);
} }
} }
@@ -130,7 +137,8 @@ public class RecentLocationApps {
* When the PackageOps is fresh enough, this method returns a Preference pointing to the App * When the PackageOps is fresh enough, this method returns a Preference pointing to the App
* Info page for that package. * Info page for that package.
*/ */
private Preference getPreferenceFromOps(long now, AppOpsManager.PackageOps ops) { private Preference getPreferenceFromOps(final UserManager um, long now,
AppOpsManager.PackageOps ops) {
String packageName = ops.getPackageName(); String packageName = ops.getPackageName();
List<AppOpsManager.OpEntry> entries = ops.getOps(); List<AppOpsManager.OpEntry> entries = ops.getOps();
boolean highBattery = false; boolean highBattery = false;
@@ -161,30 +169,24 @@ public class RecentLocationApps {
// The package is fresh enough, continue. // The package is fresh enough, continue.
Preference pref = null; int uid = ops.getUid();
int userId = UserHandle.getUserId(uid);
Preference preference = null;
try { try {
ApplicationInfo appInfo = mPackageManager.getApplicationInfo( IPackageManager ipm = AppGlobals.getPackageManager();
packageName, PackageManager.GET_META_DATA); ApplicationInfo appInfo =
// Multiple users can install the same package. Each user gets a different Uid for ipm.getApplicationInfo(packageName, PackageManager.GET_META_DATA, userId);
// the same package.
// Drawable icon = um.getBadgedDrawableForUser(
// Here we retrieve the Uid with package name, that will be the Uid for that package mPackageManager.getApplicationIcon(appInfo), UserHandle.fromUserId(userId));
// associated with the current active user. If the Uid differs from the Uid in ops, preference = createRecentLocationEntry(icon,
// that means this entry belongs to another inactive user and we should ignore that. mPackageManager.getApplicationLabel(appInfo), highBattery,
if (appInfo.uid == ops.getUid()) { new PackageEntryClickedListener(packageName));
pref = createRecentLocationEntry( } catch (RemoteException e) {
mPackageManager.getApplicationIcon(appInfo), Log.w(TAG, "Error while retrieving application info", e);
mPackageManager.getApplicationLabel(appInfo),
highBattery,
new PackageEntryClickedListener(packageName));
} else if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "package " + packageName + " with Uid " + ops.getUid() +
" belongs to another inactive account, ignored.");
}
} catch (PackageManager.NameNotFoundException e) {
Log.wtf(TAG, "Package not found: " + packageName, e);
} }
return pref; return preference;
} }
} }