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

Commit 5a31745f authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Nishith Khanna
Browse files

Updater: Utils: Move battery level check

Change-Id: I94447b33e717bd05f8a93d8a7784c862a07f8c3a
parent 91ad35ad
Loading
Loading
Loading
Loading
+1 −22
Original line number Diff line number Diff line
@@ -16,10 +16,8 @@
package org.lineageos.updater;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.text.SpannableString;
import android.text.format.Formatter;
@@ -72,10 +70,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.

    private static final String TAG = "UpdateListAdapter";

    private static final int BATTERY_PLUGGED_ANY = BatteryManager.BATTERY_PLUGGED_AC
            | BatteryManager.BATTERY_PLUGGED_USB
            | BatteryManager.BATTERY_PLUGGED_WIRELESS;

    private final float mAlphaDisabledValue;

    private List<String> mDownloadIds;
@@ -513,7 +507,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.

    private AlertDialog.Builder getInstallDialog(final String downloadId) {
        Resources resources = mActivity.getResources();
        if (!isBatteryLevelOk()) {
        if (!Utils.isBatteryLevelOk(mActivity)) {
            String message = resources.getString(R.string.dialog_battery_low_message_pct,
                    resources.getInteger(R.integer.battery_ok_percentage_discharging),
                    resources.getInteger(R.integer.battery_ok_percentage_charging));
@@ -648,21 +642,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
        }
    }

    private boolean isBatteryLevelOk() {
        Intent intent = mActivity.registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if (intent == null || !intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)) {
            return true;
        }
        int percent = Math.round(100.f * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100) /
                intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        int required = (plugged & BATTERY_PLUGGED_ANY) != 0 ?
                mActivity.getResources().getInteger(R.integer.battery_ok_percentage_charging) :
                mActivity.getResources().getInteger(R.integer.battery_ok_percentage_discharging);
        return percent >= required;
    }

    private static boolean isScratchMounted() {
        try (Stream<String> lines = Files.lines(Path.of("/proc/mounts"))) {
            return lines.anyMatch(x -> x.split(" ")[1].equals("/mnt/scratch"));
+22 −1
Original line number Diff line number Diff line
@@ -22,11 +22,11 @@ import static android.os.SystemUpdateManager.STATUS_IDLE;
import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;

import android.app.AlarmManager;
import android.os.Build;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
@@ -34,6 +34,8 @@ import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
@@ -74,6 +76,10 @@ import java.util.zip.ZipFile;

public class Utils {

    private static final int BATTERY_PLUGGED_ANY = BatteryManager.BATTERY_PLUGGED_AC
            | BatteryManager.BATTERY_PLUGGED_USB
            | BatteryManager.BATTERY_PLUGGED_WIRELESS;

    private static final String TAG = "Utils";
    private static final String CONTENT_URI_PATH = "content://custom.setting.Provider.OTA_SERVER/cte";

@@ -534,6 +540,21 @@ public class Utils {
        }
    }

    public static boolean isBatteryLevelOk(Context context) {
        Intent intent = context.registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if (intent == null || !intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)) {
            return true;
        }
        int percent = Math.round(100.f * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100) /
                intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        int required = (plugged & BATTERY_PLUGGED_ANY) != 0 ?
                context.getResources().getInteger(R.integer.battery_ok_percentage_charging) :
                context.getResources().getInteger(R.integer.battery_ok_percentage_discharging);
        return percent >= required;
    }

    public static boolean isUpdateCheckEnabled(Context context) {
        return getUpdateCheckSetting(context) != Constants.AUTO_UPDATES_CHECK_INTERVAL_NEVER;
    }