summaryrefslogtreecommitdiff
path: root/mobile/android/base/GeckoConnectivityReceiver.java
blob: 8c9d1b19ca1a647ad09c98a319aa938bdccfbfdd (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class GeckoConnectivityReceiver extends BroadcastReceiver {
    /*
     * Keep the below constants in sync with
     * http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsINetworkLinkService.idl
     */
    private static final String LINK_DATA_UP = "up";
    private static final String LINK_DATA_DOWN = "down";
    private static final String LINK_DATA_UNKNOWN = "unknown";

    private static final String LOGTAG = "GeckoConnectivityReceiver";

    private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver();

    private IntentFilter mFilter;
    private Context mApplicationContext;
    private boolean mIsEnabled;

    public static GeckoConnectivityReceiver getInstance() {
        return sInstance;
    }

    private GeckoConnectivityReceiver() {
        mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    }

    public void init(Context context) {
        mApplicationContext = context.getApplicationContext();
    }

    public synchronized void start() {
        if (!mIsEnabled) {
            // registerReceiver will return null if registering fails
            if (mApplicationContext.registerReceiver(this, mFilter) == null) {
                Log.e(LOGTAG, "Registering receiver failed");
            } else {
                mIsEnabled = true;
            }
        }
    }

    public synchronized void stop() {
        if (mIsEnabled) {
            mApplicationContext.unregisterReceiver(this);
            mIsEnabled = false;
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();

        String status;
        if (info == null) {
            status = LINK_DATA_UNKNOWN;
        } else if (!info.isConnected()) {
            status = LINK_DATA_DOWN;
        } else {
            status = LINK_DATA_UP;
        }

        if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
            GeckoAppShell.sendEventToGecko(GeckoEvent.createNetworkLinkChangeEvent(status));
        }
    }
}