1/*
2 * Copyright (C) 2009 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.emailcommon.mail.MessagingException;
20
21import android.content.Context;
22
23import java.util.ArrayList;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26
27public class GroupMessagingListener extends MessagingListener {
28    /* The synchronization of the methods in this class
29       is not needed because we use ConcurrentHashMap.
30
31       Nevertheless, let's keep the "synchronized" for a while in the case
32       we may want to change the implementation to use something else
33       than ConcurrentHashMap.
34    */
35
36    private ConcurrentHashMap<MessagingListener, Object> mListenersMap =
37        new ConcurrentHashMap<MessagingListener, Object>();
38
39    private Set<MessagingListener> mListeners = mListenersMap.keySet();
40
41    synchronized public void addListener(MessagingListener listener) {
42        // we use "this" as a dummy non-null value
43        mListenersMap.put(listener, this);
44    }
45
46    synchronized public void removeListener(MessagingListener listener) {
47        mListenersMap.remove(listener);
48    }
49
50    synchronized public boolean isActiveListener(MessagingListener listener) {
51        return mListenersMap.containsKey(listener);
52    }
53
54    @Override
55    synchronized public void listFoldersStarted(long accountId) {
56        for (MessagingListener l : mListeners) {
57            l.listFoldersStarted(accountId);
58        }
59    }
60
61    @Override
62    synchronized public void listFoldersFailed(long accountId, String message) {
63        for (MessagingListener l : mListeners) {
64            l.listFoldersFailed(accountId, message);
65        }
66    }
67
68    @Override
69    synchronized public void listFoldersFinished(long accountId) {
70        for (MessagingListener l : mListeners) {
71            l.listFoldersFinished(accountId);
72        }
73    }
74
75    @Override
76    synchronized public void synchronizeMailboxStarted(long accountId, long mailboxId) {
77        for (MessagingListener l : mListeners) {
78            l.synchronizeMailboxStarted(accountId, mailboxId);
79        }
80    }
81
82    @Override
83    synchronized public void synchronizeMailboxFinished(long accountId, long mailboxId,
84            int totalMessagesInMailbox, int numNewMessages, ArrayList<Long> addedMessages) {
85        for (MessagingListener l : mListeners) {
86            l.synchronizeMailboxFinished(accountId, mailboxId,
87                    totalMessagesInMailbox, numNewMessages, addedMessages);
88        }
89    }
90
91    @Override
92    synchronized public void synchronizeMailboxFailed(long accountId, long mailboxId, Exception e) {
93        for (MessagingListener l : mListeners) {
94            l.synchronizeMailboxFailed(accountId, mailboxId, e);
95        }
96    }
97
98    @Override
99    synchronized public void loadMessageForViewStarted(long messageId) {
100        for (MessagingListener l : mListeners) {
101            l.loadMessageForViewStarted(messageId);
102        }
103    }
104
105    @Override
106    synchronized public void loadMessageForViewFinished(long messageId) {
107        for (MessagingListener l : mListeners) {
108            l.loadMessageForViewFinished(messageId);
109        }
110    }
111
112    @Override
113    synchronized public void loadMessageForViewFailed(long messageId, String message) {
114        for (MessagingListener l : mListeners) {
115            l.loadMessageForViewFailed(messageId, message);
116        }
117    }
118
119    @Override
120    synchronized public void checkMailStarted(Context context, long accountId, long tag) {
121        for (MessagingListener l : mListeners) {
122            l.checkMailStarted(context, accountId, tag);
123        }
124    }
125
126    @Override
127    synchronized public void checkMailFinished(Context context, long accountId, long folderId,
128            long tag) {
129        for (MessagingListener l : mListeners) {
130            l.checkMailFinished(context, accountId, folderId, tag);
131        }
132    }
133
134    @Override
135    synchronized public void sendPendingMessagesStarted(long accountId, long messageId) {
136        for (MessagingListener l : mListeners) {
137            l.sendPendingMessagesStarted(accountId, messageId);
138        }
139    }
140
141    @Override
142    synchronized public void sendPendingMessagesCompleted(long accountId) {
143        for (MessagingListener l : mListeners) {
144            l.sendPendingMessagesCompleted(accountId);
145        }
146    }
147
148    @Override
149    synchronized public void sendPendingMessagesFailed(long accountId, long messageId,
150            Exception reason) {
151        for (MessagingListener l : mListeners) {
152            l.sendPendingMessagesFailed(accountId, messageId, reason);
153        }
154    }
155
156    @Override
157    synchronized public void messageUidChanged(long accountId, long mailboxId,
158            String oldUid, String newUid) {
159        for (MessagingListener l : mListeners) {
160            l.messageUidChanged(accountId, mailboxId, oldUid, newUid);
161        }
162    }
163
164    @Override
165    synchronized public void loadAttachmentStarted(
166            long accountId,
167            long messageId,
168            long attachmentId,
169            boolean requiresDownload) {
170        for (MessagingListener l : mListeners) {
171            l.loadAttachmentStarted(accountId, messageId, attachmentId, requiresDownload);
172        }
173    }
174
175    @Override
176    synchronized public void loadAttachmentFinished(
177            long accountId,
178            long messageId,
179            long attachmentId) {
180        for (MessagingListener l : mListeners) {
181            l.loadAttachmentFinished(accountId, messageId, attachmentId);
182        }
183    }
184
185    @Override
186    synchronized public void loadAttachmentFailed(
187            long accountId,
188            long messageId,
189            long attachmentId,
190            MessagingException me,
191            boolean background) {
192        for (MessagingListener l : mListeners) {
193            l.loadAttachmentFailed(accountId, messageId, attachmentId, me, background);
194        }
195    }
196
197    @Override
198    synchronized public void controllerCommandCompleted(boolean moreCommandsToRun) {
199        for (MessagingListener l : mListeners) {
200            l.controllerCommandCompleted(moreCommandsToRun);
201        }
202    }
203}
204