From 0dd9902c89e7b705fd73158aadf1bf27843ad201 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Thu, 24 Jan 2013 19:14:26 -0800 Subject: [PATCH] App ops: add UI for viewing and controlling ops per-app. Change-Id: Iadd68cbd429af4d431dcd09b9adacd09c5092ae6 --- res/layout/app_ops_details.xml | 57 +++++ res/layout/app_ops_details_item.xml | 71 ++++++ res/values/arrays.xml | 3 + res/values/strings.xml | 4 +- .../settings/applications/AppOpsCategory.java | 47 +++- .../settings/applications/AppOpsDetails.java | 174 +++++++++++++ .../settings/applications/AppOpsState.java | 235 +++++++++++------- .../applications/InstalledAppDetails.java | 2 +- 8 files changed, 496 insertions(+), 97 deletions(-) create mode 100644 res/layout/app_ops_details.xml create mode 100644 res/layout/app_ops_details_item.xml diff --git a/res/layout/app_ops_details.xml b/res/layout/app_ops_details.xml new file mode 100644 index 00000000000..d28689dc4aa --- /dev/null +++ b/res/layout/app_ops_details.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + diff --git a/res/layout/app_ops_details_item.xml b/res/layout/app_ops_details_item.xml new file mode 100644 index 00000000000..a0434fc1162 --- /dev/null +++ b/res/layout/app_ops_details_item.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + diff --git a/res/values/arrays.xml b/res/values/arrays.xml index a8a371f76df..797e6344426 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -572,6 +572,9 @@ Write contacts Read calls Write calls + Read calendar + Write calendar + Wi-Fi scan diff --git a/res/values/strings.xml b/res/values/strings.xml index 419cb11c724..01a52158e99 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2705,7 +2705,9 @@ App ops Running - + + (Never used) + Storage use diff --git a/src/com/android/settings/applications/AppOpsCategory.java b/src/com/android/settings/applications/AppOpsCategory.java index 1eafbbfe3c9..fa164abfa5d 100644 --- a/src/com/android/settings/applications/AppOpsCategory.java +++ b/src/com/android/settings/applications/AppOpsCategory.java @@ -28,6 +28,7 @@ import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; +import android.preference.PreferenceActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -45,9 +46,15 @@ import com.android.settings.applications.AppOpsState.AppOpEntry; public class AppOpsCategory extends ListFragment implements LoaderManager.LoaderCallbacks> { + private static final int RESULT_APP_DETAILS = 1; + + AppOpsState mState; + // This is the Adapter being used to display the list's data. AppListAdapter mAdapter; + String mCurrentPkgName; + public AppOpsCategory() { } @@ -115,9 +122,9 @@ public class AppOpsCategory extends ListFragment implements List mApps; PackageIntentReceiver mPackageObserver; - public AppListLoader(Context context, AppOpsState.OpsTemplate template) { + public AppListLoader(Context context, AppOpsState state, AppOpsState.OpsTemplate template) { super(context); - mState = new AppOpsState(context); + mState = state; mTemplate = template; } @@ -236,13 +243,13 @@ public class AppOpsCategory extends ListFragment implements public static class AppListAdapter extends ArrayAdapter { private final Resources mResources; private final LayoutInflater mInflater; - private final CharSequence[] mOpNames; + private final AppOpsState mState; - public AppListAdapter(Context context) { + public AppListAdapter(Context context, AppOpsState state) { super(context, android.R.layout.simple_list_item_2); mResources = context.getResources(); mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mOpNames = mResources.getTextArray(R.array.app_ops_names); + mState = state; } public void setData(List data) { @@ -268,13 +275,19 @@ public class AppOpsCategory extends ListFragment implements ((ImageView)view.findViewById(R.id.app_icon)).setImageDrawable( item.getAppEntry().getIcon()); ((TextView)view.findViewById(R.id.app_name)).setText(item.getAppEntry().getLabel()); - ((TextView)view.findViewById(R.id.op_name)).setText(item.getLabelText(mOpNames)); + ((TextView)view.findViewById(R.id.op_name)).setText(item.getLabelText(mState)); ((TextView)view.findViewById(R.id.op_time)).setText(item.getTimeText(mResources)); return view; } } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mState = new AppOpsState(getActivity()); + } + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); @@ -286,7 +299,7 @@ public class AppOpsCategory extends ListFragment implements setHasOptionsMenu(true); // Create an empty adapter we will use to display the loaded data. - mAdapter = new AppListAdapter(getActivity()); + mAdapter = new AppListAdapter(getActivity(), mState); setListAdapter(mAdapter); // Start out with a progress indicator. @@ -297,9 +310,23 @@ public class AppOpsCategory extends ListFragment implements getLoaderManager().initLoader(0, null, this); } + // utility method used to start sub activity + private void startApplicationDetailsActivity() { + // start new fragment to display extended information + Bundle args = new Bundle(); + args.putString(AppOpsDetails.ARG_PACKAGE_NAME, mCurrentPkgName); + + PreferenceActivity pa = (PreferenceActivity)getActivity(); + pa.startPreferencePanel(AppOpsDetails.class.getName(), args, + R.string.app_ops_settings, null, this, RESULT_APP_DETAILS); + } + @Override public void onListItemClick(ListView l, View v, int position, long id) { - // Insert desired behavior here. - Log.i("LoaderCustom", "Item clicked: " + id); + AppOpEntry entry = mAdapter.getItem(position); + if (entry != null) { + mCurrentPkgName = entry.getAppEntry().getApplicationInfo().packageName; + startApplicationDetailsActivity(); + } } @Override public Loader> onCreateLoader(int id, Bundle args) { @@ -308,7 +335,7 @@ public class AppOpsCategory extends ListFragment implements if (fargs != null) { template = (AppOpsState.OpsTemplate)fargs.getParcelable("template"); } - return new AppListLoader(getActivity(), template); + return new AppListLoader(getActivity(), mState, template); } @Override public void onLoadFinished(Loader> loader, List data) { diff --git a/src/com/android/settings/applications/AppOpsDetails.java b/src/com/android/settings/applications/AppOpsDetails.java index 2a071f20d91..9bc90a75827 100644 --- a/src/com/android/settings/applications/AppOpsDetails.java +++ b/src/com/android/settings/applications/AppOpsDetails.java @@ -16,8 +16,182 @@ package com.android.settings.applications; +import android.app.Activity; +import android.app.AppOpsManager; import android.app.Fragment; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionGroupInfo; +import android.content.pm.PermissionInfo; +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CompoundButton; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.Switch; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.Utils; + +import java.util.List; public class AppOpsDetails extends Fragment { + static final String TAG = "AppOpsDetails"; + public static final String ARG_PACKAGE_NAME = "package"; + + private AppOpsState mState; + private PackageManager mPm; + private AppOpsManager mAppOps; + private PackageInfo mPackageInfo; + private LayoutInflater mInflater; + private View mRootView; + private TextView mAppVersion; + private LinearLayout mOperationsSection; + + // Utility method to set application label and icon. + private void setAppLabelAndIcon(PackageInfo pkgInfo) { + final View appSnippet = mRootView.findViewById(R.id.app_snippet); + appSnippet.setPaddingRelative(0, appSnippet.getPaddingTop(), 0, appSnippet.getPaddingBottom()); + + ImageView icon = (ImageView) appSnippet.findViewById(R.id.app_icon); + icon.setImageDrawable(mPm.getApplicationIcon(pkgInfo.applicationInfo)); + // Set application name. + TextView label = (TextView) appSnippet.findViewById(R.id.app_name); + label.setText(mPm.getApplicationLabel(pkgInfo.applicationInfo)); + // Version number of application + mAppVersion = (TextView) appSnippet.findViewById(R.id.app_size); + + if (pkgInfo.versionName != null) { + mAppVersion.setVisibility(View.VISIBLE); + mAppVersion.setText(getActivity().getString(R.string.version_text, + String.valueOf(pkgInfo.versionName))); + } else { + mAppVersion.setVisibility(View.INVISIBLE); + } + } + + private String retrieveAppEntry() { + final Bundle args = getArguments(); + String packageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null; + if (packageName == null) { + Intent intent = (args == null) ? + getActivity().getIntent() : (Intent) args.getParcelable("intent"); + if (intent != null) { + packageName = intent.getData().getSchemeSpecificPart(); + } + } + try { + mPackageInfo = mPm.getPackageInfo(packageName, + PackageManager.GET_DISABLED_COMPONENTS | + PackageManager.GET_UNINSTALLED_PACKAGES); + } catch (NameNotFoundException e) { + Log.e(TAG, "Exception when retrieving package:" + packageName, e); + mPackageInfo = null; + } + + return packageName; + } + + private boolean refreshUi() { + if (mPackageInfo == null) { + return false; + } + + setAppLabelAndIcon(mPackageInfo); + + Resources res = getActivity().getResources(); + + mOperationsSection.removeAllViews(); + String lastPermGroup = ""; + for (AppOpsState.OpsTemplate tpl : AppOpsState.ALL_TEMPLATES) { + List entries = mState.buildState(tpl, + mPackageInfo.applicationInfo.uid, mPackageInfo.packageName); + for (final AppOpsState.AppOpEntry entry : entries) { + for (int i=0; i mApps; @@ -49,23 +55,18 @@ public class AppOpsState { mContext = context; mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); mPm = context.getPackageManager(); + mOpNames = context.getResources().getTextArray(R.array.app_ops_names); } public static class OpsTemplate implements Parcelable { public final int[] ops; - public final String[] perms; - public final int[] permOps; - public OpsTemplate(int[] _ops, String[] _perms, int[] _permOps) { + public OpsTemplate(int[] _ops) { ops = _ops; - perms = _perms; - permOps = _permOps; } OpsTemplate(Parcel src) { ops = src.createIntArray(); - perms = src.createStringArray(); - permOps = src.createIntArray(); } @Override @@ -76,8 +77,6 @@ public class AppOpsState { @Override public void writeToParcel(Parcel dest, int flags) { dest.writeIntArray(ops); - dest.writeStringArray(perms); - dest.writeIntArray(permOps); } public static final Creator CREATOR = new Creator() { @@ -94,34 +93,27 @@ public class AppOpsState { public static final OpsTemplate LOCATION_TEMPLATE = new OpsTemplate( new int[] { AppOpsManager.OP_COARSE_LOCATION, AppOpsManager.OP_FINE_LOCATION, - AppOpsManager.OP_GPS }, - new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, - android.Manifest.permission.ACCESS_FINE_LOCATION }, - new int[] { AppOpsManager.OP_COARSE_LOCATION, - AppOpsManager.OP_FINE_LOCATION } + AppOpsManager.OP_GPS, + AppOpsManager.OP_WIFI_SCAN } ); public static final OpsTemplate PERSONAL_TEMPLATE = new OpsTemplate( new int[] { AppOpsManager.OP_READ_CONTACTS, AppOpsManager.OP_WRITE_CONTACTS, AppOpsManager.OP_READ_CALL_LOG, - AppOpsManager.OP_WRITE_CALL_LOG }, - new String[] { android.Manifest.permission.READ_CONTACTS, - android.Manifest.permission.WRITE_CONTACTS, - android.Manifest.permission.READ_CALL_LOG, - android.Manifest.permission.WRITE_CALL_LOG }, - new int[] { AppOpsManager.OP_READ_CONTACTS, - AppOpsManager.OP_WRITE_CONTACTS, - AppOpsManager.OP_READ_CALL_LOG, - AppOpsManager.OP_WRITE_CALL_LOG } + AppOpsManager.OP_WRITE_CALL_LOG, + AppOpsManager.OP_READ_CALENDAR, + AppOpsManager.OP_WRITE_CALENDAR } ); public static final OpsTemplate DEVICE_TEMPLATE = new OpsTemplate( - new int[] { AppOpsManager.OP_VIBRATE }, - new String[] { android.Manifest.permission.VIBRATE }, new int[] { AppOpsManager.OP_VIBRATE } ); + public static final OpsTemplate[] ALL_TEMPLATES = new OpsTemplate[] { + LOCATION_TEMPLATE, PERSONAL_TEMPLATE, DEVICE_TEMPLATE + }; + /** * This class holds the per-item data in our Loader. */ @@ -129,6 +121,8 @@ public class AppOpsState { private final AppOpsState mState; private final ApplicationInfo mInfo; private final File mApkFile; + private final SparseArray mOps + = new SparseArray(); private String mLabel; private Drawable mIcon; private boolean mMounted; @@ -139,6 +133,14 @@ public class AppOpsState { mApkFile = new File(info.sourceDir); } + public void addOp(AppOpsManager.OpEntry op) { + mOps.put(op.getOp(), op); + } + + public boolean hasOp(int op) { + return mOps.indexOfKey(op) >= 0; + } + public ApplicationInfo getApplicationInfo() { return mInfo; } @@ -200,11 +202,13 @@ public class AppOpsState { public AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app) { mPkgOps = pkg; - mOps.add(op); mApp = app; + mApp.addOp(op); + mOps.add(op); } public void addOp(AppOpsManager.OpEntry op) { + mApp.addOp(op); for (int i=0; i op.getTime()) { + if (pos.getTime() < op.getTime()) { mOps.add(i, op); return; } @@ -237,16 +242,16 @@ public class AppOpsState { return mOps.get(pos); } - public CharSequence getLabelText(CharSequence opNames[]) { + public CharSequence getLabelText(AppOpsState state) { if (getNumOpEntry() == 1) { - return opNames[getOpEntry(0).getOp()]; + return state.mOpNames[getOpEntry(0).getOp()]; } else { StringBuilder builder = new StringBuilder(); for (int i=0; i 0) { builder.append(", "); } - builder.append(opNames[getOpEntry(i).getOp()]); + builder.append(state.mOpNames[getOpEntry(i).getOp()]); } return builder.toString(); } @@ -306,12 +311,16 @@ public class AppOpsState { boolean lastExe = last.getTime() != 0; boolean entryExe = opEntry.getTime() != 0; if (lastExe == entryExe) { + if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package " + + pkgOps.getPackageName() + ": append to " + last); last.addOp(opEntry); return; } } } AppOpEntry entry = new AppOpEntry(pkgOps, opEntry, appEntry); + if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package " + + pkgOps.getPackageName() + ": making new " + entry); entries.add(entry); } @@ -319,10 +328,42 @@ public class AppOpsState { return buildState(tpl, 0, null); } + private AppEntry getAppEntry(final Context context, final HashMap appEntries, + final String packageName, ApplicationInfo appInfo) { + AppEntry appEntry = appEntries.get(packageName); + if (appEntry == null) { + if (appInfo == null) { + try { + appInfo = mPm.getApplicationInfo(packageName, + PackageManager.GET_DISABLED_COMPONENTS + | PackageManager.GET_UNINSTALLED_PACKAGES); + } catch (PackageManager.NameNotFoundException e) { + Log.w(TAG, "Unable to find info for package " + packageName); + return null; + } + } + appEntry = new AppEntry(this, appInfo); + appEntry.loadLabel(context); + appEntries.put(packageName, appEntry); + } + return appEntry; + } + public List buildState(OpsTemplate tpl, int uid, String packageName) { final Context context = mContext; final HashMap appEntries = new HashMap(); + List entries = new ArrayList(); + + ArrayList perms = new ArrayList(); + ArrayList permOps = new ArrayList(); + for (int i=0; i pkgs; if (packageName != null) { @@ -330,68 +371,74 @@ public class AppOpsState { } else { pkgs = mAppOps.getPackagesForOps(tpl.ops); } - List entries = new ArrayList(pkgs.size()); - for (int i=0; i apps; - if (packageName != null) { - apps = new ArrayList(); - try { - PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); - apps.add(pi); - } catch (NameNotFoundException e) { - } - } else { - apps = mPm.getPackagesHoldingPermissions(tpl.perms, 0); + List apps; + if (packageName != null) { + apps = new ArrayList(); + try { + PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); + apps.add(pi); + } catch (NameNotFoundException e) { } - for (int i=0; i dummyOps - = new ArrayList(); - AppOpsManager.PackageOps pkgOps = new AppOpsManager.PackageOps( - appInfo.packageName, appInfo.applicationInfo.uid, dummyOps); - for (int j=0; j dummyOps = null; + AppOpsManager.PackageOps pkgOps = null; + for (int j=0; j(); + pkgOps = new AppOpsManager.PackageOps( + appInfo.packageName, appInfo.applicationInfo.uid, dummyOps); + + } + AppOpsManager.OpEntry opEntry = new AppOpsManager.OpEntry( + permOps.get(k), AppOpsManager.MODE_ALLOWED, 0, 0, 0); + dummyOps.add(opEntry); + addOp(entries, pkgOps, appEntry, opEntry); + } } } @@ -401,4 +448,22 @@ public class AppOpsState { // Done! return entries; } + + public CharSequence getLabelText(AppOpsManager.OpEntry op) { + return mOpNames[op.getOp()]; + } + + public CharSequence getTimeText(AppOpsManager.OpEntry op) { + if (op.isRunning()) { + return mContext.getResources().getText(R.string.app_ops_running); + } + if (op.getTime() > 0) { + return DateUtils.getRelativeTimeSpanString(op.getTime(), + System.currentTimeMillis(), + DateUtils.MINUTE_IN_MILLIS, + DateUtils.FORMAT_ABBREV_RELATIVE); + } + return mContext.getResources().getText(R.string.app_ops_never_used); + } + } diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java index 565c5851f47..f3723f85113 100644 --- a/src/com/android/settings/applications/InstalledAppDetails.java +++ b/src/com/android/settings/applications/InstalledAppDetails.java @@ -537,7 +537,7 @@ public class InstalledAppDetails extends Fragment } } - // Utility method to set applicaiton label and icon. + // Utility method to set application label and icon. private void setAppLabelAndIcon(PackageInfo pkgInfo) { final View appSnippet = mRootView.findViewById(R.id.app_snippet); appSnippet.setPaddingRelative(0, appSnippet.getPaddingTop(), 0, appSnippet.getPaddingBottom());