Fake wifi connection for select apps (1/2)
Largely based on https://github.com/lemonsqueeze/FakeWifiConnection
Co-authored-by: lemonsqueeze <lemonsqueeze@gmx.com>
Change-Id: I42ab1c98cc13fabc0f554fbd899bf7a67068b9b8
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index ff90e78..2b3cf43 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -23,6 +23,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.NetworkUtils;
+import android.net.wifi.FakeWifi;
import android.os.Binder;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
@@ -598,7 +599,10 @@
*/
public NetworkInfo getActiveNetworkInfo() {
try {
- return mService.getActiveNetworkInfo();
+ NetworkInfo network = mService.getActiveNetworkInfo();
+ if (FakeWifi.isHackEnabled(mContext))
+ return FakeWifi.maybeOverwrite(network);
+ return network;
} catch (RemoteException e) {
return null;
}
@@ -640,7 +644,10 @@
*/
public NetworkInfo getNetworkInfo(int networkType) {
try {
- return mService.getNetworkInfo(networkType);
+ NetworkInfo network = mService.getNetworkInfo(networkType);
+ if (networkType == ConnectivityManager.TYPE_WIFI && FakeWifi.isHackEnabled(mContext))
+ return FakeWifi.maybeOverwrite(network);
+ return network;
} catch (RemoteException e) {
return null;
}
@@ -658,7 +665,32 @@
*/
public NetworkInfo[] getAllNetworkInfo() {
try {
- return mService.getAllNetworkInfo();
+ NetworkInfo[] networks = mService.getAllNetworkInfo();
+ if (!FakeWifi.isHackEnabled(mContext))
+ return networks;
+
+ int i;
+ boolean wifi_found = false;
+ for (i = 0; i < networks.length; i++) {
+ if (networks[i].getType() == ConnectivityManager.TYPE_WIFI) {
+ wifi_found = true;
+ break;
+ }
+ }
+
+ if (wifi_found && networks[i].isConnected())
+ return networks;
+
+ if (wifi_found) {
+ networks[i] = FakeWifi.getFakeNetworkInfo();
+ } else {
+ NetworkInfo[] extended = new NetworkInfo[networks.length + 1];
+ for (i = 0; i < networks.length; i++)
+ extended[i] = networks[i];
+ extended[networks.length] = FakeWifi.getFakeNetworkInfo();
+ networks = extended;
+ }
+ return networks;
} catch (RemoteException e) {
return null;
}
@@ -1696,6 +1728,9 @@
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
*/
public boolean isActiveNetworkMetered() {
+ if (FakeWifi.isHackEnabled(mContext))
+ return false;
+
try {
return mService.isActiveNetworkMetered();
} catch (RemoteException e) {
diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java
index 2df17e2..30745c4 100644
--- a/framework/src/android/net/ConnectivityManager.java
+++ b/framework/src/android/net/ConnectivityManager.java
@@ -50,6 +50,7 @@
import android.net.TetheringManager.StartTetheringCallback;
import android.net.TetheringManager.TetheringEventCallback;
import android.net.TetheringManager.TetheringRequest;
+import android.net.wifi.FakeWifi;
import android.net.wifi.WifiNetworkSuggestion;
import android.os.Binder;
import android.os.Build;
@@ -1156,7 +1157,10 @@
@Nullable
public NetworkInfo getActiveNetworkInfo() {
try {
- return mService.getActiveNetworkInfo();
+ NetworkInfo network = mService.getActiveNetworkInfo();
+ if (FakeWifi.isHackEnabled(mContext))
+ return FakeWifi.maybeOverwrite(network);
+ return network;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
@@ -1347,7 +1351,10 @@
@Nullable
public NetworkInfo getNetworkInfo(int networkType) {
try {
- return mService.getNetworkInfo(networkType);
+ NetworkInfo network = mService.getNetworkInfo(networkType);
+ if (networkType == ConnectivityManager.TYPE_WIFI && FakeWifi.isHackEnabled(mContext))
+ return FakeWifi.maybeOverwrite(network);
+ return network;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
@@ -1396,7 +1403,32 @@
@NonNull
public NetworkInfo[] getAllNetworkInfo() {
try {
- return mService.getAllNetworkInfo();
+ NetworkInfo[] networks = mService.getAllNetworkInfo();
+ if (!FakeWifi.isHackEnabled(mContext))
+ return networks;
+
+ int i;
+ boolean wifi_found = false;
+ for (i = 0; i < networks.length; i++) {
+ if (networks[i].getType() == ConnectivityManager.TYPE_WIFI) {
+ wifi_found = true;
+ break;
+ }
+ }
+
+ if (wifi_found && networks[i].isConnected())
+ return networks;
+
+ if (wifi_found) {
+ networks[i] = FakeWifi.getFakeNetworkInfo();
+ } else {
+ NetworkInfo[] extended = new NetworkInfo[networks.length + 1];
+ for (i = 0; i < networks.length; i++)
+ extended[i] = networks[i];
+ extended[networks.length] = FakeWifi.getFakeNetworkInfo();
+ networks = extended;
+ }
+ return networks;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
@@ -3275,6 +3307,9 @@
*/
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public boolean isActiveNetworkMetered() {
+ if (FakeWifi.isHackEnabled(mContext))
+ return false;
+
try {
return mService.isActiveNetworkMetered();
} catch (RemoteException e) {