Implement development UI for background check.

Allows you to toggle off full background access for
whatever apps you want.

Change-Id: I8542ecac1449ccd65dc18af3c0ca1fc18443f89c
This commit is contained in:
Dianne Hackborn
2015-12-07 16:36:09 -08:00
parent 70b49b1b70
commit f467c0acac
11 changed files with 253 additions and 16 deletions

View File

@@ -1161,6 +1161,18 @@
</activity>
-->
<activity android:name="Settings$BackgroundCheckSummaryActivity"
android:label="@string/background_check_title"
android:taskAffinity=""
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.applications.BackgroundCheckSummary" />
</activity>
<activity android:name="Settings$LocationSettingsActivity"
android:label="@string/location_settings_title"
android:icon="@drawable/ic_settings_location"

View File

@@ -25,7 +25,7 @@
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:columnCount="3">
android:columnCount="4">
<ImageView
android:id="@+id/app_icon"
@@ -47,6 +47,14 @@
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAlignment="viewStart" />
<Switch
android:id="@+id/op_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:focusable="false"
android:clickable="false" />
<TextView
android:id="@+id/op_name"
android:layout_width="0dip"

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 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.
*/
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appops_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

View File

@@ -620,11 +620,25 @@
<item>monitor high power location</item>
<item>get usage stats</item>
<item>mute/unmute microphone</item>
<item>show toast</item>
<item>project media</item>
<item>activate VPN</item>
<item>write wallpaper</item>
<item>assist structure</item>
<item>assist screenshot</item>
<item>read phone state</item>
<item>add voicemail</item>
<item>use sip</item>
<item>process outgoing call</item>
<item>fingerprint</item>
<item>body sensors</item>
<item>read cell broadcasts</item>
<item>mock location</item>
<item>read storage</item>
<item>write storage</item>
<item>turn on screen</item>
<item>get accounts</item>
<item>run in background</item>
</string-array>
<!-- User display names for app ops codes -->
@@ -674,11 +688,25 @@
<item>Location</item>
<item>Get usage stats</item>
<item>Mute/unmute microphone</item>
<item>Show toast</item>
<item>Project media</item>
<item>Activate VPN</item>
<item>Write wallpaper</item>
<item>Assist structure</item>
<item>Assist screenshot</item>
<item>Read phone state</item>
<item>Add voicemail</item>
<item>Use sip</item>
<item>Process outgoing call</item>
<item>Fingerprint</item>
<item>Body sensors</item>
<item>Read cell broadcasts</item>
<item>Mock location</item>
<item>Read storage</item>
<item>Write storage</item>
<item>Turn on screen</item>
<item>Get accounts</item>
<item>Run in background</item>
</string-array>
<!-- Titles for the list of long press timeout options. -->

View File

@@ -6425,6 +6425,12 @@
usb_use_file_transfer, use_use_photo_transfer, and usb_use_MIDI -->
<string name="usb_use">Use USB for</string>
<!-- Settings item title for background check prefs [CHAR LIMIT=35] -->
<string name="background_check_pref">Background check</string>
<!-- Settings screen title for background check prefs [CHAR LIMIT=35] -->
<string name="background_check_title">Full background access</string>
<!-- Title for the "context" preference to determine whether assist can access the data currently displayed on-screen [CHAR LIMIT=40] -->
<string name="assist_access_context_title">Use text from screen</string>

View File

@@ -340,6 +340,10 @@
android:entries="@array/app_process_limit_entries"
android:entryValues="@array/app_process_limit_values" />
<Preference
android:key="background_check"
android:title="@string/background_check_pref" />
<SwitchPreference
android:key="show_all_anrs"
android:title="@string/show_all_anrs"

View File

@@ -76,6 +76,7 @@ import android.widget.Switch;
import android.widget.TextView;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.applications.BackgroundCheckSummary;
import com.android.settings.fuelgauge.InactiveApps;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
@@ -170,6 +171,8 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
= "immediately_destroy_activities";
private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
private static final String BACKGROUND_CHECK_KEY = "background_check";
private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
@@ -1718,6 +1721,8 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
writeForceResizableOptions();
} else if (INACTIVE_APPS_KEY.equals(preference.getKey())) {
startInactiveAppsFragment();
} else if (BACKGROUND_CHECK_KEY.equals(preference.getKey())) {
startBackgroundCheckFragment();
} else {
return super.onPreferenceTreeClick(preference);
}
@@ -1731,6 +1736,12 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
null, R.string.inactive_apps_title, null, null, 0);
}
private void startBackgroundCheckFragment() {
((SettingsActivity) getActivity()).startPreferencePanel(
BackgroundCheckSummary.class.getName(),
null, R.string.background_check_title, null, null, 0);
}
private boolean showKeyguardConfirmation(Resources resources, int requestCode) {
return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
requestCode, resources.getString(R.string.oem_unlock_enable));

View File

@@ -61,6 +61,7 @@ public class Settings extends SettingsActivity {
return super.isValidFragment(className);
}
}
public static class BackgroundCheckSummaryActivity extends SettingsActivity { /* empty */ }
public static class StorageUseActivity extends SettingsActivity { /* empty */ }
public static class DevelopmentSettingsActivity extends SettingsActivity { /* empty */ }
public static class AccessibilitySettingsActivity extends SettingsActivity { /* empty */ }

View File

@@ -16,6 +16,7 @@
package com.android.settings.applications;
import android.app.AppOpsManager;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
@@ -34,6 +35,7 @@ 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;
@@ -48,6 +50,7 @@ public class AppOpsCategory extends ListFragment implements
private static final int RESULT_APP_DETAILS = 1;
AppOpsState mState;
boolean mUserControlled;
// This is the Adapter being used to display the list's data.
AppListAdapter mAdapter;
@@ -58,8 +61,13 @@ public class AppOpsCategory extends ListFragment implements
}
public AppOpsCategory(AppOpsState.OpsTemplate template) {
this(template, false);
}
public AppOpsCategory(AppOpsState.OpsTemplate template, boolean userControlled) {
Bundle args = new Bundle();
args.putParcelable("template", template);
args.putBoolean("userControlled", userControlled);
setArguments(args);
}
@@ -117,18 +125,22 @@ public class AppOpsCategory extends ListFragment implements
final InterestingConfigChanges mLastConfig = new InterestingConfigChanges();
final AppOpsState mState;
final AppOpsState.OpsTemplate mTemplate;
final boolean mUserControlled;
List<AppOpEntry> mApps;
PackageIntentReceiver mPackageObserver;
public AppListLoader(Context context, AppOpsState state, AppOpsState.OpsTemplate template) {
public AppListLoader(Context context, AppOpsState state, AppOpsState.OpsTemplate template,
boolean userControlled) {
super(context);
mState = state;
mTemplate = template;
mUserControlled = userControlled;
}
@Override public List<AppOpEntry> loadInBackground() {
return mState.buildState(mTemplate);
return mState.buildState(mTemplate, 0, null,
mUserControlled ? AppOpsState.LABEL_COMPARATOR : AppOpsState.RECENCY_COMPARATOR);
}
/**
@@ -247,13 +259,15 @@ public class AppOpsCategory extends ListFragment implements
private final Resources mResources;
private final LayoutInflater mInflater;
private final AppOpsState mState;
private final boolean mUserControlled;
List<AppOpEntry> mList;
public AppListAdapter(Context context, AppOpsState state) {
public AppListAdapter(Context context, AppOpsState state, boolean userControlled) {
mResources = context.getResources();
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mState = state;
mUserControlled = userControlled;
}
public void setData(List<AppOpEntry> data) {
@@ -292,9 +306,18 @@ 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.getSummaryText(mState));
((TextView)view.findViewById(R.id.op_time)).setText(
item.getTimeText(mResources, false));
if (mUserControlled) {
((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);
} else {
((TextView) view.findViewById(R.id.op_name)).setText(item.getSummaryText(mState));
((TextView) view.findViewById(R.id.op_time)).setText(
item.getTimeText(mResources, false));
view.findViewById(R.id.op_switch).setVisibility(View.GONE);
}
return view;
}
@@ -304,6 +327,7 @@ public class AppOpsCategory extends ListFragment implements
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mState = new AppOpsState(getActivity());
mUserControlled = getArguments().getBoolean("userControlled");
}
@Override public void onActivityCreated(Bundle savedInstanceState) {
@@ -317,7 +341,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(), mState);
mAdapter = new AppListAdapter(getActivity(), mState, mUserControlled);
setListAdapter(mAdapter);
// Start out with a progress indicator.
@@ -341,8 +365,22 @@ public class AppOpsCategory extends ListFragment implements
@Override public void onListItemClick(ListView l, View v, int position, long id) {
AppOpEntry entry = mAdapter.getItem(position);
if (entry != null) {
mCurrentPkgName = entry.getAppEntry().getApplicationInfo().packageName;
startApplicationDetailsActivity();
if (mUserControlled) {
// We treat this as tapping on the check box, toggling the app op state.
Switch sw = ((Switch) 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);
} else {
mCurrentPkgName = entry.getAppEntry().getApplicationInfo().packageName;
startApplicationDetailsActivity();
}
}
}
@@ -352,7 +390,7 @@ public class AppOpsCategory extends ListFragment implements
if (fargs != null) {
template = (AppOpsState.OpsTemplate)fargs.getParcelable("template");
}
return new AppListLoader(getActivity(), mState, template);
return new AppListLoader(getActivity(), mState, template, mUserControlled);
}
@Override public void onLoadFinished(Loader<List<AppOpEntry>> loader, List<AppOpEntry> data) {

View File

@@ -180,6 +180,7 @@ public class AppOpsState {
false,
false,
false,
false,
false }
);
@@ -206,9 +207,14 @@ public class AppOpsState {
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
MEDIA_TEMPLATE, DEVICE_TEMPLATE, RUN_IN_BACKGROUND_TEMPLATE
};
/**
@@ -306,6 +312,7 @@ public class AppOpsState {
= 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) {
@@ -363,6 +370,14 @@ public class AppOpsState {
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) {
@@ -418,9 +433,9 @@ public class AppOpsState {
}
/**
* Perform alphabetical comparison of application entry objects.
* Perform app op state comparison of application entry objects.
*/
public static final Comparator<AppOpEntry> APP_OP_COMPARATOR = new Comparator<AppOpEntry>() {
public static final Comparator<AppOpEntry> RECENCY_COMPARATOR = new Comparator<AppOpEntry>() {
private final Collator sCollator = Collator.getInstance();
@Override
public int compare(AppOpEntry object1, AppOpEntry object2) {
@@ -440,6 +455,18 @@ public class AppOpsState {
}
};
/**
* 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) {
@@ -466,8 +493,12 @@ public class AppOpsState {
entries.add(entry);
}
public AppOpsManager getAppOpsManager() {
return mAppOps;
}
public List<AppOpEntry> buildState(OpsTemplate tpl) {
return buildState(tpl, 0, null);
return buildState(tpl, 0, null, RECENCY_COMPARATOR);
}
private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries,
@@ -492,6 +523,11 @@ public class AppOpsState {
}
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>();
@@ -593,7 +629,7 @@ public class AppOpsState {
}
// Sort the list.
Collections.sort(entries, APP_OP_COMPARATOR);
Collections.sort(entries, comparator);
// Done!
return entries;

View File

@@ -0,0 +1,66 @@
/**
* 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;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.preference.PreferenceFrameLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.InstrumentedFragment;
import com.android.settings.R;
public class BackgroundCheckSummary extends InstrumentedFragment {
// layout inflater object used to inflate views
private LayoutInflater mInflater;
@Override
protected int getMetricsCategory() {
return MetricsLogger.BACKGROUND_CHECK_SUMMARY;
}
@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,
true), "appops");
ft.commitAllowingStateLoss();
return rootView;
}
}