Merge "Add method in Handler to remove messages with equal object" into rvc-dev am: 1c21c7c74b

Change-Id: Ia247f11dcdfffa481769039e72d1e7be1cf50ccf
This commit is contained in:
Jean-Michel Trivi
2020-04-24 00:09:29 +00:00
committed by Automerger Merge Worker
2 changed files with 150 additions and 0 deletions

View File

@@ -795,6 +795,17 @@ public class Handler {
mQueue.removeMessages(this, what, object);
}
/**
* Remove any pending posts of messages with code 'what' and whose obj is
* 'object' that are in the message queue. If <var>object</var> is null,
* all messages will be removed.
*
*@hide
*/
public final void removeEqualMessages(int what, @Nullable Object object) {
mQueue.removeEqualMessages(this, what, object);
}
/**
* Remove any pending posts of callbacks and sent messages whose
* <var>obj</var> is <var>token</var>. If <var>token</var> is null,
@@ -804,6 +815,16 @@ public class Handler {
mQueue.removeCallbacksAndMessages(this, token);
}
/**
* Remove any pending posts of callbacks and sent messages whose
* <var>obj</var> is <var>token</var>. If <var>token</var> is null,
* all callbacks and messages will be removed.
*
*@hide
*/
public final void removeCallbacksAndEqualMessages(@Nullable Object token) {
mQueue.removeCallbacksAndEqualMessages(this, token);
}
/**
* Check if there are any pending posts of messages with code 'what' in
* the message queue.
@@ -828,6 +849,16 @@ public class Handler {
return mQueue.hasMessages(this, what, object);
}
/**
* Check if there are any pending posts of messages with code 'what' and
* whose obj is 'object' in the message queue.
*
*@hide
*/
public final boolean hasEqualMessages(int what, @Nullable Object object) {
return mQueue.hasEqualMessages(this, what, object);
}
/**
* Check if there are any pending posts of messages with callback r in
* the message queue.

View File

@@ -617,6 +617,23 @@ public final class MessageQueue {
}
}
boolean hasEqualMessages(Handler h, int what, Object object) {
if (h == null) {
return false;
}
synchronized (this) {
Message p = mMessages;
while (p != null) {
if (p.target == h && p.what == what && (object == null || object.equals(p.obj))) {
return true;
}
p = p.next;
}
return false;
}
}
@UnsupportedAppUsage
boolean hasMessages(Handler h, Runnable r, Object object) {
if (h == null) {
@@ -686,6 +703,40 @@ public final class MessageQueue {
}
}
void removeEqualMessages(Handler h, int what, Object object) {
if (h == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// Remove all messages at front.
while (p != null && p.target == h && p.what == what
&& (object == null || object.equals(p.obj))) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// Remove all messages after front.
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && n.what == what
&& (object == null || object.equals(n.obj))) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
void removeMessages(Handler h, Runnable r, Object object) {
if (h == null || r == null) {
return;
@@ -720,6 +771,41 @@ public final class MessageQueue {
}
}
void removeEqualMessages(Handler h, Runnable r, Object object) {
if (h == null || r == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// Remove all messages at front.
while (p != null && p.target == h && p.callback == r
&& (object == null || object.equals(p.obj))) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// Remove all messages after front.
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && n.callback == r
&& (object == null || object.equals(n.obj))) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
void removeCallbacksAndMessages(Handler h, Object object) {
if (h == null) {
return;
@@ -753,6 +839,39 @@ public final class MessageQueue {
}
}
void removeCallbacksAndEqualMessages(Handler h, Object object) {
if (h == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// Remove all messages at front.
while (p != null && p.target == h
&& (object == null || object.equals(p.obj))) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// Remove all messages after front.
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && (object == null || object.equals(n.obj))) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
private void removeAllMessagesLocked() {
Message p = mMessages;
while (p != null) {