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

Commit 8468d1ad authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "battery_tip_constants"

* changes:
  Add BatteryTipDetector and LowBatteryTip stuffs.
  Add BatteryTipPolicy
parents 7e15ff4b 5f0b0964
Loading
Loading
Loading
Loading
+27 −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.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="?android:attr/colorError">
    <path
        android:fillColor="#FF000000"
        android:pathData="M13,7h-2v2h2L13,7zM13,11h-2v6h2v-6zM17,1.01L7,1c-1.1,0 -2,0.9 -2,2v18c0,
        1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2L19,3c0,-1.1 -0.9,-1.99 -2,-1.99zM17,19L7,19L7,5h10v14z"/>
</vector>
+4 −0
Original line number Diff line number Diff line
@@ -4755,6 +4755,10 @@
    <string name="battery_tip_summary_title">Battery is in good shape</string>
    <!-- Summary for the battery summary tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_summary_summary">Apps are behaving normally</string>
    <!-- Title for the low battery tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_low_battery_title">Low battery capacity</string>
    <!-- Summary for the low battery tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_low_battery_summary">Battery can\'t provide good battery life</string>
    <!-- Title for force stop dialog [CHAR LIMIT=30] -->
    <string name="dialog_stop_title">Stop app?</string>
+40 −3
Original line number Diff line number Diff line
@@ -17,13 +17,21 @@
package com.android.settings.fuelgauge.batterytip;

import android.content.Context;
import android.support.annotation.VisibleForTesting;

import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.detectors.BatteryTipDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector;
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.utils.AsyncLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
@@ -36,18 +44,31 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
    private static final boolean USE_FAKE_DATA = false;

    private BatteryStatsHelper mBatteryStatsHelper;
    private BatteryUtils mBatteryUtils;
    @VisibleForTesting
    int mVisibleTips;

    public BatteryTipLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
        super(context);
        mBatteryStatsHelper = batteryStatsHelper;
        mBatteryUtils = BatteryUtils.getInstance(context);
    }

    @Override
    public List<BatteryTip> loadInBackground() {
        List<BatteryTip> tips = new ArrayList<>();
        if (USE_FAKE_DATA) {
            return getFakeData();
        }
        final List<BatteryTip> tips = new ArrayList<>();
        final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
        final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG);
        mVisibleTips = 0;

        //TODO(b/70570352): add battery tip detectors
        tips.add(new SummaryTip(BatteryTip.StateType.NEW));
        addBatteryTipFromDetector(tips, new LowBatteryDetector(policy, batteryInfo));
        // Add summary detector at last since it need other detectors to update the mVisibleTips
        addBatteryTipFromDetector(tips, new SummaryDetector(policy, mVisibleTips));

        Collections.sort(tips);
        return tips;
    }

@@ -55,4 +76,20 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
    protected void onDiscardResult(List<BatteryTip> result) {
    }

    private List<BatteryTip> getFakeData() {
        final List<BatteryTip> tips = new ArrayList<>();
        tips.add(new SummaryTip(BatteryTip.StateType.NEW));
        tips.add(new LowBatteryTip(BatteryTip.StateType.NEW));

        return tips;
    }

    @VisibleForTesting
    void addBatteryTipFromDetector(final List<BatteryTip> tips,
            final BatteryTipDetector detector) {
        final BatteryTip batteryTip = detector.detect();
        mVisibleTips += batteryTip.isVisible() ? 1 : 0;
        tips.add(batteryTip);
    }

}
+153 −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.batterytip;

import android.content.Context;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.util.KeyValueListParser;
import android.util.Log;

/**
 * Class to store the policy for battery tips, which comes from
 * {@link Settings.Global}
 */
public class BatteryTipPolicy {
    public static final String TAG = "BatteryTipPolicy";

    private static final String KEY_BATTERY_TIP_ENABLED = "battery_tip_enabled";
    private static final String KEY_SUMMARY_ENABLED = "summary_enabled";
    private static final String KEY_BATTERY_SAVER_TIP_ENABLED = "battery_saver_tip_enabled";
    private static final String KEY_HIGH_USAGE_ENABLED = "high_usage_enabled";
    private static final String KEY_HIGH_USAGE_APP_COUNT = "high_usage_app_count";
    private static final String KEY_APP_RESTRICTION_ENABLED = "app_restriction_enabled";
    private static final String KEY_REDUCED_BATTERY_ENABLED = "reduced_battery_enabled";
    private static final String KEY_REDUCED_BATTERY_PERCENT = "reduced_battery_percent";
    private static final String KEY_LOW_BATTERY_ENABLED = "low_battery_enabled";
    private static final String KEY_LOW_BATTERY_HOUR = "low_battery_hour";

    /**
     * {@code true} if general battery tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_BATTERY_TIP_ENABLED
     */
    public final boolean batteryTipEnabled;

    /**
     * {@code true} if summary tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_SUMMARY_ENABLED
     */
    public final boolean summaryEnabled;

    /**
     * {@code true} if battery saver tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_BATTERY_SAVER_TIP_ENABLED
     */
    public final boolean batterySaverTipEnabled;

    /**
     * {@code true} if high usage tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_HIGH_USAGE_ENABLED
     */
    public final boolean highUsageEnabled;

    /**
     * The maximum number of apps shown in high usage
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_HIGH_USAGE_APP_COUNT
     */
    public final int highUsageAppCount;

    /**
     * {@code true} if app restriction tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_APP_RESTRICTION_ENABLED
     */
    public final boolean appRestrictionEnabled;

    /**
     * {@code true} if reduced battery tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_REDUCED_BATTERY_ENABLED
     */
    public final boolean reducedBatteryEnabled;

    /**
     * The percentage of reduced battery to trigger the tip(e.g. 50%)
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_REDUCED_BATTERY_PERCENT
     */
    public final int reducedBatteryPercent;

    /**
     * {@code true} if low battery tip is enabled
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_LOW_BATTERY_ENABLED
     */
    public final boolean lowBatteryEnabled;

    /**
     * Remaining battery hour to trigger the tip(e.g. 16 hours)
     *
     * @see Settings.Global#BATTERY_TIP_CONSTANTS
     * @see #KEY_LOW_BATTERY_HOUR
     */
    public final int lowBatteryHour;

    private final KeyValueListParser mParser;

    public BatteryTipPolicy(Context context) {
        this(context, new KeyValueListParser(','));
    }

    @VisibleForTesting
    BatteryTipPolicy(Context context, KeyValueListParser parser) {
        mParser = parser;
        final String value = Settings.Global.getString(context.getContentResolver(),
                Settings.Global.BATTERY_TIP_CONSTANTS);

        try {
            mParser.setString(value);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Bad battery tip constants");
        }

        batteryTipEnabled = mParser.getBoolean(KEY_BATTERY_TIP_ENABLED, true);
        summaryEnabled = mParser.getBoolean(KEY_SUMMARY_ENABLED, true);
        batterySaverTipEnabled = mParser.getBoolean(KEY_BATTERY_SAVER_TIP_ENABLED, true);
        highUsageEnabled = mParser.getBoolean(KEY_HIGH_USAGE_ENABLED, true);
        highUsageAppCount = mParser.getInt(KEY_HIGH_USAGE_APP_COUNT, 3);
        appRestrictionEnabled = mParser.getBoolean(KEY_APP_RESTRICTION_ENABLED, true);
        reducedBatteryEnabled = mParser.getBoolean(KEY_REDUCED_BATTERY_ENABLED, true);
        reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50);
        lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, true);
        lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 16);
    }

}
+28 −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.batterytip.detectors;

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

public interface BatteryTipDetector {
    /**
     * Detect and update the status of {@link BatteryTip}
     *
     * @return a not null {@link BatteryTip}
     */
    BatteryTip detect();
}
Loading