Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 25212bc8 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

rewrite commonUtils.haveNetworkConnexion()

parent 07e97479
Loading
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
@@ -202,22 +203,21 @@ public abstract class CommonUtils {
     */
    public static boolean haveNetworkConnexion(Context context) {
        Log.i(TAG, "haveNetworkConnexion()");
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getType()== ConnectivityManager.TYPE_WIFI) // Replaced the ni.getTypeName by ni.getType to make the test from ObserverServiceTest to work. But looks a better solution in all case
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getType()== ConnectivityManager.TYPE_MOBILE)
                if (ni.isConnected())
                    haveConnectedMobile = true;

        ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());

        if (capabilities != null
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) {
            return true;
        }
        return haveConnectedWifi || haveConnectedMobile;
        return false;
    }



    /**
     * Get mimetype of file from the file itself
     *