Merge "Avoid stuck wake-locks" into qt-dev

am: 13775aa4bb

Change-Id: I0db38fdb7a757510f1f24d981c66b387c6596871
This commit is contained in:
Lucas Dupin
2019-06-27 01:26:38 -07:00
committed by android-build-merger
2 changed files with 23 additions and 12 deletions

View File

@@ -30,6 +30,11 @@ public interface WakeLock {
static final String TAG = "WakeLock";
static final String REASON_WRAP = "wrap";
/**
* Default wake-lock timeout, to avoid battery regressions.
*/
long DEFAULT_MAX_TIMEOUT = 20000;
/**
* @param why A tag that will be saved for sysui dumps.
* @see android.os.PowerManager.WakeLock#acquire()
@@ -46,7 +51,14 @@ public interface WakeLock {
Runnable wrap(Runnable r);
static WakeLock createPartial(Context context, String tag) {
return wrap(createPartialInner(context, tag));
return createPartial(context, tag, DEFAULT_MAX_TIMEOUT);
}
/**
* Creates a {@link WakeLock} that has a default release timeout.
* @see android.os.PowerManager.WakeLock#acquire(long) */
static WakeLock createPartial(Context context, String tag, long maxTimeout) {
return wrap(createPartialInner(context, tag), maxTimeout);
}
@VisibleForTesting
@@ -66,7 +78,14 @@ public interface WakeLock {
};
}
static WakeLock wrap(final PowerManager.WakeLock inner) {
/**
* Create a {@link WakeLock} containing a {@link PowerManager.WakeLock}.
* @param inner To be wrapped.
* @param maxTimeout When to expire.
* @return The new wake lock.
*/
@VisibleForTesting
static WakeLock wrap(final PowerManager.WakeLock inner, long maxTimeout) {
return new WakeLock() {
private final HashMap<String, Integer> mActiveClients = new HashMap<>();
@@ -74,7 +93,7 @@ public interface WakeLock {
public void acquire(String why) {
mActiveClients.putIfAbsent(why, 0);
mActiveClients.put(why, mActiveClients.get(why) + 1);
inner.acquire();
inner.acquire(maxTimeout);
}
/** @see PowerManager.WakeLock#release() */

View File

@@ -42,7 +42,7 @@ public class WakeLockTest extends SysuiTestCase {
@Before
public void setUp() {
mInner = WakeLock.createPartialInner(mContext, WakeLockTest.class.getName());
mWakeLock = WakeLock.wrap(mInner);
mWakeLock = WakeLock.wrap(mInner, 20000);
}
@After
@@ -69,14 +69,6 @@ public class WakeLockTest extends SysuiTestCase {
assertFalse(mInner.isHeld());
}
@Test
public void wakeLock_refCounted() {
mWakeLock.acquire(WHY);
mWakeLock.acquire(WHY);
mWakeLock.release(WHY);
assertTrue(mInner.isHeld());
}
@Test
public void wakeLock_wrap() {
boolean[] ran = new boolean[1];