📄 messagehandler.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.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.syncml.client.OTAConfigMessage;
import com.funambol.syncml.client.OTAConfigMessageParser;
import com.funambol.syncml.spds.SANMessage;
import com.funambol.syncml.spds.SANMessageParser;
import com.funambol.syncml.spds.SyncInfo;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.client.MessageParserException;
import com.funambol.syncml.protocol.SyncML;
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 {
// 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, IOException {
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);
try {
handleSANMessage(sanMessage);
} catch (ConfigException ex) {
Log.error("handleMessage(SAN): " + ex.toString());
ex.printStackTrace();
} catch (MailException ex) {
Log.error("handleMessage(SAN): " + ex.toString());
ex.printStackTrace();
}
} else if (parserType == OTA_CONFIG_MESSAGE) {
OTAConfigMessageParser otaConfigMessageParser =
new OTAConfigMessageParser(); // OTA - Over The Air Provisioning
OTAConfigMessage otaConfigMessage = (OTAConfigMessage)
otaConfigMessageParser.parseMessage(((BinaryMessage)msg).getPayloadData());
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);
try {
handleSANMessage(sanMessage);
} catch (ConfigException ex) {
Log.error("handleMessage(TextSAN): " + ex.toString());
ex.printStackTrace();
} catch (MailException ex) {
Log.error("handleMessage(TextSAN): " + ex.toString());
ex.printStackTrace();
}
}
}
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);
}
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
*/
private void handleSANMessage(SANMessage sanMessage)
throws SyncException, MailException, ConfigException {
Log.info("New SANMessage handling...");
//UIController.showAlert("New SMS with Server Alert Notification Received:\n" + sanMessage);
SyncInfo[] syncInfos = sanMessage.getSyncInfos();
if (syncInfos!=null) {
String serverUri = "";
for (int i=0; i<syncInfos.length;i++) {
serverUri = syncInfos[i].getServerUri();
Log.info("SyncInfo serverUri: [" + 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))) {
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(false);
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");
UIController.messageManager.send();
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");
UIController.messageManager.receive();
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 + -