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

📄 funambol.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            ctpService.setThreadPool(UIController.getThreadPool());
            ctpService.setCTPListener(this);
            ctpService.setPushNotificationListener(messageHandler);
            
            // If CTP is disabled, then avoid starting it
            if (ctp == null || !ctp.equals("true") || mailClientConfig.getCtpPushStatus() == MailClientConfig.CTP_DISABLED) {
                Log.debug("CTP is disabled, not starting it");
                return;
            }


            // Start the CTP service 
            ctpService.startService();
            
        }  catch (Exception e) {
            //e.printStackTrace();
            Log.error("No valid config found. CTPService cannot start " + e);
        }
       

    }

    public PushConfig getPushConfig() throws ConfigException, IOException {
        PushConfig pushConfig = new PushConfig();
        try {
            ConfigManager.load(ConfigManager.PUSH_CONFIG, pushConfig);
        } catch (Exception e) {
        // The configuration does not exist or it is invalid, we restart
        // from an empty one
        } finally {
            // Propagate some info from the other config to the PushConfig
                SyncClientConfig sc = new SyncClientConfig();
                ConfigManager.load(ConfigManager.SYNCML_CONFIG, sc);
                Account account = mailClientConfig.getMailAccount();
                pushConfig.setCtpUsername(account.getUser());
                pushConfig.setCtpPassword(account.getPassword());

                //String server = getAppProperty("CTPServer");
                String server = UIController.appProperties.get(AppProperties.CTP_SERVER);

                if (server == null) {
                    // Guess the CTP server name from the sync server name
                    server = account.getUrl();
                    // Strip the port and the protocol if present and the resource
                    if (server.startsWith("http://")) {
                        server = server.substring(7, server.length() - 1);
                    } else if (server.startsWith("https://")) {
                        server = server.substring(8, server.length() - 1);
                    }
// If we have port number in the address we strip everything
// after the port number
                    int pos = server.indexOf(':');
                    if (pos >= 0) {
                        server = server.substring(0, pos);
                    }

// If we have resource location in the address then we strip
// everything after the '/'
                    pos = server.indexOf('/');
                    if (pos >= 0) {
                        server = server.substring(0, pos);
                    }
// TODO: what shall we do here? WM adds a "ctp-" to the
// server name
//server = "ctp-" + server;
                } else {
                    int pos = server.indexOf(':');
                    if (pos >= 0) {
                        String portStr = server.substring(pos + 1);
                        try {
                            int port = Integer.parseInt(portStr);
                            Log.debug("Setting CTP port to: " + port);
                            pushConfig.setCtpPort(port);
                        } catch (NumberFormatException ne) {
                            Log.error("Bad ctp port parameter: " + portStr);
                        }

                        server = server.substring(0, pos);
                    }

                }

                Log.debug("[FunambolMIDlet] CTP server is: " + server);
                pushConfig.setCtpServer(server);
                pushConfig.setDeviceId(sc.getDeviceId());

                ConfigManager.save(ConfigManager.PUSH_CONFIG, pushConfig);
         

        }

        return pushConfig;

    }

    /**
     * This method checks if an upgrade is needed. The current implementation
     * checks if the Store version is less than 102 and in this case it starts a
     * reset inbox to resynchronize the inbox.
     * The synchronization is executed in a different thread, therefore the
     * method may report a success even if the synchronization fails. On failure
     * the inbox could be partially filled (or even empty), but data would be
     * consistent between client and server.
     *
     * @return true on successful upgrade
     */
    private boolean handleUpgrade() {

        // Check if we need to finish upgrade
        if (mailClientConfig.getStoreVersion() < Store.VERSION_102) {
            // The store is still in the old format. We must perform a
            // reset inbox for the upgrade to be completed.
            String msg = Localization.getMessages().UPGRADE_REQUIRED;
            UIController.showModalAlert(msg);

            // Reset inbox
            try {
                // This is a non blocking operation, therefore the method exits
                // immediately. If the update is not performed properly (for
                // example because of no network connectivity) then the user is
                // left without any message.
                UIController.resetAccount();
                mailClientConfig.setStoreVersion(Store.LATEST_VERSION);
                ConfigManager.saveConfig();
                return true;
            } catch (Exception e) {
                Log.error("Error while resetting account: " + e.toString());
                return false;
            }

        } else {
            // No update is necessary
            return false;
        }

    }

    /**
     * @return the splashscreen.
     * this method is used by the threadpoolmonitor to ensure robustness
     * when catching exceptions thrown at startup
     */
    public Displayable getSplashScreen() {
        return splashScreen;
    }

    private class InitConfigManager {

        Funambol midlet;

        public InitConfigManager(Funambol midlet) {
            this.midlet = midlet;
        }

        private void initConfig() {

//            UIController.initThreadPool();

            //long startInitConfig = System.currentTimeMillis();
            // Mail Configuration initInbox
            // We check in Storage then in JAD and at the end load a default config
            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;

//isBlackberry
//             // For blackberry we set the scheduled check every ten minutes
//             // and revert to manual sync once CTP succesfully started
//             if (ConfigManager.getFirstRun()) {
//                 int minutes = 10;
//                 AlarmManager alarmManager = AlarmManager.getInstance();
//                 mailClientConfig.setPollInterval(minutes);
//                 mailClientConfig.enableScheduler(true);
//                 alarmManager.startSyncTimerTask(minutes);
//                 mailClientConfig.setNextTimeAlarm(
//                         alarmManager.getDateFromMinutes(minutes).getTime());
//             }
//

                if (mailClientConfig.isSchedulerEnabled()) {
                    setAlarm();
                }
                Log.setLogLevel(mailClientConfig.getLogLevel());

            }
        }

        private void setAlarm() {
            int minutes = mailClientConfig.getPollInterval();
            // if pollInterval is 0 (previous releases) set 10 minutes
            minutes = (minutes <= 0) ? 10 : minutes;
            AlarmManager alarmManager = AlarmManager.getInstance();
            if (minutes >= 1440) {
                int days = (mailClientConfig.getPollInterval() / 60) / 24;
                alarmManager.startSyncTimerTask(
                        new Date(mailClientConfig.getNextTimeAlarm()), days);
            } else {
                alarmManager.startSyncTimerTask(minutes);
            }
            //mailClientConfig.enableScheduler(true);
            mailClientConfig.setPollInterval(minutes);
        //mailClientConfig.setNextTimeAlarm(
        //        alarmManager.getDateFromMinutes(minutes).getTime());
        }

        private void registerConnection(String port) {
            //Checking Connection for Push registry listener
            String connection = "sms://:" + port;
            String midletName = UIController.midlet.getClass().getName();

            Log.debug("Connection: " + connection);
            Log.debug("MIDlet Name: " + midletName);

            try {
                PushRegistry.registerConnection(connection, midletName, "*");
            } catch (IllegalArgumentException iae) {
                Log.error("connection string or filter string " +
                        "not valid: " + iae);
                iae.printStackTrace();
            } catch (ConnectionNotFoundException cfe) {
                Log.error("Runtime system does not support push delivery " +
                        "for the requested connection protocol: " + cfe);
                cfe.printStackTrace();
            } catch (IOException ioe) {
            //If a connection has already been registered, ignores last
            //statement
            } catch (ClassNotFoundException cnf) {
                Log.error("Runtime system does not support push delivery " +
                        "for the requested connection protocol: " + cnf);
                cnf.printStackTrace();
            } catch (SecurityException se) {
                Log.error("User didn't grant permission to push " +
                        "listener: " + se);
            //se.printStackTrace();
            }
        }

        /**
         * This method initializes SMS push listening. The push listening can be
         * disabled, in such a case the method simply returns and does nothing.
         */
        public void initListener() {

            Log.debug("[FunambolMIDlet] initializing SMS listeners");

            //String smsPush = getAppProperty("MIDlet-Push-1");
            String smsPush = UIController.appProperties.get(AppProperties.MIDLET_PUSH_1);
//#ifdef PushRuntime
//#         smsPush = "true";
//#endif
            if (smsPush == null) {
                Log.debug("[FunambolMIDlet] SMS listening disabled");
                return;
            }

            //String sanPort = getAppProperty("SAN-Port");
            String sanPort = UIController.appProperties.get(AppProperties.SAN_PORT);
            if (sanPort == null) {
                sanPort = "50001";
                Log.info("[FunambolMIDlet] SAN port not defined in JAD, using default " + sanPort);
            }
            /** Connections detected at start up. */
            try {

//#ifdef PushRuntime
//#             registerConnection(sanPort);
//#endif

                // List all the registered connections
                String[] connections = PushRegistry.listConnections(false);
                otaService =
                        new OTAService(sanPort,
                        UIController.getThreadPool());

                otaService.setPushNotificationListener(messageHandler);
                //OTAConfigListener =
                //new OTAMessagesListener(getAppProperty("OTAC-Port"));

                if (connections == null || connections.length == 0) {
                    Log.info("[FunambolMIDlet] Application not registered for incoming SMS");
                } else {
                    Log.info(connections.length + " connections registered.");
                    // list only the active registered connections
                    connections = PushRegistry.listConnections(true);

                    //OTAConfigListener.init();
                    otaService.startService();
                    if (connections == null || connections.length == 0) {
                        Log.info("[FunambolMIDlet] No active connections.");
                        Log.info("[FunambolMIDlet] User started the application.");
                    } else {
                        Log.info(connections.length + " active connections.");
                        UIController.otaStarted = true;
                        Log.info("[FunambolMIDlet] Registry connection found: " + connections[connections.length - 1]);
                        if (connections[connections.length - 1].endsWith(sanPort)) {
                            OTAService.startViaOTA = true;
                            Log.info("[FunambolMIDlet] SAN Message started the application.");
                        } else {
                            Log.info("[FunambolMIDlet] 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();
                }
            }
        }
    }

    public void CTPClosed() {
        Log.error("CTP is closing connection, reschedule alarm.");
        cm.setAlarm();
    }
}

⌨️ 快捷键说明

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