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

Commit c06fbb12 authored by Jason Monk's avatar Jason Monk
Browse files

Move battery saver notification to QS

Remove the battery saver notification and instead create a detail
panel within QS that allows it to be turned on and off.

Change-Id: I54654d26183586fa171fda04877a840701f8ef33
parent 3d4f83c0
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import android.view.View;
 */
public class MetricsLogger implements MetricsConstants {
    // Temporary constants go here, to await migration to MetricsConstants.
    public static final int QS_LOCK_TILE = 257;
    public static final int QS_USER_TILE = 258;
    public static final int QS_BATTERY_TILE = 259;
    public static final int NOTIFICATION_ZEN_MODE_VISUAL_INTERRUPTIONS = 260;
@@ -62,6 +61,8 @@ public class MetricsLogger implements MetricsConstants {
     * credentials UI.
     */
    public static final int PROFILE_CHALLENGE = 271;
    public static final int ACTION_WINDOW_DOCK_SWIPE = 272;
    public static final int QS_BATTERY_DETAIL = 273;

    public static void visible(Context context, int category) throws IllegalArgumentException {
        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
+37 −0
Original line number Diff line number Diff line
@@ -726,4 +726,41 @@
    <!-- Summary shown for color space correction preference when its value is overridden by another preference [CHAR LIMIT=35] -->
    <string name="daltonizer_type_overridden">Overridden by <xliff:g id="title" example="Simulate color space">%1$s</xliff:g></string>

    <!-- [CHAR_LIMIT=40] Label for battery level chart when discharging with duration -->
    <string name="power_discharging_duration"><xliff:g id="level">%1$s</xliff:g>
        - approx. <xliff:g id="time">%2$s</xliff:g> left</string>

    <!-- [CHAR_LIMIT=40] Label for battery level chart when charging -->
    <string name="power_charging"><xliff:g id="level">%1$s</xliff:g> -
            <xliff:g id="state">%2$s</xliff:g></string>
    <!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
    <string name="power_charging_duration"><xliff:g id="level">%1$s</xliff:g> -
            <xliff:g id="time">%2$s</xliff:g> until full</string>
    <!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
    <string name="power_charging_duration_ac"><xliff:g id="level">%1$s</xliff:g> -
            <xliff:g id="time">%2$s</xliff:g> until full on AC</string>
    <!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
    <string name="power_charging_duration_usb"><xliff:g id="level">%1$s</xliff:g> -
            <xliff:g id="time">%2$s</xliff:g> until full over USB</string>
    <!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
    <string name="power_charging_duration_wireless"><xliff:g id="level">%1$s</xliff:g> -
            <xliff:g id="time">%2$s</xliff:g> until full from wireless</string>

    <!-- Battery Info screen. Value for a status item.  Used for diagnostic info screens, precise translation isn't needed -->
    <string name="battery_info_status_unknown">Unknown</string>
    <!-- [CHAR_LIMIT=20] Battery use screen.  Battery status shown in chart label when charging from an unknown source.  -->
    <string name="battery_info_status_charging">Charging</string>
    <!-- [CHAR_LIMIT=20] Battery use screen.  Battery status shown in chart label when charging on AC.  -->
    <string name="battery_info_status_charging_ac">Charging on AC</string>
    <!-- [CHAR_LIMIT=20] Battery use screen.  Battery status shown in chart label when charging over USB.  -->
    <string name="battery_info_status_charging_usb">Charging over USB</string>
    <!-- [CHAR_LIMIT=20] Battery use screen.  Battery status shown in chart label when charging over a wireless connection.  -->
    <string name="battery_info_status_charging_wireless">Charging wirelessly</string>
    <!-- Battery Info screen. Value for a status item.  Used for diagnostic info screens, precise translation isn't needed -->
    <string name="battery_info_status_discharging">Not charging</string>
    <!-- Battery Info screen. Value for a status item.  Used for diagnostic info screens, precise translation isn't needed -->
    <string name="battery_info_status_not_charging">Not charging</string>
    <!-- Battery Info screen. Value for a status item.  Used for diagnostic info screens, precise translation isn't needed -->
    <string name="battery_info_status_full">Full</string>

</resources>
+107 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.settingslib;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.Formatter;
import com.android.internal.os.BatteryStatsHelper;

public class BatteryInfo {

    public String mChargeLabelString;
    public int mBatteryLevel;
    public boolean mDischarging = true;
    public long remainingTimeUs = 0;

    public interface Callback {
        void onBatteryInfoLoaded(BatteryInfo info);
    }

    public static void getBatteryInfo(final Context context, final Callback callback) {
        new AsyncTask<Void, Void, BatteryStats>() {
            @Override
            protected BatteryStats doInBackground(Void... params) {
                BatteryStatsHelper statsHelper = new BatteryStatsHelper(context, true);
                statsHelper.create((Bundle) null);
                return statsHelper.getStats();
            }

            @Override
            protected void onPostExecute(BatteryStats batteryStats) {
                final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
                Intent batteryBroadcast = context.registerReceiver(null,
                        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
                BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context,
                        batteryBroadcast, batteryStats, elapsedRealtimeUs);
                callback.onBatteryInfoLoaded(batteryInfo);
            }
        }.execute();
    }

    public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
            BatteryStats stats, long elapsedRealtimeUs) {
        BatteryInfo info = new BatteryInfo();
        info.mBatteryLevel = Utils.getBatteryLevel(batteryBroadcast);
        String batteryPercentString = Utils.formatPercentage(info.mBatteryLevel);
        if (batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) == 0) {
            final long drainTime = stats.computeBatteryTimeRemaining(elapsedRealtimeUs);
            if (drainTime > 0) {
                info.remainingTimeUs = drainTime;
                String timeString = Formatter.formatShortElapsedTime(context,
                        drainTime / 1000);
                info.mChargeLabelString = context.getResources().getString(
                        R.string.power_discharging_duration, batteryPercentString, timeString);
            } else {
                info.mChargeLabelString = batteryPercentString;
            }
        } else {
            final long chargeTime = stats.computeChargeTimeRemaining(elapsedRealtimeUs);
            final String statusLabel = Utils.getBatteryStatus(
                    context.getResources(), batteryBroadcast);
            final int status = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_STATUS,
                    BatteryManager.BATTERY_STATUS_UNKNOWN);
            if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
                info.mDischarging = false;
                info.remainingTimeUs = chargeTime;
                String timeString = Formatter.formatShortElapsedTime(context,
                        chargeTime / 1000);
                int plugType = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
                int resId;
                if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
                    resId = R.string.power_charging_duration_ac;
                } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
                    resId = R.string.power_charging_duration_usb;
                } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
                    resId = R.string.power_charging_duration_wireless;
                } else {
                    resId = R.string.power_charging_duration;
                }
                info.mChargeLabelString = context.getResources().getString(
                        resId, batteryPercentString, timeString);
            } else {
                info.mChargeLabelString = context.getResources().getString(
                        R.string.power_charging, batteryPercentString, statusLabel);
            }
        }
        return info;
    }
}
+59 −2
Original line number Diff line number Diff line
package com.android.settingslib;

import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.os.UserManager;

import com.android.internal.util.UserIcons;
import com.android.settingslib.drawable.CircleFramedDrawable;

public final class Utils {
import java.text.NumberFormat;

public class Utils {

    /**
     * Return string resource that best describes combination of tethering
@@ -81,4 +85,57 @@ public final class Utils {
        return CircleFramedDrawable.getInstance(context, UserIcons.convertToBitmap(
                UserIcons.getDefaultUserIcon(user.id, /* light= */ false)));
    }

    /** Formats the ratio of amount/total as a percentage. */
    public static String formatPercentage(long amount, long total) {
        return formatPercentage(((double) amount) / total);
    }

    /** Formats an integer from 0..100 as a percentage. */
    public static String formatPercentage(int percentage) {
        return formatPercentage(((double) percentage) / 100.0);
    }

    /** Formats a double from 0.0..1.0 as a percentage. */
    private static String formatPercentage(double percentage) {
      return NumberFormat.getPercentInstance().format(percentage);
    }

    public static int getBatteryLevel(Intent batteryChangedIntent) {
        int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        return (level * 100) / scale;
    }

    public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
        final Intent intent = batteryChangedIntent;

        int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
                BatteryManager.BATTERY_STATUS_UNKNOWN);
        String statusString;
        if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
            int resId;
            if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
                resId = R.string.battery_info_status_charging_ac;
            } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
                resId = R.string.battery_info_status_charging_usb;
            } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
                resId = R.string.battery_info_status_charging_wireless;
            } else {
                resId = R.string.battery_info_status_charging;
            }
            statusString = res.getString(resId);
        } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
            statusString = res.getString(R.string.battery_info_status_discharging);
        } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
            statusString = res.getString(R.string.battery_info_status_not_charging);
        } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
            statusString = res.getString(R.string.battery_info_status_full);
        } else {
            statusString = res.getString(R.string.battery_info_status_unknown);
        }

        return statusString;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -143,6 +143,9 @@
    <!-- Block notifications inline notifications -->
    <uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />

    <!-- Access battery information -->
    <uses-permission android:name="android.permission.BATTERY_STATS" />

    <application
        android:name=".SystemUIApplication"
        android:persistent="true"
Loading