33 *
44 * All rights reserved. This program and the accompanying materials
55 * are made available under the terms of the Eclipse Public License v1.0
6- * and Eclipse Distribution License v1.0 which accompany this distribution.
6+ * and Eclipse Distribution License v1.0 which accompany this distribution.
77 *
8- * The Eclipse Public License is available at
8+ * The Eclipse Public License is available at
99 * http://www.eclipse.org/legal/epl-v10.html
10- * and the Eclipse Distribution License is available at
10+ * and the Eclipse Distribution License is available at
1111 * http://www.eclipse.org/org/documents/edl-v10.php.
12+ *
13+ * Contributors:
14+ * James Sutton - isOnline Null Pointer (bug 473775)
1215 */
1316package org .eclipse .paho .android .service ;
1417
3235import android .content .Intent ;
3336import android .content .IntentFilter ;
3437import android .net .ConnectivityManager ;
38+ import android .net .NetworkInfo ;
3539import android .os .Build ;
3640import android .os .Bundle ;
3741import android .os .IBinder ;
@@ -242,21 +246,21 @@ public class MqttService extends Service implements MqttTraceHandler {
242246 // android docs
243247 private BackgroundDataPreferenceReceiver backgroundDataPreferenceMonitor ;
244248 private volatile boolean backgroundDataEnabled = true ;
245-
249+
246250 // a way to pass ourself back to the activity
247251 private MqttServiceBinder mqttServiceBinder ;
248252
249253 // mapping from client handle strings to actual client connections.
250254 private Map <String /* clientHandle */ , MqttConnection /* client */ > connections = new ConcurrentHashMap <String , MqttConnection >();
251-
255+
252256 public MqttService () {
253257 super ();
254258 }
255259
256260 /**
257261 * pass data back to the Activity, by building a suitable Intent object and
258262 * broadcasting it
259- *
263+ *
260264 * @param clientHandle
261265 * source of the data
262266 * @param status
@@ -285,7 +289,7 @@ void callbackToActivity(String clientHandle, Status status,
285289
286290 /**
287291 * Get an MqttConnection object to represent a connection to a server
288- *
292+ *
289293 * @param serverURI specifies the protocol, host name and port to be used to connect to an MQTT server
290294 * @param clientId specifies the name by which this connection should be identified to the server
291295 * @param contextId specifies the app conext info to make a difference between apps
@@ -304,7 +308,7 @@ public String getClient(String serverURI, String clientId, String contextId,Mqtt
304308
305309 /**
306310 * Connect to the MQTT server specified by a particular client
307- *
311+ *
308312 * @param clientHandle
309313 * identifies the MqttConnection to use
310314 * @param connectOptions
@@ -321,9 +325,9 @@ public void connect(String clientHandle, MqttConnectOptions connectOptions,
321325 throws MqttSecurityException , MqttException {
322326 MqttConnection client = getConnection (clientHandle );
323327 client .connect (connectOptions , invocationContext , activityToken );
324-
328+
325329 }
326-
330+
327331 /**
328332 * Request all clients to reconnect if appropriate
329333 */
@@ -337,10 +341,10 @@ void reconnect() {
337341 }
338342 }
339343 }
340-
344+
341345 /**
342346 * Close connection from a particular client
343- *
347+ *
344348 * @param clientHandle
345349 * identifies the MqttConnection to use
346350 */
@@ -351,7 +355,7 @@ public void close(String clientHandle) {
351355
352356 /**
353357 * Disconnect from the server
354- *
358+ *
355359 * @param clientHandle
356360 * identifies the MqttConnection to use
357361 * @param invocationContext
@@ -365,7 +369,7 @@ public void disconnect(String clientHandle, String invocationContext,
365369 client .disconnect (invocationContext , activityToken );
366370 connections .remove (clientHandle );
367371
368-
372+
369373 // the activity has finished using us, so we can stop the service
370374 // the activities are bound with BIND_AUTO_CREATE, so the service will
371375 // remain around until the last activity disconnects
@@ -374,7 +378,7 @@ public void disconnect(String clientHandle, String invocationContext,
374378
375379 /**
376380 * Disconnect from the server
377- *
381+ *
378382 * @param clientHandle
379383 * identifies the MqttConnection to use
380384 * @param quiesceTimeout
@@ -398,7 +402,7 @@ public void disconnect(String clientHandle, long quiesceTimeout,
398402
399403 /**
400404 * Get the status of a specific client
401- *
405+ *
402406 * @param clientHandle
403407 * identifies the MqttConnection to use
404408 * @return true if the specified client is connected to an MQTT server
@@ -410,7 +414,7 @@ public boolean isConnected(String clientHandle) {
410414
411415 /**
412416 * Publish a message to a topic
413- *
417+ *
414418 * @param clientHandle
415419 * identifies the MqttConnection to use
416420 * @param topic
@@ -440,7 +444,7 @@ public IMqttDeliveryToken publish(String clientHandle, String topic,
440444
441445 /**
442446 * Publish a message to a topic
443- *
447+ *
444448 * @param clientHandle
445449 * identifies the MqttConnection to use
446450 * @param topic
@@ -464,7 +468,7 @@ public IMqttDeliveryToken publish(String clientHandle, String topic,
464468
465469 /**
466470 * Subscribe to a topic
467- *
471+ *
468472 * @param clientHandle
469473 * identifies the MqttConnection to use
470474 * @param topic
@@ -484,7 +488,7 @@ public void subscribe(String clientHandle, String topic, int qos,
484488
485489 /**
486490 * Subscribe to one or more topics
487- *
491+ *
488492 * @param clientHandle
489493 * identifies the MqttConnection to use
490494 * @param topic
@@ -504,7 +508,7 @@ public void subscribe(String clientHandle, String[] topic, int[] qos,
504508
505509 /**
506510 * Unsubscribe from a topic
507- *
511+ *
508512 * @param clientHandle
509513 * identifies the MqttConnection
510514 * @param topic
@@ -522,7 +526,7 @@ public void unsubscribe(String clientHandle, final String topic,
522526
523527 /**
524528 * Unsubscribe from one or more topics
525- *
529+ *
526530 * @param clientHandle
527531 * identifies the MqttConnection
528532 * @param topic
@@ -540,7 +544,7 @@ public void unsubscribe(String clientHandle, final String[] topic,
540544
541545 /**
542546 * Get tokens for all outstanding deliveries for a client
543- *
547+ *
544548 * @param clientHandle
545549 * identifies the MqttConnection
546550 * @return an array (possibly empty) of tokens
@@ -552,7 +556,7 @@ public IMqttDeliveryToken[] getPendingDeliveryTokens(String clientHandle) {
552556
553557 /**
554558 * Get the MqttConnection identified by this client handle
555- *
559+ *
556560 * @param clientHandle identifies the MqttConnection
557561 * @return the MqttConnection identified by this handle
558562 */
@@ -567,7 +571,7 @@ private MqttConnection getConnection(String clientHandle) {
567571 /**
568572 * Called by the Activity when a message has been passed back to the
569573 * application
570- *
574+ *
571575 * @param clientHandle identifier for the client which received the message
572576 * @param id identifier for the MQTT message
573577 */
@@ -616,7 +620,7 @@ public void onDestroy() {
616620 }
617621
618622 unregisterBroadcastReceivers ();
619-
623+
620624 if (this .messageStore !=null )
621625 this .messageStore .close ();
622626
@@ -645,14 +649,14 @@ public int onStartCommand(final Intent intent, int flags, final int startId) {
645649 // run till explicitly stopped, restart when
646650 // process restarted
647651 registerBroadcastReceivers ();
648-
652+
649653 return START_STICKY ;
650654 }
651655
652656 /**
653657 * Identify the callbackId to be passed when making tracing calls back into
654658 * the Activity
655- *
659+ *
656660 * @param traceCallbackId identifier to the callback into the Activity
657661 */
658662 public void setTraceCallbackId (String traceCallbackId ) {
@@ -661,16 +665,16 @@ public void setTraceCallbackId(String traceCallbackId) {
661665
662666 /**
663667 * Turn tracing on and off
664- *
668+ *
665669 * @param traceEnabled set <code>true</code> to turn on tracing, <code>false</code> to turn off tracing
666670 */
667671 public void setTraceEnabled (boolean traceEnabled ) {
668672 this .traceEnabled = traceEnabled ;
669673 }
670-
674+
671675 /**
672676 * Check whether trace is on or off.
673- *
677+ *
674678 * @return the state of trace
675679 */
676680 public boolean isTraceEnabled (){
@@ -679,7 +683,7 @@ public boolean isTraceEnabled(){
679683
680684 /**
681685 * Trace debugging information
682- *
686+ *
683687 * @param tag
684688 * identifier for the source of the trace
685689 * @param message
@@ -693,7 +697,7 @@ public void traceDebug(String tag, String message) {
693697
694698 /**
695699 * Trace error information
696- *
700+ *
697701 * @param tag
698702 * identifier for the source of the trace
699703 * @param message
@@ -718,7 +722,7 @@ private void traceCallback(String severity, String tag, String message) {
718722
719723 /**
720724 * trace exceptions
721- *
725+ *
722726 * @param tag
723727 * identifier for the source of the trace
724728 * @param message
@@ -733,13 +737,13 @@ public void traceException(String tag, String message, Exception e) {
733737 dataBundle .putString (MqttServiceConstants .CALLBACK_ACTION , MqttServiceConstants .TRACE_ACTION );
734738 dataBundle .putString (MqttServiceConstants .CALLBACK_TRACE_SEVERITY , MqttServiceConstants .TRACE_EXCEPTION );
735739 dataBundle .putString (MqttServiceConstants .CALLBACK_ERROR_MESSAGE , message );
736- dataBundle .putSerializable (MqttServiceConstants .CALLBACK_EXCEPTION , e ); //TODO: Check
740+ dataBundle .putSerializable (MqttServiceConstants .CALLBACK_EXCEPTION , e ); //TODO: Check
737741 dataBundle .putString (MqttServiceConstants .CALLBACK_TRACE_TAG , tag );
738742 //dataBundle.putString(MqttServiceConstants.CALLBACK_TRACE_ID, traceCallbackId);
739743 callbackToActivity (traceCallbackId , Status .ERROR , dataBundle );
740744 }
741745 }
742-
746+
743747 @ SuppressWarnings ("deprecation" )
744748 private void registerBroadcastReceivers () {
745749 if (networkConnectionMonitor == null ) {
@@ -761,13 +765,13 @@ private void registerBroadcastReceivers() {
761765 }
762766 }
763767 }
764-
768+
765769 private void unregisterBroadcastReceivers (){
766770 if (networkConnectionMonitor != null ){
767771 unregisterReceiver (networkConnectionMonitor );
768772 networkConnectionMonitor = null ;
769773 }
770-
774+
771775 if (Build .VERSION .SDK_INT < 14 /**Build.VERSION_CODES.ICE_CREAM_SANDWICH**/ ) {
772776 if (backgroundDataPreferenceMonitor != null ){
773777 unregisterReceiver (backgroundDataPreferenceMonitor );
@@ -801,26 +805,27 @@ public void onReceive(Context context, Intent intent) {
801805 } else {
802806 notifyClientsOffline ();
803807 }
804-
808+
805809 wl .release ();
806810 }
807811 }
808-
812+
809813 /**
810814 * @return whether the android service can be regarded as online
811815 */
812816 public boolean isOnline () {
813817 ConnectivityManager cm = (ConnectivityManager ) getSystemService (CONNECTIVITY_SERVICE );
814- if (cm .getActiveNetworkInfo () != null
815- && cm .getActiveNetworkInfo ().isAvailable ()
816- && cm .getActiveNetworkInfo ().isConnected ()
817- && backgroundDataEnabled ) {
818+ NetworkInfo networkInfo = cm .getActiveNetworkInfo ();
819+ if (networkInfo != null
820+ && networkInfo .isAvailable ()
821+ && networkInfo .isConnected ()
822+ && backgroundDataEnabled ) {
818823 return true ;
819824 }
820825
821826 return false ;
822827 }
823-
828+
824829 /**
825830 * Notify clients we're offline
826831 */
0 commit comments