Linked the recent location apps to battery page

* Extracted BatteryStatsHelper from PowerUsageSummary

* Dropped the "pause" and "resume" method from the
RadioButtonPreference

* Opening battery usage page when the user clicks the recent location
apps item

Change-Id: I5a2d03271434765780154265976d8caa7f8060d8
This commit is contained in:
Lifu Tang
2013-08-12 16:34:45 -07:00
parent 5db411db98
commit c3e9ac937a
7 changed files with 1049 additions and 808 deletions

View File

@@ -53,17 +53,11 @@ public class LocationMode extends LocationSettingsBase
public void onResume() {
super.onResume();
createPreferenceHierarchy();
mHighAccuracy.resume();
mBatterySaving.resume();
mSensorsOnly.resume();
}
@Override
public void onPause() {
super.onPause();
mHighAccuracy.pause();
mBatterySaving.pause();
mSensorsOnly.pause();
}
private PreferenceScreen createPreferenceHierarchy() {

View File

@@ -19,23 +19,23 @@ package com.android.settings.location;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.Gravity;
import android.widget.CompoundButton;
import android.widget.Switch;
import com.android.settings.R;
import com.android.settings.fuelgauge.BatteryStatsHelper;
/**
* Location access settings.
*/
public class LocationSettings extends LocationSettingsBase
implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = LocationSettings.class.getSimpleName();
/** Key for preference screen "Mode" */
private static final String KEY_LOCATION_MODE = "location_mode";
/** Key for preference category "Recent location requests" */
@@ -49,6 +49,8 @@ public class LocationSettings extends LocationSettingsBase
private PreferenceCategory mRecentLocationRequests;
private PreferenceCategory mLocationServices;
private BatteryStatsHelper mStatsHelper;
public LocationSettings() {
mValidListener = false;
}
@@ -59,6 +61,18 @@ public class LocationSettings extends LocationSettingsBase
createPreferenceHierarchy();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mStatsHelper = new BatteryStatsHelper(activity, null);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mStatsHelper.create(icicle);
}
@Override
public void onResume() {
super.onResume();
@@ -73,6 +87,13 @@ public class LocationSettings extends LocationSettingsBase
super.onPause();
mValidListener = false;
mSwitch.setOnCheckedChangeListener(null);
mStatsHelper.pause();
}
@Override
public void onDestroy() {
super.onDestroy();
mStatsHelper.destroy();
}
private PreferenceScreen createPreferenceHierarchy() {
@@ -88,9 +109,8 @@ public class LocationSettings extends LocationSettingsBase
new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
PreferenceActivity preferenceActivity =
(PreferenceActivity) getActivity();
preferenceActivity.startPreferencePanel(
PreferenceActivity activity = (PreferenceActivity) getActivity();
activity.startPreferencePanel(
LocationMode.class.getName(), null,
R.string.location_mode_screen_title, null, LocationSettings.this,
0);
@@ -101,30 +121,24 @@ public class LocationSettings extends LocationSettingsBase
(PreferenceCategory) root.findPreference(KEY_RECENT_LOCATION_REQUESTS);
mLocationServices = (PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES);
Activity activity = getActivity();
RecentLocationApps recentApps = new RecentLocationApps(activity);
PreferenceActivity activity = (PreferenceActivity) getActivity();
RecentLocationApps recentApps = new RecentLocationApps(activity, mStatsHelper);
recentApps.fillAppList(mRecentLocationRequests);
SettingsInjector.addInjectedSettings(mLocationServices, activity, getPreferenceManager());
if (activity instanceof PreferenceActivity) {
PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
// Only show the master switch when we're not in multi-pane mode, and not being used as
// Setup Wizard.
if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
final int padding = activity.getResources().getDimensionPixelSize(
R.dimen.action_bar_switch_padding);
mSwitch.setPaddingRelative(0, 0, padding, 0);
activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
}
} else {
Log.wtf(TAG, "Current activity is not an instance of PreferenceActivity!");
// Only show the master switch when we're not in multi-pane mode, and not being used as
// Setup Wizard.
if (activity.onIsHidingHeaders() || !activity.onIsMultiPane()) {
final int padding = activity.getResources().getDimensionPixelSize(
R.dimen.action_bar_switch_padding);
mSwitch.setPaddingRelative(0, 0, padding, 0);
activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
}
setHasOptionsMenu(true);

View File

@@ -19,9 +19,7 @@ package com.android.settings.location;
import android.content.Context;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.settings.R;
@@ -37,8 +35,6 @@ import com.android.settings.R;
* uncheck all the other preferences, you should do that by code yourself.
*/
public class RadioButtonPreference extends CheckBoxPreference {
private boolean mValidListener;
public interface OnClickListener {
public abstract void onRadioButtonClicked(RadioButtonPreference emiter);
}
@@ -48,7 +44,6 @@ public class RadioButtonPreference extends CheckBoxPreference {
public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
mValidListener = false;
}
public RadioButtonPreference(Context context, AttributeSet attrs) {
@@ -63,14 +58,6 @@ public class RadioButtonPreference extends CheckBoxPreference {
mListener = listener;
}
public void pause() {
mValidListener = false;
}
public void resume() {
mValidListener = true;
}
@Override
public void onClick() {
if (mListener != null) {

View File

@@ -20,14 +20,22 @@ import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import com.android.settings.R;
import com.android.settings.applications.InstalledAppDetails;
import com.android.settings.fuelgauge.BatterySipper;
import com.android.settings.fuelgauge.BatteryStatsHelper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Retrieves the information of applications which accessed location recently.
@@ -37,12 +45,52 @@ public class RecentLocationApps {
private static final int RECENT_TIME_INTERVAL_MILLIS = 15 * 60 * 1000;
private Context mContext;
PackageManager mPackageManager;
private final PreferenceActivity mActivity;
private final BatteryStatsHelper mStatsHelper;
private final PackageManager mPackageManager;
public RecentLocationApps(Context context) {
mContext = context;
mPackageManager = context.getPackageManager();
// Stores all the packages that requested location within the designated interval
// key - package name of the app
// value - whether the app has requested high power location
public RecentLocationApps(PreferenceActivity activity, BatteryStatsHelper sipperUtil) {
mActivity = activity;
mPackageManager = activity.getPackageManager();
mStatsHelper = sipperUtil;
}
private class UidEntryClickedListener
implements Preference.OnPreferenceClickListener {
private BatterySipper mSipper;
public UidEntryClickedListener(BatterySipper sipper) {
mSipper = sipper;
}
@Override
public boolean onPreferenceClick(Preference preference) {
mStatsHelper.startBatteryDetailPage(mActivity, mSipper);
return true;
}
}
private class PackageEntryClickedListener
implements Preference.OnPreferenceClickListener {
private String mPackage;
public PackageEntryClickedListener(String packageName) {
mPackage = packageName;
}
@Override
public boolean onPreferenceClick(Preference preference) {
// start new fragment to display extended information
Bundle args = new Bundle();
args.putString(InstalledAppDetails.ARG_PACKAGE_NAME, mPackage);
mActivity.startPreferencePanel(InstalledAppDetails.class.getName(), args,
R.string.application_info_label, null, null, 0);
return true;
}
}
/**
@@ -50,17 +98,68 @@ public class RecentLocationApps {
* specified time.
*/
public void fillAppList(PreferenceCategory container) {
HashMap<String, Boolean> packageMap = new HashMap<String, Boolean>();
AppOpsManager aoManager =
(AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
(AppOpsManager) mActivity.getSystemService(Context.APP_OPS_SERVICE);
List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps(
new int[] {
AppOpsManager.OP_MONITOR_LOCATION,
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION,
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION,
});
PreferenceManager preferenceManager = container.getPreferenceManager();
long now = System.currentTimeMillis();
for (AppOpsManager.PackageOps ops : appOps) {
processPackageOps(now, container, preferenceManager, ops);
processPackageOps(now, container, preferenceManager, ops, packageMap);
}
mStatsHelper.refreshStats();
List<BatterySipper> usageList = mStatsHelper.getUsageList();
for (BatterySipper sipper : usageList) {
sipper.loadNameAndIcon();
String[] packages = sipper.getPackages();
if (packages == null) {
continue;
}
for (String curPackage : packages) {
if (packageMap.containsKey(curPackage)) {
PreferenceScreen screen = preferenceManager.createPreferenceScreen(mActivity);
screen.setIcon(sipper.getIcon());
screen.setTitle(sipper.getLabel());
if (packageMap.get(curPackage)) {
screen.setSummary(R.string.location_high_battery_use);
} else {
screen.setSummary(R.string.location_low_battery_use);
}
container.addPreference(screen);
screen.setOnPreferenceClickListener(new UidEntryClickedListener(sipper));
packageMap.remove(curPackage);
break;
}
}
}
// Typically there shouldn't be any entry left in the HashMap. But if there are any, add
// them to the list and link them to the app info page.
for (Map.Entry<String, Boolean> entry : packageMap.entrySet()) {
try {
PreferenceScreen screen = preferenceManager.createPreferenceScreen(mActivity);
ApplicationInfo appInfo = mPackageManager.getApplicationInfo(
entry.getKey(), PackageManager.GET_META_DATA);
screen.setIcon(mPackageManager.getApplicationIcon(appInfo));
screen.setTitle(mPackageManager.getApplicationLabel(appInfo));
// if used both high and low battery within the time interval, show as "high
// battery"
if (entry.getValue()) {
screen.setSummary(R.string.location_high_battery_use);
} else {
screen.setSummary(R.string.location_low_battery_use);
}
screen.setOnPreferenceClickListener(
new PackageEntryClickedListener(entry.getKey()));
container.addPreference(screen);
} catch (PackageManager.NameNotFoundException e) {
// ignore the current app and move on to the next.
}
}
}
@@ -68,7 +167,8 @@ public class RecentLocationApps {
long now,
PreferenceCategory container,
PreferenceManager preferenceManager,
AppOpsManager.PackageOps ops) {
AppOpsManager.PackageOps ops,
HashMap<String, Boolean> packageMap) {
String packageName = ops.getPackageName();
List<AppOpsManager.OpEntry> entries = ops.getOps();
boolean highBattery = false;
@@ -96,22 +196,6 @@ public class RecentLocationApps {
return;
}
try {
PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
ApplicationInfo appInfo = mPackageManager.getApplicationInfo(
packageName, PackageManager.GET_META_DATA);
screen.setIcon(mPackageManager.getApplicationIcon(appInfo));
screen.setTitle(mPackageManager.getApplicationLabel(appInfo));
// if used both high and low battery within the time interval, show as "high
// battery"
if (highBattery) {
screen.setSummary(R.string.location_high_battery_use);
} else {
screen.setSummary(R.string.location_low_battery_use);
}
container.addPreference(screen);
} catch (PackageManager.NameNotFoundException e) {
// ignore the current app and move on to the next.
}
packageMap.put(packageName, highBattery);
}
}