📄 mailsynclistener.java
字号:
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*
*
*/
package com.funambol.mailclient.ui.controller;
import com.funambol.mail.Folder;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mailclient.mm.MessageManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.syncml.MailSyncSource;
import com.funambol.mailclient.syncml.FlagQueue;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.mailclient.ui.view.InboxMessageList;
import com.funambol.mailclient.ui.view.ModalPopup;
import com.funambol.mailclient.ui.view.PopupAction;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.util.Log;
import com.funambol.util.BasicSyncListener;
import com.funambol.util.MailDateFormatter;
import com.funambol.mailclient.ui.utils.UserNotificationManager;
import com.funambol.push.CTPService;
import com.funambol.util.BlackberryHelper;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
/* Note:
* In multisession mode (when we synchronize more repositories in the same
* session), we assume that the MailSyncListener is the main listener.
* The main listener is in charge of performing operations related to the
* beginning/end of the entire session (such as enabling/disabling sync button).
*/
public class MailSyncListener extends BasicSyncListener {
/** Number of items received so far (in the current session) */
private int received;
/** Number of items sent so far (in the current session) */
private int sent;
/** Total number of items to be received in this session */
private int toReceive;
/** Total number of items to be sent in this session */
private int toSend;
private boolean alerted;
/** Indicates if this sync is part of a multisession (sync of more
* repositories)
**/
private boolean multisession;
/** Session exit status */
private int status;
/** Indicates if this is the first block to be sent */
private boolean firstSend;
private int syncMode;
private boolean isFirstMessageReceived;
private ModalPopup popup;
/** Creates a new instance of MailSyncListener */
public MailSyncListener() {
init();
}
public void itemReceived(Object item) {
Log.debug(this, "Item received");
// we need to reset the inbox but we don't want the user
// to catch us performing in a slow sync required
// from the server... so we refresh the inbox
// only when we are doing a refresh from server (that
// has probably been requested by our client)
// reload of th inbox has been moved to itemReceived() method
if ((syncMode == SyncML.ALERT_CODE_REFRESH_FROM_SERVER
|| syncMode == SyncML.ALERT_CODE_SLOW)
&& isFirstMessageReceived) {
Log.debug("[MailSyncListener] Reloading Message List");
UIController.reloadInboxMessageListMessages();
isFirstMessageReceived=false;
}
// Today folders that are received do not generate an event for the UI,
// therefore we must be prepared to get here with a null item
if (item == null) {
return;
}
// In the future we may need to distinguish between a message and a
// folder, but as of today we only need to notify messages
Message msg = (Message) item;
String userInfoMessage;
// Update the number of received items
received++;
if (toReceive != ITEMS_NUMBER_UNKNOWN) {
userInfoMessage = Localization.getMessages().
getMESSAGE_X_OF_Y_RECEIVED(received, toReceive);
} else {
userInfoMessage = Localization.getMessages().
getMESSAGE_X_RECEIVED(received);
}
UIController.updateInboxMessageList(userInfoMessage);
Folder parent = msg.getParent();
if (parent != null) {
String parentName = parent.getName();
// TODO: play sound only if the message has unread flag
if ((Store.INBOX).equals(parentName)) {
if (!alerted && (!msg.getFlags().isSet(MessageFlags.OPENED))) {
UserNotificationManager unm = UserNotificationManager.getInstance();
unm.playNewEmailTone();
unm.showNewEmailIcon();
alerted = true;
}
Folder folder = MessageManager.getInstance().getFolder(MessageManager.INBOX);
InboxMessageList messageList =
UIController.getInboxMessageList();
try {
messageList.addMessage(msg);
} catch (MailException ex) {
Log.error("handleInboxMessage: " + ex.toString());
ex.printStackTrace();
}
}
}
}
public void startSession() {
Log.debug(this, "Start session");
if (!multisession) {
initializeSession();
}
}
private void initializeSession() {
//DISABLE GetMail Command during sync email
UIController.enableSyncCommands(false);
// Update msg
String msg = Localization.getMessages().CONNECTING;
UIController.updateInboxMessageList(msg);
}
public void endSession(int status) {
Log.debug("[MailSyncListener] End session");
this.status = status;
// If this is not a multisession then we update the UI
if (!multisession) {
finalizeSession();
}
// At this stage of the synchronization the client has already
// authenticated. It is therefore safe to start CTP (if not running
// already). If authentication fails, what should we do?
boolean startCtp = !CTPService.getInstance().isRunning();
//#ifdef isBlackberry
//# if (!BlackberryHelper.workingConfigHasBeenFound()) {
//# startCtp = false;
//# Log.debug("[MailSyncListener] no working config found, Not starting ctp ");
//# }
//#endif
if (startCtp) {
Log.debug("Starting ctp");
UIController.midlet.startCTP();
}
}
public void endConnecting(int status) {
Log.debug(this, "End connecting");
// We anticipate the "Checking emails..." message as we want it to
// last a little bit. If we display it in the startReceiving it gets
// clobbered too soon by the "Receiving #"
String msg = Localization.getMessages().LOADING;
// Resetting inbox: the server answer OK to the
// Refresh from server request
if (status == SyncML.ALERT_CODE_REFRESH_FROM_SERVER) {
msg = Localization.getMessages().RESETTING_MSG;
// Messages are deleted only after the connection has been
// established
//UIController.messageManager.emptyFolder(UIController.messageManager.INBOX);
}
UIController.updateInboxMessageList(msg);
}
public void syncStarted(int syncMode) {
this.syncMode=syncMode;
}
public boolean startSyncing(int syncMode) {
if (syncMode==SyncML.ALERT_CODE_SLOW&&
UIController.messageManager.getFolder(MessageManager.INBOX)
.getMessageCount()>0) {
String title = Localization.getMessages().SLOW_SYNC_POPUP_TITLE;
String msg = Localization.getMessages().SLOW_SYNC_POPUP_MSG;
SlowSyncPopupAction sspa = new SlowSyncPopupAction();
popup = new ModalPopup(title, msg, sspa);
UIController.showModalPopup(popup, UIController.getInboxMessageList());
synchronized(sspa) {
try {
sspa.wait();
} catch (InterruptedException ex) {
Log.error("Error waiting for user action" + ex);
}
}
UIController.showBackScreen();
return sspa.isOkToSlowSync();
}
return true;
}
public void startReceiving(int numItems) {
Log.debug(this, "Start receiving (" + numItems + ")");
// Save the number of items to be received
toReceive = numItems;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -