Java Code Examples for android.net.ConnectivityManager#TYPE_VPN
The following examples show how to use
android.net.ConnectivityManager#TYPE_VPN .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SystemBroadcastReceiver.java From PresencePublisher with MIT License | 6 votes |
@SuppressWarnings("deprecation") @Override public void onReceive(final Context context, final Intent intent) { String action = intent.getAction(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); boolean useMobile = sharedPreferences.getBoolean(SEND_OFFLINE_MESSAGE, false) && sharedPreferences.getBoolean(SEND_VIA_MOBILE_NETWORK, false); if (networkInfo != null && networkInfo.isConnected() && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_VPN || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET || useMobile)) { HyperLog.i(TAG, "Reacting to network change"); new Publisher(context).scheduleNow(); } } else if (Intent.ACTION_BOOT_COMPLETED.equals(action) && sharedPreferences.getBoolean(AUTOSTART, false)) { HyperLog.i(TAG, "Starting after boot"); new Publisher(context).scheduleNow(); } }
Example 2
Source File: Publisher.java From PresencePublisher with MIT License | 6 votes |
private boolean sendMessageViaCurrentConnection() { if (connectivityManager == null) { HyperLog.e(TAG, "Connectivity Manager not found"); return false; } //noinspection deprecation NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); //noinspection deprecation if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { return false; } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { for (Network network : connectivityManager.getAllNetworks()) { NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if (networkCapabilities != null && networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { return true; } } return sendViaMobile(); } else { //noinspection deprecation return activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || activeNetworkInfo.getType() == ConnectivityManager.TYPE_VPN || activeNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET || sendViaMobile(); } }
Example 3
Source File: NetworkManager.java From Intra with Apache License 2.0 | 6 votes |
private void connectivityChanged(Intent intent) { NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo intentNetworkInfo = intent.getParcelableExtra(EXTRA_NETWORK_INFO); Log.v(LOG_TAG, "ACTIVE NETWORK " + activeNetworkInfo); Log.v(LOG_TAG, "INTENT NETWORK " + intentNetworkInfo); if (networkListener == null) { return; } if (isConnectedNetwork(activeNetworkInfo) && intentNetworkInfo != null && intentNetworkInfo.getType() == ConnectivityManager.TYPE_VPN) { // VPN state changed, we have connectivity, ignore. return; } else if (!isConnectedNetwork(activeNetworkInfo)) { // No active network, signal disconnect event. networkListener.onNetworkDisconnected(); } else if (activeNetworkInfo.getType() != ConnectivityManager.TYPE_VPN) { // We have an active network, make sure it is not a VPN to signal a connected event. networkListener.onNetworkConnected(activeNetworkInfo); } }
Example 4
Source File: ConnectivityTracker.java From Easer with GNU General Public License v3.0 | 6 votes |
private int convertType(NetworkInfo activeNetworkInfo) { if (activeNetworkInfo == null) { return TYPE_NOT_CONNECTED; } switch (activeNetworkInfo.getType()) { case ConnectivityManager.TYPE_WIFI: return TYPE_WIFI; case ConnectivityManager.TYPE_MOBILE: return TYPE_MOBILE; case ConnectivityManager.TYPE_ETHERNET: return TYPE_ETHERNET; case ConnectivityManager.TYPE_BLUETOOTH: return TYPE_BLUETOOTH; case ConnectivityManager.TYPE_VPN: return TYPE_VPN; } return -1; }
Example 5
Source File: ConnectivitySlot.java From Easer with GNU General Public License v3.0 | 6 votes |
private int convertType(NetworkInfo activeNetworkInfo) { if (activeNetworkInfo == null) { return TYPE_NOT_CONNECTED; } switch (activeNetworkInfo.getType()) { case ConnectivityManager.TYPE_WIFI: return TYPE_WIFI; case ConnectivityManager.TYPE_MOBILE: return TYPE_MOBILE; case ConnectivityManager.TYPE_ETHERNET: return TYPE_ETHERNET; case ConnectivityManager.TYPE_BLUETOOTH: return TYPE_BLUETOOTH; case ConnectivityManager.TYPE_VPN: return TYPE_VPN; } return -1; }
Example 6
Source File: Util.java From NetGuard with GNU General Public License v3.0 | 6 votes |
public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.isConnected()) return true; Network[] networks = cm.getAllNetworks(); if (networks == null) return false; for (Network network : networks) { ni = cm.getNetworkInfo(network); if (ni != null && ni.getType() != ConnectivityManager.TYPE_VPN && ni.isConnected()) return true; } return false; }
Example 7
Source File: NetworkInfoBroadcastReceiver.java From android-sdk with MIT License | 6 votes |
private static String typeToString(int type) { switch (type) { case ConnectivityManager.TYPE_ETHERNET: return "TYPE_ETHERNET"; case ConnectivityManager.TYPE_BLUETOOTH: return "TYPE_BLUETOOTH"; case ConnectivityManager.TYPE_MOBILE: return "TYPE_MOBILE"; case ConnectivityManager.TYPE_MOBILE_DUN: return "TYPE_MOBILE_DUN"; case ConnectivityManager.TYPE_MOBILE_HIPRI: return "TYPE_MOBILE_HIPRI"; case ConnectivityManager.TYPE_MOBILE_MMS: return "TYPE_MOBILE_MMS"; case ConnectivityManager.TYPE_MOBILE_SUPL: return "TYPE_MOBILE_SUPL"; case ConnectivityManager.TYPE_VPN: return "TYPE_VPN"; case ConnectivityManager.TYPE_WIFI: return "TYPE_WIFI"; case ConnectivityManager.TYPE_WIMAX: return "TYPE_WIMAX"; default: return "unknown"; } }
Example 8
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 5 votes |
private static ConnectionType getUnderlyingConnectionTypeForVpn(NetworkState networkState) { if (networkState.getNetworkType() != ConnectivityManager.TYPE_VPN) { return ConnectionType.CONNECTION_NONE; } return getConnectionType(networkState.isConnected(), networkState.getUnderlyingNetworkTypeForVpn(), networkState.getUnderlyingNetworkSubtypeForVpn()); }
Example 9
Source File: Vpn.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@VisibleForTesting protected Vpn(Looper looper, Context context, INetworkManagementService netService, int userHandle, SystemServices systemServices) { mContext = context; mNetd = netService; mUserHandle = userHandle; mLooper = looper; mSystemServices = systemServices; mPackage = VpnConfig.LEGACY_VPN; mOwnerUID = getAppUid(mPackage, mUserHandle); try { netService.registerObserver(mObserver); } catch (RemoteException e) { Log.wtf(TAG, "Problem registering observer", e); } mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_VPN, 0 /* subtype */, NETWORKTYPE, "" /* subtypeName */); mNetworkCapabilities = new NetworkCapabilities(); mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_VPN); mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN); updateCapabilities(null /* defaultNetwork */); loadAlwaysOnPackage(); }
Example 10
Source File: ServiceVPN.java From InviZible with GNU General Public License v3.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { // Filter VPN connectivity changes int networkType = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY); if (networkType == ConnectivityManager.TYPE_VPN) return; // Reload rules Log.i(LOG_TAG, "VPN Received " + intent); reload("Connectivity changed", ServiceVPN.this); }
Example 11
Source File: Util.java From android-notification-log with MIT License | 5 votes |
public static String getConnectivityType(Context context) { try { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if(cm != null) { NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if(networkInfo != null) { int type = networkInfo.getType(); switch (type) { case ConnectivityManager.TYPE_BLUETOOTH: return "bluetooth"; case ConnectivityManager.TYPE_DUMMY: return "dummy"; case ConnectivityManager.TYPE_ETHERNET: return "ethernet"; case ConnectivityManager.TYPE_MOBILE: return "mobile"; case ConnectivityManager.TYPE_MOBILE_DUN: return "mobile dun"; case ConnectivityManager.TYPE_VPN: return "vpn"; case ConnectivityManager.TYPE_WIFI: return "wifi"; case ConnectivityManager.TYPE_WIMAX: return "wimax"; default: return ""+type; } } else { return "none"; } } } catch (Exception e) { if(Const.DEBUG) e.printStackTrace(); } return "undefined"; }
Example 12
Source File: DeviceStatusUtil.java From under-the-hood with Apache License 2.0 | 5 votes |
/** * The current network (ie. internet) connectivity state. Needs correct permission to work. * * @param context * @return state */ //@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE) public static ConnectionState getNetworkConnectivityState(@NonNull Context context) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null) { if (activeNetwork.isConnectedOrConnecting()) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { return ConnectionState.CONNECTED_WIFI; } if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE_DUN) { return ConnectionState.CONNECTED_MOBILE; } if (activeNetwork.getType() == ConnectivityManager.TYPE_ETHERNET) { return ConnectionState.CONNECTED_ETHERNET; } if (activeNetwork.getType() == ConnectivityManager.TYPE_BLUETOOTH) { return ConnectionState.CONNECTED_BT; } if (activeNetwork.getType() == ConnectivityManager.TYPE_VPN) { return ConnectionState.CONNECTED_BT; } return ConnectionState.CONNECTED_OTHER; } } return ConnectionState.DISCONNECTED; } return ConnectionState.PERMISSION_NEEDED; }
Example 13
Source File: AndroidUsingLinkProperties.java From Pix-Art-Messenger with GNU General Public License v3.0 | 5 votes |
@Override @TargetApi(21) public String[] getDnsServerAddresses() { final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return new String[0]; } final Network activeNetwork = getActiveNetwork(connectivityManager); final List<String> servers = new ArrayList<>(); int vpnOffset = 0; for (Network network : networks) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties == null) { continue; } final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); final boolean isActiveNetwork = network.equals(activeNetwork); final boolean isVpn = networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN; if (isActiveNetwork && isVpn) { final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); servers.addAll(0, tmp); vpnOffset += tmp.size(); } else if (hasDefaultRoute(linkProperties) || isActiveNetwork || activeNetwork == null || isVpn) { servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); } } return servers.toArray(new String[0]); }
Example 14
Source File: AndroidUsingLinkProperties.java From Conversations with GNU General Public License v3.0 | 5 votes |
@Override @TargetApi(21) public String[] getDnsServerAddresses() { final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return new String[0]; } final Network activeNetwork = getActiveNetwork(connectivityManager); final List<String> servers = new ArrayList<>(); int vpnOffset = 0; for(Network network : networks) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties == null) { continue; } final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); final boolean isActiveNetwork = network.equals(activeNetwork); final boolean isVpn = networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN; if (isActiveNetwork && isVpn) { final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); servers.addAll(0, tmp); vpnOffset += tmp.size(); } else if (hasDefaultRoute(linkProperties) || isActiveNetwork || activeNetwork == null || isVpn) { servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); } } return servers.toArray(new String[0]); }
Example 15
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 4 votes |
/** * Returns connection type and status information about |network|. * Only callable on Lollipop and newer releases. */ @SuppressLint("NewApi") NetworkState getNetworkState( Network network) { if (network == null || connectivityManager == null) { return new NetworkState(false, -1, -1, -1, -1); } NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (networkInfo == null) { Logging.w(TAG, "Couldn't retrieve information from network " + network.toString()); return new NetworkState(false, -1, -1, -1, -1); } // The general logic of handling a VPN in this method is as follows. getNetworkInfo will // return the info of the network with the same id as in |network| when it is registered via // ConnectivityManager.registerNetworkAgent in Android. |networkInfo| may or may not indicate // the type TYPE_VPN if |network| is a VPN. To reliably detect the VPN interface, we need to // query the network capability as below in the case when networkInfo.getType() is not // TYPE_VPN. On the other hand when networkInfo.getType() is TYPE_VPN, the only solution so // far to obtain the underlying network information is to query the active network interface. // However, the active network interface may not be used for the VPN, for example, if the VPN // is restricted to WiFi by the implementation but the WiFi interface is currently turned // off and the active interface is the Cell. Using directly the result from // getActiveNetworkInfo may thus give the wrong interface information, and one should note // that getActiveNetworkInfo would return the default network interface if the VPN does not // specify its underlying networks in the implementation. Therefore, we need further compare // |network| to the active network. If they are not the same network, we will have to fall // back to report an unknown network. if (networkInfo.getType() != ConnectivityManager.TYPE_VPN) { // Note that getNetworkCapabilities returns null if the network is unknown. NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if (networkCapabilities == null || !networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { return getNetworkState(networkInfo); } // When |network| is in fact a VPN after querying its capability but |networkInfo| is not of // type TYPE_VPN, |networkInfo| contains the info for the underlying network, and we return // a NetworkState constructed from it. return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1, networkInfo.getType(), networkInfo.getSubtype()); } // When |networkInfo| is of type TYPE_VPN, which implies |network| is a VPN, we return the // NetworkState of the active network via getActiveNetworkInfo(), if |network| is the active // network that supports the VPN. Otherwise, NetworkState of an unknown network with type -1 // will be returned. // // Note that getActiveNetwork and getActiveNetworkInfo return null if no default network is // currently active. if (networkInfo.getType() == ConnectivityManager.TYPE_VPN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && network.equals(connectivityManager.getActiveNetwork())) { // If a VPN network is in place, we can find the underlying network type via querying the // active network info thanks to // https://android.googlesource.com/platform/frameworks/base/+/d6a7980d NetworkInfo underlyingActiveNetworkInfo = connectivityManager.getActiveNetworkInfo(); // We use the NetworkInfo of the underlying network if it is not of TYPE_VPN itself. if (underlyingActiveNetworkInfo != null && underlyingActiveNetworkInfo.getType() != ConnectivityManager.TYPE_VPN) { return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1, underlyingActiveNetworkInfo.getType(), underlyingActiveNetworkInfo.getSubtype()); } } return new NetworkState( networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1, -1, -1); } return getNetworkState(networkInfo); }
Example 16
Source File: NetworkMonitorAutoDetect.java From webrtc_android with MIT License | 4 votes |
private static ConnectionType getConnectionType( boolean isConnected, int networkType, int networkSubtype) { if (!isConnected) { return ConnectionType.CONNECTION_NONE; } switch (networkType) { case ConnectivityManager.TYPE_ETHERNET: return ConnectionType.CONNECTION_ETHERNET; case ConnectivityManager.TYPE_WIFI: return ConnectionType.CONNECTION_WIFI; case ConnectivityManager.TYPE_WIMAX: return ConnectionType.CONNECTION_4G; case ConnectivityManager.TYPE_BLUETOOTH: return ConnectionType.CONNECTION_BLUETOOTH; case ConnectivityManager.TYPE_MOBILE: // Use information from TelephonyManager to classify the connection. switch (networkSubtype) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return ConnectionType.CONNECTION_2G; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return ConnectionType.CONNECTION_3G; case TelephonyManager.NETWORK_TYPE_LTE: return ConnectionType.CONNECTION_4G; default: return ConnectionType.CONNECTION_UNKNOWN_CELLULAR; } case ConnectivityManager.TYPE_VPN: return ConnectionType.CONNECTION_VPN; default: return ConnectionType.CONNECTION_UNKNOWN; } }