Search in sources :

Example 71 with NetworkInfo

use of android.net.NetworkInfo in project remusic by aa112901.

the class NetworkUtils method isConnectInternet.

public static boolean isConnectInternet(final Context pContext) {
    final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        return networkInfo.isAvailable();
    }
    return false;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 72 with NetworkInfo

use of android.net.NetworkInfo in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadFromLigerServer.

private void downloadFromLigerServer() {
    String ligerUrl = indexItem.getExpansionFileUrl();
    String ligerPath = StorymakerIndexManager.buildFilePath(indexItem, context);
    String ligerObb = fileName;
    Timber.d("DOWNLOADING " + ligerObb + " FROM " + ligerUrl + " TO " + ligerPath);
    try {
        URI expansionFileUri = null;
        HttpGet request = null;
        HttpResponse response = null;
        File targetFolder = new File(ligerPath);
        String nameFilter = "";
        if (ligerObb.contains(indexItem.getExpansionFileVersion())) {
            nameFilter = fileName.replace(indexItem.getExpansionFileVersion(), "*") + "*.tmp";
        } else {
            nameFilter = fileName + "*.tmp";
        }
        Timber.d("CLEANUP: DELETING " + nameFilter + " FROM " + targetFolder.getPath());
        WildcardFileFilter oldFileFilter = new WildcardFileFilter(nameFilter);
        for (File oldFile : FileUtils.listFiles(targetFolder, oldFileFilter, null)) {
            Timber.d("CLEANUP: FOUND " + oldFile.getPath() + ", DELETING");
            FileUtils.deleteQuietly(oldFile);
        }
        // additional cleanup of pre-name-change files
        if (fileName.contains(scal.io.liger.Constants.MAIN)) {
            nameFilter = fileName.replace(scal.io.liger.Constants.MAIN + ".", "").replace(indexItem.getExpansionFileVersion(), "*");
            Timber.d("CLEANUP: DELETING OLD FILES " + nameFilter + " FROM " + targetFolder.getPath());
            oldFileFilter = new WildcardFileFilter(nameFilter);
            for (File oldFile : FileUtils.listFiles(targetFolder, oldFileFilter, null)) {
                Timber.d("CLEANUP: FOUND OLD FILE " + oldFile.getPath() + ", DELETING");
                FileUtils.deleteQuietly(oldFile);
            }
        }
        File targetFile = new File(targetFolder, ligerObb + ".tmp");
        // if there is no connectivity, do not queue item (no longer seems to pause if connection is unavailable)
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if ((ni != null) && (ni.isConnectedOrConnecting())) {
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Starting download of " + indexItem.getTitle() + ".", false);
            }
            // check preferences.  will also need to check whether tor is active within method
            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            boolean useTor = settings.getBoolean("pusetor", false);
            boolean useManager = settings.getBoolean("pusedownloadmanager", false);
            //if (checkTor(useTor, context)) {
            if (useTor && useManager) {
                Timber.e("ANDROID DOWNLOAD MANAGER IS NOT COMPATABLE WITH TOR");
                if (context instanceof Activity) {
                    // FIXME move to strings
                    Utils.toastOnUiThread((Activity) context, "Check settings, can't use download manager and tor", true);
                }
                StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
            } else if (useTor || !useManager) {
                downloadWithTor(useTor, Uri.parse(ligerUrl + ligerObb), mAppTitle + " content download", ligerObb, targetFile);
            } else {
                downloadWithManager(Uri.parse(ligerUrl + ligerObb), mAppTitle + " content download", ligerObb, Uri.fromFile(targetFile));
            }
        } else {
            Timber.d("NO CONNECTION, NOT QUEUEING DOWNLOAD: " + ligerUrl + ligerObb + " -> " + targetFile.getPath());
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Check settings, no connection, can't start download", true);
            }
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
        }
    } catch (Exception e) {
        Timber.e(e, "DOWNLOAD ERROR: " + ligerUrl + ligerObb + " -> " + e.getMessage());
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) SharedPreferences(android.content.SharedPreferences) ConnectivityManager(android.net.ConnectivityManager) HttpGet(ch.boye.httpclientandroidlib.client.methods.HttpGet) HttpResponse(ch.boye.httpclientandroidlib.HttpResponse) Activity(android.app.Activity) URI(java.net.URI) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ConnectTimeoutException(ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)

Example 73 with NetworkInfo

use of android.net.NetworkInfo in project GT by Tencent.

the class NetUtils method getNetWorkType.

/**
	 * 获取网络连接类型
	 * 
	 * @return -1表示没有网络
	 */
public static final int getNetWorkType() {
    Context c = GTApp.getContext();
    ConnectivityManager conn = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conn == null) {
        return -1;
    }
    NetworkInfo info = conn.getActiveNetworkInfo();
    if (info == null || !info.isAvailable()) {
        return -1;
    }
    int type = info.getType();
    if (type == ConnectivityManager.TYPE_WIFI) {
        return TYPE_WIFI;
    } else {
        TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
        switch(tm.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return TYPE_GPRS;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return TYPE_GPRS;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return TYPE_GPRS;
            default:
                return TYPE_3G;
        }
    }
}
Also used : Context(android.content.Context) NetworkInfo(android.net.NetworkInfo) TelephonyManager(android.telephony.TelephonyManager) ConnectivityManager(android.net.ConnectivityManager)

Example 74 with NetworkInfo

use of android.net.NetworkInfo in project android-common by Trinea.

the class NetWorkUtils method getNetworkType.

/**
     * Get network type
     * 
     * @param context
     * @return
     */
public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? -1 : networkInfo.getType();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 75 with NetworkInfo

use of android.net.NetworkInfo in project android-common by Trinea.

the class NetWorkUtils method getNetworkTypeName.

/**
     * Get network type name
     * 
     * @param context
     * @return
     */
public static String getNetworkTypeName(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo;
    String type = NETWORK_TYPE_DISCONNECT;
    if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
        return type;
    }
    ;
    if (networkInfo.isConnected()) {
        String typeName = networkInfo.getTypeName();
        if ("WIFI".equalsIgnoreCase(typeName)) {
            type = NETWORK_TYPE_WIFI;
        } else if ("MOBILE".equalsIgnoreCase(typeName)) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP;
        } else {
            type = NETWORK_TYPE_UNKNOWN;
        }
    }
    return type;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Aggregations

NetworkInfo (android.net.NetworkInfo)511 ConnectivityManager (android.net.ConnectivityManager)274 Intent (android.content.Intent)47 LinkProperties (android.net.LinkProperties)38 NetworkState (android.net.NetworkState)26 Test (org.junit.Test)26 PendingIntent (android.app.PendingIntent)23 NetworkCapabilities (android.net.NetworkCapabilities)18 Network (android.net.Network)17 LargeTest (android.test.suitebuilder.annotation.LargeTest)17 RemoteException (android.os.RemoteException)16 Context (android.content.Context)15 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 WifiInfo (android.net.wifi.WifiInfo)13 Bundle (android.os.Bundle)13 TestUtils.mockNetworkInfo (com.squareup.picasso.TestUtils.mockNetworkInfo)12 IOException (java.io.IOException)11 WifiManager (android.net.wifi.WifiManager)9 IntentFilter (android.content.IntentFilter)8 BroadcastReceiver (android.content.BroadcastReceiver)7