de.enough.polish.android.messaging
Class MessageConnectionImpl

java.lang.Object
  extended by android.content.BroadcastReceiver
      extended by de.enough.polish.android.messaging.MessageConnectionImpl
All Implemented Interfaces:
Connection, MessageConnection

public class MessageConnectionImpl
extends android.content.BroadcastReceiver
implements MessageConnection

Provides MessageConnection functionalities. Note: is required for receiving SMS Note 2: when using the PushRegistry, we could register the service the Manifest.xml as well.


Field Summary
 
Fields inherited from interface de.enough.polish.android.messaging.MessageConnection
BINARY_MESSAGE, TEXT_MESSAGE
 
Constructor Summary
MessageConnectionImpl(String url, int mode, boolean timeouts)
           
 
Method Summary
 void close()
           
 Message newMessage(String type)
          Constructs a new message object of a given type.
 Message newMessage(String type, String address)
          Constructs a new Message object of a given type and initializes it with the given destination address.
 int numberOfSegments(Message msg)
          Returns the number of segments in the underlying protocol that would be needed for sending the specified Message.
 void onReceive(android.content.Context ctx, android.content.Intent intent)
           
 Message receive()
          Receives a message.
 void send(Message msg)
          Sends a message.
 void setMessageListener(MessageListener l)
          Registers a MessageListener object that the platform can notify when a message has been received on this MessageConnection.
 
Methods inherited from class android.content.BroadcastReceiver
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getResultCode, getResultData, getResultExtras, isInitialStickyBroadcast, isOrderedBroadcast, peekService, setDebugUnregister, setOrderedHint, setResult, setResultCode, setResultData, setResultExtras
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageConnectionImpl

public MessageConnectionImpl(String url,
                             int mode,
                             boolean timeouts)
Method Detail

newMessage

public Message newMessage(String type)
Description copied from interface: MessageConnection
Constructs a new message object of a given type. When the string text is passed in, the created object implements the TextMessage interface. When the binary constant is passed in, the created object implements the BinaryMessage interface. Adapter definitions for messaging protocols can define new constants and new subinterfaces for the Messages. The type strings are case-sensitive.

For adapter definitions that are not defined within the JCP process, the strings used MUST begin with an inverted domain name controlled by the defining organization, as is used for Java package names. Strings that do not contain a full stop character "." are reserved for specifications done within the JCP process and MUST NOT be used by other organizations defining adapter specification.

When this method is called from a client mode connection, the newly created Message has the destination address set to the address identified when this Connection was created.

When this method is called from a server mode connection, the newly created Message does not have the destination address set. It must be set by the application before trying to send the message.

Specified by:
newMessage in interface MessageConnection
Parameters:
type - - the type of message to be created. There are constants for basic types defined in this interface.
Returns:
Message object for a given type of message

newMessage

public Message newMessage(String type,
                          String address)
Description copied from interface: MessageConnection
Constructs a new Message object of a given type and initializes it with the given destination address. The semantics related to the parameter type are the same as for the method signature with just the type parameter.

Specified by:
newMessage in interface MessageConnection
Parameters:
type - - the type of message to be created. There are constants for basic types defined in this interface.
address - - destination address for the new message
Returns:
Message object for a given type of message
See Also:
MessageConnection.newMessage(String type)

numberOfSegments

public int numberOfSegments(Message msg)
Description copied from interface: MessageConnection
Returns the number of segments in the underlying protocol that would be needed for sending the specified Message.

Note that this method does not actually send the message. It will only calculate the number of protocol segments needed for sending the message.

This method will calculate the number of segments needed when this message is split into the protocol segments using the appropriate features of the underlying protocol. This method does not take into account possible limitations of the implementation that may limit the number of segments that can be sent using this feature. These limitations are protocol-specific and are documented with the adapter definition for that protocol.

Specified by:
numberOfSegments in interface MessageConnection
Parameters:
msg - - the message to be used for the calculation
Returns:
number of protocol segments needed for sending the message. Returns 0 if the Message object cannot be sent using the underlying protocol.

receive

public Message receive()
                throws IOException,
                       InterruptedIOException
Description copied from interface: MessageConnection
Receives a message.

If there are no Messages for this MessageConnection waiting, this method will block until either a message for this Connection is received or the MessageConnection is closed.

Specified by:
receive in interface MessageConnection
Returns:
a Message object representing the information in the received message
Throws:
IOException - - if an error occurs while receiving a message
InterruptedIOException - - if this MessageConnection object is closed during this receive method call
See Also:
MessageConnection.send(Message)

send

public void send(Message msg)
          throws IOException,
                 InterruptedIOException
Description copied from interface: MessageConnection
Sends a message.

Specified by:
send in interface MessageConnection
Parameters:
msg - - the message to be sent
Throws:
IOException - - if the message could not be sent or because of network failure
InterruptedIOException - - if a timeout occurs while either trying to send the message or if this Connection object is closed during this send operation
See Also:
MessageConnection.receive()

setMessageListener

public void setMessageListener(MessageListener l)
                        throws IOException
Description copied from interface: MessageConnection
Registers a MessageListener object that the platform can notify when a message has been received on this MessageConnection.

If there are incoming messages in the queue of this MessageConnection that have not been retrieved by the application prior to calling this method, the newly registered listener object will be notified immediately once for each such incoming message in the queue.

There can be at most one listener object registered for a MessageConnection object at any given point in time. Setting a new listener will de-register any previously set listener.

Passing null as the parameter will de-register any currenly registered listener.

Specified by:
setMessageListener in interface MessageConnection
Parameters:
l - - MessageListener object to be registered. If null, any currently registered listener will be de-registered and will not receive notifications.
Throws:
IOException - - if the connection has been closed, or if an attempt is made to register a listener on a client connection

close

public void close()
           throws IOException
Specified by:
close in interface Connection
Throws:
IOException

onReceive

public void onReceive(android.content.Context ctx,
                      android.content.Intent intent)
Specified by:
onReceive in class android.content.BroadcastReceiver