Merge "Several improvements and fixes in restrictions UI" into jb-mr2-dev

This commit is contained in:
Amith Yamasani
2013-04-19 23:48:11 +00:00
committed by Android (Google) Code Review
3 changed files with 86 additions and 37 deletions

View File

@@ -4470,6 +4470,8 @@
<string name="user_restrictions_controlled_by">Controlled by <xliff:g id="app">%1$s</xliff:g></string> <string name="user_restrictions_controlled_by">Controlled by <xliff:g id="app">%1$s</xliff:g></string>
<!-- Summary for apps that aren't supported in limited users [CHAR LIMIT=none] --> <!-- Summary for apps that aren't supported in limited users [CHAR LIMIT=none] -->
<string name="app_not_supported_in_limited">This app is not supported in limited users</string> <string name="app_not_supported_in_limited">This app is not supported in limited users</string>
<!-- Summary text for apps that are allowed to access accounts from the primary user [CHAR LIMIT=none] -->
<string name="app_sees_restricted_accounts">This app can access your accounts</string>
<!-- Restrictions title for configuring wifi and mobile [CHAR LIMIT=35] --> <!-- Restrictions title for configuring wifi and mobile [CHAR LIMIT=35] -->
<string name="restriction_wifi_config_title">Wi\u2011Fi and Mobile</string> <string name="restriction_wifi_config_title">Wi\u2011Fi and Mobile</string>

View File

@@ -38,6 +38,7 @@ import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.RemoteException; import android.os.RemoteException;
@@ -117,6 +118,9 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap = private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap =
new HashMap<Integer,AppRestrictionsPreference>(); new HashMap<Integer,AppRestrictionsPreference>();
private List<SelectableAppInfo> mVisibleApps;
private List<ApplicationInfo> mUserApps;
static class SelectableAppInfo { static class SelectableAppInfo {
String packageName; String packageName;
CharSequence appName; CharSequence appName;
@@ -265,10 +269,8 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mAppListChanged = false; mAppListChanged = false;
if (mFirstTime) { new AppLoadingTask().execute((Void[]) null);
mFirstTime = false;
populateApps();
}
UserInfo info = mUserManager.getUserInfo(mUser.getIdentifier()); UserInfo info = mUserManager.getUserInfo(mUser.getIdentifier());
mUserPreference.setTitle(info.name); mUserPreference.setTitle(info.name);
Bitmap userIcon = mUserManager.getUserIcon(mUser.getIdentifier()); Bitmap userIcon = mUserManager.getUserIcon(mUser.getIdentifier());
@@ -292,18 +294,30 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
private void updateUserAppList() { private void updateUserAppList() {
IPackageManager ipm = IPackageManager.Stub.asInterface( IPackageManager ipm = IPackageManager.Stub.asInterface(
ServiceManager.getService("package")); ServiceManager.getService("package"));
final int userId = mUser.getIdentifier();
if (!mUserManager.getUserInfo(userId).isRestricted()) {
Log.e(TAG, "Cannot apply application restrictions on a regular user!");
return;
}
for (Map.Entry<String,Boolean> entry : mSelectedPackages.entrySet()) { for (Map.Entry<String,Boolean> entry : mSelectedPackages.entrySet()) {
String packageName = entry.getKey();
if (entry.getValue()) { if (entry.getValue()) {
// Enable selected apps // Enable selected apps
try { try {
ipm.installExistingPackageAsUser(entry.getKey(), mUser.getIdentifier()); ApplicationInfo info = ipm.getApplicationInfo(packageName, 0, userId);
if (info == null || info.enabled == false) {
ipm.installExistingPackageAsUser(packageName, mUser.getIdentifier());
}
} catch (RemoteException re) { } catch (RemoteException re) {
} }
} else { } else {
// Blacklist all other apps, system or downloaded // Blacklist all other apps, system or downloaded
try { try {
ipm.deletePackageAsUser(entry.getKey(), null, mUser.getIdentifier(), ApplicationInfo info = ipm.getApplicationInfo(packageName, 0, userId);
PackageManager.DELETE_SYSTEM_APP); if (info != null) {
ipm.deletePackageAsUser(entry.getKey(), null, mUser.getIdentifier(),
PackageManager.DELETE_SYSTEM_APP);
}
} catch (RemoteException re) { } catch (RemoteException re) {
} }
} }
@@ -331,10 +345,27 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
} }
} }
private void populateApps() { private class AppLoadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
fetchAndMergeApps();
return null;
}
@Override
protected void onPostExecute(Void result) {
populateApps();
}
@Override
protected void onPreExecute() {
}
}
private void fetchAndMergeApps() {
mAppList.setOrderingAsAdded(false); mAppList.setOrderingAsAdded(false);
List<SelectableAppInfo> visibleApps = new ArrayList<SelectableAppInfo>(); mVisibleApps = new ArrayList<SelectableAppInfo>();
// TODO: Do this asynchronously since it can be a long operation
final Context context = getActivity(); final Context context = getActivity();
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
IPackageManager ipm = AppGlobals.getPackageManager(); IPackageManager ipm = AppGlobals.getPackageManager();
@@ -342,11 +373,11 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
// Add launchers // Add launchers
Intent launcherIntent = new Intent(Intent.ACTION_MAIN); Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addSystemApps(visibleApps, launcherIntent); addSystemApps(mVisibleApps, launcherIntent);
// Add widgets // Add widgets
Intent widgetIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); Intent widgetIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
addSystemApps(visibleApps, widgetIntent); addSystemApps(mVisibleApps, widgetIntent);
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0); List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
for (ApplicationInfo app : installedApps) { for (ApplicationInfo app : installedApps) {
@@ -358,20 +389,19 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
info.appName = app.loadLabel(pm); info.appName = app.loadLabel(pm);
info.activityName = info.appName; info.activityName = info.appName;
info.icon = app.loadIcon(pm); info.icon = app.loadIcon(pm);
visibleApps.add(info); mVisibleApps.add(info);
} }
} }
// Now check apps that are installed on target user mUserApps = null;
List<ApplicationInfo> userApps = null;
try { try {
userApps = ipm.getInstalledApplications( mUserApps = ipm.getInstalledApplications(
0, mUser.getIdentifier()).getList(); 0, mUser.getIdentifier()).getList();
} catch (RemoteException re) { } catch (RemoteException re) {
} }
if (userApps != null) { if (mUserApps != null) {
for (ApplicationInfo app : userApps) { for (ApplicationInfo app : mUserApps) {
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0
&& (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) { && (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
// Downloaded app // Downloaded app
@@ -380,25 +410,30 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
info.appName = app.loadLabel(pm); info.appName = app.loadLabel(pm);
info.activityName = info.appName; info.activityName = info.appName;
info.icon = app.loadIcon(pm); info.icon = app.loadIcon(pm);
visibleApps.add(info); mVisibleApps.add(info);
} }
} }
} }
Collections.sort(visibleApps, new AppLabelComparator()); Collections.sort(mVisibleApps, new AppLabelComparator());
// Remove dupes // Remove dupes
for (int i = visibleApps.size() - 1; i > 1; i--) { Set<String> dedupPackageSet = new HashSet<String>();
SelectableAppInfo info = visibleApps.get(i); for (int i = mVisibleApps.size() - 1; i >= 0; i--) {
SelectableAppInfo info = mVisibleApps.get(i);
if (DEBUG) Log.i(TAG, info.toString()); if (DEBUG) Log.i(TAG, info.toString());
if (info.packageName.equals(visibleApps.get(i-1).packageName) String both = info.packageName + "+" + info.activityName;
&& info.activityName.equals(visibleApps.get(i-1).activityName)) { if (!TextUtils.isEmpty(info.packageName)
visibleApps.remove(i); && !TextUtils.isEmpty(info.activityName)
&& dedupPackageSet.contains(both)) {
mVisibleApps.remove(i);
} else {
dedupPackageSet.add(both);
} }
} }
// Establish master/slave relationship for entries that share a package name // Establish master/slave relationship for entries that share a package name
HashMap<String,SelectableAppInfo> packageMap = new HashMap<String,SelectableAppInfo>(); HashMap<String,SelectableAppInfo> packageMap = new HashMap<String,SelectableAppInfo>();
for (SelectableAppInfo info : visibleApps) { for (SelectableAppInfo info : mVisibleApps) {
if (packageMap.containsKey(info.packageName)) { if (packageMap.containsKey(info.packageName)) {
info.masterEntry = packageMap.get(info.packageName); info.masterEntry = packageMap.get(info.packageName);
} else { } else {
@@ -406,11 +441,17 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
} }
} }
}
private void populateApps() {
final Context context = getActivity();
PackageManager pm = context.getPackageManager();
IPackageManager ipm = AppGlobals.getPackageManager();
Intent restrictionsIntent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES); Intent restrictionsIntent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(restrictionsIntent, 0); final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(restrictionsIntent, 0);
int i = 0; int i = 0;
if (visibleApps.size() > 0) { if (mVisibleApps.size() > 0) {
for (SelectableAppInfo app : visibleApps) { for (SelectableAppInfo app : mVisibleApps) {
String packageName = app.packageName; String packageName = app.packageName;
if (packageName == null) continue; if (packageName == null) continue;
final boolean isSettingsApp = packageName.equals(getActivity().getPackageName()); final boolean isSettingsApp = packageName.equals(getActivity().getPackageName());
@@ -442,7 +483,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
p.setImmutable(true); p.setImmutable(true);
// If the app is required and has no restrictions, skip showing it // If the app is required and has no restrictions, skip showing it
if (!hasSettings && !isSettingsApp) continue; if (!hasSettings && !isSettingsApp) continue;
} else if (!mNewUser && appInfoListHasPackage(userApps, packageName)) { } else if (!mNewUser && appInfoListHasPackage(mUserApps, packageName)) {
p.setChecked(true); p.setChecked(true);
} }
if (pi.requiredAccountType != null && pi.restrictedAccountType == null) { if (pi.requiredAccountType != null && pi.restrictedAccountType == null) {
@@ -450,6 +491,9 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
p.setImmutable(true); p.setImmutable(true);
p.setSummary(R.string.app_not_supported_in_limited); p.setSummary(R.string.app_not_supported_in_limited);
} }
if (pi.restrictedAccountType != null) {
p.setSummary(R.string.app_sees_restricted_accounts);
}
if (app.masterEntry != null) { if (app.masterEntry != null) {
p.setImmutable(true); p.setImmutable(true);
p.setChecked(mSelectedPackages.get(packageName)); p.setChecked(mSelectedPackages.get(packageName));
@@ -634,8 +678,10 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
RestrictionUtils.restrictionsToBundle(restrictions), mUser); RestrictionUtils.restrictionsToBundle(restrictions), mUser);
} else if (restrictionsIntent != null) { } else if (restrictionsIntent != null) {
final Intent customIntent = restrictionsIntent; final Intent customIntent = restrictionsIntent;
customIntent.putExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE, if (restrictions != null) {
RestrictionUtils.restrictionsToBundle(restrictions)); customIntent.putExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE,
RestrictionUtils.restrictionsToBundle(restrictions));
}
Preference p = new Preference(context); Preference p = new Preference(context);
p.setTitle(R.string.app_restrictions_custom_label); p.setTitle(R.string.app_restrictions_custom_label);
p.setOnPreferenceClickListener(new OnPreferenceClickListener() { p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -732,9 +778,6 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Got activity resultCode=" + resultCode + ", requestCode="
+ requestCode + ", data=" + data);
AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode); AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode);
if (pref == null) { if (pref == null) {
Log.w(TAG, "Unknown requestCode " + requestCode); Log.w(TAG, "Unknown requestCode " + requestCode);
@@ -742,14 +785,18 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
} }
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
String packageName = pref.getKey().substring(PKG_PREFIX.length());
ArrayList<RestrictionEntry> list = ArrayList<RestrictionEntry> list =
data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST); data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST);
Bundle bundle = data.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
if (list != null) { if (list != null) {
// If there's a valid result, persist it to the user manager. // If there's a valid result, persist it to the user manager.
String packageName = pref.getKey().substring(PKG_PREFIX.length());
pref.setRestrictions(list); pref.setRestrictions(list);
mUserManager.setApplicationRestrictions(packageName, mUserManager.setApplicationRestrictions(packageName,
RestrictionUtils.restrictionsToBundle(list), mUser); RestrictionUtils.restrictionsToBundle(list), mUser);
} else if (bundle != null) {
// If there's a valid result, persist it to the user manager.
mUserManager.setApplicationRestrictions(packageName, bundle, mUser);
} }
toggleAppPanel(pref); toggleAppPanel(pref);
} }

View File

@@ -468,7 +468,7 @@ public class UserSettings extends SettingsPreferenceFragment
private void addUserNow(final int userType) { private void addUserNow(final int userType) {
synchronized (mUserLock) { synchronized (mUserLock) {
mAddingUser = true; mAddingUser = true;
updateUserList(); //updateUserList();
new Thread() { new Thread() {
public void run() { public void run() {
UserInfo user = null; UserInfo user = null;
@@ -480,8 +480,8 @@ public class UserSettings extends SettingsPreferenceFragment
} }
synchronized (mUserLock) { synchronized (mUserLock) {
mAddingUser = false; mAddingUser = false;
mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
if (userType == USER_TYPE_TRUSTED) { if (userType == USER_TYPE_TRUSTED) {
mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
mHandler.sendMessage(mHandler.obtainMessage( mHandler.sendMessage(mHandler.obtainMessage(
MESSAGE_SETUP_USER, user.id, user.serialNumber)); MESSAGE_SETUP_USER, user.id, user.serialNumber));
} else { } else {