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

This commit is contained in:
TreeHugger Robot
2019-06-27 08:00:00 +00:00
committed by Android (Google) Code Review
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 TAG = "WakeLock";
static final String REASON_WRAP = "wrap"; 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. * @param why A tag that will be saved for sysui dumps.
* @see android.os.PowerManager.WakeLock#acquire() * @see android.os.PowerManager.WakeLock#acquire()
@@ -46,7 +51,14 @@ public interface WakeLock {
Runnable wrap(Runnable r); Runnable wrap(Runnable r);
static WakeLock createPartial(Context context, String tag) { 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 @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() { return new WakeLock() {
private final HashMap<String, Integer> mActiveClients = new HashMap<>(); private final HashMap<String, Integer> mActiveClients = new HashMap<>();
@@ -74,7 +93,7 @@ public interface WakeLock {
public void acquire(String why) { public void acquire(String why) {
mActiveClients.putIfAbsent(why, 0); mActiveClients.putIfAbsent(why, 0);
mActiveClients.put(why, mActiveClients.get(why) + 1); mActiveClients.put(why, mActiveClients.get(why) + 1);
inner.acquire(); inner.acquire(maxTimeout);
} }
/** @see PowerManager.WakeLock#release() */ /** @see PowerManager.WakeLock#release() */

View File

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