Add method in Handler to remove messages with equal object

Bug: 142293357
Test: atest AudioDeviceBrokerTest

Change-Id: Ibae80f5008df54f4ac8544abb4cc5863ea3db86d
Merged-In: Ibae80f5008df54f4ac8544abb4cc5863ea3db86d
Signed-off-by: fengjinlan <fengjinlan@xiaomi.com>
This commit is contained in:
fengjinlan
2020-02-03 15:50:11 +08:00
committed by Jean-Michel Trivi
parent 634642bc22
commit 6c2adc82cc
2 changed files with 150 additions and 0 deletions

View File

@@ -795,6 +795,17 @@ public class Handler {
mQueue.removeMessages(this, what, object); 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 * Remove any pending posts of callbacks and sent messages whose
* <var>obj</var> is <var>token</var>. If <var>token</var> is null, * <var>obj</var> is <var>token</var>. If <var>token</var> is null,
@@ -804,6 +815,16 @@ public class Handler {
mQueue.removeCallbacksAndMessages(this, token); 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 * Check if there are any pending posts of messages with code 'what' in
* the message queue. * the message queue.
@@ -828,6 +849,16 @@ public class Handler {
return mQueue.hasMessages(this, what, object); 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 * Check if there are any pending posts of messages with callback r in
* the message queue. * 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 @UnsupportedAppUsage
boolean hasMessages(Handler h, Runnable r, Object object) { boolean hasMessages(Handler h, Runnable r, Object object) {
if (h == null) { 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) { void removeMessages(Handler h, Runnable r, Object object) {
if (h == null || r == null) { if (h == null || r == null) {
return; 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) { void removeCallbacksAndMessages(Handler h, Object object) {
if (h == null) { if (h == null) {
return; 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() { private void removeAllMessagesLocked() {
Message p = mMessages; Message p = mMessages;
while (p != null) { while (p != null) {