Search in sources :

Example 96 with ConnectivityManager

use of android.net.ConnectivityManager in project android_frameworks_base by DirtyUnicorns.

the class NetworkTraffic method getConnectAvailable.

private boolean getConnectAvailable() {
    ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = (connManager != null) ? connManager.getActiveNetworkInfo() : null;
    return network != null && network.isConnected();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 97 with ConnectivityManager

use of android.net.ConnectivityManager in project android_frameworks_base by DirtyUnicorns.

the class UidDetailProvider method buildUidDetail.

/**
     * Build {@link UidDetail} object, blocking until all {@link Drawable}
     * lookup is finished.
     */
private UidDetail buildUidDetail(int uid) {
    final Resources res = mContext.getResources();
    final PackageManager pm = mContext.getPackageManager();
    final UidDetail detail = new UidDetail();
    detail.label = pm.getNameForUid(uid);
    detail.icon = pm.getDefaultActivityIcon();
    // handle special case labels
    switch(uid) {
        case android.os.Process.SYSTEM_UID:
            detail.label = res.getString(R.string.process_kernel_label);
            detail.icon = pm.getDefaultActivityIcon();
            return detail;
        case TrafficStats.UID_REMOVED:
            detail.label = res.getString(UserManager.supportsMultipleUsers() ? R.string.data_usage_uninstalled_apps_users : R.string.data_usage_uninstalled_apps);
            detail.icon = pm.getDefaultActivityIcon();
            return detail;
        case TrafficStats.UID_TETHERING:
            final ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            detail.label = res.getString(Utils.getTetheringLabel(cm));
            detail.icon = pm.getDefaultActivityIcon();
            return detail;
    }
    final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    // Handle keys that are actually user handles
    if (isKeyForUser(uid)) {
        final int userHandle = getUserIdForKey(uid);
        final UserInfo info = um.getUserInfo(userHandle);
        if (info != null) {
            detail.label = Utils.getUserLabel(mContext, info);
            detail.icon = Utils.getUserIcon(mContext, um, info);
            return detail;
        }
    }
    // otherwise fall back to using packagemanager labels
    final String[] packageNames = pm.getPackagesForUid(uid);
    final int length = packageNames != null ? packageNames.length : 0;
    try {
        final int userId = UserHandle.getUserId(uid);
        UserHandle userHandle = new UserHandle(userId);
        IPackageManager ipm = AppGlobals.getPackageManager();
        if (length == 1) {
            final ApplicationInfo info = ipm.getApplicationInfo(packageNames[0], 0, /* no flags */
            userId);
            if (info != null) {
                detail.label = info.loadLabel(pm).toString();
                detail.icon = um.getBadgedIconForUser(info.loadIcon(pm), new UserHandle(userId));
            }
        } else if (length > 1) {
            detail.detailLabels = new CharSequence[length];
            detail.detailContentDescriptions = new CharSequence[length];
            for (int i = 0; i < length; i++) {
                final String packageName = packageNames[i];
                final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
                final ApplicationInfo appInfo = ipm.getApplicationInfo(packageName, 0, /* no flags */
                userId);
                if (appInfo != null) {
                    detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
                    detail.detailContentDescriptions[i] = um.getBadgedLabelForUser(detail.detailLabels[i], userHandle);
                    if (packageInfo.sharedUserLabel != 0) {
                        detail.label = pm.getText(packageName, packageInfo.sharedUserLabel, packageInfo.applicationInfo).toString();
                        detail.icon = um.getBadgedIconForUser(appInfo.loadIcon(pm), userHandle);
                    }
                }
            }
        }
        detail.contentDescription = um.getBadgedLabelForUser(detail.label, userHandle);
    } catch (NameNotFoundException e) {
        Log.w(TAG, "Error while building UI detail for uid " + uid, e);
    } catch (RemoteException e) {
        Log.w(TAG, "Error while building UI detail for uid " + uid, e);
    }
    if (TextUtils.isEmpty(detail.label)) {
        detail.label = Integer.toString(uid);
    }
    return detail;
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ConnectivityManager(android.net.ConnectivityManager) PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo) UserInfo(android.content.pm.UserInfo) PackageManager(android.content.pm.PackageManager) IPackageManager(android.content.pm.IPackageManager) IPackageManager(android.content.pm.IPackageManager) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) Resources(android.content.res.Resources) RemoteException(android.os.RemoteException)

Example 98 with ConnectivityManager

use of android.net.ConnectivityManager in project android_frameworks_base by DirtyUnicorns.

the class NetworkActivity method updateConnectedFlags.

// Checks the network connection and sets the wifiConnected and mobileConnected
// variables accordingly.
private void updateConnectedFlags() {
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 99 with ConnectivityManager

use of android.net.ConnectivityManager in project kdeconnect-android by KDE.

the class NetworkHelper method isOnMobileNetwork.

public static boolean isOnMobileNetwork(Context context) {
    if (context == null || android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
        //No good way to know it
        return false;
    }
    boolean mobile = false;
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network[] networks = connMgr.getAllNetworks();
    for (Network network : networks) {
        NetworkInfo info = connMgr.getNetworkInfo(network);
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            mobile = info.isConnected();
            continue;
        }
        Log.e(info.getTypeName(), "" + info.isAvailable());
        //We are connected to at least one non-mobile network
        if (info.isAvailable())
            return false;
    }
    return mobile;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) Network(android.net.Network)

Example 100 with ConnectivityManager

use of android.net.ConnectivityManager in project AgentWeb by Justson.

the class AgentWebUtils method checkNetwork.

public static boolean checkNetwork(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    }
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    return info != null && info.isConnected();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Aggregations

ConnectivityManager (android.net.ConnectivityManager)361 NetworkInfo (android.net.NetworkInfo)258 Context (android.content.Context)15 TelephonyManager (android.telephony.TelephonyManager)13 RemoteException (android.os.RemoteException)11 Test (org.junit.Test)11 Intent (android.content.Intent)10 WifiManager (android.net.wifi.WifiManager)9 Method (java.lang.reflect.Method)9 Network (android.net.Network)8 IOException (java.io.IOException)8 PackageManager (android.content.pm.PackageManager)7 SharedPreferences (android.content.SharedPreferences)6 NetworkCapabilities (android.net.NetworkCapabilities)6 UserManager (android.os.UserManager)6 Activity (android.app.Activity)5 ApplicationInfo (android.content.pm.ApplicationInfo)5 IPackageManager (android.content.pm.IPackageManager)5 PackageInfo (android.content.pm.PackageInfo)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5