mobile/android/base/GeckoConnectivityReceiver.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import android.content.BroadcastReceiver;
michael@0 9 import android.content.Context;
michael@0 10 import android.content.Intent;
michael@0 11 import android.content.IntentFilter;
michael@0 12 import android.net.ConnectivityManager;
michael@0 13 import android.net.NetworkInfo;
michael@0 14 import android.util.Log;
michael@0 15
michael@0 16 public class GeckoConnectivityReceiver extends BroadcastReceiver {
michael@0 17 /*
michael@0 18 * Keep the below constants in sync with
michael@0 19 * http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsINetworkLinkService.idl
michael@0 20 */
michael@0 21 private static final String LINK_DATA_UP = "up";
michael@0 22 private static final String LINK_DATA_DOWN = "down";
michael@0 23 private static final String LINK_DATA_UNKNOWN = "unknown";
michael@0 24
michael@0 25 private static final String LOGTAG = "GeckoConnectivityReceiver";
michael@0 26
michael@0 27 private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver();
michael@0 28
michael@0 29 private final IntentFilter mFilter;
michael@0 30 private Context mApplicationContext;
michael@0 31 private boolean mIsEnabled;
michael@0 32
michael@0 33 public static GeckoConnectivityReceiver getInstance() {
michael@0 34 return sInstance;
michael@0 35 }
michael@0 36
michael@0 37 private GeckoConnectivityReceiver() {
michael@0 38 mFilter = new IntentFilter();
michael@0 39 mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
michael@0 40 }
michael@0 41
michael@0 42 public synchronized void start(Context context) {
michael@0 43 if (mIsEnabled) {
michael@0 44 Log.w(LOGTAG, "Already started!");
michael@0 45 return;
michael@0 46 }
michael@0 47
michael@0 48 mApplicationContext = context.getApplicationContext();
michael@0 49
michael@0 50 // registerReceiver will return null if registering fails.
michael@0 51 if (mApplicationContext.registerReceiver(this, mFilter) == null) {
michael@0 52 Log.e(LOGTAG, "Registering receiver failed");
michael@0 53 } else {
michael@0 54 mIsEnabled = true;
michael@0 55 }
michael@0 56 }
michael@0 57
michael@0 58 public synchronized void stop() {
michael@0 59 if (!mIsEnabled) {
michael@0 60 Log.w(LOGTAG, "Already stopped!");
michael@0 61 return;
michael@0 62 }
michael@0 63
michael@0 64 mApplicationContext.unregisterReceiver(this);
michael@0 65 mApplicationContext = null;
michael@0 66 mIsEnabled = false;
michael@0 67 }
michael@0 68
michael@0 69 @Override
michael@0 70 public void onReceive(Context context, Intent intent) {
michael@0 71 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
michael@0 72 NetworkInfo info = cm.getActiveNetworkInfo();
michael@0 73
michael@0 74 final String status;
michael@0 75 if (info == null) {
michael@0 76 status = LINK_DATA_UNKNOWN;
michael@0 77 } else if (!info.isConnected()) {
michael@0 78 status = LINK_DATA_DOWN;
michael@0 79 } else {
michael@0 80 status = LINK_DATA_UP;
michael@0 81 }
michael@0 82
michael@0 83 if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
michael@0 84 GeckoAppShell.sendEventToGecko(GeckoEvent.createNetworkLinkChangeEvent(status));
michael@0 85 }
michael@0 86 }
michael@0 87 }

mercurial