aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/service/ConnectionManagementService.java
blob: 2f703064233990352c40f320152f73cf5fc216ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//
// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp 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 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp 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 RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import fil.libre.repwifiapp.Commons;
import fil.libre.repwifiapp.Prefs;
import fil.libre.repwifiapp.R;
import fil.libre.repwifiapp.Utils;
import fil.libre.repwifiapp.activities.MainActivity;
import fil.libre.repwifiapp.fwproxies.LinkAddressProxy;
import fil.libre.repwifiapp.fwproxies.LinkPropertiesProxy;
import fil.libre.repwifiapp.fwproxies.NetworkCapabilitiesProxy;
import fil.libre.repwifiapp.fwproxies.NetworkInfoProxy;
import fil.libre.repwifiapp.fwproxies.RepWifiNetworkAgent;
import fil.libre.repwifiapp.fwproxies.RouteInfoProxy;
import fil.libre.repwifiapp.helpers.Logger;
import fil.libre.repwifiapp.network.AccessPointInfo;
import fil.libre.repwifiapp.network.ConnectionResult;
import fil.libre.repwifiapp.network.ConnectionStatus;
import fil.libre.repwifiapp.network.Engine6p0;
import fil.libre.repwifiapp.network.IEngine;
import fil.libre.repwifiapp.network.NetworkManager;
import fil.libre.repwifiapp.network.WpaCli;
import fil.libre.repwifiapp.network.WpaSupplicant;
import fil.libre.repwifiapp.service.StatusManager.ConnectionStatusChangeListener;
import org.apache.http.conn.util.InetAddressUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class ConnectionManagementService extends Service implements ConnectionStatusChangeListener {

    public static final String LOG_TAG_SERVICE = "RepWifiConnectionManagementService";

    public static final String ACTION_DOMAIN = "fil.libre.repwifiapp.ConnectionService";
    public static final String ACTION_CONNECT = ACTION_DOMAIN + ".ACTION_CONNECT";
    public static final String ACTION_DISCONNECT = ACTION_DOMAIN + ".ACTION_DISCONNECT";
    public static final String ACTION_VOID = ACTION_DOMAIN + ".ACTION_VOID";

    public static final int MSG_BASE = 0;
    public static final int CMD_START_CONNECT = MSG_BASE + 1;
    public static final int CMD_ABORT_CONNECTION = MSG_BASE + 2;
    public static final int CMD_DISCONNECT = MSG_BASE + 3;
    public static final int CMD_GET_AVAILABLE_NETWORKS = MSG_BASE + 4;
    public static final int CMD_START_MONITOR_CONNECTION_STATUS = MSG_BASE + 5;
    public static final int CMD_STOP_MONITOR_CONNECTION_STATUS = MSG_BASE + 6;
    public static final int CMD_STATUS_UPDATE = MSG_BASE + 7;
    public static final int CMD_AUTOCONNECT = MSG_BASE + 8;
    public static final int CMD_CLIENT_UNBINDING = MSG_BASE + 9;

    /**
     * TODO:
     * Remove this command when a better model for the application's settings is
     * implemented using @ContentProvider.
     * For now, the UI will use this command to signal the service's process
     * when a change is detected in the @SharedPreferences.
     * A @ContentProvider should be used instead, to properly share the
     * application's settings between the UI process and the background service.
     * This command should be removed especially if the service is ever made
     * "exported", i.e. available to other applications, in order to prevent
     * external apps from tampering with the inner state of the service.
     */
    public static final int CMD_PREF_CHANGED = MSG_BASE + 10;

    public static final int MSG_STATUS_CHANGE = MSG_BASE + 1001;
    public static final int MSG_CONNECTION_RESULT = MSG_BASE + 1002;
    public static final int MSG_AVAILABLE_NETWORKS = MSG_BASE + 1003;
    public static final int MSG_AUTOCONNECT_REPORT = MSG_BASE + 1004;
    public static final int MSG_DISCONNECT_REPORT = MSG_BASE + 1005;

    /**
     * This message is returned to a calling client, upon reception of a Command
     * which the caller is not allowed to request, the original command id is
     * reported in "arg1" field of this reply message.
     */
    public static final int MSG_PERMISSION_DENIED = MSG_BASE + 1403;

    public static final int CHECK_STATUS_INTERVAL_SECS = 15;
    public static final String PLACEHOLDER_CHECK_STATUS_INTERVAL = "[CHK_STS_INTERVAL]";

    public static final String LOG_TAG_NETWORKAGENT = "RepWifiNetworkAgent";

    private RepWifiNetworkAgent currentNetworkAgent = null;
    private IEngine eng = null;
    private StatusManager smonitor;
    private ArrayList<Channel> statusWatchers = new ArrayList<Channel>();

    private final Messenger messenger = new Messenger(new Handler() {

        @Override
        public void handleMessage(Message msg) {

            Channel channel = new Channel(ConnectionManagementService.this, msg.replyTo);

            switch (msg.what) {
            case CMD_START_CONNECT:

                AccessPointInfo info = channel.getAccessPointInfoPayload(msg);
                if (info == null) {
                    Logger.logError("Received connect message without valid AccessPointInfo.");
                } else {
                    connect(info, channel);
                }
                break;

            case CMD_ABORT_CONNECTION:
                abortConnection();
                break;

            case CMD_DISCONNECT:
                disconnect(channel);
                break;

            case CMD_GET_AVAILABLE_NETWORKS:
                getAvailableNetworks(channel);
                break;

            case CMD_START_MONITOR_CONNECTION_STATUS:
                startMonitoringNetworkStatus(channel);
                break;

            case CMD_STOP_MONITOR_CONNECTION_STATUS:
                stopMonitoringNetworkStatus(channel);
                break;

            case CMD_STATUS_UPDATE:
                getStatus(channel);
                break;

            case CMD_CLIENT_UNBINDING:
                onClientUnbinding(channel);
                break;

            case CMD_PREF_CHANGED:
                String prefKey = channel.getStringPayload(msg, Channel.PAYLOAD_PREFKEY);
                onPreferenceChanged(prefKey);
                break;

            default:
                Logger.logError("Received message with unknown what: " + msg.what);
            }
        }

    });

    public ConnectionManagementService() {
    }

    @Override
    public void onCreate() {

        Commons.init(getApplicationContext());
        initEngine();

        Logger.APP_NAME = LOG_TAG_SERVICE;
        Logger.setLogPriority(Prefs.getLogPriority(getApplicationContext()));

        // Reset wpa_supplicant when the service is first run
        // This is needed in case the internal WifiManager is controlling the
        // current instance of wpa_supplicant.
        Utils.killBackEnd(getApplicationContext(), true);

        smonitor = new StatusManager(eng, this);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent == null) {
            Logger.logDebug("[WRN]Service started with null intent!");
            return START_STICKY;
        }

        String a = intent.getAction();

        if (a == null) {
            Logger.logDebug("[WRN] Service started with null action");
            return START_STICKY;
        }

        Logger.logDebug("Service started with action: " + a);

        if (a == ACTION_CONNECT) {
            handleActionConnect(intent.getExtras());

        } else if (a == ACTION_DISCONNECT) {
            handleActionDisconnect();

        } else if (a == ACTION_VOID) {
            // Just start the service and wait for bindings
            Logger.logDebug("Started with void action.");
        } else {
            Logger.logError("Unknown action " + a);
        }

        getStatus();
        return START_STICKY;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        smonitor.unsetListener();
    }

    private void handleActionConnect(Bundle xtras) {

        if (xtras == null || xtras.containsKey(Channel.PAYLOAD_APINFO)) {
            Logger.logError("Requested action connect without AccespointInfo extra!");
            return;
        }

        try {
            AccessPointInfo info = (AccessPointInfo) xtras.get(Channel.PAYLOAD_APINFO);

            connect(info);

        } catch (Exception e) {
            Logger.logError("Exception while extracting AccessPointInfo object from start intent's extras.",
                            e);
        }

    }

    private void handleActionDisconnect() {
        disconnect();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.messenger.getBinder();
    }

    private ConnectionResult connect(AccessPointInfo info, Channel callback) {

        initEngine();

        int result = eng.connect(info);
        ConnectionResult connectionResult = new ConnectionResult(result);
        ConnectionStatus status = null;

        if (result == ConnectionResult.CONN_OK) {

            Logger.logDebug("Result code CONN_OK");

            status = smonitor.getConnectionStatus();
            connectionResult.setStatus(status);

            if (info.needsPassword()) {

                if (status != null) {
                    // update APinfo with the right BSSID
                    info.setBssid(status.BSSID);
                }

                // Save network
                if (NetworkManager.save(info)) {
                    Logger.logDebug("Network saved: " + status.SSID);

                } else {
                    Logger.logError("FAILED saving network: " + status.SSID);

                }

            }

        }

        reportConnectionResult(connectionResult, callback);
        return connectionResult;
    }

    public ConnectionResult connect(AccessPointInfo info) {
        return connect(info, null);
    }

    /**
     * Attempts to connect to any nearby known network if found.
     * 
     * @return Returns null if a network is found and connected.
     *         Returns an empty array if no network is found.
     *         Returns an array of AccessPointInfo if no nearby network is
     *         known.
     */
    public AccessPointInfo[] autoConnect() {
        return autoConnect(null);
    }

    private AccessPointInfo[] autoConnect(Channel callback) {
        try {

            AccessPointInfo[] nets = eng.getAvailableNetworks();
            if (nets == null || nets.length == 0) {
                nets = new AccessPointInfo[] {};
            }

            for (AccessPointInfo i : nets) {

                if (NetworkManager.isKnown(i)) {
                    connect(i, null);
                    nets = null;
                }

            }

            // if no network is known, return available networks:
            if (callback != null) {
                reportAutoconnectResult(nets, callback);
            }
            return nets;

        } catch (Exception e) {
            Logger.logError("Error while autoconnecting", e);
            return null;
        }
    }

    private ConnectionStatus getStatus(Channel callback) {

        ConnectionStatus status = smonitor.getConnectionStatus();

        if (callback != null) {
            callback.sendMsg(MSG_STATUS_CHANGE, status, Channel.PAYLOAD_CONNSTATUS);
        }
        return status;

    }

    public ConnectionStatus getStatus() {
        return getStatus(null);
    }

    public void abortConnection() {
        initEngine();
        eng.abortConnection();
    }

    public boolean disconnect() {
        return disconnect(null);
    }

    private boolean disconnect(Channel callback) {

        initEngine();
        boolean res = eng.disconnect();
        ConnectionStatus status = getStatus();

        if (callback != null) {
            callback.sendMsg(MSG_DISCONNECT_REPORT, status, Channel.PAYLOAD_CONNSTATUS);
        }

        return res;
    }

    private AccessPointInfo[] getAvailableNetworks(Channel callback) {
        initEngine();
        AccessPointInfo[] nets = eng.getAvailableNetworks();
        if (callback != null) {
            callback.sendMsg(MSG_AVAILABLE_NETWORKS, nets, Channel.PAYLOAD_APINFO);
        }
        return nets;
    }

    public AccessPointInfo[] getAvailableNetworks() {
        return getAvailableNetworks(null);
    }

    private void initEngine() {
        if (eng == null) {
            eng = new Engine6p0();
        }
    }

    private void reportConnectionResult(ConnectionResult result, Channel callback) {
        callback.sendMsg(MSG_CONNECTION_RESULT, result, Channel.PAYLOAD_CONNRES);
    }

    private void reportAutoconnectResult(AccessPointInfo[] infos, Channel callback) {
        callback.sendMsg(MSG_AUTOCONNECT_REPORT, infos, Channel.PAYLOAD_APINFO);
    }

    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onConnectionStatusChange(ConnectionStatus status) {

        Logger.logDebug("Received connection status changed");
        notifyWifiState(status);
        updateNotification(status);
        reportNetworkStatus(status);

    }

    private void onClientUnbinding(Channel client) {

        if (client == null) {
            return;
        }

        Logger.logDebug("Processing client unbinding.. ");
        stopMonitoringNetworkStatus(client);

    }

    private boolean notifyWifiState(ConnectionStatus status) {

        try {

            if (status == null) {
                Logger.logDebug("Received null ConnectionStatus; using dummy status disconnected.");
                status = ConnectionStatus.getDummyDisconnected();
            }

            Logger.logDebug("Notifying wifi state with status object: " + status.toString());

            NetworkInfoProxy ni = NetworkInfoProxy.getForWifi();
            NetworkCapabilitiesProxy nc = new NetworkCapabilitiesProxy();
            LinkPropertiesProxy lp = new LinkPropertiesProxy();
            lp.setInterfaceName(WpaSupplicant.INTERFACE_NAME);

            if (status.isConnected()) {

                ni.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null);
                ni.setIsAvailable(true);

                if (!lp.addLinkAddress(new LinkAddressProxy(status.getInetAddress(), status
                                .getSubnetMaskInt()))) {
                    Logger.logError("Failed to add LinkAddress to LinkProperties.");
                    return false;
                }

                if (!lp.addRoute(new RouteInfoProxy(status.getGatewayInetAddress(),
                                WpaSupplicant.INTERFACE_NAME))) {
                    Logger.logError("Failed to add route to linkProperties");
                    return false;
                }

                InetAddress[] dnss = getUseableDnss(status.gateway);

                if (dnss == null || dnss.length == 0) {
                    Logger.logError("Received null or empty dns array");
                    return false;
                }

                for (InetAddress d : dnss) {
                    if (d != null && !lp.addDnsServer(d)) {
                        Logger.logError("Failed to add dns to LinkProperties.");
                        return false;
                    }
                }

                nc.addCapability(NetworkCapabilitiesProxy.NET_CAPABILITY_NOT_METERED);
                nc.addCapability(NetworkCapabilitiesProxy.NET_CAPABILITY_INTERNET);

                if (!agentIsAvailable()) {
                    Logger.logDebug("Willing to communicate netwtork connection, but no NetworkAgent available. Creating new NetworkAgent..");
                    createNetworkAgent(ni, lp, nc, 100);
                }

            } else if (agentIsAvailable()) {
                ni.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null);
                ni.setIsAvailable(true);

            } else {
                // status is "disconnected" and we have no active communication
                // channel with the ConnectivityService.
                // no need to establish a new channel just to communicate
                // disconnection:
                // ConnectivityService should know we're disconnected already.
                return true;
            }

            Logger.logDebug("About to call NetworkAgent.sendNetworkIngfo() connected="
                            + status.isConnected());

            currentNetworkAgent.sendNetworkInfo(ni.getNetworkInfo());
            Logger.logDebug("Called NetworkAgent.sendNetworkIngfo()..");

            return true;

        } catch (Exception e) {
            Logger.logError("FAIL registerNetworkAgent", e);
            return false;
        }

    }

    private boolean agentIsAvailable() {
        return (currentNetworkAgent != null && currentNetworkAgent.isChannellConnected());
    }

    private int createNetworkAgent(NetworkInfoProxy ni, LinkPropertiesProxy lp,
                    NetworkCapabilitiesProxy nc, int score) {

        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        Logger.logDebug("About to create new RepWifiNetworkAgent...");
        try {

            currentNetworkAgent = new RepWifiNetworkAgent(Looper.myLooper(),
                            getApplicationContext().getApplicationContext(), LOG_TAG_NETWORKAGENT,
                            ni.getNetworkInfo(), nc, lp, score);
            Logger.logDebug("Created RepWifiNetworkAgent, netId: " + currentNetworkAgent.netId);
            return currentNetworkAgent.netId;

        } catch (Exception e) {
            Logger.logError("Exception while creating RepWifiNetworkAgent", e);
            return -1;
        }

    }

    private String[] getConfiguredDnss() {

        // no more default DNS, it's a stupid thing to do.
        // instead, default to empty dns (using gateway as dns)
        // it should be up to the user to chose their own dns explicitly.
        String dns1 = Prefs.getString(getApplicationContext(),Prefs.PREF_DNS_1, "");
        String dns2 = Prefs.getString(getApplicationContext(), Prefs.PREF_DNS_2, "");

        if (dns1 == null || dns1.isEmpty()) {
            return null;
        }

        return new String[] { dns1, dns2 };

    }

    private InetAddress[] getUseableDnss(String gateway) {

        String[] dnss = getConfiguredDnss();

        if (dnss == null || dnss.length == 0) {
            // the DNS setting has been left blank
            // try to use the gateway as dns

            if (gateway == null || gateway.length() == 0) {
                // no possible DNS.
                return null;
            }

            dnss = new String[] { gateway, null };

        }

        InetAddress d1 = null;
        InetAddress d2 = null;

        if (InetAddressUtils.isIPv4Address(dnss[0])) {
            try {

                d1 = InetAddress.getByName(dnss[0]);

                if (dnss[1] != null && InetAddressUtils.isIPv4Address(dnss[1])) {

                    d2 = InetAddress.getByName(dnss[1]);

                }

            } catch (UnknownHostException e) {
                Logger.logError("Exception while parsing dns address!", e);
                return null;
            }

            return new InetAddress[] { d1, d2 };

        } else {
            Logger.logError("Wrong dns1 format!");
            return null;
        }

    }

    private void updateNotification(ConnectionStatus status) {

        if (status == null) {
            status = WpaCli.getConnectionStatus();
        }

        Notification.Builder builder = new Notification.Builder(getApplicationContext());

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
                        0);
        builder.setContentIntent(pendingIntent);

        int iconId = R.drawable.ic_stat_discon;
        String msg = "RepWifi";
        if (status != null) {
            if (status.isConnected()) {
                iconId = R.drawable.ic_stat_repwifi;
                msg += " - " + status.SSID;
            } else {
                msg += " - " + status.wpaStatus;
            }

        }

        builder.setSmallIcon(iconId);

        builder.setContentTitle(msg);
        builder.setContentText(getString(R.string.msg_touch_open));

        Notification n = builder.build();
        n.flags |= Notification.FLAG_NO_CLEAR;

        NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, n);

    }

    private void onPreferenceChanged(String prefName) {

        if (prefName == null) {
            Logger.logError("Received preference changed event, but prefName is null!");
            return;
        }

        if (prefName.equals(Prefs.PREF_MONITOR_NET_STATE)) {
            setMonitorNetworkStatus(Prefs.isNetworkStateMonitoringEnabled(getApplicationContext()));

        } else if (prefName.equals(Prefs.PREF_LOG_LEVEL)) {
            Logger.setLogPriority(Prefs.getLogPriority(getApplicationContext()));

        }

    }

    private boolean monitoringExplicitlyEnabled = false;

    private void setMonitorNetworkStatus(boolean enabled) {

        monitoringExplicitlyEnabled = enabled;
        if (enabled) {
            startMonitoringNetworkStatus(null);

        } else {
            stopMonitoringNetworkStatus(null);

        }
    }

    private void startMonitoringNetworkStatus(Channel watcher) {

        synchronized (statusWatchers) {
            if (watcher != null && !statusWatchers.contains(watcher)) {
                Logger.logDebug("Added watcher for network status: " + watcher.toString());
                statusWatchers.add(watcher);
            }
        }

        smonitor.startPolling(CHECK_STATUS_INTERVAL_SECS * 1000);

    }

    private void stopMonitoringNetworkStatus(Channel watcher) {

        synchronized (statusWatchers) {

            if (watcher != null && statusWatchers.remove(watcher)) {
                Logger.logDebug("Removed watcher for network status: " + watcher.toString());
            }

            if (statusWatchers.isEmpty() && !monitoringExplicitlyEnabled) {
                if (smonitor != null) {
                    smonitor.stopPolling();
                }
            }
        }

    }

    private void reportNetworkStatus(ConnectionStatus status) {

        synchronized (statusWatchers) {

            for (Channel m : statusWatchers) {
                if (!m.sendMsg(MSG_STATUS_CHANGE, status, Channel.PAYLOAD_CONNSTATUS)) {
                    // remove recipient from watchers as it's not able to
                    // receive messages anymore
                    statusWatchers.remove(m);
                }
            }
        }
    }

}