2010-05-24 18:35:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email;
|
|
|
|
|
2011-07-19 00:33:38 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
|
2010-05-24 18:35:43 +00:00
|
|
|
import com.android.email.Controller.Result;
|
2011-02-10 02:47:43 +00:00
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
2010-05-24 18:35:43 +00:00
|
|
|
|
2011-05-03 21:42:26 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2010-05-24 18:35:43 +00:00
|
|
|
/**
|
|
|
|
* A {@link Result} that wraps another {@link Result} and makes sure methods gets called back
|
|
|
|
* on the UI thread.
|
2010-08-16 21:14:40 +00:00
|
|
|
*
|
|
|
|
* <p>Optionally it supports the "synchronous" mode, if you pass null for the {@code handler}
|
|
|
|
* parameter, which allows unit tests to run synchronously.
|
2010-05-24 18:35:43 +00:00
|
|
|
*/
|
2010-06-07 21:03:40 +00:00
|
|
|
public class ControllerResultUiThreadWrapper<T extends Result> extends Result {
|
2010-06-01 21:09:11 +00:00
|
|
|
private final Handler mHandler;
|
|
|
|
private final T mWrappee;
|
2010-05-24 18:35:43 +00:00
|
|
|
|
2010-06-01 21:09:11 +00:00
|
|
|
public ControllerResultUiThreadWrapper(Handler handler, T wrappee) {
|
|
|
|
mHandler = handler;
|
2010-05-24 18:35:43 +00:00
|
|
|
mWrappee = wrappee;
|
|
|
|
}
|
|
|
|
|
2010-06-01 21:09:11 +00:00
|
|
|
public T getWrappee() {
|
|
|
|
return mWrappee;
|
|
|
|
}
|
|
|
|
|
2010-12-07 23:17:52 +00:00
|
|
|
@Override
|
|
|
|
protected void setRegistered(boolean registered) {
|
|
|
|
super.setRegistered(registered);
|
|
|
|
mWrappee.setRegistered(registered);
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:14:40 +00:00
|
|
|
private void run(Runnable runnable) {
|
|
|
|
if (mHandler == null) {
|
|
|
|
runnable.run();
|
|
|
|
} else {
|
|
|
|
mHandler.post(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-12-16 23:09:03 +00:00
|
|
|
public void loadAttachmentCallback(final MessagingException result, final long accountId,
|
|
|
|
final long messageId, final long attachmentId, final int progress) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
/* 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;
|
2010-12-10 21:36:18 +00:00
|
|
|
mWrappee.loadAttachmentCallback(result, accountId, messageId, attachmentId,
|
|
|
|
progress);
|
2010-05-24 18:35:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-12-10 21:36:18 +00:00
|
|
|
public void loadMessageForViewCallback(final MessagingException result, final long accountId,
|
2010-05-24 18:35:43 +00:00
|
|
|
final long messageId, final int progress) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
if (!isRegistered()) return;
|
2010-12-10 21:36:18 +00:00
|
|
|
mWrappee.loadMessageForViewCallback(result, accountId, messageId, progress);
|
2010-05-24 18:35:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-05-24 18:35:43 +00:00
|
|
|
public void sendMailCallback(final MessagingException result, final long accountId,
|
|
|
|
final long messageId, final int progress) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
if (!isRegistered()) return;
|
2010-05-24 18:35:43 +00:00
|
|
|
mWrappee.sendMailCallback(result, accountId, messageId, progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-05-24 18:35:43 +00:00
|
|
|
public void serviceCheckMailCallback(final MessagingException result, final long accountId,
|
|
|
|
final long mailboxId, final int progress, final long tag) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
if (!isRegistered()) return;
|
2010-05-24 18:35:43 +00:00
|
|
|
mWrappee.serviceCheckMailCallback(result, accountId, mailboxId, progress, tag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-05-24 18:35:43 +00:00
|
|
|
public void updateMailboxCallback(final MessagingException result, final long accountId,
|
2011-05-03 21:42:26 +00:00
|
|
|
final long mailboxId, final int progress, final int numNewMessages,
|
|
|
|
final ArrayList<Long> addedMessages) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
if (!isRegistered()) return;
|
2010-05-24 18:35:43 +00:00
|
|
|
mWrappee.updateMailboxCallback(result, accountId, mailboxId, progress,
|
2011-05-03 21:42:26 +00:00
|
|
|
numNewMessages, addedMessages);
|
2010-05-24 18:35:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:03:40 +00:00
|
|
|
@Override
|
2010-05-24 18:35:43 +00:00
|
|
|
public void updateMailboxListCallback(final MessagingException result, final long accountId,
|
|
|
|
final int progress) {
|
2010-08-16 21:14:40 +00:00
|
|
|
run(new Runnable() {
|
2010-05-24 18:35:43 +00:00
|
|
|
public void run() {
|
2010-09-22 22:35:20 +00:00
|
|
|
if (!isRegistered()) return;
|
2010-05-24 18:35:43 +00:00
|
|
|
mWrappee.updateMailboxListCallback(result, accountId, progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2011-07-19 00:33:38 +00:00
|
|
|
|