You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
implement ConnectivityManager using GNetworkMonitor
This commit is contained in:
@@ -103,6 +103,7 @@ libtranslationlayer_so = shared_library('translation_layer_main', [
|
|||||||
'src/api-impl-jni/graphics/android_graphics_drawable_DrawableContainer.c',
|
'src/api-impl-jni/graphics/android_graphics_drawable_DrawableContainer.c',
|
||||||
'src/api-impl-jni/location/android_location_LocationManager.c',
|
'src/api-impl-jni/location/android_location_LocationManager.c',
|
||||||
'src/api-impl-jni/media/android_media_MediaCodec.c',
|
'src/api-impl-jni/media/android_media_MediaCodec.c',
|
||||||
|
'src/api-impl-jni/net/android_net_ConnectivityManager.c',
|
||||||
'src/api-impl-jni/sensors/android_hardware_SensorManager.c',
|
'src/api-impl-jni/sensors/android_hardware_SensorManager.c',
|
||||||
'src/api-impl-jni/util.c',
|
'src/api-impl-jni/util.c',
|
||||||
'src/api-impl-jni/views/AndroidLayout.c',
|
'src/api-impl-jni/views/AndroidLayout.c',
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||||
|
#include <jni.h>
|
||||||
|
/* Header for class android_net_ConnectivityManager */
|
||||||
|
|
||||||
|
#ifndef _Included_android_net_ConnectivityManager
|
||||||
|
#define _Included_android_net_ConnectivityManager
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
* Class: android_net_ConnectivityManager
|
||||||
|
* Method: registerNetworkCallback
|
||||||
|
* Signature: (Landroid/net/NetworkRequest;Landroid/net/ConnectivityManager/NetworkCallback;)V
|
||||||
|
*/
|
||||||
|
JNIEXPORT void JNICALL Java_android_net_ConnectivityManager_registerNetworkCallback
|
||||||
|
(JNIEnv *, jobject, jobject, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_net_ConnectivityManager
|
||||||
|
* Method: isActiveNetworkMetered
|
||||||
|
* Signature: ()Z
|
||||||
|
*/
|
||||||
|
JNIEXPORT jboolean JNICALL Java_android_net_ConnectivityManager_isActiveNetworkMetered
|
||||||
|
(JNIEnv *, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_net_ConnectivityManager
|
||||||
|
* Method: nativeGetNetworkAvailable
|
||||||
|
* Signature: ()Z
|
||||||
|
*/
|
||||||
|
JNIEXPORT jboolean JNICALL Java_android_net_ConnectivityManager_nativeGetNetworkAvailable
|
||||||
|
(JNIEnv *, jobject);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
35
src/api-impl-jni/net/android_net_ConnectivityManager.c
Normal file
35
src/api-impl-jni/net/android_net_ConnectivityManager.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include <gio/gio.h>
|
||||||
|
|
||||||
|
#include "../defines.h"
|
||||||
|
#include "../util.h"
|
||||||
|
|
||||||
|
#include "../generated_headers/android_net_ConnectivityManager.h"
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL Java_android_net_ConnectivityManager_isActiveNetworkMetered(JNIEnv *env, jobject this)
|
||||||
|
{
|
||||||
|
return g_network_monitor_get_network_metered(g_network_monitor_get_default());
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL Java_android_net_ConnectivityManager_nativeGetNetworkAvailable(JNIEnv *env, jobject this)
|
||||||
|
{
|
||||||
|
return g_network_monitor_get_network_available(g_network_monitor_get_default());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_network_changed(GNetworkMonitor* self, gboolean network_available, jobject callback)
|
||||||
|
{
|
||||||
|
JNIEnv *env = get_jni_env();
|
||||||
|
jmethodID method;
|
||||||
|
if (network_available) {
|
||||||
|
method = _METHOD(_CLASS(callback), "onAvailable", "(Landroid/net/Network;)V");
|
||||||
|
} else {
|
||||||
|
method = _METHOD(_CLASS(callback), "onLost", "(Landroid/net/Network;)V");
|
||||||
|
}
|
||||||
|
(*env)->CallVoidMethod(env, callback, method, NULL);
|
||||||
|
if ((*env)->ExceptionCheck(env))
|
||||||
|
(*env)->ExceptionDescribe(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL Java_android_net_ConnectivityManager_registerNetworkCallback(JNIEnv *env, jobject this, jobject request, jobject callback)
|
||||||
|
{
|
||||||
|
g_signal_connect(g_network_monitor_get_default(), "network-changed", G_CALLBACK(on_network_changed), _REF(callback));
|
||||||
|
}
|
||||||
@@ -1,20 +1,25 @@
|
|||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
|
class Network {}
|
||||||
|
|
||||||
public class ConnectivityManager {
|
public class ConnectivityManager {
|
||||||
|
|
||||||
public class NetworkCallback {}
|
public class NetworkCallback {
|
||||||
|
public void onAvailable(Network network) {}
|
||||||
|
public void onLost(Network network) {}
|
||||||
|
}
|
||||||
|
|
||||||
public NetworkInfo getNetworkInfo(int networkType) {
|
public NetworkInfo getNetworkInfo(int networkType) {
|
||||||
return null; // this means the network type is not supported, which should make properly coded apps cease any attempts to use network-related APIs
|
return null; // this means the network type is not supported, which should make properly coded apps cease any attempts to use network-related APIs
|
||||||
}
|
}
|
||||||
|
|
||||||
public NetworkInfo getActiveNetworkInfo() {
|
public NetworkInfo getActiveNetworkInfo() {
|
||||||
return new NetworkInfo();
|
return new NetworkInfo(nativeGetNetworkAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerNetworkCallback(NetworkRequest request, NetworkCallback callback) {}
|
public native void registerNetworkCallback(NetworkRequest request, NetworkCallback callback);
|
||||||
|
|
||||||
public boolean isActiveNetworkMetered() {
|
public native boolean isActiveNetworkMetered();
|
||||||
return false;
|
|
||||||
}
|
protected native boolean nativeGetNetworkAvailable();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,21 @@ public class NetworkInfo {
|
|||||||
UNKNOWN
|
UNKNOWN
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private State state = State.DISCONNECTED;
|
||||||
|
|
||||||
|
public NetworkInfo(boolean available) {
|
||||||
|
state = available ? State.CONNECTED : State.DISCONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
public NetworkInfo.State getState() {
|
public NetworkInfo.State getState() {
|
||||||
return State.DISCONNECTED;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getType() {
|
public int getType() {
|
||||||
return 0x8; // where did you even get a NetworkInfo object... there is no network
|
return 0x8; // TYPE_DUMMY
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return true;
|
return state == State.CONNECTED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user