Java Code Examples for android.net.NetworkInfo#getDetailedState()

The following examples show how to use android.net.NetworkInfo#getDetailedState() . 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: Tethering.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void handleConnectivityAction(Intent intent) {
    final NetworkInfo networkInfo =
            (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO);
    if (networkInfo == null ||
            networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) {
        return;
    }

    if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString());
    mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
}
 
Example 2
Source File: LockdownVpnTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void onVpnStateChanged(NetworkInfo info) {
    if (info.getDetailedState() == DetailedState.FAILED) {
        mErrorCount++;
    }
    synchronized (mStateLock) {
        handleStateChangedLocked();
    }
}
 
Example 3
Source File: NetworkChangeNotifierAutoDetect.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param networkInfo The NetworkInfo for the active network.
 * @return the info of the network that is available to this app.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private NetworkInfo processActiveNetworkInfo(NetworkInfo networkInfo) {
    if (networkInfo == null) {
        return null;
    }

    if (networkInfo.isConnected()) {
        return networkInfo;
    }

    // If |networkInfo| is BLOCKED, but the app is in the foreground, then it's likely that
    // Android hasn't finished updating the network access permissions as BLOCKED is only
    // meant for apps in the background.  See https://crbug.com/677365 for more details.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // https://crbug.com/677365 primarily affects only Lollipop and higher versions.
        return null;
    }

    if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOCKED) {
        // Network state is not blocked which implies that network access is
        // unavailable (not just blocked to this app).
        return null;
    }

    if (ApplicationStatus.getStateForApplication()
            != ApplicationState.HAS_RUNNING_ACTIVITIES) {
        // The app is not in the foreground.
        return null;
    }
    return networkInfo;
}
 
Example 4
Source File: ConnectivityHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isNetworkReachable(
	@NonNull final NetworkCapabilities capabilities,
	@NonNull final NetworkInfo info) {

	if (DEBUG) Log.v(TAG, "isNetworkReachable:capabilities=" + capabilities);
	if (DEBUG) Log.v(TAG, "isNetworkReachable:info=" + info);
	final NetworkInfo.DetailedState state = info.getDetailedState();
	final boolean isConnectedOrConnecting
		= (state == NetworkInfo.DetailedState.CONNECTED)
			|| (state == NetworkInfo.DetailedState.CONNECTING);
	final boolean hasCapability;
	if (BuildCheck.isAPI28()) {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)	// API>=21
			&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)			// API>=23
			&& (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)	// API>=28
				|| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND));	// API>=28
	} else if (BuildCheck.isAPI23()) {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)	// API>=21
			&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);		// API>=23
	} else {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);// API>=21
	}
	if (DEBUG) Log.v(TAG, "isNetworkReachable:isConnectedOrConnecting="
		+ isConnectedOrConnecting + ",hasCapability=" + hasCapability
		+ ",NOT_SUSPENDED=" + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)
		+ ",FOREGROUND=" + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND));
	return isConnectedOrConnecting && hasCapability;
}
 
Example 5
Source File: NetworkChangeNotifierAutoDetect.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @return the info of the network that is available to this app.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private NetworkInfo getActiveNetworkInfo() {
    final NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return null;
    }

    if (networkInfo.isConnected()) {
        return networkInfo;
    }

    // If |networkInfo| is BLOCKED, but the app is in the foreground, then it's likely that
    // Android hasn't finished updating the network access permissions as BLOCKED is only
    // meant for apps in the background.  See https://crbug.com/677365 for more details.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // https://crbug.com/677365 primarily affects only Lollipop and higher versions.
        return null;
    }

    if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOCKED) {
        // Network state is not blocked which implies that network access is
        // unavailable (not just blocked to this app).
        return null;
    }

    if (ApplicationStatus.getStateForApplication()
            != ApplicationState.HAS_RUNNING_ACTIVITIES) {
        // The app is not in the foreground.
        return null;
    }
    return networkInfo;
}
 
Example 6
Source File: NetworkHelper.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public static android.net.NetworkInfo.DetailedState getWifiConnectivityState(Context context)
{
    NetworkInfo networkinfo = getNetworkInfo(context, 1);
    if (networkinfo == null)
    {
        return android.net.NetworkInfo.DetailedState.FAILED;
    } else
    {
        return networkinfo.getDetailedState();
    }
}
 
Example 7
Source File: LockdownVpnTracker.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Watch for state changes to both active egress network, kicking off a VPN
 * connection when ready, or setting firewall rules once VPN is connected.
 */
private void handleStateChangedLocked() {

    final NetworkInfo egressInfo = mConnService.getActiveNetworkInfoUnfiltered();
    final LinkProperties egressProp = mConnService.getActiveLinkProperties();

    final NetworkInfo vpnInfo = mVpn.getNetworkInfo();
    final VpnConfig vpnConfig = mVpn.getLegacyVpnConfig();

    // Restart VPN when egress network disconnected or changed
    final boolean egressDisconnected = egressInfo == null
            || State.DISCONNECTED.equals(egressInfo.getState());
    final boolean egressChanged = egressProp == null
            || !TextUtils.equals(mAcceptedEgressIface, egressProp.getInterfaceName());

    final String egressTypeName = (egressInfo == null) ?
            null : ConnectivityManager.getNetworkTypeName(egressInfo.getType());
    final String egressIface = (egressProp == null) ?
            null : egressProp.getInterfaceName();
    Slog.d(TAG, "handleStateChanged: egress=" + egressTypeName +
            " " + mAcceptedEgressIface + "->" + egressIface);

    if (egressDisconnected || egressChanged) {
        mAcceptedEgressIface = null;
        mVpn.stopLegacyVpnPrivileged();
    }
    if (egressDisconnected) {
        hideNotification();
        return;
    }

    final int egressType = egressInfo.getType();
    if (vpnInfo.getDetailedState() == DetailedState.FAILED) {
        EventLogTags.writeLockdownVpnError(egressType);
    }

    if (mErrorCount > MAX_ERROR_COUNT) {
        showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);

    } else if (egressInfo.isConnected() && !vpnInfo.isConnectedOrConnecting()) {
        if (mProfile.isValidLockdownProfile()) {
            Slog.d(TAG, "Active network connected; starting VPN");
            EventLogTags.writeLockdownVpnConnecting(egressType);
            showNotification(R.string.vpn_lockdown_connecting, R.drawable.vpn_disconnected);

            mAcceptedEgressIface = egressProp.getInterfaceName();
            try {
                // Use the privileged method because Lockdown VPN is initiated by the system, so
                // no additional permission checks are necessary.
                mVpn.startLegacyVpnPrivileged(mProfile, KeyStore.getInstance(), egressProp);
            } catch (IllegalStateException e) {
                mAcceptedEgressIface = null;
                Slog.e(TAG, "Failed to start VPN", e);
                showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);
            }
        } else {
            Slog.e(TAG, "Invalid VPN profile; requires IP-based server and DNS");
            showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);
        }

    } else if (vpnInfo.isConnected() && vpnConfig != null) {
        final String iface = vpnConfig.interfaze;
        final List<LinkAddress> sourceAddrs = vpnConfig.addresses;

        if (TextUtils.equals(iface, mAcceptedIface)
              && sourceAddrs.equals(mAcceptedSourceAddr)) {
            return;
        }

        Slog.d(TAG, "VPN connected using iface=" + iface +
                ", sourceAddr=" + sourceAddrs.toString());
        EventLogTags.writeLockdownVpnConnected(egressType);
        showNotification(R.string.vpn_lockdown_connected, R.drawable.vpn_connected);

        final NetworkInfo clone = new NetworkInfo(egressInfo);
        augmentNetworkInfo(clone);
        mConnService.sendConnectedBroadcast(clone);
    }
}
 
Example 8
Source File: NetworkHelper.java    From letv with Apache License 2.0 4 votes vote down vote up
public static DetailedState getWifiConnectivityState(Context context) {
    NetworkInfo networkInfo = getNetworkInfo(context, 1);
    return networkInfo == null ? DetailedState.FAILED : networkInfo.getDetailedState();
}
 
Example 9
Source File: MmsRadio.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
private boolean isConnectivityFailure() {
  NetworkInfo networkInfo = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);

  return networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED;
}
 
Example 10
Source File: General.java    From RedReader with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isConnectionWifi(final Context context){
	final ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	return info != null && info.getDetailedState() == NetworkInfo.DetailedState.CONNECTED;
}