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

Commit 87b883c4 authored by Kweku Adams's avatar Kweku Adams Committed by Automerger Merge Worker
Browse files

Use BatteryManagerInternal to determine low battery. am: 93735581 am: 8cdf84dd

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1702028

Change-Id: Ie60678098a4430f28d6de23f7dc4f3c805e1f407
parents 0fed008c 8cdf84dd
Loading
Loading
Loading
Loading
+4 −28
Original line number Diff line number Diff line
@@ -26,10 +26,8 @@ import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.os.BatteryManager;
import android.os.BatteryManagerInternal;
import android.os.Environment;
import android.os.ServiceManager;
import android.os.SystemProperties;
@@ -149,27 +147,6 @@ public class BackgroundDexOptService extends JobService {
        }
    }

    // Returns the current battery level as a 0-100 integer.
    private int getBatteryLevel() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent intent = registerReceiver(null, filter);
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);

        if (!present) {
            // No battery, treat as if 100%, no possibility of draining battery.
            return 100;
        }

        if (level < 0 || scale <= 0) {
            // Battery data unavailable. This should never happen, so assume the worst.
            return 0;
        }

        return (100 * level / scale);
    }

    private long getLowStorageThreshold(Context context) {
        @SuppressWarnings("deprecation")
        final long lowThreshold = StorageManager.from(context).getStorageLowBytes(mDataDir);
@@ -198,9 +175,8 @@ public class BackgroundDexOptService extends JobService {

    private void postBootUpdate(JobParameters jobParams, PackageManagerService pm,
            ArraySet<String> pkgs) {
        // Load low battery threshold from the system config. This is a 0-100 integer.
        final int lowBatteryThreshold = getResources().getInteger(
                com.android.internal.R.integer.config_lowBatteryWarningLevel);
        final BatteryManagerInternal batteryManagerInternal =
                LocalServices.getService(BatteryManagerInternal.class);
        final long lowThreshold = getLowStorageThreshold(this);

        mAbortPostBootUpdate.set(false);
@@ -215,7 +191,7 @@ public class BackgroundDexOptService extends JobService {
                // Different job, which supersedes this one, is running.
                break;
            }
            if (getBatteryLevel() < lowBatteryThreshold) {
            if (batteryManagerInternal.getBatteryLevelLow()) {
                // Rather bail than completely drain the battery.
                break;
            }