1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email; 18 19 import android.os.Handler; 20 21 import com.android.email.Controller.Result; 22 import com.android.emailcommon.mail.MessagingException; 23 24 import java.util.ArrayList; 25 26 /** 27 * A {@link Result} that wraps another {@link Result} and makes sure methods gets called back 28 * on the UI thread. 29 * 30 * <p>Optionally it supports the "synchronous" mode, if you pass null for the {@code handler} 31 * parameter, which allows unit tests to run synchronously. 32 */ 33 public class ControllerResultUiThreadWrapper<T extends Result> extends Result { 34 private final Handler mHandler; 35 private final T mWrappee; 36 ControllerResultUiThreadWrapper(Handler handler, T wrappee)37 public ControllerResultUiThreadWrapper(Handler handler, T wrappee) { 38 mHandler = handler; 39 mWrappee = wrappee; 40 } 41 getWrappee()42 public T getWrappee() { 43 return mWrappee; 44 } 45 46 @Override setRegistered(boolean registered)47 protected void setRegistered(boolean registered) { 48 super.setRegistered(registered); 49 mWrappee.setRegistered(registered); 50 } 51 run(Runnable runnable)52 private void run(Runnable runnable) { 53 if (mHandler == null) { 54 runnable.run(); 55 } else { 56 mHandler.post(runnable); 57 } 58 } 59 60 @Override loadAttachmentCallback(final MessagingException result, final long accountId, final long messageId, final long attachmentId, final int progress)61 public void loadAttachmentCallback(final MessagingException result, final long accountId, 62 final long messageId, final long attachmentId, final int progress) { 63 run(new Runnable() { 64 public void run() { 65 /* It's possible this callback is unregistered after this Runnable was posted and 66 * sitting in the handler queue, so we always need to check if it's still registered 67 * on the UI thread. 68 */ 69 if (!isRegistered()) return; 70 mWrappee.loadAttachmentCallback(result, accountId, messageId, attachmentId, 71 progress); 72 } 73 }); 74 } 75 76 @Override loadMessageForViewCallback(final MessagingException result, final long accountId, final long messageId, final int progress)77 public void loadMessageForViewCallback(final MessagingException result, final long accountId, 78 final long messageId, final int progress) { 79 run(new Runnable() { 80 public void run() { 81 if (!isRegistered()) return; 82 mWrappee.loadMessageForViewCallback(result, accountId, messageId, progress); 83 } 84 }); 85 } 86 87 @Override sendMailCallback(final MessagingException result, final long accountId, final long messageId, final int progress)88 public void sendMailCallback(final MessagingException result, final long accountId, 89 final long messageId, final int progress) { 90 run(new Runnable() { 91 public void run() { 92 if (!isRegistered()) return; 93 mWrappee.sendMailCallback(result, accountId, messageId, progress); 94 } 95 }); 96 } 97 98 @Override serviceCheckMailCallback(final MessagingException result, final long accountId, final long mailboxId, final int progress, final long tag)99 public void serviceCheckMailCallback(final MessagingException result, final long accountId, 100 final long mailboxId, final int progress, final long tag) { 101 run(new Runnable() { 102 public void run() { 103 if (!isRegistered()) return; 104 mWrappee.serviceCheckMailCallback(result, accountId, mailboxId, progress, tag); 105 } 106 }); 107 } 108 109 @Override updateMailboxCallback(final MessagingException result, final long accountId, final long mailboxId, final int progress, final int numNewMessages, final ArrayList<Long> addedMessages)110 public void updateMailboxCallback(final MessagingException result, final long accountId, 111 final long mailboxId, final int progress, final int numNewMessages, 112 final ArrayList<Long> addedMessages) { 113 run(new Runnable() { 114 public void run() { 115 if (!isRegistered()) return; 116 mWrappee.updateMailboxCallback(result, accountId, mailboxId, progress, 117 numNewMessages, addedMessages); 118 } 119 }); 120 } 121 122 @Override updateMailboxListCallback(final MessagingException result, final long accountId, final int progress)123 public void updateMailboxListCallback(final MessagingException result, final long accountId, 124 final int progress) { 125 run(new Runnable() { 126 public void run() { 127 if (!isRegistered()) return; 128 mWrappee.updateMailboxListCallback(result, accountId, progress); 129 } 130 }); 131 } 132 } 133