зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1286662 - Add native methods for GeckoNetworkManager notifications; r=snorp
Add and use onConnectionChanged and onStatusChanged native methods in GeckoNetworkManager in place of the network events in GeckoEvent.
This commit is contained in:
Родитель
44909016ff
Коммит
071e1acf10
|
@ -6,6 +6,7 @@
|
|||
package org.mozilla.gecko;
|
||||
|
||||
import org.mozilla.gecko.annotation.JNITarget;
|
||||
import org.mozilla.gecko.annotation.WrapForJNI;
|
||||
import org.mozilla.gecko.util.NativeEventListener;
|
||||
import org.mozilla.gecko.util.NativeJSObject;
|
||||
import org.mozilla.gecko.util.EventCallback;
|
||||
|
@ -72,8 +73,11 @@ public class GeckoNetworkManager extends BroadcastReceiver implements NativeEven
|
|||
|
||||
private ManagerState currentState = ManagerState.OffNoListeners;
|
||||
private ConnectionType currentConnectionType = ConnectionType.NONE;
|
||||
private NetworkStatus currentNetworkStatus = NetworkStatus.UNKNOWN;
|
||||
private ConnectionType previousConnectionType = ConnectionType.NONE;
|
||||
private ConnectionSubType currentConnectionSubtype = ConnectionSubType.UNKNOWN;
|
||||
private ConnectionSubType previousConnectionSubtype = ConnectionSubType.UNKNOWN;
|
||||
private NetworkStatus currentNetworkStatus = NetworkStatus.UNKNOWN;
|
||||
private NetworkStatus previousNetworkStatus = NetworkStatus.UNKNOWN;
|
||||
|
||||
private enum InfoType {
|
||||
MCC,
|
||||
|
@ -283,27 +287,52 @@ public class GeckoNetworkManager extends BroadcastReceiver implements NativeEven
|
|||
Log.d(LOGTAG, "New network state: " + currentNetworkStatus + ", " + currentConnectionType + ", " + currentConnectionSubtype);
|
||||
}
|
||||
|
||||
@WrapForJNI
|
||||
private static native void onConnectionChanged(int type, String subType,
|
||||
boolean isWifi, int DHCPGateway);
|
||||
|
||||
@WrapForJNI
|
||||
private static native void onStatusChanged(String status);
|
||||
|
||||
/**
|
||||
* Send current network state and connection type as a GeckoEvent, to whomever is listening.
|
||||
*/
|
||||
private void sendNetworkStateToListeners() {
|
||||
final Context applicationContext = GeckoAppShell.getApplicationContext();
|
||||
final GeckoEvent networkEvent = GeckoEvent.createNetworkEvent(
|
||||
currentConnectionType.value,
|
||||
currentConnectionType == ConnectionType.WIFI,
|
||||
wifiDhcpGatewayAddress(applicationContext),
|
||||
currentConnectionSubtype.value
|
||||
);
|
||||
final GeckoEvent networkLinkChangeValueEvent = GeckoEvent.createNetworkLinkChangeEvent(
|
||||
currentNetworkStatus.value
|
||||
);
|
||||
final GeckoEvent networkLinkChangeNotificationEvent = GeckoEvent.createNetworkLinkChangeEvent(
|
||||
LINK_DATA_CHANGED
|
||||
);
|
||||
if (currentConnectionType != previousConnectionType ||
|
||||
currentConnectionSubtype != previousConnectionSubtype) {
|
||||
previousConnectionType = currentConnectionType;
|
||||
previousConnectionSubtype = currentConnectionSubtype;
|
||||
|
||||
GeckoAppShell.sendEventToGecko(networkEvent);
|
||||
GeckoAppShell.sendEventToGecko(networkLinkChangeValueEvent);
|
||||
GeckoAppShell.sendEventToGecko(networkLinkChangeNotificationEvent);
|
||||
final boolean isWifi = currentConnectionType == ConnectionType.WIFI;
|
||||
final int gateway = !isWifi ? 0 :
|
||||
wifiDhcpGatewayAddress(GeckoAppShell.getApplicationContext());
|
||||
|
||||
if (GeckoThread.isRunning()) {
|
||||
onConnectionChanged(currentConnectionType.value,
|
||||
currentConnectionSubtype.value, isWifi, gateway);
|
||||
} else {
|
||||
GeckoThread.queueNativeCall(GeckoNetworkManager.class, "onConnectionChanged",
|
||||
currentConnectionType.value,
|
||||
String.class, currentConnectionSubtype.value,
|
||||
isWifi, gateway);
|
||||
}
|
||||
}
|
||||
|
||||
final String status;
|
||||
|
||||
if (currentNetworkStatus != previousNetworkStatus) {
|
||||
previousNetworkStatus = currentNetworkStatus;
|
||||
status = currentNetworkStatus.value;
|
||||
} else {
|
||||
status = LINK_DATA_CHANGED;
|
||||
}
|
||||
|
||||
if (GeckoThread.isRunning()) {
|
||||
onStatusChanged(status);
|
||||
} else {
|
||||
GeckoThread.queueNativeCall(GeckoNetworkManager.class, "onStatusChanged",
|
||||
String.class, status);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef GeckoNetworkManager_h
|
||||
#define GeckoNetworkManager_h
|
||||
|
||||
#include "GeneratedJNINatives.h"
|
||||
#include "nsAppShell.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsINetworkLinkService.h"
|
||||
|
||||
#include "mozilla/Services.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
class GeckoNetworkManager final
|
||||
: public widget::GeckoNetworkManager::Natives<GeckoNetworkManager>
|
||||
, public UsesGeckoThreadProxy
|
||||
{
|
||||
GeckoNetworkManager() = delete;
|
||||
|
||||
public:
|
||||
static void
|
||||
OnConnectionChanged(int32_t aType, jni::String::Param aSubType,
|
||||
bool aIsWifi, int32_t aGateway)
|
||||
{
|
||||
hal::NotifyNetworkChange(hal::NetworkInformation(
|
||||
aType, aIsWifi, aGateway));
|
||||
|
||||
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
||||
if (os) {
|
||||
os->NotifyObservers(nullptr,
|
||||
NS_NETWORK_LINK_TYPE_TOPIC,
|
||||
aSubType->ToString().get());
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
OnStatusChanged(jni::String::Param aStatus)
|
||||
{
|
||||
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
||||
if (os) {
|
||||
os->NotifyObservers(nullptr,
|
||||
NS_NETWORK_LINK_TOPIC,
|
||||
aStatus->ToString().get());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
#endif // GeckoNetworkManager_h
|
|
@ -61,6 +61,7 @@
|
|||
#endif
|
||||
|
||||
#include "ANRReporter.h"
|
||||
#include "GeckoNetworkManager.h"
|
||||
#include "PrefsHelper.h"
|
||||
|
||||
#ifdef DEBUG_ANDROID_EVENTS
|
||||
|
@ -385,6 +386,7 @@ nsAppShell::nsAppShell()
|
|||
GeckoAppShellSupport::Init();
|
||||
GeckoThreadSupport::Init();
|
||||
mozilla::ANRReporter::Init();
|
||||
mozilla::GeckoNetworkManager::Init();
|
||||
mozilla::PrefsHelper::Init();
|
||||
nsWindow::InitNatives();
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче