Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c3e9ac93 authored by Lifu Tang's avatar Lifu Tang
Browse files

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
parent 5db411db
Loading
Loading
Loading
Loading
+47 −15
Original line number Diff line number Diff line
@@ -30,7 +30,11 @@ import android.os.BatteryStats.Uid;
import java.util.ArrayList;
import java.util.HashMap;

class BatterySipper implements Comparable<BatterySipper> {
/**
 * Contains information about package name, icon image, power usage about an
 * application or a system service.
 */
public class BatterySipper implements Comparable<BatterySipper> {
    final Context mContext;
    /* Cache cleared when PowerUsageSummary is destroyed */
    static final HashMap<String,UidToDetail> sUidCache = new HashMap<String,UidToDetail>();
@@ -56,6 +60,7 @@ class BatterySipper implements Comparable<BatterySipper> {
    double percent;
    double noCoveragePercent;
    String defaultPackageName;
    String[] mPackages;

    static class UidToDetail {
        String name;
@@ -90,15 +95,30 @@ class BatterySipper implements Comparable<BatterySipper> {
        return values;
    }

    Drawable getIcon() {
    public Drawable getIcon() {
        return icon;
    }

    /**
     * Gets the application name
     */
    public String getLabel() {
        return name;
    }

    @Override
    public int compareTo(BatterySipper other) {
        // Return the flipped value because we want the items in descending order
        return Double.compare(other.getSortValue(), getSortValue());
    }

    /**
     * Gets a list of packages associated with the current user
     */
    public String[] getPackages() {
        return mPackages;
    }

    void getQuickNameIconForUid(Uid uidObj) {
        final int uid = uidObj.getUid();
        final String uidString = Integer.toString(uid);
@@ -125,27 +145,36 @@ class BatterySipper implements Comparable<BatterySipper> {
        } else {
            //name = packages[0];
        }
        if (mHandler != null) {
            synchronized (mRequestQueue) {
                mRequestQueue.add(this);
            }
        }
    }

    public static void clearUidCache() {
        sUidCache.clear();
    }

    /**
     * Sets name and icon
     * @param uid Uid of the application
     * Loads the app label and icon image and stores into the cache.
     */
    void getNameIcon() {
    public void loadNameAndIcon() {
        // Bail out if the current sipper is not an App sipper.
        if (uidObj == null) {
            return;
        }
        PackageManager pm = mContext.getPackageManager();
        final int uid = uidObj.getUid();
        final Drawable defaultActivityIcon = pm.getDefaultActivityIcon();
        String[] packages = pm.getPackagesForUid(uid);
        if (packages == null) {
        mPackages = pm.getPackagesForUid(uid);
        if (mPackages == null) {
            name = Integer.toString(uid);
            return;
        }

        String[] packageLabels = new String[packages.length];
        System.arraycopy(packages, 0, packageLabels, 0, packages.length);
        String[] packageLabels = new String[mPackages.length];
        System.arraycopy(mPackages, 0, packageLabels, 0, mPackages.length);

        int preferredIndex = -1;
        // Convert package names to user-facing labels where possible
@@ -159,7 +188,7 @@ class BatterySipper implements Comparable<BatterySipper> {
                    packageLabels[i] = label.toString();
                }
                if (ai.icon != 0) {
                    defaultPackageName = packages[i];
                    defaultPackageName = mPackages[i];
                    icon = ai.loadIcon(pm);
                    break;
                }
@@ -172,7 +201,7 @@ class BatterySipper implements Comparable<BatterySipper> {
            name = packageLabels[0];
        } else {
            // Look for an official name for this UID.
            for (String pkgName : packages) {
            for (String pkgName : mPackages) {
                try {
                    final PackageInfo pi = pm.getPackageInfo(pkgName, 0);
                    if (pi.sharedUserLabel != 0) {
@@ -197,6 +226,9 @@ class BatterySipper implements Comparable<BatterySipper> {
        utd.icon = icon;
        utd.packageName = defaultPackageName;
        sUidCache.put(uidString, utd);
        mHandler.sendMessage(mHandler.obtainMessage(PowerUsageSummary.MSG_UPDATE_NAME_ICON, this));
        if (mHandler != null) {
            mHandler.sendMessage(
                    mHandler.obtainMessage(BatteryStatsHelper.MSG_UPDATE_NAME_ICON, this));
        }
    }
}
+819 −0

File added.

Preview size limit exceeded, changes collapsed.

+34 −723

File changed.

Preview size limit exceeded, changes collapsed.

+0 −6
Original line number Diff line number Diff line
@@ -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() {
+39 −25
Original line number Diff line number Diff line
@@ -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,18 +121,15 @@ 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()) {
        if (activity.onIsHidingHeaders() || !activity.onIsMultiPane()) {
            final int padding = activity.getResources().getDimensionPixelSize(
                    R.dimen.action_bar_switch_padding);
            mSwitch.setPaddingRelative(0, 0, padding, 0);
@@ -123,9 +140,6 @@ public class LocationSettings extends LocationSettingsBase
                    ActionBar.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_VERTICAL | Gravity.END));
        }
        } else {
            Log.wtf(TAG, "Current activity is not an instance of PreferenceActivity!");
        }

        setHasOptionsMenu(true);

Loading