michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import android.content.BroadcastReceiver; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.content.IntentFilter; michael@0: import android.net.ConnectivityManager; michael@0: import android.net.NetworkInfo; michael@0: import android.util.Log; michael@0: michael@0: public class GeckoConnectivityReceiver extends BroadcastReceiver { michael@0: /* michael@0: * Keep the below constants in sync with michael@0: * http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsINetworkLinkService.idl michael@0: */ michael@0: private static final String LINK_DATA_UP = "up"; michael@0: private static final String LINK_DATA_DOWN = "down"; michael@0: private static final String LINK_DATA_UNKNOWN = "unknown"; michael@0: michael@0: private static final String LOGTAG = "GeckoConnectivityReceiver"; michael@0: michael@0: private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver(); michael@0: michael@0: private final IntentFilter mFilter; michael@0: private Context mApplicationContext; michael@0: private boolean mIsEnabled; michael@0: michael@0: public static GeckoConnectivityReceiver getInstance() { michael@0: return sInstance; michael@0: } michael@0: michael@0: private GeckoConnectivityReceiver() { michael@0: mFilter = new IntentFilter(); michael@0: mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); michael@0: } michael@0: michael@0: public synchronized void start(Context context) { michael@0: if (mIsEnabled) { michael@0: Log.w(LOGTAG, "Already started!"); michael@0: return; michael@0: } michael@0: michael@0: mApplicationContext = context.getApplicationContext(); michael@0: michael@0: // registerReceiver will return null if registering fails. michael@0: if (mApplicationContext.registerReceiver(this, mFilter) == null) { michael@0: Log.e(LOGTAG, "Registering receiver failed"); michael@0: } else { michael@0: mIsEnabled = true; michael@0: } michael@0: } michael@0: michael@0: public synchronized void stop() { michael@0: if (!mIsEnabled) { michael@0: Log.w(LOGTAG, "Already stopped!"); michael@0: return; michael@0: } michael@0: michael@0: mApplicationContext.unregisterReceiver(this); michael@0: mApplicationContext = null; michael@0: mIsEnabled = false; michael@0: } michael@0: michael@0: @Override michael@0: public void onReceive(Context context, Intent intent) { michael@0: ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); michael@0: NetworkInfo info = cm.getActiveNetworkInfo(); michael@0: michael@0: final String status; michael@0: if (info == null) { michael@0: status = LINK_DATA_UNKNOWN; michael@0: } else if (!info.isConnected()) { michael@0: status = LINK_DATA_DOWN; michael@0: } else { michael@0: status = LINK_DATA_UP; michael@0: } michael@0: michael@0: if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createNetworkLinkChangeEvent(status)); michael@0: } michael@0: } michael@0: }