diff options
| author | 2019-02-18 17:04:58 -0800 | |
|---|---|---|
| committer | 2019-02-20 09:59:45 -0800 | |
| commit | ee4c9b7d3919688564dabc2cd56b13686ba14d2c (patch) | |
| tree | fc8ad38949bba8d2e21f711d4366d3679f0f11fe | |
| parent | 9a8bcf5fe4c4c83cd23d4a9dacb35e2d182c3948 (diff) | |
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
14 files changed, 95 insertions, 54 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java b/packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java index 152b9fc3db4d..4d89a391a14f 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 @@ 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); diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenState.java index f14d396de383..368451ad379d 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 @@ public class DozeScreenState implements DozeMachine.Part {          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 e207cb13d311..70bf903cd712 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 @@ 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;          } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java index 164f5826061e..a9fe3b251f7b 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 @@ 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); 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 ee1e3c09597f..7bbd3b526892 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 @@ 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;          } 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 b83590979ef7..7991e388af7e 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 @@ 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; +    }  } 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 a54b0e1e6c40..283be867ffd9 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 @@ 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);      }  } 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 13729dfbccda..a7972878171a 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 @@ 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;          } 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 b0f1b54b262e..bebc20b116a3 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 @@ 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"; + +    /** +     * @param why A tag that will be saved for sysui dumps. +     * @see android.os.PowerManager.WakeLock#acquire() +     **/ +    void acquire(String why); -    /** @see android.os.PowerManager.WakeLock#release() */ -    void release(); +    /** +     * @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(); +            }          };      }  } 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 c20d37f123b1..e4da859946af 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 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 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 ab9b0c979fd3..0792a05ce6bc 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 @@ 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) 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 39316eda1565..f739fe61508a 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 @@ public class SettableWakeLockTest extends SysuiTestCase {      @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 4cefb994b9be..ef20df2f0214 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 @@ 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());              }          };      } 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 d925364b0e69..4425def5c21e 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 @@ 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());      }  |