Skip to content

Commit 57fbefa

Browse files
authored
Merge pull request Blankj#1492 from NotNotMarshall/patch-4
NEW Methods: NetworkUtils: isBehindProxy(), isUsingVPN() (All APIs supported)
2 parents 1c95bb6 + cbea6b4 commit 57fbefa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/NetworkUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,34 @@ public static boolean getMobileDataEnabled() {
261261
}
262262
return false;
263263
}
264+
265+
/**
266+
* Returns true if device is connecting to the internet via a proxy, works for both Wi-Fi and Mobile Data.
267+
*
268+
* @return true if using proxy to connect to the internet.
269+
*/
270+
public static boolean isBehindProxy(){
271+
return !(System.getProperty("http.proxyHost") == null || System.getProperty("http.proxyPort") == null);
272+
}
273+
274+
/**
275+
* Returns true if device is connecting to the internet via a VPN.
276+
*
277+
* @return true if using VPN to conncet to the internet.
278+
*/
279+
public static boolean isUsingVPN(){
280+
ConnectivityManager cm = (ConnectivityManager) com.blankj.utilcode.util.Utils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
281+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
282+
return cm.getNetworkInfo(ConnectivityManager.TYPE_VPN).isConnectedOrConnecting()
283+
} else {
284+
return cm.getNetworkInfo(NetworkCapabilities.TRANSPORT_VPN).isConnectedOrConnecting()
285+
}
286+
}
287+
288+
289+
290+
291+
264292

265293
/**
266294
* Return whether using mobile data.

0 commit comments

Comments
 (0)