|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
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; |
|
15 |
|
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"; |
|
24 |
|
25 private static final String LOGTAG = "GeckoConnectivityReceiver"; |
|
26 |
|
27 private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver(); |
|
28 |
|
29 private final IntentFilter mFilter; |
|
30 private Context mApplicationContext; |
|
31 private boolean mIsEnabled; |
|
32 |
|
33 public static GeckoConnectivityReceiver getInstance() { |
|
34 return sInstance; |
|
35 } |
|
36 |
|
37 private GeckoConnectivityReceiver() { |
|
38 mFilter = new IntentFilter(); |
|
39 mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
|
40 } |
|
41 |
|
42 public synchronized void start(Context context) { |
|
43 if (mIsEnabled) { |
|
44 Log.w(LOGTAG, "Already started!"); |
|
45 return; |
|
46 } |
|
47 |
|
48 mApplicationContext = context.getApplicationContext(); |
|
49 |
|
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 } |
|
57 |
|
58 public synchronized void stop() { |
|
59 if (!mIsEnabled) { |
|
60 Log.w(LOGTAG, "Already stopped!"); |
|
61 return; |
|
62 } |
|
63 |
|
64 mApplicationContext.unregisterReceiver(this); |
|
65 mApplicationContext = null; |
|
66 mIsEnabled = false; |
|
67 } |
|
68 |
|
69 @Override |
|
70 public void onReceive(Context context, Intent intent) { |
|
71 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
|
72 NetworkInfo info = cm.getActiveNetworkInfo(); |
|
73 |
|
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 } |
|
82 |
|
83 if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { |
|
84 GeckoAppShell.sendEventToGecko(GeckoEvent.createNetworkLinkChangeEvent(status)); |
|
85 } |
|
86 } |
|
87 } |