Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* James Sutton - isOnline Null Pointer (bug 473775)
*/
package org.eclipse.paho.android.service;

Expand All @@ -32,6 +35,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
Expand Down Expand Up @@ -242,21 +246,21 @@ public class MqttService extends Service implements MqttTraceHandler {
// android docs
private BackgroundDataPreferenceReceiver backgroundDataPreferenceMonitor;
private volatile boolean backgroundDataEnabled = true;

// a way to pass ourself back to the activity
private MqttServiceBinder mqttServiceBinder;

// mapping from client handle strings to actual client connections.
private Map<String/* clientHandle */, MqttConnection/* client */> connections = new ConcurrentHashMap<String, MqttConnection>();

public MqttService() {
super();
}

/**
* pass data back to the Activity, by building a suitable Intent object and
* broadcasting it
*
*
* @param clientHandle
* source of the data
* @param status
Expand Down Expand Up @@ -285,7 +289,7 @@ void callbackToActivity(String clientHandle, Status status,

/**
* Get an MqttConnection object to represent a connection to a server
*
*
* @param serverURI specifies the protocol, host name and port to be used to connect to an MQTT server
* @param clientId specifies the name by which this connection should be identified to the server
* @param contextId specifies the app conext info to make a difference between apps
Expand All @@ -304,7 +308,7 @@ public String getClient(String serverURI, String clientId, String contextId,Mqtt

/**
* Connect to the MQTT server specified by a particular client
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param connectOptions
Expand All @@ -321,9 +325,9 @@ public void connect(String clientHandle, MqttConnectOptions connectOptions,
throws MqttSecurityException, MqttException {
MqttConnection client = getConnection(clientHandle);
client.connect(connectOptions, invocationContext, activityToken);

}

/**
* Request all clients to reconnect if appropriate
*/
Expand All @@ -337,10 +341,10 @@ void reconnect() {
}
}
}

/**
* Close connection from a particular client
*
*
* @param clientHandle
* identifies the MqttConnection to use
*/
Expand All @@ -351,7 +355,7 @@ public void close(String clientHandle) {

/**
* Disconnect from the server
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param invocationContext
Expand All @@ -365,7 +369,7 @@ public void disconnect(String clientHandle, String invocationContext,
client.disconnect(invocationContext, activityToken);
connections.remove(clientHandle);


// the activity has finished using us, so we can stop the service
// the activities are bound with BIND_AUTO_CREATE, so the service will
// remain around until the last activity disconnects
Expand All @@ -374,7 +378,7 @@ public void disconnect(String clientHandle, String invocationContext,

/**
* Disconnect from the server
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param quiesceTimeout
Expand All @@ -398,7 +402,7 @@ public void disconnect(String clientHandle, long quiesceTimeout,

/**
* Get the status of a specific client
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @return true if the specified client is connected to an MQTT server
Expand All @@ -410,7 +414,7 @@ public boolean isConnected(String clientHandle) {

/**
* Publish a message to a topic
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param topic
Expand Down Expand Up @@ -440,7 +444,7 @@ public IMqttDeliveryToken publish(String clientHandle, String topic,

/**
* Publish a message to a topic
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param topic
Expand All @@ -464,7 +468,7 @@ public IMqttDeliveryToken publish(String clientHandle, String topic,

/**
* Subscribe to a topic
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param topic
Expand All @@ -484,7 +488,7 @@ public void subscribe(String clientHandle, String topic, int qos,

/**
* Subscribe to one or more topics
*
*
* @param clientHandle
* identifies the MqttConnection to use
* @param topic
Expand All @@ -504,7 +508,7 @@ public void subscribe(String clientHandle, String[] topic, int[] qos,

/**
* Unsubscribe from a topic
*
*
* @param clientHandle
* identifies the MqttConnection
* @param topic
Expand All @@ -522,7 +526,7 @@ public void unsubscribe(String clientHandle, final String topic,

/**
* Unsubscribe from one or more topics
*
*
* @param clientHandle
* identifies the MqttConnection
* @param topic
Expand All @@ -540,7 +544,7 @@ public void unsubscribe(String clientHandle, final String[] topic,

/**
* Get tokens for all outstanding deliveries for a client
*
*
* @param clientHandle
* identifies the MqttConnection
* @return an array (possibly empty) of tokens
Expand All @@ -552,7 +556,7 @@ public IMqttDeliveryToken[] getPendingDeliveryTokens(String clientHandle) {

/**
* Get the MqttConnection identified by this client handle
*
*
* @param clientHandle identifies the MqttConnection
* @return the MqttConnection identified by this handle
*/
Expand All @@ -567,7 +571,7 @@ private MqttConnection getConnection(String clientHandle) {
/**
* Called by the Activity when a message has been passed back to the
* application
*
*
* @param clientHandle identifier for the client which received the message
* @param id identifier for the MQTT message
*/
Expand Down Expand Up @@ -616,7 +620,7 @@ public void onDestroy() {
}

unregisterBroadcastReceivers();

if (this.messageStore !=null )
this.messageStore.close();

Expand Down Expand Up @@ -645,14 +649,14 @@ public int onStartCommand(final Intent intent, int flags, final int startId) {
// run till explicitly stopped, restart when
// process restarted
registerBroadcastReceivers();

return START_STICKY;
}

/**
* Identify the callbackId to be passed when making tracing calls back into
* the Activity
*
*
* @param traceCallbackId identifier to the callback into the Activity
*/
public void setTraceCallbackId(String traceCallbackId) {
Expand All @@ -661,16 +665,16 @@ public void setTraceCallbackId(String traceCallbackId) {

/**
* Turn tracing on and off
*
*
* @param traceEnabled set <code>true</code> to turn on tracing, <code>false</code> to turn off tracing
*/
public void setTraceEnabled(boolean traceEnabled) {
this.traceEnabled = traceEnabled;
}

/**
* Check whether trace is on or off.
*
*
* @return the state of trace
*/
public boolean isTraceEnabled(){
Expand All @@ -679,7 +683,7 @@ public boolean isTraceEnabled(){

/**
* Trace debugging information
*
*
* @param tag
* identifier for the source of the trace
* @param message
Expand All @@ -693,7 +697,7 @@ public void traceDebug(String tag, String message) {

/**
* Trace error information
*
*
* @param tag
* identifier for the source of the trace
* @param message
Expand All @@ -718,7 +722,7 @@ private void traceCallback(String severity, String tag, String message) {

/**
* trace exceptions
*
*
* @param tag
* identifier for the source of the trace
* @param message
Expand All @@ -733,13 +737,13 @@ public void traceException(String tag, String message, Exception e) {
dataBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.TRACE_ACTION);
dataBundle.putString(MqttServiceConstants.CALLBACK_TRACE_SEVERITY, MqttServiceConstants.TRACE_EXCEPTION);
dataBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, message);
dataBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, e); //TODO: Check
dataBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, e); //TODO: Check
dataBundle.putString(MqttServiceConstants.CALLBACK_TRACE_TAG, tag);
//dataBundle.putString(MqttServiceConstants.CALLBACK_TRACE_ID, traceCallbackId);
callbackToActivity(traceCallbackId, Status.ERROR, dataBundle);
}
}

@SuppressWarnings("deprecation")
private void registerBroadcastReceivers() {
if (networkConnectionMonitor == null) {
Expand All @@ -761,13 +765,13 @@ private void registerBroadcastReceivers() {
}
}
}

private void unregisterBroadcastReceivers(){
if(networkConnectionMonitor != null){
unregisterReceiver(networkConnectionMonitor);
networkConnectionMonitor = null;
}

if (Build.VERSION.SDK_INT < 14 /**Build.VERSION_CODES.ICE_CREAM_SANDWICH**/) {
if(backgroundDataPreferenceMonitor != null){
unregisterReceiver(backgroundDataPreferenceMonitor);
Expand Down Expand Up @@ -801,26 +805,27 @@ public void onReceive(Context context, Intent intent) {
} else {
notifyClientsOffline();
}

wl.release();
}
}

/**
* @return whether the android service can be regarded as online
*/
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()
&& backgroundDataEnabled) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.isConnected()
&& backgroundDataEnabled) {
return true;
}

return false;
}

/**
* Notify clients we're offline
*/
Expand Down