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

Commit 55a97a9a authored by jackqdyulei's avatar jackqdyulei
Browse files

Add abnormal app page for battery settings

1. Add abnormal app page
2. Add strings for abnormal app page
3. Change AnomalyPreferenceController to open abnormal app page,
when there are more than one anomaly.
4. Add AnomalyPreference, who stores a reference of anomaly.

Also rename AnomalyPreferenceController to
AnomalySummaryPreferenceController because this controller is not
used to handle AnomalyPreference.

Following cls will add summary and icon for each abnormal app.

Bug: 37681665
Test: RunSettingsRoboTests
Change-Id: I4266f906476ff8daccd962572c8cfa99f948080a
parent 4aa3358c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -4540,6 +4540,9 @@
    <!-- Subtitle for list of packages -->
    <string name="packages_subtitle">Included packages</string>
    <!-- Activity title for battery abnormal details page [CHAR LIMIT=60] -->
    <string name="battery_abnormal_details_title">Abnormal app behavior</string>
    <!-- Label for power consumed by the screen -->
    <string name="power_screen">Screen</string>
    <!-- Label for power consumed by the flashlight -->
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2017 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.
-->

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/battery_abnormal_details_title">

    <PreferenceCategory
        android:key="app_abnormal_list"/>

</PreferenceScreen>
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.fuelgauge;

import android.content.Context;
import android.os.Bundle;
import android.os.UserHandle;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
import com.android.settings.fuelgauge.anomaly.AnomalyPreference;

import java.util.List;

/**
 * Fragment to show a list of anomaly apps, where user could handle these anomalies
 */
public class PowerUsageAnomalyDetails extends DashboardFragment implements
        AnomalyDialogFragment.AnomalyDialogListener {

    public static final String TAG = "PowerAbnormalUsageDetail";
    @VisibleForTesting
    static final String EXTRA_ANOMALY_LIST = "anomaly_list";
    private static final int REQUEST_ANOMALY_ACTION = 0;
    private static final String KEY_PREF_ANOMALY_LIST = "app_abnormal_list";

    @VisibleForTesting
    List<Anomaly> mAnomalies;
    @VisibleForTesting
    PreferenceGroup mAbnormalListGroup;

    public static void startBatteryAbnormalPage(SettingsActivity caller,
            PreferenceFragment fragment, List<Anomaly> anomalies) {
        Bundle args = new Bundle();
        args.putParcelableList(EXTRA_ANOMALY_LIST, anomalies);

        caller.startPreferencePanelAsUser(fragment, PowerUsageAnomalyDetails.class.getName(), args,
                R.string.battery_abnormal_details_title, null,
                new UserHandle(UserHandle.myUserId()));
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mAnomalies = getArguments().getParcelableArrayList(EXTRA_ANOMALY_LIST);
        mAbnormalListGroup = (PreferenceGroup) findPreference(KEY_PREF_ANOMALY_LIST);
    }

    @Override
    public void onResume() {
        super.onResume();

        refreshUi();
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference instanceof AnomalyPreference) {
            AnomalyPreference anomalyPreference = (AnomalyPreference) preference;
            final Anomaly anomaly = anomalyPreference.getAnomaly();

            AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly);
            dialogFragment.setTargetFragment(this, REQUEST_ANOMALY_ACTION);
            dialogFragment.show(getFragmentManager(), TAG);

            return true;
        }

        return super.onPreferenceTreeClick(preference);
    }

    @Override
    protected String getLogTag() {
        return TAG;
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.power_abnormal_detail;
    }

    @Override
    protected List<PreferenceController> getPreferenceControllers(Context context) {
        return null;
    }

    @Override
    public int getMetricsCategory() {
        //TODO(b/37681923): add correct metrics category
        return 0;
    }

    void refreshUi() {
        //TODO(b/37681665): cache the preference so we don't need to create new one every time.
        mAbnormalListGroup.removeAll();
        for (int i = 0, size = mAnomalies.size(); i < size; i++) {
            final Anomaly anomaly = mAnomalies.get(i);
            Preference pref = new AnomalyPreference(getPrefContext(), anomaly);

            mAbnormalListGroup.addPreference(pref);
        }
    }

    @Override
    public void onAnomalyHandled(Anomaly anomaly) {
        mAnomalies.remove(anomaly);
        refreshUi();
    }
}
+7 −7
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ import com.android.settings.display.TimeoutPreferenceController;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
import com.android.settings.fuelgauge.anomaly.AnomalyPreferenceController;
import com.android.settings.fuelgauge.anomaly.AnomalySummaryPreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.FooterPreferenceMixin;
@@ -125,7 +125,7 @@ public class PowerUsageSummary extends PowerUsageBase implements

    private LayoutPreference mBatteryLayoutPref;
    private PreferenceGroup mAppListGroup;
    private AnomalyPreferenceController mAnomalyPreferenceController;
    private AnomalySummaryPreferenceController mAnomalySummaryPreferenceController;
    private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;

    private LoaderManager.LoaderCallbacks<List<Anomaly>> mAnomalyLoaderCallbacks =
@@ -139,7 +139,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
                @Override
                public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
                    // show high usage preference if possible
                    mAnomalyPreferenceController.updateAnomalyPreference(data);
                    mAnomalySummaryPreferenceController.updateHighUsagePreference(data);
                }

                @Override
@@ -159,8 +159,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
        mLastFullChargePref = (PowerGaugePreference) findPreference(
                KEY_TIME_SINCE_LAST_FULL_CHARGE);
        mFooterPreferenceMixin.createFooterPreference().setTitle(R.string.battery_footer_summary);
        mAnomalyPreferenceController = new AnomalyPreferenceController(this);

        mAnomalySummaryPreferenceController = new AnomalySummaryPreferenceController(
                (SettingsActivity) getActivity(), this);
        mBatteryUtils = BatteryUtils.getInstance(getContext());

        initFeatureProvider();
@@ -193,7 +193,7 @@ public class PowerUsageSummary extends PowerUsageBase implements

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (mAnomalyPreferenceController.onPreferenceTreeClick(preference)) {
        if (mAnomalySummaryPreferenceController.onPreferenceTreeClick(preference)) {
            return true;
        }
        if (KEY_BATTERY_HEADER.equals(preference.getKey())) {
@@ -727,7 +727,7 @@ public class PowerUsageSummary extends PowerUsageBase implements

    @Override
    public void onAnomalyHandled(Anomaly anomaly) {
        mAnomalyPreferenceController.hideAnomalyPreference();
        mAnomalySummaryPreferenceController.hideHighUsagePreference();
    }

    private static class SummaryProvider implements SummaryLoader.SummaryProvider {
+0 −2
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ public class Anomaly implements Parcelable {
    public final int type;
    public final int uid;
    public final long wakelockTimeMs;

    /**
     * Display name of this anomaly, usually it is the app name
     */
@@ -98,7 +97,6 @@ public class Anomaly implements Parcelable {
        }

        Anomaly other = (Anomaly) obj;

        return type == other.type
                && uid == other.uid
                && wakelockTimeMs == other.wakelockTimeMs
Loading