mobile/android/base/GeckoConnectivityReceiver.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial