Handle charging state separately from temporary parole

am: 693d869b32

Change-Id: I0f5eae05922bea1d4285504cb1dce41b56f98ad5
This commit is contained in:
Amith Yamasani
2016-09-15 22:12:55 +00:00
committed by android-build-merger

View File

@@ -94,7 +94,7 @@ public class UsageStatsService extends SystemService implements
static final String TAG = "UsageStatsService"; 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; static final boolean COMPRESS_TIME = false;
private static final long TEN_SECONDS = 10 * 1000; private static final long TEN_SECONDS = 10 * 1000;
@@ -139,8 +139,8 @@ public class UsageStatsService extends SystemService implements
long mSystemTimeSnapshot; long mSystemTimeSnapshot;
boolean mAppIdleEnabled; boolean mAppIdleEnabled;
boolean mAppIdleParoled; boolean mAppIdleTempParoled;
private boolean mScreenOn; boolean mCharging;
private long mLastAppIdleParoledTime; private long mLastAppIdleParoledTime;
private volatile boolean mPendingOneTimeCheckIdleStates; private volatile boolean mPendingOneTimeCheckIdleStates;
@@ -191,7 +191,7 @@ public class UsageStatsService extends SystemService implements
mAppIdleEnabled = getContext().getResources().getBoolean( mAppIdleEnabled = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_enableAutoPowerModes); com.android.internal.R.bool.config_enableAutoPowerModes);
if (mAppIdleEnabled) { if (mAppIdleEnabled) {
IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING); IntentFilter deviceStates = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
deviceStates.addAction(BatteryManager.ACTION_DISCHARGING); deviceStates.addAction(BatteryManager.ACTION_DISCHARGING);
deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
getContext().registerReceiver(new DeviceStateReceiver(), deviceStates); getContext().registerReceiver(new DeviceStateReceiver(), deviceStates);
@@ -237,7 +237,7 @@ public class UsageStatsService extends SystemService implements
mSystemServicesReady = true; mSystemServicesReady = true;
} else if (phase == PHASE_BOOT_COMPLETED) { } 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 @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
final String action = intent.getAction(); final String action = intent.getAction();
if (BatteryManager.ACTION_CHARGING.equals(action) if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
|| BatteryManager.ACTION_DISCHARGING.equals(action)) { setChargingState(intent.getIntExtra("plugged", 0) != 0);
setAppIdleParoled(BatteryManager.ACTION_CHARGING.equals(action));
} else if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) { } else if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) {
onDeviceIdleModeChanged(); 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 */ /** Paroled here means temporary pardon from being inactive */
void setAppIdleParoled(boolean paroled) { void setAppIdleParoled(boolean paroled) {
synchronized (mLock) { synchronized (mLock) {
if (mAppIdleParoled != paroled) { if (mAppIdleTempParoled != paroled) {
mAppIdleParoled = paroled; mAppIdleTempParoled = paroled;
if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleParoled); if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleTempParoled);
if (paroled) { if (paroled) {
postParoleEndTimeout(); postParoleEndTimeout();
} else { } else {
@@ -393,6 +401,12 @@ public class UsageStatsService extends SystemService implements
} }
} }
boolean isParoledOrCharging() {
synchronized (mLock) {
return mAppIdleTempParoled || mCharging;
}
}
private void postNextParoleTimeout() { private void postNextParoleTimeout() {
if (DEBUG) Slog.d(TAG, "Posting MSG_CHECK_PAROLE_TIMEOUT"); if (DEBUG) Slog.d(TAG, "Posting MSG_CHECK_PAROLE_TIMEOUT");
mHandler.removeMessages(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 */ /** Check if it's been a while since last parole and let idle apps do some work */
void checkParoleTimeout() { void checkParoleTimeout() {
synchronized (mLock) { synchronized (mLock) {
if (!mAppIdleParoled) { if (!mAppIdleTempParoled) {
final long timeSinceLastParole = checkAndGetTimeLocked() - mLastAppIdleParoledTime; final long timeSinceLastParole = checkAndGetTimeLocked() - mLastAppIdleParoledTime;
if (timeSinceLastParole > mAppIdleParoleIntervalMillis) { if (timeSinceLastParole > mAppIdleParoleIntervalMillis) {
if (DEBUG) Slog.d(TAG, "Crossed default parole interval"); 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) { boolean isAppIdleFilteredOrParoled(String packageName, int userId, long elapsedRealtime) {
if (mAppIdleParoled) { if (isParoledOrCharging()) {
return false; return false;
} }
return isAppIdleFiltered(packageName, getAppId(packageName), userId, elapsedRealtime); return isAppIdleFiltered(packageName, getAppId(packageName), userId, elapsedRealtime);
@@ -989,8 +1003,9 @@ public class UsageStatsService extends SystemService implements
} }
void informParoleStateChanged() { void informParoleStateChanged() {
final boolean paroled = isParoledOrCharging();
for (AppIdleStateChangeListener listener : mPackageAccessListeners) { for (AppIdleStateChangeListener listener : mPackageAccessListeners) {
listener.onParoleStateChanged(mAppIdleParoled); listener.onParoleStateChanged(paroled);
} }
} }
@@ -1072,8 +1087,8 @@ public class UsageStatsService extends SystemService implements
pw.println(); pw.println();
pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled); pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled);
pw.print(" mAppIdleParoled="); pw.print(mAppIdleParoled); pw.print(" mAppIdleTempParoled="); pw.print(mAppIdleTempParoled);
pw.print(" mScreenOn="); pw.println(mScreenOn); pw.print(" mCharging="); pw.print(mCharging);
pw.print(" mLastAppIdleParoledTime="); pw.print(" mLastAppIdleParoledTime=");
TimeUtils.formatDuration(mLastAppIdleParoledTime, pw); TimeUtils.formatDuration(mLastAppIdleParoledTime, pw);
pw.println(); pw.println();
@@ -1139,7 +1154,8 @@ public class UsageStatsService extends SystemService implements
break; break;
case MSG_PAROLE_STATE_CHANGED: 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(); informParoleStateChanged();
break; break;
@@ -1466,7 +1482,7 @@ public class UsageStatsService extends SystemService implements
@Override @Override
public boolean isAppIdleParoleOn() { public boolean isAppIdleParoleOn() {
return mAppIdleParoled; return isParoledOrCharging();
} }
@Override @Override