Permalink
Browse files

Bug 667980: Expose network connection type to chrome. r=dougt

Add an attribute to nsINetworkLinkService that represents the connection type,
and add code to the android back-end to expose the connection type and notify
when it changes.
  • Loading branch information...
dougt committed Jul 10, 2011
1 parent 78f11dd commit 3c5b5c5560f16f1f95c66a3ec5870d2e29b05825
@@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+ <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-feature android:name="android.hardware.location" android:required="false"/>
@@ -61,6 +61,7 @@
import android.net.*;
import android.database.*;
import android.provider.*;
+import android.telephony.*;
abstract public class GeckoApp
extends Activity
@@ -77,6 +78,7 @@
public Handler mMainHandler;
private IntentFilter mConnectivityFilter;
private BroadcastReceiver mConnectivityReceiver;
+ private PhoneStateListener mPhoneStateListener;
enum LaunchState {PreLaunch, Launching, WaitButton,
Launched, GeckoRunning, GeckoExiting};
@@ -232,6 +234,8 @@ public void run() {
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mConnectivityReceiver = new GeckoConnectivityReceiver();
+ mPhoneStateListener = new GeckoPhoneStateListener();
+
if (!checkAndSetLaunchState(LaunchState.PreLaunch,
LaunchState.Launching))
return;
@@ -326,6 +330,10 @@ public void onPause()
super.onPause();
unregisterReceiver(mConnectivityReceiver);
+
+ TelephonyManager tm = (TelephonyManager)
+ GeckoApp.mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
+ tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
@Override
@@ -344,6 +352,13 @@ public void onResume()
onNewIntent(getIntent());
registerReceiver(mConnectivityReceiver, mConnectivityFilter);
+
+ TelephonyManager tm = (TelephonyManager)
+ GeckoApp.mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
+ tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
+
+ // Notify if network state changed since we paused
+ GeckoAppShell.onNetworkStateChange(true);
}
@Override
@@ -58,6 +58,7 @@
import android.widget.*;
import android.hardware.*;
import android.location.*;
+import android.telephony.*;
import android.webkit.MimeTypeMap;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
@@ -94,6 +95,10 @@ private GeckoAppShell() { }
static private File sCacheFile = null;
static private int sFreeSpace = -1;
+ static private String sNetworkState = "unknown";
+ static private String sNetworkType = "unknown";
+ static private int sNetworkTypeCode = 0;
+
/* The Android-side API: API methods that Android calls */
// Initialization methods
@@ -108,7 +113,7 @@ private GeckoAppShell() { }
public static native void callObserver(String observerKey, String topic, String data);
public static native void removeObserver(String observerKey);
public static native void loadLibs(String apkName, boolean shouldExtract);
- public static native void onChangeNetworkLinkStatus(String status);
+ public static native void onChangeNetworkLinkStatus(String status, String type);
public static native void reportJavaCrash(String stack);
// A looper thread, accessed by GeckoAppShell.getHandler
@@ -654,6 +659,9 @@ static void onAppShellReady()
// mLaunchState can only be Launched at this point
GeckoApp.setLaunchState(GeckoApp.LaunchState.GeckoRunning);
sendPendingEventsToGecko();
+
+ // Refresh the network connectivity state
+ onNetworkStateChange(false);
}
static void onXreExit() {
@@ -1010,20 +1018,92 @@ public void run() {
}
public static boolean isNetworkLinkUp() {
- ConnectivityManager cm = (ConnectivityManager)
- GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- if (info == null || !info.isConnected())
+ if (sNetworkState == "up")
+ return true;
+ return false;
+ }
+
+ public static boolean isNetworkLinkKnown() {
+ if (sNetworkState == "unknown")
return false;
return true;
}
- public static boolean isNetworkLinkKnown() {
+ public static int getNetworkLinkType() {
+ return sNetworkTypeCode;
+ }
+
+ public static void onNetworkStateChange(boolean notifyChanged) {
+ String state;
+ String type;
+ int typeCode;
+
ConnectivityManager cm = (ConnectivityManager)
GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm.getActiveNetworkInfo() == null)
- return false;
- return true;
+ NetworkInfo info = cm.getActiveNetworkInfo();
+
+ // Note, these strings and codes correspond to those specified in
+ // nsINetworkLinkService. Make sure to keep them in sync!
+ type = "unknown";
+ typeCode = 0;
+ if (info == null) {
+ state = "unknown";
+ } else if (!info.isConnected()) {
+ state = "down";
+ } else {
+ state = "up";
+
+ int androidType = info.getType();
+
+ if (androidType == ConnectivityManager.TYPE_WIFI) {
+ type = "wifi";
+ typeCode = 3;
+ } else if (androidType == ConnectivityManager.TYPE_WIMAX) {
+ type = "wimax";
+ typeCode = 4;
+ } else if (androidType == ConnectivityManager.TYPE_MOBILE) {
+ TelephonyManager tm = (TelephonyManager)
+ GeckoApp.mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
+ typeCode = tm.getNetworkType();
+
+ // Note that the value of some of these constants are used due
+ // to not all of these existing in API level 8.
+ //
+ // In particular, EVDO_B appears at level 9, and EHRPD and LTE
+ // appear at level 11.
+ if (androidType == TelephonyManager.NETWORK_TYPE_GPRS ||
+ androidType == TelephonyManager.NETWORK_TYPE_EDGE ||
+ androidType == TelephonyManager.NETWORK_TYPE_CDMA ||
+ androidType == TelephonyManager.NETWORK_TYPE_IDEN ||
+ androidType == TelephonyManager.NETWORK_TYPE_1xRTT) {
+ type = "2g";
+ typeCode = 5;
+ } else if (androidType == TelephonyManager.NETWORK_TYPE_UMTS ||
+ androidType == TelephonyManager.NETWORK_TYPE_HSDPA ||
+ androidType == TelephonyManager.NETWORK_TYPE_HSUPA ||
+ androidType == TelephonyManager.NETWORK_TYPE_HSPA ||
+ androidType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
+ androidType == TelephonyManager.NETWORK_TYPE_EVDO_A ||
+ androidType == 12 || // TelephonyManager.NETWORK_TYPE_EVDO_B
+ androidType == 14) { // TelephonyManager.NETWORK_TYPE_EHRPD
+ type = "3g";
+ typeCode = 6;
+ } else if (androidType == 13) { // TelephonyManager.NETWORK_TYPE_LTE
+ type = "4g";
+ typeCode = 7;
+ }
+ }
+ }
+
+ // If the network state has changed, notify Gecko
+ if (notifyChanged && (state != sNetworkState || typeCode != sNetworkTypeCode)) {
+ Log.i("GeckoAppShell", "Network state changed: (" + state + ", " + type + ") ");
+ sNetworkState = state;
+ sNetworkType = type;
+ sNetworkTypeCode = typeCode;
+ if (GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning))
+ onChangeNetworkLinkStatus(sNetworkState, sNetworkType);
+ }
}
public static void setSelectedLocale(String localeCode) {
@@ -38,25 +38,12 @@
package org.mozilla.gecko;
import android.content.*;
-import android.net.*;
public class GeckoConnectivityReceiver
extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
- String status;
- ConnectivityManager cm = (ConnectivityManager)
- context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- if (info == null)
- status = "unknown";
- else if (!info.isConnected())
- status = "down";
- else
- status = "up";
-
- if (GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning))
- GeckoAppShell.onChangeNetworkLinkStatus(status);
+ GeckoAppShell.onNetworkStateChange(true);
}
}
@@ -0,0 +1,50 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Android code.
+ *
+ * The Initial Developer of the Original Code is Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Chris Lord <chrislord.net@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+package org.mozilla.gecko;
+
+import android.telephony.*;
+
+public class GeckoPhoneStateListener
+ extends PhoneStateListener
+{
+ @Override
+ public void onDataConnectionStateChanged(int state, int networkType) {
+ GeckoAppShell.onNetworkStateChange(true);
+ }
+}
+
@@ -52,6 +52,7 @@ JAVAFILES = \
GeckoEvent.java \
GeckoSurfaceView.java \
GeckoInputConnection.java \
+ GeckoPhoneStateListener.java \
AlertNotification.java \
$(NULL)
@@ -42,9 +42,19 @@
/**
* Network link status monitoring service.
*/
-[scriptable, uuid(61618a52-ea91-4277-a4ab-ebe10d7b9a64)]
+[scriptable, uuid(f7d3be87-7403-4a1e-b89f-2797776e9b08)]
interface nsINetworkLinkService : nsISupports
{
+ /* Link type constants */
+ const unsigned long LINK_TYPE_UNKNOWN = 0;
+ const unsigned long LINK_TYPE_ETHERNET = 1;
+ const unsigned long LINK_TYPE_USB = 2;
+ const unsigned long LINK_TYPE_WIFI = 3;
+ const unsigned long LINK_TYPE_WIMAX = 4;
+ const unsigned long LINK_TYPE_2G = 5;
+ const unsigned long LINK_TYPE_3G = 6;
+ const unsigned long LINK_TYPE_4G = 7;
+
/**
* This is set to true when the system is believed to have a usable
* network connection.
@@ -62,6 +72,11 @@ interface nsINetworkLinkService : nsISupports
* This is set to true when we believe that isLinkUp is accurate.
*/
readonly attribute boolean linkStatusKnown;
+
+ /**
+ * The type of network connection.
+ */
+ readonly attribute unsigned long linkType;
};
%{C++
@@ -85,4 +100,36 @@ interface nsINetworkLinkService : nsISupports
* linkStatusKnown is now false.
*/
#define NS_NETWORK_LINK_DATA_UNKNOWN "unknown"
+
+/**
+ * We send notifications through nsIObserverService with topic
+ * NS_NETWORK_LINK_TYPE_TOPIC whenever the network connection type
+ * changes. We pass one of the valid connection type constants
+ * below as the aData parameter of the notification.
+ */
+#define NS_NETWORK_LINK_TYPE_TOPIC "network:link-type-changed"
+
+/** We were unable to determine the network connection type */
+#define NS_NETWORK_LINK_TYPE_UNKNOWN "unknown"
+
+/** A standard wired ethernet connection */
+#define NS_NETWORK_LINK_TYPE_ETHERNET "ethernet"
+
+/** A connection via a USB port */
+#define NS_NETWORK_LINK_TYPE_USB "usb"
+
+/** A connection via a WiFi access point (IEEE802.11) */
+#define NS_NETWORK_LINK_TYPE_WIFI "wifi"
+
+/** A connection via WiMax (IEEE802.16) */
+#define NS_NETWORK_LINK_TYPE_WIMAX "wimax"
+
+/** A '2G' mobile connection (e.g. GSM, GPRS, EDGE) */
+#define NS_NETWORK_LINK_TYPE_2G "2g"
+
+/** A '3G' mobile connection (e.g. UMTS, CDMA) */
+#define NS_NETWORK_LINK_TYPE_3G "3g"
+
+/** A '4G' mobile connection (e.g. LTE, UMB) */
+#define NS_NETWORK_LINK_TYPE_4G "4g"
%}
@@ -70,3 +70,13 @@ nsAndroidNetworkLinkService::GetLinkStatusKnown(PRBool *aIsKnown)
*aIsKnown = mozilla::AndroidBridge::Bridge()->IsNetworkLinkKnown();
return NS_OK;
}
+
+NS_IMETHODIMP
+nsAndroidNetworkLinkService::GetLinkType(PRUint32 *aLinkType)
+{
+ NS_ENSURE_ARG_POINTER(aLinkType);
+ NS_ENSURE_TRUE(mozilla::AndroidBridge::Bridge(), NS_ERROR_UNEXPECTED);
+
+ *aLinkType = mozilla::AndroidBridge::Bridge()->GetNetworkLinkType();
+ return NS_OK;
+}
@@ -41,6 +41,7 @@
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsString.h"
+#include "nsCRT.h"
#import <Cocoa/Cocoa.h>
#import <netinet/in.h>
@@ -75,6 +76,16 @@
return NS_OK;
}
+NS_IMETHODIMP
+nsNetworkLinkService::GetLinkType(PRUint32 *aLinkType)
+{
+ NS_ENSURE_ARG_POINTER(aLinkType);
+
+ // XXX This function has not yet been implemented for this platform
+ *aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
+ return NS_OK;
+}
+
NS_IMETHODIMP
nsNetworkLinkService::Observe(nsISupports *subject,
const char *topic,
Oops, something went wrong.

0 comments on commit 3c5b5c5

Please sign in to comment.