Move app ops classes to their own package
Bug: 68426851 Test: robotests Change-Id: I076b7adf59d4bf66792eb0aebaa3dc305d6d0c4b
This commit is contained in:
372
src/com/android/settings/applications/appops/AppOpsCategory.java
Normal file
372
src/com/android/settings/applications/appops/AppOpsCategory.java
Normal file
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.applications.appops;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.app.ListFragment;
|
||||
import android.app.LoaderManager;
|
||||
import android.content.AsyncTaskLoader;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.Loader;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.appops.AppOpsState.AppOpEntry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AppOpsCategory extends ListFragment implements
|
||||
LoaderManager.LoaderCallbacks<List<AppOpEntry>> {
|
||||
|
||||
AppOpsState mState;
|
||||
|
||||
// This is the Adapter being used to display the list's data.
|
||||
AppListAdapter mAdapter;
|
||||
|
||||
public AppOpsCategory() {
|
||||
}
|
||||
|
||||
public AppOpsCategory(AppOpsState.OpsTemplate template) {
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable("template", template);
|
||||
setArguments(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for determining if the configuration has changed in an interesting
|
||||
* way so we need to rebuild the app list.
|
||||
*/
|
||||
public static class InterestingConfigChanges {
|
||||
final Configuration mLastConfiguration = new Configuration();
|
||||
int mLastDensity;
|
||||
|
||||
boolean applyNewConfig(Resources res) {
|
||||
int configChanges = mLastConfiguration.updateFrom(res.getConfiguration());
|
||||
boolean densityChanged = mLastDensity != res.getDisplayMetrics().densityDpi;
|
||||
if (densityChanged || (configChanges&(ActivityInfo.CONFIG_LOCALE
|
||||
|ActivityInfo.CONFIG_UI_MODE|ActivityInfo.CONFIG_SCREEN_LAYOUT)) != 0) {
|
||||
mLastDensity = res.getDisplayMetrics().densityDpi;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class to look for interesting changes to the installed apps
|
||||
* so that the loader can be updated.
|
||||
*/
|
||||
public static class PackageIntentReceiver extends BroadcastReceiver {
|
||||
final AppListLoader mLoader;
|
||||
|
||||
public PackageIntentReceiver(AppListLoader loader) {
|
||||
mLoader = loader;
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
|
||||
filter.addDataScheme("package");
|
||||
mLoader.getContext().registerReceiver(this, filter);
|
||||
// Register for events related to sdcard installation.
|
||||
IntentFilter sdFilter = new IntentFilter();
|
||||
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
|
||||
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
|
||||
mLoader.getContext().registerReceiver(this, sdFilter);
|
||||
}
|
||||
|
||||
@Override public void onReceive(Context context, Intent intent) {
|
||||
// Tell the loader about the change.
|
||||
mLoader.onContentChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A custom Loader that loads all of the installed applications.
|
||||
*/
|
||||
public static class AppListLoader extends AsyncTaskLoader<List<AppOpEntry>> {
|
||||
final InterestingConfigChanges mLastConfig = new InterestingConfigChanges();
|
||||
final AppOpsState mState;
|
||||
final AppOpsState.OpsTemplate mTemplate;
|
||||
|
||||
List<AppOpEntry> mApps;
|
||||
PackageIntentReceiver mPackageObserver;
|
||||
|
||||
public AppListLoader(Context context, AppOpsState state, AppOpsState.OpsTemplate template) {
|
||||
super(context);
|
||||
mState = state;
|
||||
mTemplate = template;
|
||||
}
|
||||
|
||||
@Override public List<AppOpEntry> loadInBackground() {
|
||||
return mState.buildState(mTemplate, 0, null, AppOpsState.LABEL_COMPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when there is new data to deliver to the client. The
|
||||
* super class will take care of delivering it; the implementation
|
||||
* here just adds a little more logic.
|
||||
*/
|
||||
@Override public void deliverResult(List<AppOpEntry> apps) {
|
||||
if (isReset()) {
|
||||
// An async query came in while the loader is stopped. We
|
||||
// don't need the result.
|
||||
if (apps != null) {
|
||||
onReleaseResources(apps);
|
||||
}
|
||||
}
|
||||
List<AppOpEntry> oldApps = apps;
|
||||
mApps = apps;
|
||||
|
||||
if (isStarted()) {
|
||||
// If the Loader is currently started, we can immediately
|
||||
// deliver its results.
|
||||
super.deliverResult(apps);
|
||||
}
|
||||
|
||||
// At this point we can release the resources associated with
|
||||
// 'oldApps' if needed; now that the new result is delivered we
|
||||
// know that it is no longer in use.
|
||||
if (oldApps != null) {
|
||||
onReleaseResources(oldApps);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request to start the Loader.
|
||||
*/
|
||||
@Override protected void onStartLoading() {
|
||||
// We don't monitor changed when loading is stopped, so need
|
||||
// to always reload at this point.
|
||||
onContentChanged();
|
||||
|
||||
if (mApps != null) {
|
||||
// If we currently have a result available, deliver it
|
||||
// immediately.
|
||||
deliverResult(mApps);
|
||||
}
|
||||
|
||||
// Start watching for changes in the app data.
|
||||
if (mPackageObserver == null) {
|
||||
mPackageObserver = new PackageIntentReceiver(this);
|
||||
}
|
||||
|
||||
// Has something interesting in the configuration changed since we
|
||||
// last built the app list?
|
||||
boolean configChange = mLastConfig.applyNewConfig(getContext().getResources());
|
||||
|
||||
if (takeContentChanged() || mApps == null || configChange) {
|
||||
// If the data has changed since the last time it was loaded
|
||||
// or is not currently available, start a load.
|
||||
forceLoad();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request to stop the Loader.
|
||||
*/
|
||||
@Override protected void onStopLoading() {
|
||||
// Attempt to cancel the current load task if possible.
|
||||
cancelLoad();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request to cancel a load.
|
||||
*/
|
||||
@Override public void onCanceled(List<AppOpEntry> apps) {
|
||||
super.onCanceled(apps);
|
||||
|
||||
// At this point we can release the resources associated with 'apps'
|
||||
// if needed.
|
||||
onReleaseResources(apps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request to completely reset the Loader.
|
||||
*/
|
||||
@Override protected void onReset() {
|
||||
super.onReset();
|
||||
|
||||
// Ensure the loader is stopped
|
||||
onStopLoading();
|
||||
|
||||
// At this point we can release the resources associated with 'apps'
|
||||
// if needed.
|
||||
if (mApps != null) {
|
||||
onReleaseResources(mApps);
|
||||
mApps = null;
|
||||
}
|
||||
|
||||
// Stop monitoring for changes.
|
||||
if (mPackageObserver != null) {
|
||||
getContext().unregisterReceiver(mPackageObserver);
|
||||
mPackageObserver = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to take care of releasing resources associated
|
||||
* with an actively loaded data set.
|
||||
*/
|
||||
protected void onReleaseResources(List<AppOpEntry> apps) {
|
||||
// For a simple List<> there is nothing to do. For something
|
||||
// like a Cursor, we would close it here.
|
||||
}
|
||||
}
|
||||
|
||||
public static class AppListAdapter extends BaseAdapter {
|
||||
private final Resources mResources;
|
||||
private final LayoutInflater mInflater;
|
||||
private final AppOpsState mState;
|
||||
|
||||
List<AppOpEntry> mList;
|
||||
|
||||
public AppListAdapter(Context context, AppOpsState state) {
|
||||
mResources = context.getResources();
|
||||
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mState = state;
|
||||
}
|
||||
|
||||
public void setData(List<AppOpEntry> data) {
|
||||
mList = data;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mList != null ? mList.size() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppOpEntry getItem(int position) {
|
||||
return mList.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate new items in the list.
|
||||
*/
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
View view;
|
||||
|
||||
if (convertView == null) {
|
||||
view = mInflater.inflate(R.layout.app_ops_item, parent, false);
|
||||
} else {
|
||||
view = convertView;
|
||||
}
|
||||
|
||||
AppOpEntry item = getItem(position);
|
||||
((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.getTimeText(mResources, false));
|
||||
view.findViewById(R.id.op_time).setVisibility(View.GONE);
|
||||
((Switch) view.findViewById(R.id.op_switch)).setChecked(
|
||||
item.getPrimaryOpMode() == AppOpsManager.MODE_ALLOWED);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mState = new AppOpsState(getActivity());
|
||||
}
|
||||
|
||||
@Override public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
// Give some text to display if there is no data. In a real
|
||||
// application this would come from a resource.
|
||||
setEmptyText("No applications");
|
||||
|
||||
// We have a menu item to show in action bar.
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
// Create an empty adapter we will use to display the loaded data.
|
||||
mAdapter = new AppListAdapter(getActivity(), mState);
|
||||
setListAdapter(mAdapter);
|
||||
|
||||
// Start out with a progress indicator.
|
||||
setListShown(false);
|
||||
|
||||
// Prepare the loader.
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
@Override public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
AppOpEntry entry = mAdapter.getItem(position);
|
||||
if (entry != null) {
|
||||
// We treat this as tapping on the check box, toggling the app op state.
|
||||
Switch sw = v.findViewById(R.id.op_switch);
|
||||
boolean checked = !sw.isChecked();
|
||||
sw.setChecked(checked);
|
||||
AppOpsManager.OpEntry op = entry.getOpEntry(0);
|
||||
int mode = checked ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED;
|
||||
mState.getAppOpsManager().setMode(op.getOp(),
|
||||
entry.getAppEntry().getApplicationInfo().uid,
|
||||
entry.getAppEntry().getApplicationInfo().packageName,
|
||||
mode);
|
||||
entry.overridePrimaryOpMode(mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public Loader<List<AppOpEntry>> onCreateLoader(int id, Bundle args) {
|
||||
Bundle fargs = getArguments();
|
||||
AppOpsState.OpsTemplate template = null;
|
||||
if (fargs != null) {
|
||||
template = fargs.getParcelable("template");
|
||||
}
|
||||
return new AppListLoader(getActivity(), mState, template);
|
||||
}
|
||||
|
||||
@Override public void onLoadFinished(Loader<List<AppOpEntry>> loader, List<AppOpEntry> data) {
|
||||
// Set the new data in the adapter.
|
||||
mAdapter.setData(data);
|
||||
|
||||
// The list should now be shown.
|
||||
if (isResumed()) {
|
||||
setListShown(true);
|
||||
} else {
|
||||
setListShownNoAnimation(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onLoaderReset(Loader<List<AppOpEntry>> loader) {
|
||||
// Clear the data in the adapter.
|
||||
mAdapter.setData(null);
|
||||
}
|
||||
}
|
||||
636
src/com/android/settings/applications/appops/AppOpsState.java
Normal file
636
src/com/android/settings/applications/appops/AppOpsState.java
Normal file
@@ -0,0 +1,636 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.applications.appops;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class AppOpsState {
|
||||
static final String TAG = "AppOpsState";
|
||||
static final boolean DEBUG = false;
|
||||
|
||||
final Context mContext;
|
||||
final AppOpsManager mAppOps;
|
||||
final PackageManager mPm;
|
||||
final CharSequence[] mOpSummaries;
|
||||
final CharSequence[] mOpLabels;
|
||||
|
||||
public AppOpsState(Context context) {
|
||||
mContext = context;
|
||||
mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
|
||||
mPm = context.getPackageManager();
|
||||
mOpSummaries = context.getResources().getTextArray(R.array.app_ops_summaries);
|
||||
mOpLabels = context.getResources().getTextArray(R.array.app_ops_labels);
|
||||
}
|
||||
|
||||
public static class OpsTemplate implements Parcelable {
|
||||
public final int[] ops;
|
||||
public final boolean[] showPerms;
|
||||
|
||||
public OpsTemplate(int[] _ops, boolean[] _showPerms) {
|
||||
ops = _ops;
|
||||
showPerms = _showPerms;
|
||||
}
|
||||
|
||||
OpsTemplate(Parcel src) {
|
||||
ops = src.createIntArray();
|
||||
showPerms = src.createBooleanArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeIntArray(ops);
|
||||
dest.writeBooleanArray(showPerms);
|
||||
}
|
||||
|
||||
public static final Creator<OpsTemplate> CREATOR = new Creator<OpsTemplate>() {
|
||||
@Override public OpsTemplate createFromParcel(Parcel source) {
|
||||
return new OpsTemplate(source);
|
||||
}
|
||||
|
||||
@Override public OpsTemplate[] newArray(int size) {
|
||||
return new OpsTemplate[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static final OpsTemplate LOCATION_TEMPLATE = new OpsTemplate(
|
||||
new int[] { AppOpsManager.OP_COARSE_LOCATION,
|
||||
AppOpsManager.OP_FINE_LOCATION,
|
||||
AppOpsManager.OP_GPS,
|
||||
AppOpsManager.OP_WIFI_SCAN,
|
||||
AppOpsManager.OP_NEIGHBORING_CELLS,
|
||||
AppOpsManager.OP_MONITOR_LOCATION,
|
||||
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION },
|
||||
new boolean[] { true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false }
|
||||
);
|
||||
|
||||
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,
|
||||
AppOpsManager.OP_READ_CALENDAR,
|
||||
AppOpsManager.OP_WRITE_CALENDAR,
|
||||
AppOpsManager.OP_READ_CLIPBOARD,
|
||||
AppOpsManager.OP_WRITE_CLIPBOARD },
|
||||
new boolean[] { true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false }
|
||||
);
|
||||
|
||||
public static final OpsTemplate MESSAGING_TEMPLATE = new OpsTemplate(
|
||||
new int[] { AppOpsManager.OP_READ_SMS,
|
||||
AppOpsManager.OP_RECEIVE_SMS,
|
||||
AppOpsManager.OP_RECEIVE_EMERGECY_SMS,
|
||||
AppOpsManager.OP_RECEIVE_MMS,
|
||||
AppOpsManager.OP_RECEIVE_WAP_PUSH,
|
||||
AppOpsManager.OP_WRITE_SMS,
|
||||
AppOpsManager.OP_SEND_SMS,
|
||||
AppOpsManager.OP_READ_ICC_SMS,
|
||||
AppOpsManager.OP_WRITE_ICC_SMS },
|
||||
new boolean[] { true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true }
|
||||
);
|
||||
|
||||
public static final OpsTemplate MEDIA_TEMPLATE = new OpsTemplate(
|
||||
new int[] { AppOpsManager.OP_VIBRATE,
|
||||
AppOpsManager.OP_CAMERA,
|
||||
AppOpsManager.OP_RECORD_AUDIO,
|
||||
AppOpsManager.OP_PLAY_AUDIO,
|
||||
AppOpsManager.OP_TAKE_MEDIA_BUTTONS,
|
||||
AppOpsManager.OP_TAKE_AUDIO_FOCUS,
|
||||
AppOpsManager.OP_AUDIO_MASTER_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_VOICE_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_RING_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_MEDIA_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_ALARM_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME,
|
||||
AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME,
|
||||
AppOpsManager.OP_MUTE_MICROPHONE},
|
||||
new boolean[] { false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false }
|
||||
);
|
||||
|
||||
public static final OpsTemplate DEVICE_TEMPLATE = new OpsTemplate(
|
||||
new int[] { AppOpsManager.OP_POST_NOTIFICATION,
|
||||
AppOpsManager.OP_ACCESS_NOTIFICATIONS,
|
||||
AppOpsManager.OP_CALL_PHONE,
|
||||
AppOpsManager.OP_WRITE_SETTINGS,
|
||||
AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
|
||||
AppOpsManager.OP_WAKE_LOCK,
|
||||
AppOpsManager.OP_PROJECT_MEDIA,
|
||||
AppOpsManager.OP_ACTIVATE_VPN,
|
||||
AppOpsManager.OP_ASSIST_STRUCTURE,
|
||||
AppOpsManager.OP_ASSIST_SCREENSHOT},
|
||||
new boolean[] { false,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false }
|
||||
);
|
||||
|
||||
public static final OpsTemplate RUN_IN_BACKGROUND_TEMPLATE = new OpsTemplate(
|
||||
new int[] { AppOpsManager.OP_RUN_IN_BACKGROUND },
|
||||
new boolean[] { false }
|
||||
);
|
||||
|
||||
public static final OpsTemplate[] ALL_TEMPLATES = new OpsTemplate[] {
|
||||
LOCATION_TEMPLATE, PERSONAL_TEMPLATE, MESSAGING_TEMPLATE,
|
||||
MEDIA_TEMPLATE, DEVICE_TEMPLATE, RUN_IN_BACKGROUND_TEMPLATE
|
||||
};
|
||||
|
||||
/**
|
||||
* This class holds the per-item data in our Loader.
|
||||
*/
|
||||
public static class AppEntry {
|
||||
private final AppOpsState mState;
|
||||
private final ApplicationInfo mInfo;
|
||||
private final File mApkFile;
|
||||
private final SparseArray<AppOpsManager.OpEntry> mOps
|
||||
= new SparseArray<AppOpsManager.OpEntry>();
|
||||
private final SparseArray<AppOpEntry> mOpSwitches
|
||||
= new SparseArray<AppOpEntry>();
|
||||
private String mLabel;
|
||||
private Drawable mIcon;
|
||||
private boolean mMounted;
|
||||
|
||||
public AppEntry(AppOpsState state, ApplicationInfo info) {
|
||||
mState = state;
|
||||
mInfo = info;
|
||||
mApkFile = new File(info.sourceDir);
|
||||
}
|
||||
|
||||
public void addOp(AppOpEntry entry, AppOpsManager.OpEntry op) {
|
||||
mOps.put(op.getOp(), op);
|
||||
mOpSwitches.put(AppOpsManager.opToSwitch(op.getOp()), entry);
|
||||
}
|
||||
|
||||
public boolean hasOp(int op) {
|
||||
return mOps.indexOfKey(op) >= 0;
|
||||
}
|
||||
|
||||
public AppOpEntry getOpSwitch(int op) {
|
||||
return mOpSwitches.get(AppOpsManager.opToSwitch(op));
|
||||
}
|
||||
|
||||
public ApplicationInfo getApplicationInfo() {
|
||||
return mInfo;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return mLabel;
|
||||
}
|
||||
|
||||
public Drawable getIcon() {
|
||||
if (mIcon == null) {
|
||||
if (mApkFile.exists()) {
|
||||
mIcon = mInfo.loadIcon(mState.mPm);
|
||||
return mIcon;
|
||||
} else {
|
||||
mMounted = false;
|
||||
}
|
||||
} else if (!mMounted) {
|
||||
// If the app wasn't mounted but is now mounted, reload
|
||||
// its icon.
|
||||
if (mApkFile.exists()) {
|
||||
mMounted = true;
|
||||
mIcon = mInfo.loadIcon(mState.mPm);
|
||||
return mIcon;
|
||||
}
|
||||
} else {
|
||||
return mIcon;
|
||||
}
|
||||
|
||||
return mState.mContext.getDrawable(
|
||||
android.R.drawable.sym_def_app_icon);
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return mLabel;
|
||||
}
|
||||
|
||||
void loadLabel(Context context) {
|
||||
if (mLabel == null || !mMounted) {
|
||||
if (!mApkFile.exists()) {
|
||||
mMounted = false;
|
||||
mLabel = mInfo.packageName;
|
||||
} else {
|
||||
mMounted = true;
|
||||
CharSequence label = mInfo.loadLabel(context.getPackageManager());
|
||||
mLabel = label != null ? label.toString() : mInfo.packageName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class holds the per-item data in our Loader.
|
||||
*/
|
||||
public static class AppOpEntry {
|
||||
private final AppOpsManager.PackageOps mPkgOps;
|
||||
private final ArrayList<AppOpsManager.OpEntry> mOps
|
||||
= new ArrayList<AppOpsManager.OpEntry>();
|
||||
private final ArrayList<AppOpsManager.OpEntry> mSwitchOps
|
||||
= new ArrayList<AppOpsManager.OpEntry>();
|
||||
private final AppEntry mApp;
|
||||
private final int mSwitchOrder;
|
||||
private int mOverriddenPrimaryMode = -1;
|
||||
|
||||
public AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app,
|
||||
int switchOrder) {
|
||||
mPkgOps = pkg;
|
||||
mApp = app;
|
||||
mSwitchOrder = switchOrder;
|
||||
mApp.addOp(this, op);
|
||||
mOps.add(op);
|
||||
mSwitchOps.add(op);
|
||||
}
|
||||
|
||||
private static void addOp(ArrayList<AppOpsManager.OpEntry> list, AppOpsManager.OpEntry op) {
|
||||
for (int i=0; i<list.size(); i++) {
|
||||
AppOpsManager.OpEntry pos = list.get(i);
|
||||
if (pos.isRunning() != op.isRunning()) {
|
||||
if (op.isRunning()) {
|
||||
list.add(i, op);
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (pos.getTime() < op.getTime()) {
|
||||
list.add(i, op);
|
||||
return;
|
||||
}
|
||||
}
|
||||
list.add(op);
|
||||
}
|
||||
|
||||
public void addOp(AppOpsManager.OpEntry op) {
|
||||
mApp.addOp(this, op);
|
||||
addOp(mOps, op);
|
||||
if (mApp.getOpSwitch(AppOpsManager.opToSwitch(op.getOp())) == null) {
|
||||
addOp(mSwitchOps, op);
|
||||
}
|
||||
}
|
||||
|
||||
public AppEntry getAppEntry() {
|
||||
return mApp;
|
||||
}
|
||||
|
||||
public int getSwitchOrder() {
|
||||
return mSwitchOrder;
|
||||
}
|
||||
|
||||
public AppOpsManager.PackageOps getPackageOps() {
|
||||
return mPkgOps;
|
||||
}
|
||||
|
||||
public int getNumOpEntry() {
|
||||
return mOps.size();
|
||||
}
|
||||
|
||||
public AppOpsManager.OpEntry getOpEntry(int pos) {
|
||||
return mOps.get(pos);
|
||||
}
|
||||
|
||||
public int getPrimaryOpMode() {
|
||||
return mOverriddenPrimaryMode >= 0 ? mOverriddenPrimaryMode : mOps.get(0).getMode();
|
||||
}
|
||||
|
||||
public void overridePrimaryOpMode(int mode) {
|
||||
mOverriddenPrimaryMode = mode;
|
||||
}
|
||||
|
||||
private CharSequence getCombinedText(ArrayList<AppOpsManager.OpEntry> ops,
|
||||
CharSequence[] items) {
|
||||
if (ops.size() == 1) {
|
||||
return items[ops.get(0).getOp()];
|
||||
} else {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i=0; i<ops.size(); i++) {
|
||||
if (i > 0) {
|
||||
builder.append(", ");
|
||||
}
|
||||
builder.append(items[ops.get(i).getOp()]);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public CharSequence getSummaryText(AppOpsState state) {
|
||||
return getCombinedText(mOps, state.mOpSummaries);
|
||||
}
|
||||
|
||||
public CharSequence getSwitchText(AppOpsState state) {
|
||||
if (mSwitchOps.size() > 0) {
|
||||
return getCombinedText(mSwitchOps, state.mOpLabels);
|
||||
} else {
|
||||
return getCombinedText(mOps, state.mOpLabels);
|
||||
}
|
||||
}
|
||||
|
||||
public CharSequence getTimeText(Resources res, boolean showEmptyText) {
|
||||
if (isRunning()) {
|
||||
return res.getText(R.string.app_ops_running);
|
||||
}
|
||||
if (getTime() > 0) {
|
||||
return DateUtils.getRelativeTimeSpanString(getTime(),
|
||||
System.currentTimeMillis(),
|
||||
DateUtils.MINUTE_IN_MILLIS,
|
||||
DateUtils.FORMAT_ABBREV_RELATIVE);
|
||||
}
|
||||
return showEmptyText ? res.getText(R.string.app_ops_never_used) : "";
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return mOps.get(0).isRunning();
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return mOps.get(0).getTime();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return mApp.getLabel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform app op state comparison of application entry objects.
|
||||
*/
|
||||
public static final Comparator<AppOpEntry> RECENCY_COMPARATOR = new Comparator<AppOpEntry>() {
|
||||
private final Collator sCollator = Collator.getInstance();
|
||||
@Override
|
||||
public int compare(AppOpEntry object1, AppOpEntry object2) {
|
||||
if (object1.getSwitchOrder() != object2.getSwitchOrder()) {
|
||||
return object1.getSwitchOrder() < object2.getSwitchOrder() ? -1 : 1;
|
||||
}
|
||||
if (object1.isRunning() != object2.isRunning()) {
|
||||
// Currently running ops go first.
|
||||
return object1.isRunning() ? -1 : 1;
|
||||
}
|
||||
if (object1.getTime() != object2.getTime()) {
|
||||
// More recent times go first.
|
||||
return object1.getTime() > object2.getTime() ? -1 : 1;
|
||||
}
|
||||
return sCollator.compare(object1.getAppEntry().getLabel(),
|
||||
object2.getAppEntry().getLabel());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform alphabetical comparison of application entry objects.
|
||||
*/
|
||||
public static final Comparator<AppOpEntry> LABEL_COMPARATOR = new Comparator<AppOpEntry>() {
|
||||
private final Collator sCollator = Collator.getInstance();
|
||||
@Override
|
||||
public int compare(AppOpEntry object1, AppOpEntry object2) {
|
||||
return sCollator.compare(object1.getAppEntry().getLabel(),
|
||||
object2.getAppEntry().getLabel());
|
||||
}
|
||||
};
|
||||
|
||||
private void addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps,
|
||||
AppEntry appEntry, AppOpsManager.OpEntry opEntry, boolean allowMerge, int switchOrder) {
|
||||
if (allowMerge && entries.size() > 0) {
|
||||
AppOpEntry last = entries.get(entries.size()-1);
|
||||
if (last.getAppEntry() == appEntry) {
|
||||
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 = appEntry.getOpSwitch(opEntry.getOp());
|
||||
if (entry != null) {
|
||||
entry.addOp(opEntry);
|
||||
return;
|
||||
}
|
||||
entry = new AppOpEntry(pkgOps, opEntry, appEntry, switchOrder);
|
||||
if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package "
|
||||
+ pkgOps.getPackageName() + ": making new " + entry);
|
||||
entries.add(entry);
|
||||
}
|
||||
|
||||
public AppOpsManager getAppOpsManager() {
|
||||
return mAppOps;
|
||||
}
|
||||
|
||||
public List<AppOpEntry> buildState(OpsTemplate tpl) {
|
||||
return buildState(tpl, 0, null, RECENCY_COMPARATOR);
|
||||
}
|
||||
|
||||
private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries,
|
||||
final String packageName, ApplicationInfo appInfo) {
|
||||
AppEntry appEntry = appEntries.get(packageName);
|
||||
if (appEntry == null) {
|
||||
if (appInfo == null) {
|
||||
try {
|
||||
appInfo = mPm.getApplicationInfo(packageName,
|
||||
PackageManager.MATCH_DISABLED_COMPONENTS
|
||||
| PackageManager.MATCH_ANY_USER);
|
||||
} 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<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName) {
|
||||
return buildState(tpl, uid, packageName, RECENCY_COMPARATOR);
|
||||
}
|
||||
|
||||
public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName,
|
||||
Comparator<AppOpEntry> comparator) {
|
||||
final Context context = mContext;
|
||||
|
||||
final HashMap<String, AppEntry> appEntries = new HashMap<String, AppEntry>();
|
||||
final List<AppOpEntry> entries = new ArrayList<AppOpEntry>();
|
||||
|
||||
final ArrayList<String> perms = new ArrayList<String>();
|
||||
final ArrayList<Integer> permOps = new ArrayList<Integer>();
|
||||
final int[] opToOrder = new int[AppOpsManager._NUM_OP];
|
||||
for (int i=0; i<tpl.ops.length; i++) {
|
||||
if (tpl.showPerms[i]) {
|
||||
String perm = AppOpsManager.opToPermission(tpl.ops[i]);
|
||||
if (perm != null && !perms.contains(perm)) {
|
||||
perms.add(perm);
|
||||
permOps.add(tpl.ops[i]);
|
||||
opToOrder[tpl.ops[i]] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<AppOpsManager.PackageOps> pkgs;
|
||||
if (packageName != null) {
|
||||
pkgs = mAppOps.getOpsForPackage(uid, packageName, tpl.ops);
|
||||
} else {
|
||||
pkgs = mAppOps.getPackagesForOps(tpl.ops);
|
||||
}
|
||||
|
||||
if (pkgs != null) {
|
||||
for (int i=0; i<pkgs.size(); i++) {
|
||||
AppOpsManager.PackageOps pkgOps = pkgs.get(i);
|
||||
AppEntry appEntry = getAppEntry(context, appEntries, pkgOps.getPackageName(), null);
|
||||
if (appEntry == null) {
|
||||
continue;
|
||||
}
|
||||
for (int j=0; j<pkgOps.getOps().size(); j++) {
|
||||
AppOpsManager.OpEntry opEntry = pkgOps.getOps().get(j);
|
||||
addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
|
||||
packageName == null ? 0 : opToOrder[opEntry.getOp()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<PackageInfo> apps;
|
||||
if (packageName != null) {
|
||||
apps = new ArrayList<PackageInfo>();
|
||||
try {
|
||||
PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
|
||||
apps.add(pi);
|
||||
} catch (NameNotFoundException e) {
|
||||
}
|
||||
} else {
|
||||
String[] permsArray = new String[perms.size()];
|
||||
perms.toArray(permsArray);
|
||||
apps = mPm.getPackagesHoldingPermissions(permsArray, 0);
|
||||
}
|
||||
for (int i=0; i<apps.size(); i++) {
|
||||
PackageInfo appInfo = apps.get(i);
|
||||
AppEntry appEntry = getAppEntry(context, appEntries, appInfo.packageName,
|
||||
appInfo.applicationInfo);
|
||||
if (appEntry == null) {
|
||||
continue;
|
||||
}
|
||||
List<AppOpsManager.OpEntry> dummyOps = null;
|
||||
AppOpsManager.PackageOps pkgOps = null;
|
||||
if (appInfo.requestedPermissions != null) {
|
||||
for (int j=0; j<appInfo.requestedPermissions.length; j++) {
|
||||
if (appInfo.requestedPermissionsFlags != null) {
|
||||
if ((appInfo.requestedPermissionsFlags[j]
|
||||
& PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
|
||||
if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm "
|
||||
+ appInfo.requestedPermissions[j] + " not granted; skipping");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + ": requested perm "
|
||||
+ appInfo.requestedPermissions[j]);
|
||||
for (int k=0; k<perms.size(); k++) {
|
||||
if (!perms.get(k).equals(appInfo.requestedPermissions[j])) {
|
||||
continue;
|
||||
}
|
||||
if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm " + perms.get(k)
|
||||
+ " has op " + permOps.get(k) + ": " + appEntry.hasOp(permOps.get(k)));
|
||||
if (appEntry.hasOp(permOps.get(k))) {
|
||||
continue;
|
||||
}
|
||||
if (dummyOps == null) {
|
||||
dummyOps = new ArrayList<AppOpsManager.OpEntry>();
|
||||
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, -1, null);
|
||||
dummyOps.add(opEntry);
|
||||
addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
|
||||
packageName == null ? 0 : opToOrder[opEntry.getOp()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the list.
|
||||
Collections.sort(entries, comparator);
|
||||
|
||||
// Done!
|
||||
return entries;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.applications.appops;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceFrameLayout;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.R;
|
||||
|
||||
public class BackgroundCheckSummary extends InstrumentedPreferenceFragment {
|
||||
// layout inflater object used to inflate views
|
||||
private LayoutInflater mInflater;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.BACKGROUND_CHECK_SUMMARY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (usePreferenceScreenTitle()) {
|
||||
getActivity().setTitle(R.string.background_check_pref);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
// initialize the inflater
|
||||
mInflater = inflater;
|
||||
|
||||
View rootView = mInflater.inflate(R.layout.background_check_summary,
|
||||
container, false);
|
||||
|
||||
// We have to do this now because PreferenceFrameLayout looks at it
|
||||
// only when the view is added.
|
||||
if (container instanceof PreferenceFrameLayout) {
|
||||
((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
|
||||
}
|
||||
|
||||
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
|
||||
ft.add(R.id.appops_content, new AppOpsCategory(AppOpsState.RUN_IN_BACKGROUND_TEMPLATE),
|
||||
"appops");
|
||||
ft.commitAllowingStateLoss();
|
||||
|
||||
return rootView;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user