Named wake lock reasons
Fixes: 121039718 Test: atest ScrimControllerTest Test: atest KeepAwakeAnimationListenerTest Test: atest SettableWakeLockTest Test: atest WakeLockTest Test: adb shell dumpsys activity service com.android.systemui Change-Id: I612874da597fba6974edd1f702932ee1a5629fc7
This commit is contained in:
@@ -43,6 +43,8 @@ public class DozeMachine {
|
||||
|
||||
static final String TAG = "DozeMachine";
|
||||
static final boolean DEBUG = DozeService.DEBUG;
|
||||
private static final String REASON_CHANGE_STATE = "DozeMachine#requestState";
|
||||
private static final String REASON_HELD_FOR_STATE = "DozeMachine#heldForState";
|
||||
|
||||
public enum State {
|
||||
/** Default state. Transition to INITIALIZED to get Doze going. */
|
||||
@@ -167,14 +169,14 @@ public class DozeMachine {
|
||||
boolean runNow = !isExecutingTransition();
|
||||
mQueuedRequests.add(requestedState);
|
||||
if (runNow) {
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire(REASON_CHANGE_STATE);
|
||||
for (int i = 0; i < mQueuedRequests.size(); i++) {
|
||||
// Transitions in Parts can call back into requestState, which will
|
||||
// cause mQueuedRequests to grow.
|
||||
transitionTo(mQueuedRequests.get(i), pulseReason);
|
||||
}
|
||||
mQueuedRequests.clear();
|
||||
mWakeLock.release();
|
||||
mWakeLock.release(REASON_CHANGE_STATE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,10 +313,10 @@ public class DozeMachine {
|
||||
private void updateWakeLockState(State newState) {
|
||||
boolean staysAwake = newState.staysAwake();
|
||||
if (mWakeLockHeldForCurrentState && !staysAwake) {
|
||||
mWakeLock.release();
|
||||
mWakeLock.release(REASON_HELD_FOR_STATE);
|
||||
mWakeLockHeldForCurrentState = false;
|
||||
} else if (!mWakeLockHeldForCurrentState && staysAwake) {
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire(REASON_HELD_FOR_STATE);
|
||||
mWakeLockHeldForCurrentState = true;
|
||||
}
|
||||
}
|
||||
@@ -336,6 +338,7 @@ public class DozeMachine {
|
||||
public void dump(PrintWriter pw) {
|
||||
pw.print(" state="); pw.println(mState);
|
||||
pw.print(" wakeLockHeldForCurrentState="); pw.println(mWakeLockHeldForCurrentState);
|
||||
pw.print(" wakeLock="); pw.println(mWakeLock);
|
||||
pw.println("Parts:");
|
||||
for (Part p : mParts) {
|
||||
p.dump(pw);
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DozeScreenState implements DozeMachine.Part {
|
||||
mDozeService = service;
|
||||
mHandler = handler;
|
||||
mParameters = parameters;
|
||||
mWakeLock = new SettableWakeLock(wakeLock);
|
||||
mWakeLock = new SettableWakeLock(wakeLock, TAG);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -348,7 +348,7 @@ public class DozeTriggers implements DozeMachine.Part {
|
||||
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0,
|
||||
mHandler);
|
||||
mHandler.postDelayed(this, TIMEOUT_DELAY_MS);
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire(TAG);
|
||||
mRegistered = true;
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ public class DozeTriggers implements DozeMachine.Part {
|
||||
}
|
||||
onProximityResult(result);
|
||||
if (wasRegistered) {
|
||||
mWakeLock.release();
|
||||
mWakeLock.release(TAG);
|
||||
}
|
||||
mFinished = true;
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ public class KeyguardIndicationController implements StateListener {
|
||||
mTextView.getTextColors() : ColorStateList.valueOf(Color.WHITE);
|
||||
mDisclosure = indicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure);
|
||||
mLockIcon = lockIcon;
|
||||
mWakeLock = new SettableWakeLock(wakeLock);
|
||||
mWakeLock = new SettableWakeLock(wakeLock, TAG);
|
||||
|
||||
Resources res = context.getResources();
|
||||
mSlowThreshold = res.getInteger(R.integer.config_chargingSlowlyThreshold);
|
||||
|
||||
@@ -340,7 +340,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
|
||||
if (!mWakeLockHeld) {
|
||||
if (mWakeLock != null) {
|
||||
mWakeLockHeld = true;
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire(TAG);
|
||||
} else {
|
||||
Log.w(TAG, "Cannot hold wake lock, it has not been set yet");
|
||||
}
|
||||
@@ -654,7 +654,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
|
||||
|
||||
private void onFinished(Callback callback) {
|
||||
if (mWakeLockHeld) {
|
||||
mWakeLock.release();
|
||||
mWakeLock.release(TAG);
|
||||
mWakeLockHeld = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,30 +23,34 @@ import android.os.Handler;
|
||||
*/
|
||||
public class DelayedWakeLock implements WakeLock {
|
||||
|
||||
private static final String TO_STRING_PREFIX = "[DelayedWakeLock] ";
|
||||
private static final long RELEASE_DELAY_MS = 100;
|
||||
|
||||
private final Handler mHandler;
|
||||
private final WakeLock mInner;
|
||||
private final Runnable mRelease;
|
||||
|
||||
public DelayedWakeLock(Handler h, WakeLock inner) {
|
||||
mHandler = h;
|
||||
mInner = inner;
|
||||
mRelease = mInner::release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acquire() {
|
||||
mInner.acquire();
|
||||
public void acquire(String why) {
|
||||
mInner.acquire(why);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
mHandler.postDelayed(mRelease, RELEASE_DELAY_MS);
|
||||
public void release(String why) {
|
||||
mHandler.postDelayed(() -> mInner.release(why), RELEASE_DELAY_MS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Runnable wrap(Runnable r) {
|
||||
return WakeLock.wrapImpl(this, r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return TO_STRING_PREFIX + mInner;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.android.systemui.util.Assert;
|
||||
|
||||
public class KeepAwakeAnimationListener extends AnimatorListenerAdapter
|
||||
implements Animation.AnimationListener {
|
||||
private static final String TAG = "KeepAwakeAnimListener";
|
||||
@VisibleForTesting
|
||||
static WakeLock sWakeLock;
|
||||
|
||||
@@ -63,11 +64,11 @@ public class KeepAwakeAnimationListener extends AnimatorListenerAdapter
|
||||
|
||||
private void onStart() {
|
||||
Assert.isMainThread();
|
||||
sWakeLock.acquire();
|
||||
sWakeLock.acquire(TAG);
|
||||
}
|
||||
|
||||
private void onEnd() {
|
||||
Assert.isMainThread();
|
||||
sWakeLock.release();
|
||||
sWakeLock.release(TAG);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,13 +21,15 @@ import com.android.internal.util.Preconditions;
|
||||
public class SettableWakeLock {
|
||||
|
||||
private final WakeLock mInner;
|
||||
private final String mWhy;
|
||||
|
||||
private boolean mAcquired;
|
||||
|
||||
public SettableWakeLock(WakeLock inner) {
|
||||
public SettableWakeLock(WakeLock inner, String why) {
|
||||
Preconditions.checkNotNull(inner, "inner wakelock required");
|
||||
|
||||
mInner = inner;
|
||||
mWhy = why;
|
||||
}
|
||||
|
||||
public synchronized boolean isAcquired() {
|
||||
@@ -37,9 +39,9 @@ public class SettableWakeLock {
|
||||
public synchronized void setAcquired(boolean acquired) {
|
||||
if (mAcquired != acquired) {
|
||||
if (acquired) {
|
||||
mInner.acquire();
|
||||
mInner.acquire(mWhy);
|
||||
} else {
|
||||
mInner.release();
|
||||
mInner.release(mWhy);
|
||||
}
|
||||
mAcquired = acquired;
|
||||
}
|
||||
|
||||
@@ -18,17 +18,29 @@ package com.android.systemui.util.wakelock;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/** WakeLock wrapper for testability */
|
||||
public interface WakeLock {
|
||||
|
||||
/** @see android.os.PowerManager.WakeLock#acquire() */
|
||||
void acquire();
|
||||
static final String TAG = "WakeLock";
|
||||
static final String REASON_WRAP = "wrap";
|
||||
|
||||
/** @see android.os.PowerManager.WakeLock#release() */
|
||||
void release();
|
||||
/**
|
||||
* @param why A tag that will be saved for sysui dumps.
|
||||
* @see android.os.PowerManager.WakeLock#acquire()
|
||||
**/
|
||||
void acquire(String why);
|
||||
|
||||
/**
|
||||
* @param why Same tag used in {@link #acquire(String)}
|
||||
* @see android.os.PowerManager.WakeLock#release()
|
||||
**/
|
||||
void release(String why);
|
||||
|
||||
/** @see android.os.PowerManager.WakeLock#wrap(Runnable) */
|
||||
Runnable wrap(Runnable r);
|
||||
@@ -44,25 +56,38 @@ public interface WakeLock {
|
||||
}
|
||||
|
||||
static Runnable wrapImpl(WakeLock w, Runnable r) {
|
||||
w.acquire();
|
||||
w.acquire(REASON_WRAP);
|
||||
return () -> {
|
||||
try {
|
||||
r.run();
|
||||
} finally {
|
||||
w.release();
|
||||
w.release(REASON_WRAP);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static WakeLock wrap(final PowerManager.WakeLock inner) {
|
||||
return new WakeLock() {
|
||||
private final HashMap<String, Integer> mActiveClients = new HashMap<>();
|
||||
|
||||
/** @see PowerManager.WakeLock#acquire() */
|
||||
public void acquire() {
|
||||
public void acquire(String why) {
|
||||
mActiveClients.putIfAbsent(why, 0);
|
||||
mActiveClients.put(why, mActiveClients.get(why) + 1);
|
||||
inner.acquire();
|
||||
}
|
||||
|
||||
/** @see PowerManager.WakeLock#release() */
|
||||
public void release() {
|
||||
public void release(String why) {
|
||||
Integer count = mActiveClients.get(why);
|
||||
if (count == null) {
|
||||
Log.wtf(TAG, "Releasing WakeLock with invalid reason: " + why,
|
||||
new Throwable());
|
||||
} else if (count == 1) {
|
||||
mActiveClients.remove(why);
|
||||
} else {
|
||||
mActiveClients.put(why, count - 1);
|
||||
}
|
||||
inner.release();
|
||||
}
|
||||
|
||||
@@ -70,6 +95,11 @@ public interface WakeLock {
|
||||
public Runnable wrap(Runnable runnable) {
|
||||
return wrapImpl(this, runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "active clients= " + mActiveClients.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import static com.android.systemui.statusbar.phone.ScrimController.VISIBILITY_SE
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
@@ -441,10 +442,10 @@ public class ScrimControllerTest extends SysuiTestCase {
|
||||
@Test
|
||||
public void testHoldsWakeLock_whenAOD() {
|
||||
mScrimController.transitionTo(ScrimState.AOD);
|
||||
verify(mWakeLock).acquire();
|
||||
verify(mWakeLock, never()).release();
|
||||
verify(mWakeLock).acquire(anyString());
|
||||
verify(mWakeLock, never()).release(anyString());
|
||||
mScrimController.finishAnimationsImmediately();
|
||||
verify(mWakeLock).release();
|
||||
verify(mWakeLock).release(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -471,10 +472,10 @@ public class ScrimControllerTest extends SysuiTestCase {
|
||||
reset(mWakeLock);
|
||||
|
||||
mScrimController.onHideWallpaperTimeout();
|
||||
verify(mWakeLock).acquire();
|
||||
verify(mWakeLock, never()).release();
|
||||
verify(mWakeLock).acquire(anyString());
|
||||
verify(mWakeLock, never()).release(anyString());
|
||||
mScrimController.finishAnimationsImmediately();
|
||||
verify(mWakeLock).release();
|
||||
verify(mWakeLock).release(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -486,10 +487,10 @@ public class ScrimControllerTest extends SysuiTestCase {
|
||||
reset(mWakeLock);
|
||||
|
||||
mScrimController.onHideWallpaperTimeout();
|
||||
verify(mWakeLock).acquire();
|
||||
verify(mWakeLock, never()).release();
|
||||
verify(mWakeLock).acquire(anyString());
|
||||
verify(mWakeLock, never()).release(anyString());
|
||||
mScrimController.finishAnimationsImmediately();
|
||||
verify(mWakeLock).release();
|
||||
verify(mWakeLock).release(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.android.systemui.util.wakelock;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -52,11 +53,11 @@ public class KeepAwakeAnimationListenerTest extends SysuiTestCase {
|
||||
@Test
|
||||
public void onAnimationStart_holdsWakeLock() {
|
||||
mKeepAwakeAnimationListener.onAnimationStart((Animator) null);
|
||||
verify(mWakeLock).acquire();
|
||||
verify(mWakeLock, never()).release();
|
||||
verify(mWakeLock).acquire(anyString());
|
||||
verify(mWakeLock, never()).release(anyString());
|
||||
|
||||
mKeepAwakeAnimationListener.onAnimationEnd((Animator) null);
|
||||
verify(mWakeLock).release();
|
||||
verify(mWakeLock).release(anyString());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
|
||||
@@ -40,7 +40,7 @@ public class SettableWakeLockTest extends SysuiTestCase {
|
||||
@Before
|
||||
public void setup() {
|
||||
mFake = new WakeLockFake();
|
||||
mSettable = new SettableWakeLock(mFake);
|
||||
mSettable = new SettableWakeLock(mFake, "Fake");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -23,24 +23,24 @@ public class WakeLockFake implements WakeLock {
|
||||
private int mAcquired = 0;
|
||||
|
||||
@Override
|
||||
public void acquire() {
|
||||
public void acquire(String why) {
|
||||
mAcquired++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
public void release(String why) {
|
||||
Preconditions.checkState(mAcquired > 0);
|
||||
mAcquired--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Runnable wrap(Runnable runnable) {
|
||||
acquire();
|
||||
acquire(WakeLockFake.class.getSimpleName());
|
||||
return () -> {
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
release();
|
||||
release(WakeLockFake.class.getSimpleName());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -19,9 +19,7 @@ package com.android.systemui.util.wakelock;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
@@ -36,6 +34,7 @@ import org.junit.runner.RunWith;
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class WakeLockTest extends SysuiTestCase {
|
||||
|
||||
private static final String WHY = "test";
|
||||
WakeLock mWakeLock;
|
||||
PowerManager.WakeLock mInner;
|
||||
|
||||
@@ -58,22 +57,22 @@ public class WakeLockTest extends SysuiTestCase {
|
||||
|
||||
@Test
|
||||
public void wakeLock_acquire() {
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire(WHY);
|
||||
assertTrue(mInner.isHeld());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wakeLock_release() {
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.release();
|
||||
mWakeLock.acquire(WHY);
|
||||
mWakeLock.release(WHY);
|
||||
assertFalse(mInner.isHeld());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wakeLock_refCounted() {
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.acquire();
|
||||
mWakeLock.release();
|
||||
mWakeLock.acquire(WHY);
|
||||
mWakeLock.acquire(WHY);
|
||||
mWakeLock.release(WHY);
|
||||
assertTrue(mInner.isHeld());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user