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

Commit 2593fcd1 authored by ykhung's avatar ykhung
Browse files

Clean up useless battery tips and detectors

Bug: 263193978
Test: make -j64 RunSettingsRoboTests
ROBOTEST_FILTER="com.android.settings.fuelgauge.batterytip"

Change-Id: Idfe03eee5acee33126f1805ef45ccc4a9fc7e7a8
parent 2209c02d
Loading
Loading
Loading
Loading
+0 −16
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
import com.android.settingslib.fuelgauge.EstimateKt;
import com.android.settingslib.utils.AsyncLoaderCompat;

@@ -45,8 +44,6 @@ import java.util.List;
public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
    private static final String TAG = "BatteryTipLoader";

    private static final boolean USE_FAKE_DATA = false;

    private BatteryUsageStats mBatteryUsageStats;
    @VisibleForTesting
    BatteryUtils mBatteryUtils;
@@ -59,9 +56,6 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {

    @Override
    public List<BatteryTip> loadInBackground() {
        if (USE_FAKE_DATA) {
            return getFakeData();
        }
        final List<BatteryTip> tips = new ArrayList<>();
        final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
        final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
@@ -81,14 +75,4 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
    @Override
    protected void onDiscardResult(List<BatteryTip> result) {
    }

    private List<BatteryTip> getFakeData() {
        final List<BatteryTip> tips = new ArrayList<>();
        tips.add(new SummaryTip(BatteryTip.StateType.NEW,
                EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
        tips.add(new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */));

        return tips;
    }

}
+0 −96
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.batterytip.detectors;

import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;

import android.content.Context;

import androidx.annotation.VisibleForTesting;

import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.BatteryTipUtils;
import com.android.settings.fuelgauge.batterytip.tips.AppLabelPredicate;
import com.android.settings.fuelgauge.batterytip.tips.AppRestrictionPredicate;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Detector whether to show summary tip. This detector should be executed as the last
 * {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
 */
public class RestrictAppDetector implements BatteryTipDetector {
    @VisibleForTesting
    static final boolean USE_FAKE_DATA = false;
    private BatteryTipPolicy mPolicy;
    @VisibleForTesting
    BatteryDatabaseManager mBatteryDatabaseManager;
    private Context mContext;

    private AppRestrictionPredicate mAppRestrictionPredicate;
    private AppLabelPredicate mAppLabelPredicate;

    public RestrictAppDetector(Context context, BatteryTipPolicy policy) {
        mContext = context;
        mPolicy = policy;
        mBatteryDatabaseManager = BatteryDatabaseManager.getInstance(context);
        mAppRestrictionPredicate = AppRestrictionPredicate.getInstance(context);
        mAppLabelPredicate = AppLabelPredicate.getInstance(context);
    }

    @Override
    public BatteryTip detect() {
        if (USE_FAKE_DATA) {
            return getFakeData();
        }
        if (mPolicy.appRestrictionEnabled) {
            final long oneDayBeforeMs = System.currentTimeMillis()
                    - TimeUnit.HOURS.toMillis(mPolicy.appRestrictionActiveHour);
            final List<AppInfo> highUsageApps = BatteryTipUtils.detectAnomalies(mContext,
                    oneDayBeforeMs);
            if (!highUsageApps.isEmpty()) {
                // If there are new anomalies, show them
                return new RestrictAppTip(BatteryTip.StateType.NEW, highUsageApps);
            } else {
                // Otherwise, show auto-handled one if it exists
                final List<AppInfo> autoHandledApps = mBatteryDatabaseManager.queryAllAnomalies(
                        oneDayBeforeMs, AnomalyDatabaseHelper.State.AUTO_HANDLED);
                // Remove it if it doesn't have label or unrestricted
                autoHandledApps.removeIf(mAppLabelPredicate.or(mAppRestrictionPredicate.negate()));
                return new RestrictAppTip(autoHandledApps.isEmpty() ? BatteryTip.StateType.INVISIBLE
                        : BatteryTip.StateType.HANDLED, autoHandledApps);
            }
        } else {
            return new RestrictAppTip(BatteryTip.StateType.INVISIBLE, new ArrayList<>());
        }
    }

    private BatteryTip getFakeData() {
        final List<AppInfo> highUsageApps = new ArrayList<>();
        highUsageApps.add(new AppInfo.Builder()
                .setPackageName(SETTINGS_PACKAGE_NAME)
                .build());
        return new RestrictAppTip(BatteryTip.StateType.NEW, highUsageApps);
    }
}
+0 −44
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.batterytip.detectors;

import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;

/**
 * Detector whether to show summary tip. This detector should be executed as the last
 * {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
 */
public class SummaryDetector implements BatteryTipDetector {
    private BatteryTipPolicy mPolicy;
    private long mAverageTimeMs;

    public SummaryDetector(BatteryTipPolicy policy, long averageTimeMs) {
        mPolicy = policy;
        mAverageTimeMs = averageTimeMs;
    }

    @Override
    public BatteryTip detect() {
        // Show it if there is no other tips shown
        final int state = mPolicy.summaryEnabled
                ? BatteryTip.StateType.NEW
                : BatteryTip.StateType.INVISIBLE;
        return new SummaryTip(state, mAverageTimeMs);
    }
}
+0 −97
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.batterytip.tips;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

/**
 * Tip to show general summary about battery life
 */
public class SummaryTip extends BatteryTip {
    private long mAverageTimeMs;

    public SummaryTip(@StateType int state, long averageTimeMs) {
        super(TipType.SUMMARY, state, true /* showDialog */);
        mAverageTimeMs = averageTimeMs;
    }

    @VisibleForTesting
    SummaryTip(Parcel in) {
        super(in);
        mAverageTimeMs = in.readLong();
    }

    @Override
    public CharSequence getTitle(Context context) {
        return context.getString(R.string.battery_tip_summary_title);
    }

    @Override
    public CharSequence getSummary(Context context) {
        return context.getString(R.string.battery_tip_summary_summary);
    }

    @Override
    public int getIconId() {
        return R.drawable.ic_battery_status_good_24dp;
    }

    @Override
    public int getIconTintColorId() {
        return R.color.battery_good_color_light;
    }

    @Override
    public void updateState(BatteryTip tip) {
        mState = tip.mState;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeLong(mAverageTimeMs);
    }

    @Override
    public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) {
        metricsFeatureProvider.action(context, SettingsEnums.ACTION_SUMMARY_TIP,
                mState);
    }

    public long getAverageTimeMs() {
        return mAverageTimeMs;
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public BatteryTip createFromParcel(Parcel in) {
            return new SummaryTip(in);
        }

        public BatteryTip[] newArray(int size) {
            return new SummaryTip[size];
        }
    };
}
+0 −19
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import com.android.settings.fuelgauge.batterytip.tips.BatteryDefenderTip;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
@@ -74,7 +73,6 @@ public class BatteryTipDialogFragmentTest {
    private RestrictAppTip mRestrictedOneAppTip;
    private RestrictAppTip mRestrictTwoAppsTip;
    private UnrestrictAppTip mUnrestrictAppTip;
    private SummaryTip mSummaryTip;
    private BatteryDefenderTip mDefenderTip;
    private AppInfo mAppInfo;
    private ShadowPackageManager mPackageManager;
@@ -116,8 +114,6 @@ public class BatteryTipDialogFragmentTest {
                new ArrayList<>(restrictApps));

        mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
        mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
                EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
        mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
    }

@@ -229,19 +225,4 @@ public class BatteryTipDialogFragmentTest {
        assertThat(shadowDialog.getMessage())
                .isEqualTo(mContext.getString(R.string.battery_tip_unrestrict_app_dialog_message));
    }

    @Test
    public void testOnCreateDialog_summaryTip_fireDialog() {
        doReturn(AVERAGE_TIME_MS).when(mSummaryTip).getAverageTimeMs();
        mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);

        FragmentController.setupFragment(mDialogFragment, FragmentActivity.class,
                0 /* containerViewId */, null /* bundle */);

        final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);

        assertThat(shadowDialog.getMessage()).isEqualTo(
                mContext.getText(R.string.battery_tip_dialog_summary_message));
    }
}
Loading