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

Commit 42cdd682 authored by Amith Yamasani's avatar Amith Yamasani Committed by android-build-merger
Browse files

Handle charging state separately from temporary parole

am: 693d869b

Change-Id: I0f5eae05922bea1d4285504cb1dce41b56f98ad5
parents 75627f0a 693d869b
Loading
Loading
Loading
Loading
+35 −19
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ public class UsageStatsService extends SystemService implements

    static final String TAG = "UsageStatsService";

    static final boolean DEBUG = false;
    static final boolean DEBUG = false; // Never submit with true
    static final boolean COMPRESS_TIME = false;

    private static final long TEN_SECONDS = 10 * 1000;
@@ -139,8 +139,8 @@ public class UsageStatsService extends SystemService implements
    long mSystemTimeSnapshot;

    boolean mAppIdleEnabled;
    boolean mAppIdleParoled;
    private boolean mScreenOn;
    boolean mAppIdleTempParoled;
    boolean mCharging;
    private long mLastAppIdleParoledTime;

    private volatile boolean mPendingOneTimeCheckIdleStates;
@@ -191,7 +191,7 @@ public class UsageStatsService extends SystemService implements
        mAppIdleEnabled = getContext().getResources().getBoolean(
                com.android.internal.R.bool.config_enableAutoPowerModes);
        if (mAppIdleEnabled) {
            IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING);
            IntentFilter deviceStates = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            deviceStates.addAction(BatteryManager.ACTION_DISCHARGING);
            deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
            getContext().registerReceiver(new DeviceStateReceiver(), deviceStates);
@@ -237,7 +237,7 @@ public class UsageStatsService extends SystemService implements

            mSystemServicesReady = true;
        } else if (phase == PHASE_BOOT_COMPLETED) {
            setAppIdleParoled(getContext().getSystemService(BatteryManager.class).isCharging());
            setChargingState(getContext().getSystemService(BatteryManager.class).isCharging());
        }
    }

@@ -284,9 +284,8 @@ public class UsageStatsService extends SystemService implements
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BatteryManager.ACTION_CHARGING.equals(action)
                    || BatteryManager.ACTION_DISCHARGING.equals(action)) {
                setAppIdleParoled(BatteryManager.ACTION_CHARGING.equals(action));
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                setChargingState(intent.getIntExtra("plugged", 0) != 0);
            } else if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) {
                onDeviceIdleModeChanged();
            }
@@ -376,12 +375,21 @@ public class UsageStatsService extends SystemService implements
        }
    }

    void setChargingState(boolean charging) {
        synchronized (mLock) {
            if (mCharging != charging) {
                mCharging = charging;
                postParoleStateChanged();
            }
        }
    }

    /** Paroled here means temporary pardon from being inactive */
    void setAppIdleParoled(boolean paroled) {
        synchronized (mLock) {
            if (mAppIdleParoled != paroled) {
                mAppIdleParoled = paroled;
                if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleParoled);
            if (mAppIdleTempParoled != paroled) {
                mAppIdleTempParoled = paroled;
                if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleTempParoled);
                if (paroled) {
                    postParoleEndTimeout();
                } else {
@@ -393,6 +401,12 @@ public class UsageStatsService extends SystemService implements
        }
    }

    boolean isParoledOrCharging() {
        synchronized (mLock) {
            return mAppIdleTempParoled || mCharging;
        }
    }

    private void postNextParoleTimeout() {
        if (DEBUG) Slog.d(TAG, "Posting MSG_CHECK_PAROLE_TIMEOUT");
        mHandler.removeMessages(MSG_CHECK_PAROLE_TIMEOUT);
@@ -495,7 +509,7 @@ public class UsageStatsService extends SystemService implements
    /** Check if it's been a while since last parole and let idle apps do some work */
    void checkParoleTimeout() {
        synchronized (mLock) {
            if (!mAppIdleParoled) {
            if (!mAppIdleTempParoled) {
                final long timeSinceLastParole = checkAndGetTimeLocked() - mLastAppIdleParoledTime;
                if (timeSinceLastParole > mAppIdleParoleIntervalMillis) {
                    if (DEBUG) Slog.d(TAG, "Crossed default parole interval");
@@ -786,7 +800,7 @@ public class UsageStatsService extends SystemService implements
    }

    boolean isAppIdleFilteredOrParoled(String packageName, int userId, long elapsedRealtime) {
        if (mAppIdleParoled) {
        if (isParoledOrCharging()) {
            return false;
        }
        return isAppIdleFiltered(packageName, getAppId(packageName), userId, elapsedRealtime);
@@ -989,8 +1003,9 @@ public class UsageStatsService extends SystemService implements
    }

    void informParoleStateChanged() {
        final boolean paroled = isParoledOrCharging();
        for (AppIdleStateChangeListener listener : mPackageAccessListeners) {
            listener.onParoleStateChanged(mAppIdleParoled);
            listener.onParoleStateChanged(paroled);
        }
    }

@@ -1072,8 +1087,8 @@ public class UsageStatsService extends SystemService implements

            pw.println();
            pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled);
            pw.print(" mAppIdleParoled="); pw.print(mAppIdleParoled);
            pw.print(" mScreenOn="); pw.println(mScreenOn);
            pw.print(" mAppIdleTempParoled="); pw.print(mAppIdleTempParoled);
            pw.print(" mCharging="); pw.print(mCharging);
            pw.print(" mLastAppIdleParoledTime=");
            TimeUtils.formatDuration(mLastAppIdleParoledTime, pw);
            pw.println();
@@ -1139,7 +1154,8 @@ public class UsageStatsService extends SystemService implements
                    break;

                case MSG_PAROLE_STATE_CHANGED:
                    if (DEBUG) Slog.d(TAG, "Parole state changed: " + mAppIdleParoled);
                    if (DEBUG) Slog.d(TAG, "Parole state: " + mAppIdleTempParoled
                            + ", Charging state:" + mCharging);
                    informParoleStateChanged();
                    break;

@@ -1466,7 +1482,7 @@ public class UsageStatsService extends SystemService implements

        @Override
        public boolean isAppIdleParoleOn() {
            return mAppIdleParoled;
            return isParoledOrCharging();
        }

        @Override