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
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java b/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java
index 152b9fc..4d89a39 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java
@@ -43,6 +43,8 @@
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 @@
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 @@
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 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);
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java
index f14d396..368451a 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java
@@ -57,7 +57,7 @@
mDozeService = service;
mHandler = handler;
mParameters = parameters;
- mWakeLock = new SettableWakeLock(wakeLock);
+ mWakeLock = new SettableWakeLock(wakeLock, TAG);
}
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
index e207cb1..70bf903 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
@@ -348,7 +348,7 @@
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 @@
}
onProximityResult(result);
if (wasRegistered) {
- mWakeLock.release();
+ mWakeLock.release(TAG);
}
mFinished = true;
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
index 164f582..a9fe3b2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
@@ -130,7 +130,7 @@
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);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
index ee1e3c0..7bbd3b5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
@@ -340,7 +340,7 @@
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 @@
private void onFinished(Callback callback) {
if (mWakeLockHeld) {
- mWakeLock.release();
+ mWakeLock.release(TAG);
mWakeLockHeld = false;
}
diff --git a/packages/SystemUI/src/com/android/systemui/util/wakelock/DelayedWakeLock.java b/packages/SystemUI/src/com/android/systemui/util/wakelock/DelayedWakeLock.java
index b835909..7991e38 100644
--- a/packages/SystemUI/src/com/android/systemui/util/wakelock/DelayedWakeLock.java
+++ b/packages/SystemUI/src/com/android/systemui/util/wakelock/DelayedWakeLock.java
@@ -23,30 +23,34 @@
*/
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;
+ }
}
diff --git a/packages/SystemUI/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListener.java b/packages/SystemUI/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListener.java
index a54b0e1..283be86 100644
--- a/packages/SystemUI/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListener.java
+++ b/packages/SystemUI/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListener.java
@@ -26,6 +26,7 @@
public class KeepAwakeAnimationListener extends AnimatorListenerAdapter
implements Animation.AnimationListener {
+ private static final String TAG = "KeepAwakeAnimListener";
@VisibleForTesting
static WakeLock sWakeLock;
@@ -63,11 +64,11 @@
private void onStart() {
Assert.isMainThread();
- sWakeLock.acquire();
+ sWakeLock.acquire(TAG);
}
private void onEnd() {
Assert.isMainThread();
- sWakeLock.release();
+ sWakeLock.release(TAG);
}
}
diff --git a/packages/SystemUI/src/com/android/systemui/util/wakelock/SettableWakeLock.java b/packages/SystemUI/src/com/android/systemui/util/wakelock/SettableWakeLock.java
index 13729df..a797287 100644
--- a/packages/SystemUI/src/com/android/systemui/util/wakelock/SettableWakeLock.java
+++ b/packages/SystemUI/src/com/android/systemui/util/wakelock/SettableWakeLock.java
@@ -21,13 +21,15 @@
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 synchronized void setAcquired(boolean acquired) {
if (mAcquired != acquired) {
if (acquired) {
- mInner.acquire();
+ mInner.acquire(mWhy);
} else {
- mInner.release();
+ mInner.release(mWhy);
}
mAcquired = acquired;
}
diff --git a/packages/SystemUI/src/com/android/systemui/util/wakelock/WakeLock.java b/packages/SystemUI/src/com/android/systemui/util/wakelock/WakeLock.java
index b0f1b54..bebc20b 100644
--- a/packages/SystemUI/src/com/android/systemui/util/wakelock/WakeLock.java
+++ b/packages/SystemUI/src/com/android/systemui/util/wakelock/WakeLock.java
@@ -18,17 +18,29 @@
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 @@
}
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 Runnable wrap(Runnable runnable) {
return wrapImpl(this, runnable);
}
+
+ @Override
+ public String toString() {
+ return "active clients= " + mActiveClients.toString();
+ }
};
}
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
index c20d37f..e4da859 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
@@ -23,6 +23,7 @@
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 @@
@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 @@
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 @@
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
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListenerTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListenerTest.java
index ab9b0c9..0792a05 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListenerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/KeepAwakeAnimationListenerTest.java
@@ -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 @@
@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)
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java
index 39316ed..f739fe6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java
@@ -40,7 +40,7 @@
@Before
public void setup() {
mFake = new WakeLockFake();
- mSettable = new SettableWakeLock(mFake);
+ mSettable = new SettableWakeLock(mFake, "Fake");
}
@Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
index 4cefb99..ef20df2 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
@@ -23,24 +23,24 @@
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());
}
};
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java
index d925364..4425def 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java
@@ -19,9 +19,7 @@
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 @@
@RunWith(AndroidJUnit4.class)
public class WakeLockTest extends SysuiTestCase {
+ private static final String WHY = "test";
WakeLock mWakeLock;
PowerManager.WakeLock mInner;
@@ -58,22 +57,22 @@
@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());
}