ControllerResultUiThreadWrapper.java revision 45e04b009d570235c542f3c97eaa7e1d00e6cc7b
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 17package com.android.email; 18 19import com.android.email.Controller.Result; 20import com.android.email.mail.MessagingException; 21 22import android.os.Handler; 23 24/** 25 * A {@link Result} that wraps another {@link Result} and makes sure methods gets called back 26 * on the UI thread. 27 * 28 * <p>Optionally it supports the "synchronous" mode, if you pass null for the {@code handler} 29 * parameter, which allows unit tests to run synchronously. 30 */ 31public class ControllerResultUiThreadWrapper<T extends Result> extends Result { 32 private final Handler mHandler; 33 private final T mWrappee; 34 35 public ControllerResultUiThreadWrapper(Handler handler, T wrappee) { 36 mHandler = handler; 37 mWrappee = wrappee; 38 } 39 40 public T getWrappee() { 41 return mWrappee; 42 } 43 44 @Override 45 protected void setRegistered(boolean registered) { 46 super.setRegistered(registered); 47 mWrappee.setRegistered(registered); 48 } 49 50 private void run(Runnable runnable) { 51 if (mHandler == null) { 52 runnable.run(); 53 } else { 54 mHandler.post(runnable); 55 } 56 } 57 58 @Override 59 public void loadAttachmentCallback(final MessagingException result, final long messageId, 60 final long attachmentId, final int progress) { 61 run(new Runnable() { 62 public void run() { 63 /* It's possible this callback is unregistered after this Runnable was posted and 64 * sitting in the handler queue, so we always need to check if it's still registered 65 * on the UI thread. 66 */ 67 if (!isRegistered()) return; 68 mWrappee.loadAttachmentCallback(result, messageId, attachmentId, progress); 69 } 70 }); 71 } 72 73 @Override 74 public void loadMessageForViewCallback(final MessagingException result, 75 final long messageId, final int progress) { 76 run(new Runnable() { 77 public void run() { 78 if (!isRegistered()) return; 79 mWrappee.loadMessageForViewCallback(result, messageId, progress); 80 } 81 }); 82 } 83 84 @Override 85 public void sendMailCallback(final MessagingException result, final long accountId, 86 final long messageId, final int progress) { 87 run(new Runnable() { 88 public void run() { 89 if (!isRegistered()) return; 90 mWrappee.sendMailCallback(result, accountId, messageId, progress); 91 } 92 }); 93 } 94 95 @Override 96 public void serviceCheckMailCallback(final MessagingException result, final long accountId, 97 final long mailboxId, final int progress, final long tag) { 98 run(new Runnable() { 99 public void run() { 100 if (!isRegistered()) return; 101 mWrappee.serviceCheckMailCallback(result, accountId, mailboxId, progress, tag); 102 } 103 }); 104 } 105 106 @Override 107 public void updateMailboxCallback(final MessagingException result, final long accountId, 108 final long mailboxId, final int progress, final int numNewMessages) { 109 run(new Runnable() { 110 public void run() { 111 if (!isRegistered()) return; 112 mWrappee.updateMailboxCallback(result, accountId, mailboxId, progress, 113 numNewMessages); 114 } 115 }); 116 } 117 118 @Override 119 public void updateMailboxListCallback(final MessagingException result, final long accountId, 120 final int progress) { 121 run(new Runnable() { 122 public void run() { 123 if (!isRegistered()) return; 124 mWrappee.updateMailboxListCallback(result, accountId, progress); 125 } 126 }); 127 } 128 129 @Override 130 public void deleteAccountCallback(final long accountId) { 131 run(new Runnable() { 132 public void run() { 133 if (!isRegistered()) return; 134 mWrappee.deleteAccountCallback(accountId); 135 } 136 }); 137 } 138} 139