📄 messagehandler.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.MailException;
import com.funambol.mailclient.config.*;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.syncml.MailSyncSource;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.push.MessageParserException;
import com.funambol.push.PushNotificationListener;
import com.funambol.push.SANMessage;
import com.funambol.push.SANMessageParser;
import com.funambol.push.SyncInfo;
import com.funambol.util.Log;
import java.io.IOException;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.TextMessage;
/**
* Class in charge of handling OTA messages.
* It selects the right parser to create related
* messages objects (OTA Configuration or Server Alert Notification)
*/
public class MessageHandler implements PushNotificationListener {
// SAN_MESSAGE: 0x85 10000101
private static int SAN_MESSAGE = 133;
// OTA_CONFIG_MESSAGE: 0xB6 10110110
private static int OTA_CONFIG_MESSAGE = 182;
private MailClientConfig config;
/** Creates a new instance of MessageHandler */
public MessageHandler() {
}
/**
* Select appropriate parser for OTA messages
*/
public void handleMessage(Message msg) throws MessageParserException {
// If a sync is already in progres, we just ignore this notification
if ( SyncClient.getSyncClient().isBusy() ) {
Log.info("[OTAMessageListener] " +
"sync in progress, notification ignored.");
return;
}
//#ifdef IgnoreSAN
//# Log.info("Ignoring SAN Message Content");
//# UIController.sync(false);
//#else
if (msg instanceof BinaryMessage) {
Log.info("Parsing BinaryMessage...");
/*
* byte n. 5 of binary payload determines notification type.
*
* The value contained in the byte array has to be
* considered as an unsigned byte. In Java integer types are
* all signed, so we have to tell the platform to treat the
* octet as an 'int', and not as a 'byte' type (a 'byte' can
* contain only values in the range -128/127). Bitwise
* operations in Java return 'long' or 'int' values. A
* bitwise AND with 11111111 (0xff, 255) lets the original
* octet to be interpreted as an 'int', granting so that
* values greater than 127 are preserved and not interpreted
* as negative numbers, as they would in the two's
* complement notation
*
*/
int parserType = ((BinaryMessage) msg).getPayloadData()[5] & 0xff;
String address = new String(msg.getAddress());
Log.info("Message received! From: " + address + " - " + "flag = " + parserType);
if (parserType == SAN_MESSAGE) {
// check if push sms enabled from config
//if (UIController.getConfig().isSmsListenerEnabled()) {
SANMessageParser sanMessageParser =
new SANMessageParser(); // SAN - Server Alerted Notification
byte[] messagePayload = ((BinaryMessage)msg).getPayloadData();
SANMessage sanMessage = (SANMessage)
sanMessageParser.parseMessage(messagePayload, true);
handleMessage(sanMessage);
} else if (parserType == OTA_CONFIG_MESSAGE) {
/* Commented until we enable this feature on server side for JavaME Client
*
OTAConfigMessageParser otaConfigMessageParser =
new OTAConfigMessageParser(); // OTA - Over The Air Provisioning
OTAConfigMessage otaConfigMessage = (OTAConfigMessage)
otaConfigMessageParser.parseMessage(((BinaryMessage)msg).getPayloadData(), true);
storeConfiguration(getConfiguration(otaConfigMessage));
*
*/
} else {
Log.error("No parser found for this message");
throw new MessageParserException("No parser found for this message");
}
}
// TODO: TextMessage management
else if (msg instanceof TextMessage) {
Log.info("Parsing TextMessage...");
SANMessageParser sanMessageParser =
new SANMessageParser(); // SAN - Server Alerted Notification
String messageText = ((TextMessage) msg).getPayloadText();
// TODO: extract private method parseHexString(messageText)
int msgLenght = messageText.length()/2;
byte[] payloadBytes = new byte[msgLenght];
String tmp = "";
for (int i=0; i<msgLenght*2; i+=2) {
tmp = messageText.substring(i, i+2);
payloadBytes[i/2] = (byte)Integer.parseInt(tmp, 16);
}
SANMessage sanMessage = (SANMessage)
sanMessageParser.parseMessage(payloadBytes, true);
handleMessage(sanMessage);
}
//#endif
}
private void storeConfiguration(MailClientConfig newConfig) {
Log.info("Storing new OTA configuration");
UIController.showAlert(Localization.getMessages().OTA_CONFIG_ALERT);
/* TODO: enable the following code only with actual SMS not tests*/
MailClientConfig configFromOTA = new MailClientConfig();
configFromOTA.setMailAccount(newConfig.getMailAccount());
UIController.saveNewConfig(configFromOTA);
}
/* Commented until we enable this feature on server side for JavaME Client
*
private MailClientConfig getConfiguration(OTAConfigMessage message) {
config = new MailClientConfig();
Account account = new Account();
account.setUser(message.getUserName());
account.setPassword(message.getPassword());
account.setName(message.getVisibleName());
account.setRemoteURI(message.getRemoteUri(OTAConfigMessage.MAIL));
account.setAddress(message.getMailAddress());
account.setUrl(message.getSyncUrl());
config.setMailAccount(account);
Log.info("New Configuration from SMS:\n"+config.toString());
return config;
}
*
*/
/**
* Method to manage the SAN Messages
*/
public void handleMessage(SANMessage sanMessage) {
Log.info("New SANMessage handling...");
//UIController.showAlert("New SMS with Server Alert Notification Received:\n" + sanMessage);
if (SyncClient.getSyncClient().isBusy() ) {
Log.info("[OTAMessageListener] " +
"sync in progress, notification ignored.");
return;
}
SyncInfo[] syncInfos = sanMessage.getSyncInfos();
if (syncInfos!=null) {
Log.debug("syncinfo length= " + syncInfos.length);
String serverUri = "";
for (int i=0; i<syncInfos.length;i++) {
serverUri = syncInfos[i].getServerUri();
Log.info("SyncInfo serverUri[" + i + "]: [" + serverUri + "]");
String remoteURI = UIController.mailClientConfig.getMailAccount().getRemoteURI();
Log.info("SyncInfo remoteURI from config: [" + remoteURI + "]");
Log.info("ALERT: " + syncInfos[i].getSyncType());
if ( !serverUri.equals("") && serverUri.equals(remoteURI) ) {
// Disable scheduled sync because push notification has arrived
// It means push is working and the user doesn't need scheduling
// This choice will change in next implementations when
// configuration sync will be implemented
Log.info("[MessageHandler] Disable alarm because of a push");
AlarmManager.getInstance().cancelTimerTask();
// UIController.getConfig().enableScheduler(false);
//Set the SAN sync initiator here
//The sync initiator is a push initiator
UIController.setSyncCaller(UIController.SAN_MESSAGE);
switch (syncInfos[i].getSyncType()) {
case SyncML.ALERT_CODE_SLOW:
case SyncML.ALERT_CODE_REFRESH_FROM_SERVER:
Log.info("init Store started by SAN message");
UIController.initStore(true);
case SyncML.ALERT_CODE_FAST:
case SyncML.ALERT_CODE_REFRESH_FROM_SERVER_BY_SERVER:
case SyncML.ALERT_CODE_TWO_WAY_BY_SERVER:
Log.info("sync started by SAN message");
UIController.sync(UIController.isFirstSync);
break;
case SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT:
case SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT_BY_SERVER:
Log.info("send invoked by SAN message");
//SyncClient.getSyncClient().sync(SyncClient.MESSAGES, SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT);
UIController.sync(SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT, false);
break;
case SyncML.ALERT_CODE_REFRESH_FROM_CLIENT:
case SyncML.ALERT_CODE_REFRESH_FROM_CLIENT_BY_SERVER:
break;
case SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER:
case SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER_BY_SERVER:
Log.info("receive invoked by SAN message");
//SyncClient.getSyncClient().sync(SyncClient.MESSAGES, SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER);
UIController.sync(SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER, false);
break;
default:
break;
}
} else {
Log.info("ServerURI not equals remoteURI stored in client. Sync not started.");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -