Merge "Prevent extra work on the same timestamp" into qt-dev
This commit is contained in:
@@ -270,7 +270,7 @@ public class AttentionManagerService extends SystemService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!userState.mCurrentAttentionCheck.mCallbackInternal.equals(callbackInternal)) {
|
if (!userState.mCurrentAttentionCheck.mCallbackInternal.equals(callbackInternal)) {
|
||||||
Slog.e(LOG_TAG, "Cannot cancel a non-current request");
|
Slog.w(LOG_TAG, "Cannot cancel a non-current request");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cancel(userState);
|
cancel(userState);
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ public class AttentionDetector {
|
|||||||
*/
|
*/
|
||||||
private final AtomicBoolean mRequested;
|
private final AtomicBoolean mRequested;
|
||||||
|
|
||||||
|
private long mLastActedOnNextScreenDimming;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Monotonously increasing ID for the requests sent.
|
* Monotonously increasing ID for the requests sent.
|
||||||
*/
|
*/
|
||||||
@@ -150,6 +152,9 @@ public class AttentionDetector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public long updateUserActivity(long nextScreenDimming) {
|
public long updateUserActivity(long nextScreenDimming) {
|
||||||
|
if (nextScreenDimming == mLastActedOnNextScreenDimming) {
|
||||||
|
return nextScreenDimming;
|
||||||
|
}
|
||||||
if (!mIsSettingEnabled) {
|
if (!mIsSettingEnabled) {
|
||||||
return nextScreenDimming;
|
return nextScreenDimming;
|
||||||
}
|
}
|
||||||
@@ -190,13 +195,14 @@ public class AttentionDetector {
|
|||||||
// afterwards if AttentionManager couldn't deliver it.
|
// afterwards if AttentionManager couldn't deliver it.
|
||||||
mRequested.set(true);
|
mRequested.set(true);
|
||||||
mRequestId++;
|
mRequestId++;
|
||||||
|
mLastActedOnNextScreenDimming = nextScreenDimming;
|
||||||
mCallback = new AttentionCallbackInternalImpl(mRequestId);
|
mCallback = new AttentionCallbackInternalImpl(mRequestId);
|
||||||
|
Slog.v(TAG, "Checking user attention, ID: " + mRequestId);
|
||||||
final boolean sent = mAttentionManager.checkAttention(getAttentionTimeout(), mCallback);
|
final boolean sent = mAttentionManager.checkAttention(getAttentionTimeout(), mCallback);
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
mRequested.set(false);
|
mRequested.set(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Slog.v(TAG, "Checking user attention, ID: " + mRequestId);
|
|
||||||
return whenToCheck;
|
return whenToCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.Mockito.atMost;
|
import static org.mockito.Mockito.atMost;
|
||||||
|
import static org.mockito.Mockito.clearInvocations;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.reset;
|
import static org.mockito.Mockito.reset;
|
||||||
@@ -181,11 +182,23 @@ public class AttentionDetectorTest extends AndroidTestCase {
|
|||||||
verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
|
verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnUserActivity_ignoresIfAlreadyDoneForThatNextScreenDimming() {
|
||||||
|
long when = registerAttention();
|
||||||
|
verify(mAttentionManagerInternal).checkAttention(anyLong(), any());
|
||||||
|
assertThat(when).isLessThan(mNextDimming);
|
||||||
|
clearInvocations(mAttentionManagerInternal);
|
||||||
|
|
||||||
|
long redundantWhen = mAttentionDetector.updateUserActivity(mNextDimming);
|
||||||
|
assertThat(redundantWhen).isEqualTo(mNextDimming);
|
||||||
|
verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnUserActivity_skipsIfAlreadyScheduled() {
|
public void testOnUserActivity_skipsIfAlreadyScheduled() {
|
||||||
registerAttention();
|
registerAttention();
|
||||||
reset(mAttentionManagerInternal);
|
reset(mAttentionManagerInternal);
|
||||||
long when = mAttentionDetector.updateUserActivity(mNextDimming);
|
long when = mAttentionDetector.updateUserActivity(mNextDimming + 1);
|
||||||
verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
|
verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
|
||||||
assertThat(when).isLessThan(mNextDimming);
|
assertThat(when).isLessThan(mNextDimming);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user