App ops: add UI for viewing and controlling ops per-app.
Change-Id: Iadd68cbd429af4d431dcd09b9adacd09c5092ae6
This commit is contained in:
@@ -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<AppOpsState.AppOpEntry> entries = mState.buildState(tpl,
|
||||
mPackageInfo.applicationInfo.uid, mPackageInfo.packageName);
|
||||
for (final AppOpsState.AppOpEntry entry : entries) {
|
||||
for (int i=0; i<entry.getNumOpEntry(); i++) {
|
||||
final AppOpsManager.OpEntry op = entry.getOpEntry(i);
|
||||
final View view = mInflater.inflate(R.layout.app_ops_details_item,
|
||||
mOperationsSection, false);
|
||||
mOperationsSection.addView(view);
|
||||
String perm = AppOpsManager.opToPermission(op.getOp());
|
||||
try {
|
||||
PermissionInfo pi = mPm.getPermissionInfo(perm, 0);
|
||||
if (pi.group != null && !lastPermGroup.equals(pi.group)) {
|
||||
lastPermGroup = pi.group;
|
||||
PermissionGroupInfo pgi = mPm.getPermissionGroupInfo(pi.group, 0);
|
||||
if (pgi.icon != 0) {
|
||||
((ImageView)view.findViewById(R.id.op_icon)).setImageDrawable(
|
||||
pgi.loadIcon(mPm));
|
||||
}
|
||||
}
|
||||
} catch (NameNotFoundException e) {
|
||||
}
|
||||
((TextView)view.findViewById(R.id.op_name)).setText(mState.getLabelText(op));
|
||||
((TextView)view.findViewById(R.id.op_time)).setText(mState.getTimeText(op));
|
||||
Switch sw = (Switch)view.findViewById(R.id.switchWidget);
|
||||
sw.setChecked(op.getMode() == AppOpsManager.MODE_ALLOWED);
|
||||
sw.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mAppOps.setMode(op.getOp(), entry.getPackageOps().getUid(),
|
||||
entry.getPackageOps().getPackageName(), isChecked
|
||||
? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setIntentAndFinish(boolean finish, boolean appChanged) {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(ManageApplications.APP_CHG, appChanged);
|
||||
PreferenceActivity pa = (PreferenceActivity)getActivity();
|
||||
pa.finishPreferencePanel(this, Activity.RESULT_OK, intent);
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
mState = new AppOpsState(getActivity());
|
||||
mPm = getActivity().getPackageManager();
|
||||
mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mAppOps = (AppOpsManager)getActivity().getSystemService(Context.APP_OPS_SERVICE);
|
||||
|
||||
retrieveAppEntry();
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(
|
||||
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
final View view = inflater.inflate(R.layout.app_ops_details, container, false);
|
||||
Utils.prepareCustomPreferencesList(container, view, view, false);
|
||||
|
||||
mRootView = view;
|
||||
mOperationsSection = (LinearLayout)view.findViewById(R.id.operations_section);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (!refreshUi()) {
|
||||
setIntentAndFinish(true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user