Make sure callback is still registered when calling wrappee
This caused callback methods getting run after onDestroy() problem. Bug 2799534 Bug 3011802 Change-Id: Id505e328bbff096a9f7474c033443ff6663a5fdf
This commit is contained in:
parent
58843f0dc3
commit
e069246d48
|
@ -161,6 +161,7 @@ public class Controller {
|
|||
*/
|
||||
public void addResultCallback(Result listener) {
|
||||
synchronized (mListeners) {
|
||||
listener.setRegistered(true);
|
||||
mListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
@ -174,6 +175,7 @@ public class Controller {
|
|||
*/
|
||||
public void removeResultCallback(Result listener) {
|
||||
synchronized (mListeners) {
|
||||
listener.setRegistered(false);
|
||||
mListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
@ -986,6 +988,16 @@ public class Controller {
|
|||
* made from the UI thread, so you may need further handlers to safely make UI updates.
|
||||
*/
|
||||
public static abstract class Result {
|
||||
private volatile boolean mRegistered;
|
||||
|
||||
private void setRegistered(boolean registered) {
|
||||
mRegistered = registered;
|
||||
}
|
||||
|
||||
protected final boolean isRegistered() {
|
||||
return mRegistered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for updateMailboxList
|
||||
*
|
||||
|
|
|
@ -54,6 +54,11 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final long attachmentId, final int progress) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
/* It's possible this callback is unregistered after this Runnable was posted and
|
||||
* sitting in the handler queue, so we always need to check if it's still registered
|
||||
* on the UI thread.
|
||||
*/
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.loadAttachmentCallback(result, messageId, attachmentId, progress);
|
||||
}
|
||||
});
|
||||
|
@ -64,6 +69,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final long messageId, final int progress) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.loadMessageForViewCallback(result, messageId, progress);
|
||||
}
|
||||
});
|
||||
|
@ -74,6 +80,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final long messageId, final int progress) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.sendMailCallback(result, accountId, messageId, progress);
|
||||
}
|
||||
});
|
||||
|
@ -84,6 +91,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final long mailboxId, final int progress, final long tag) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.serviceCheckMailCallback(result, accountId, mailboxId, progress, tag);
|
||||
}
|
||||
});
|
||||
|
@ -94,6 +102,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final long mailboxId, final int progress, final int numNewMessages) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.updateMailboxCallback(result, accountId, mailboxId, progress,
|
||||
numNewMessages);
|
||||
}
|
||||
|
@ -105,6 +114,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
final int progress) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.updateMailboxListCallback(result, accountId, progress);
|
||||
}
|
||||
});
|
||||
|
@ -114,6 +124,7 @@ public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
|||
public void deleteAccountCallback(final long accountId) {
|
||||
run(new Runnable() {
|
||||
public void run() {
|
||||
if (!isRegistered()) return;
|
||||
mWrappee.deleteAccountCallback(accountId);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue