⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synctaskmanager.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Created on 5 dicembre 2006, 17.12
 *
 */

package com.funambol.mailclient.mm;

import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mailclient.AccountListener;
import com.funambol.mailclient.cm.ContactManager;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.ContactManagerFactory;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.syncml.spds.SyncException;
import com.funambol.util.MailDateFormatter;


import com.funambol.util.Log;
import com.funambol.util.Observer;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import javax.microedition.rms.RecordStoreException;

/**
 * Extends TaskManager to monitor a sync thread.
 * <p>
 * update() method of observer is called when something changes.
 * The object passed to the observer gives a hint of what has changed.
 * </p>
 * <ul>
 * <li>each time the userInfoMessage is changed an update(:String) is sent,
 *     and the string passed contains the new userInfoMessage</li>
 * <li>each time a new userInfoMessage is received an update(:Message) is
 *     sent. Message is an empty userInfoMessage</li>
 * </ul>
 */
public class SyncTaskManager extends TaskManager {
    private static final int MAX = 100;
    
    private int toReceive = AccountListener.MESSAGE_NUMBER_UNKNOWN;
    private int toSend = AccountListener.MESSAGE_NUMBER_UNKNOWN;
    private int received = 0;
    private int sent = 0;
    
    private boolean connected = false;
    private boolean completed = false;
    
    
    // FIXME: this should be called syncContacts!
    private boolean resetContacts = true;
    
    private MessageManager mm = null;
    
    // SyncEventInfo permits to propagate to the upper level the info for
    // each Sync Event
    private SyncEventInfo syncEventInfo;
    
    private int syncMode;
    
    private boolean syncMessages;
    
    /**
     * Creates a new instance of SyncTaskManager
     *
     * @param MessageManager the userInfoMessage manager
     * @numObservers the max number of observers this
     * task manager will be able to have
     */
    public SyncTaskManager(MessageManager mm, int numObservers) {
        super(numObservers);
        this.mm = mm;
        mm.setListener(this);
        max = MAX;
        syncEventInfo = new SyncEventInfo();
        this.syncMode = -1;
    }
    
    /**
     * Returns the current sync mode, -1 means that the one defined for the
     * source will be used.
     */
    public int getSyncMode() {
        return syncMode;
    }
    
    /**
     * Force to use a sync mode different from the one defined for the source.
     */
    public void setSyncMode(int mode) {
        syncMode = mode;
    }
    
    /**
     * Javabean standard definition for 'syncContacts' field setter method
     *
     * @param resetContacts is the new value to give to resetContacts variable
     */
    public void setResetContacts(boolean syncContacts) {
        this.resetContacts = syncContacts;
    }
    
    /**
     * Javabean standard definition for 'resetContacts' field setter method
     *
     * @return boolean value related to resetContacts variable
     */
    public boolean isResetContacts() {
        return resetContacts;
    }
    
    /**
     * @return true is task is finished
     */
    public boolean isFinished() {
        return finished;
    }
    
    /**
     * run the sync
     */
    public void run() {
        //Sync Mail Messages;
        
        if (syncMessages) {
            
            syncEventInfo.setInfoCode(SyncEventInfo.START_OPERATION);
            update(syncEventInfo);
            try {
                Log.info("*** Initial Sync for Emails in progress ***");
                
                //setting message to have better response time
                syncEventInfo.setInfoCode(SyncEventInfo.START_MESSAGE_SESSION);
                syncEventInfo.infoMessage = Localization.getMessages().CONNECTING;
                update(syncEventInfo);
                
                // Bad trick to use the source mode. XXX
                if(syncMode == -1) {
                    mm.sync();
                } else {
                    mm.sync(syncMode);
                }
                
                Log.info("*** Initial Sync for Emails is complete ***");
                
            } catch (ConfigException ce) {
                Log.error(this, "Config error" + ce.toString());
                userInfoMessage = Localization.getMessages().CONFIGURATION_ERROR;
                
            } finally {
                syncEventInfo.setInfoCode(SyncEventInfo.END_MESSAGE_SESSION);
                update(syncEventInfo);
                
            }
            finished = true;
        }
        
        //Sync Email Client Address Book;
        //check resetContacts attribute: if it's set to true
        //a contact Synchronization Takes place after message sync.
        //This is needed when the application is started
        //or a new account with a different username is
        //declared and saved into configuration by the user;
        if (this.resetContacts) {
            try {
                Log.info("*** Initial Sync for Contacts in progress ***");
                syncEventInfo.setInfoCode(SyncEventInfo.START_CONTACT_SESSION);
                syncEventInfo.infoMessage =
                        Localization.getMessages().SYNC_CONTACTS;
                update(syncEventInfo);
                ContactManager cm =
                        ContactManagerFactory.getContactManager(ContactManager.RMS_TYPE);
                // Bad trick to use the source mode. XXX
                if(syncMode == -1) {
                    cm.sync();
                } else {
                    cm.sync(syncMode);
                }
                Log.info("*** Initial Sync for Contacts is complete ***");
                
                if (syncMessages) {
                    userInfoMessage = Localization.getMessages().getX_MESSAGES_RECEIVED(received);
                } else   {
                    userInfoMessage = Localization.getMessages().SYNC_CONTACTS_COMPLETED;
                }
            } catch (RecordStoreException ex) {
                Log.error("Error updating contacts:"+ex.toString());
                ex.printStackTrace();
            } catch (ContactManagerException ex) {
                Log.error("Error updating contacts:"+ex.toString());
                ex.printStackTrace();
            } catch (ConfigException ex) {
                Log.error(this, "Config error" + ex.toString());
                userInfoMessage = Localization.getMessages().CONFIGURATION_ERROR;
            } finally {
                this.resetContacts=false;
                syncEventInfo.infoMessage = userInfoMessage;
                syncEventInfo.setInfoCode(SyncEventInfo.END_CONTACT_SESSION);
                update(syncEventInfo);
                mm.setListener(null);
            }
        }
        
        syncEventInfo.setInfoCode(SyncEventInfo.END_OPERATION);
        update(syncEventInfo);
    }
    
    /**
     * should be called when a communication session is started
     */
    public void startSession() {
        syncEventInfo.setInfoCode(SyncEventInfo.START_MESSAGE_SESSION);
        syncEventInfo.infoMessage =
                Localization.getMessages().CONNECTING;
        update(syncEventInfo);
    }
    
    /**
     * should be called when connnection is completed
     */
    public void connectionCompleted(int onConnectAction) {
        connected = true;
        if (onConnectAction == AccountListener.ACTION_RESET_INBOX) {
            syncEventInfo.setInfoCode(SyncEventInfo.RELOAD_INBOX);
            update(syncEventInfo);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -