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

📄 funambol.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
字号:
/*
 * 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
 */

package com.funambol.mailclient;

import com.funambol.mailclient.config.*;
import com.funambol.mailclient.ui.controller.AlarmManager;
import com.funambol.mailclient.ui.controller.Killable;
import com.funambol.mailclient.ui.controller.OTAMessagesListener;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.controller.LifeCycleController;
import com.funambol.mailclient.ui.view.FunSplashScreen;
import com.funambol.mailclient.loc.*;
import com.funambol.mailclient.ui.view.ExitPopupAction;
import com.funambol.mailclient.ui.view.FunModalPopup;
import com.funambol.util.Log;
import com.funambol.util.RMSAppender;
import java.util.Date;
import javax.microedition.io.PushRegistry;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Funambol extends MIDlet implements Killable, Runnable {
    
    private MailClientConfig mailClientConfig;
    private OTAMessagesListener SANlistener;
    private OTAMessagesListener OTAConfigListener;
    
    //private long startAppProbe ;
    private FunSplashScreen splashScreen;
   
    
    public Funambol() {
        //startAppProbe = System.currentTimeMillis();
        splashScreen = new FunSplashScreen();
        Display.getDisplay(this).setCurrent(splashScreen);
    }
    
    protected void startApp() {
        if (LifeCycleController.isPaused == false) {
            // init localization for specific language
            //Localization.setMessages(new IT_LocalizedMessages());
            new Thread(this).start();
        } else {
            // Resuming the MIDlet from a PAUSE state
            LifeCycleController.isPaused = false;
            LifeCycleController.getInstance().resume();
        }
    }

    public void setSplashPhase(String message, int iconNumber) {
        splashScreen.setInitPhase(message, iconNumber);
    }
    
    private void startSync(final boolean ok2Sync) {
        if (ok2Sync) {
            if (!UIController.otaStarted) {
                UIController.sync(true);
            } else {
                Log.info("Ota started the application... sync using SAN");
            }
        }
    }
    
    public void destroyApp(boolean unconditional) {
        Log.debug("DestroyAPP");
        FunModalPopup popup =
                new FunModalPopup(Localization.getMessages().POPUP_EXIT_TITLE,
                Localization.getMessages().POPUP_EXIT_TEXT,
                new ExitPopupAction());
        UIController.showModalPopup(popup, UIController.getInboxMessageList());
        
    }
    
    /**
     * Pausing the app means to put the Controller in paused mode.
     * In this way it will perform all the operations needed later when resuming
     */
    protected void pauseApp() {
     
        LifeCycleController.getInstance().pause();
     
    }
    
    /**
     * Exit the application. The exit method is invoked by the Controller
     * to exit the MIDlet
     */
    public void exit() throws MIDletStateChangeException {
        try {
            SANlistener.close();
            OTAConfigListener.close();
        } catch (Exception e) {
            e.printStackTrace();
            Log.error(this, "IOException trying to close san and otaconfig listener");
        } finally {
            notifyDestroyed();
        }
    }
    
    public void run() {

        boolean ok2Sync = false;

        UIController.midlet=this;
        UIController.display = Display.getDisplay(this);
        //UIController.startAppProbe = startAppProbe;
        UIController.startupFreeMem = Runtime.getRuntime().freeMemory();
        InitConfigManager cm = new InitConfigManager(this);
        cm.initConfig();
        //new Thread(new InitManager()).start();
        
        setSplashPhase("Opening Inbox...", 0);
        ok2Sync = UIController.initInbox();
        cm.initListener();
        startSync(ok2Sync);
        
        // Let the gc collect the splashScreen memory
        splashScreen = null;
    }
    
    
    private class InitConfigManager {
        Funambol midlet;
        
        public InitConfigManager(Funambol midlet) {
            this.midlet = midlet;
        }
        
        private void initConfig() {
            initLogger();
            
            // Initialize the ThreadPoolMonitor
            UIController.initThreadPool();
            
            //initListener();
            
            //long startInitConfig = System.currentTimeMillis();
            // Mail Configuration initInbox
            // We check in Storage then in JAD and at the end load a default config
            // Log.debug("Init mail client configuration");
            try {
                mailClientConfig = ConfigManager.getConfig(midlet);
            } catch (Exception e) {
                Log.error("Invalid mail client configuration: " +
                       "creating a default configuration");
                mailClientConfig = new MailClientConfig();
                e.printStackTrace();
            } finally {
                UIController.mailClientConfig = mailClientConfig;
                if (mailClientConfig.isSchedulerEnabled()) {
                    setAlarm();
                }
                Log.setLogLevel(mailClientConfig.getLogLevel());
            }
            //long endInitConfig = System.currentTimeMillis();
            //Log.addProbe("\n_probe_start_initConfig = " + startInitConfig);
            //Log.addProbe("\n_probe_end_initConfig = " + endInitConfig);
            
        }
        
        
        private void setAlarm() {
            if (mailClientConfig.getPollInterval()>=1440) {
                int days = (mailClientConfig.getPollInterval()/60)/24;
                AlarmManager.getInstance().startSyncTimerTask(
                        new Date(mailClientConfig.getNextTimeAlarm()), days);
            } else {
                AlarmManager.getInstance().startSyncTimerTask(
                        mailClientConfig.getPollInterval());
            }
        }
        
        private void initLogger() {
            // Set the appropriate appender and log level.
            if ("console".equals(midlet.getAppProperty("LogAppender"))) {
                Log.setLogLevel(Log.INFO);
            } else {
                Log.initLog(
                        new RMSAppender(RMSAppender.LOGDBNAME),
                        Log.INFO);
            }
        }
        
        public void initListener() {
            /** Connections detected at start up. */
            try {
                // List all the registered connections
                String[] connections = PushRegistry.listConnections(false);
                SANlistener =
                        new OTAMessagesListener(getAppProperty("SAN-Port"));
                OTAConfigListener =
                        new OTAMessagesListener(getAppProperty("OTAC-Port"));
                
                if (connections == null || connections.length == 0) {
                    Log.info("Application not registered for incoming SMS");
                } else {
                    Log.info(connections.length  + " connections registered.");
                    // list only the active registered connections
                    connections = PushRegistry.listConnections(true);
                    Log.info(connections.length  + " active connections.");
                    OTAConfigListener.init();
                    SANlistener.init();
                    if (connections == null || connections.length == 0) {
                        Log.info("User started the application.");
                    } else {
                        UIController.otaStarted = true;
                        Log.info("Registry connection found: "
                                + connections[connections.length - 1]);
                        if (connections[connections.length - 1]
                                .endsWith(getAppProperty("SAN-Port"))) {
                            OTAMessagesListener.startViaOTA = true;
                            Log.info("SAN Message started the application.");
                        } else {
                            Log.info("OTA Config Message started the application.");
                        }
                    }
                }
            } catch (Exception exp) {
                Log.error("Exception occurred listening for SMS: "+exp.getMessage());
                UIController.showErrorAlert(
                        Localization.getMessages().ALERT_SECURITY_EXCEPTION_MESSAGE);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    Log.error(this, "interruptedException in Exception in " +
                            "initlistener() method ");
                    ex.printStackTrace();
                }
            }
        }
        
    }
    
}

⌨️ 快捷键说明

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