1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/GeckoConnectivityReceiver.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import android.content.BroadcastReceiver; 1.12 +import android.content.Context; 1.13 +import android.content.Intent; 1.14 +import android.content.IntentFilter; 1.15 +import android.net.ConnectivityManager; 1.16 +import android.net.NetworkInfo; 1.17 +import android.util.Log; 1.18 + 1.19 +public class GeckoConnectivityReceiver extends BroadcastReceiver { 1.20 + /* 1.21 + * Keep the below constants in sync with 1.22 + * http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsINetworkLinkService.idl 1.23 + */ 1.24 + private static final String LINK_DATA_UP = "up"; 1.25 + private static final String LINK_DATA_DOWN = "down"; 1.26 + private static final String LINK_DATA_UNKNOWN = "unknown"; 1.27 + 1.28 + private static final String LOGTAG = "GeckoConnectivityReceiver"; 1.29 + 1.30 + private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver(); 1.31 + 1.32 + private final IntentFilter mFilter; 1.33 + private Context mApplicationContext; 1.34 + private boolean mIsEnabled; 1.35 + 1.36 + public static GeckoConnectivityReceiver getInstance() { 1.37 + return sInstance; 1.38 + } 1.39 + 1.40 + private GeckoConnectivityReceiver() { 1.41 + mFilter = new IntentFilter(); 1.42 + mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 1.43 + } 1.44 + 1.45 + public synchronized void start(Context context) { 1.46 + if (mIsEnabled) { 1.47 + Log.w(LOGTAG, "Already started!"); 1.48 + return; 1.49 + } 1.50 + 1.51 + mApplicationContext = context.getApplicationContext(); 1.52 + 1.53 + // registerReceiver will return null if registering fails. 1.54 + if (mApplicationContext.registerReceiver(this, mFilter) == null) { 1.55 + Log.e(LOGTAG, "Registering receiver failed"); 1.56 + } else { 1.57 + mIsEnabled = true; 1.58 + } 1.59 + } 1.60 + 1.61 + public synchronized void stop() { 1.62 + if (!mIsEnabled) { 1.63 + Log.w(LOGTAG, "Already stopped!"); 1.64 + return; 1.65 + } 1.66 + 1.67 + mApplicationContext.unregisterReceiver(this); 1.68 + mApplicationContext = null; 1.69 + mIsEnabled = false; 1.70 + } 1.71 + 1.72 + @Override 1.73 + public void onReceive(Context context, Intent intent) { 1.74 + ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 1.75 + NetworkInfo info = cm.getActiveNetworkInfo(); 1.76 + 1.77 + final String status; 1.78 + if (info == null) { 1.79 + status = LINK_DATA_UNKNOWN; 1.80 + } else if (!info.isConnected()) { 1.81 + status = LINK_DATA_DOWN; 1.82 + } else { 1.83 + status = LINK_DATA_UP; 1.84 + } 1.85 + 1.86 + if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { 1.87 + GeckoAppShell.sendEventToGecko(GeckoEvent.createNetworkLinkChangeEvent(status)); 1.88 + } 1.89 + } 1.90 +}