Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.os.Build;

import android.telephony.TelephonyManager;

import java.util.Arrays;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static List<String> CELLPHONEBLACKLIST = Arrays.asList(new String[] { "d2spr" });

    public static Map<String, String> getNetworkInfo(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        Map<String, String> map = new HashMap();
        NetworkInfo info = cm.getActiveNetworkInfo();
        if ((info == null) || (!info.isConnectedOrConnecting()) || (withinInBlackList())) {
            map.put("access_subtype", "offline");
            map.put("access", "offline");
            map.put("carrier", "");
        } else {
            map.put("access_subtype", info.getSubtypeName());
            map.put("access", cleanNetworkTypeName(info.getTypeName()));
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            String carrierName = manager.getNetworkOperatorName();
            map.put("carrier", carrierName);
        }
        return map;
    }

    private static boolean withinInBlackList() {
        if (CELLPHONEBLACKLIST.contains(Build.DEVICE)) {
            return true;
        }
        return false;
    }

    private static String cleanNetworkTypeName(String type) {
        /*  if (AVUtils.isBlankString(type)) {
          return "offline";
          }*/
        String t = type.toUpperCase();
        if (t.contains("WIFI")) {
            return "WiFi";
        }
        if (type.contains("MOBILE")) {
            return "Mobile";
        }
        return type;
    }
}