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.
This commit is contained in:
Doug Turner 2011-07-10 15:24:05 -07:00
Родитель 039573fbff
Коммит 7dbf23a135
17 изменённых файлов: 291 добавлений и 27 удалений

Просмотреть файл

@ -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.util.*;
import android.net.*;
import android.database.*;
import android.provider.*;
import android.telephony.*;
abstract public class GeckoApp
extends Activity
@ -77,6 +78,7 @@ abstract public class GeckoApp
public Handler mMainHandler;
private IntentFilter mConnectivityFilter;
private BroadcastReceiver mConnectivityReceiver;
private PhoneStateListener mPhoneStateListener;
enum LaunchState {PreLaunch, Launching, WaitButton,
Launched, GeckoRunning, GeckoExiting};
@ -232,6 +234,8 @@ abstract public class GeckoApp
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mConnectivityReceiver = new GeckoConnectivityReceiver();
mPhoneStateListener = new GeckoPhoneStateListener();
if (!checkAndSetLaunchState(LaunchState.PreLaunch,
LaunchState.Launching))
return;
@ -326,6 +330,10 @@ abstract public class GeckoApp
super.onPause();
unregisterReceiver(mConnectivityReceiver);
TelephonyManager tm = (TelephonyManager)
GeckoApp.mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
@Override
@ -344,6 +352,13 @@ abstract public class GeckoApp
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.graphics.*;
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 @@ public class 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 @@ public class 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 @@ public class GeckoAppShell
// 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 class GeckoAppShell
}
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 @@ nsNetworkLinkService::GetLinkStatusKnown(PRBool *aIsUp)
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,

Просмотреть файл

@ -43,6 +43,7 @@
#include "nsString.h"
#include "nsMaemoNetworkManager.h"
#include "mozilla/Services.h"
#include "nsCRT.h"
NS_IMPL_ISUPPORTS2(nsMaemoNetworkLinkService,
nsINetworkLinkService,
@ -70,6 +71,16 @@ nsMaemoNetworkLinkService::GetLinkStatusKnown(PRBool *aIsKnown)
return NS_OK;
}
NS_IMETHODIMP
nsMaemoNetworkLinkService::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
nsMaemoNetworkLinkService::Observe(nsISupports *aSubject,
const char *aTopic,

Просмотреть файл

@ -42,6 +42,7 @@
#include "nsServiceManagerUtils.h"
#include "nsString.h"
#include "mozilla/Services.h"
#include "nsCRT.h"
NS_IMPL_ISUPPORTS2(nsQtNetworkLinkService,
nsINetworkLinkService,
@ -69,6 +70,16 @@ nsQtNetworkLinkService::GetLinkStatusKnown(PRBool* aIsKnown)
return NS_OK;
}
NS_IMETHODIMP
nsQtNetworkLinkService::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
nsQtNetworkLinkService::Observe(nsISupports* aSubject,
const char* aTopic,

Просмотреть файл

@ -54,6 +54,7 @@
#include "nsString.h"
#include "nsAutoPtr.h"
#include "mozilla/Services.h"
#include "nsCRT.h"
#include <iptypes.h>
#include <iphlpapi.h>
@ -166,6 +167,16 @@ nsNotifyAddrListener::GetLinkStatusKnown(PRBool *aIsUp)
return NS_OK;
}
NS_IMETHODIMP
nsNotifyAddrListener::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
nsNotifyAddrListener::Run()
{

Просмотреть файл

@ -238,7 +238,7 @@ SHELL_WRAPPER0(onResume)
SHELL_WRAPPER0(onLowMemory)
SHELL_WRAPPER3(callObserver, jstring, jstring, jstring)
SHELL_WRAPPER1(removeObserver, jstring)
SHELL_WRAPPER1(onChangeNetworkLinkStatus, jstring)
SHELL_WRAPPER2(onChangeNetworkLinkStatus, jstring, jstring)
SHELL_WRAPPER1(reportJavaCrash, jstring)
static void * xul_handle = NULL;

Просмотреть файл

@ -45,6 +45,7 @@
#include "nsServiceManagerUtils.h"
#include "nsIObserverService.h"
#include "nsStringAPI.h"
#include "nsCRT.h"
// Define NetworkManager API constants. This avoids a dependency on
// NetworkManager-devel.
@ -87,6 +88,16 @@ nsNetworkManagerListener::GetLinkStatusKnown(PRBool* aKnown) {
return NS_OK;
}
nsresult
nsNetworkManagerListener::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;
}
nsresult
nsNetworkManagerListener::Init() {
mDBUS = nsDBusService::Get();

Просмотреть файл

@ -48,6 +48,7 @@
#include "nsOSHelperAppService.h"
#include "nsWindow.h"
#include "mozilla/Preferences.h"
#include "nsINetworkLinkService.h"
#ifdef DEBUG
#define ALOG_BRIDGE(args...) ALOG(args)
@ -141,6 +142,7 @@ AndroidBridge::Init(JNIEnv *jEnv,
jSetKeepScreenOn = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "setKeepScreenOn", "(Z)V");
jIsNetworkLinkUp = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "isNetworkLinkUp", "()Z");
jIsNetworkLinkKnown = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "isNetworkLinkKnown", "()Z");
jGetNetworkLinkType = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getNetworkLinkType", "()I");
jSetSelectedLocale = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "setSelectedLocale", "(Ljava/lang/String;)V");
jScanMedia = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "scanMedia", "(Ljava/lang/String;Ljava/lang/String;)V");
jGetSystemColors = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getSystemColors", "()[I");
@ -669,6 +671,13 @@ AndroidBridge::IsNetworkLinkKnown()
return !!mJNIEnv->CallStaticBooleanMethod(mGeckoAppShellClass, jIsNetworkLinkKnown);
}
int
AndroidBridge::GetNetworkLinkType()
{
ALOG_BRIDGE("AndroidBridge::GetNetworkLinkType");
return (int) mJNIEnv->CallStaticIntMethod(mGeckoAppShellClass, jGetNetworkLinkType);
}
void
AndroidBridge::SetSelectedLocale(const nsAString& aLocale)
{

Просмотреть файл

@ -204,6 +204,8 @@ public:
bool IsNetworkLinkKnown();
int GetNetworkLinkType();
void SetSelectedLocale(const nsAString&);
void GetSystemColors(AndroidSystemColors *aColors);
@ -312,6 +314,7 @@ protected:
jmethodID jSetKeepScreenOn;
jmethodID jIsNetworkLinkUp;
jmethodID jIsNetworkLinkKnown;
jmethodID jGetNetworkLinkType;
jmethodID jSetSelectedLocale;
jmethodID jScanMedia;
jmethodID jGetSystemColors;

Просмотреть файл

@ -69,7 +69,7 @@ extern "C" {
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_onLowMemory(JNIEnv *, jclass);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_callObserver(JNIEnv *, jclass, jstring observerKey, jstring topic, jstring data);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_removeObserver(JNIEnv *jenv, jclass, jstring jObserverKey);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *, jclass, jstring status);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *, jclass, jstring status, jstring type);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash(JNIEnv *, jclass, jstring stack);
}
@ -143,7 +143,7 @@ Java_org_mozilla_gecko_GeckoAppShell_removeObserver(JNIEnv *jenv, jclass, jstrin
}
NS_EXPORT void JNICALL
Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *jenv, jclass, jstring jStatus)
Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *jenv, jclass, jstring jStatus, jstring jType)
{
if (!nsAppShell::gAppShell)
return;
@ -153,6 +153,12 @@ Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *jenv, jcl
nsAppShell::gAppShell->NotifyObservers(nsnull,
NS_NETWORK_LINK_TOPIC,
sStatus.get());
nsJNIString sType(jType, jenv);
nsAppShell::gAppShell->NotifyObservers(nsnull,
NS_NETWORK_LINK_TYPE_TOPIC,
sType.get());
}
NS_EXPORT void JNICALL