Example usage for android.telephony TelephonyManager getNetworkCountryIso

List of usage examples for android.telephony TelephonyManager getNetworkCountryIso

Introduction

In this page you can find the example usage for android.telephony TelephonyManager getNetworkCountryIso.

Prototype

public String getNetworkCountryIso() 

Source Link

Document

Returns the ISO-3166 country code equivalent of the MCC (Mobile Country Code) of the current registered operator or the cell nearby, if available.

Usage

From source file:im.neon.util.PhoneNumberUtils.java

/**
 * Provide the selected country code/*from  ww w.  j a  v  a 2 s . com*/
 *
 * @param context the application context
 * @return the ISO country code or "" if it does not exist
 */
public static String getCountryCode(final Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    if (!preferences.contains(COUNTRY_CODE_PREF_KEY)
            || TextUtils.isEmpty(preferences.getString(COUNTRY_CODE_PREF_KEY, ""))) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String countryCode = tm.getNetworkCountryIso().toUpperCase();
            if (TextUtils.isEmpty(countryCode) && !TextUtils.isEmpty(Locale.getDefault().getCountry())
                    && PhoneNumberUtil.getInstance()
                            .getCountryCodeForRegion(Locale.getDefault().getCountry()) != 0) {
                // Use Locale as a last resort
                setCountryCode(context, Locale.getDefault().getCountry());
            } else {
                setCountryCode(context, countryCode);
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## getCountryCode failed " + e.getMessage());
        }
    }

    return preferences.getString(COUNTRY_CODE_PREF_KEY, "");
}

From source file:Main.java

/**
 * Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
 *
 * @param context Context reference to get the TelephonyManager instance from
 * @return country code or null/*  w w  w  .  j ava 2 s .  c o  m*/
 */
public static String getDeviceCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toLowerCase(Locale.US);
        } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toLowerCase(Locale.US);
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.quantcast.measurement.service.QCPolicy.java

public static QCPolicy getQuantcastPolicy(Context context, String apiKey, String networkCode,
        String packageName, boolean kidDirected) {
    Uri.Builder builder = Uri.parse(QCUtility.addScheme(POLICY_REQUEST_BASE_WITHOUT_SCHEME)).buildUpon();
    builder.appendQueryParameter(POLICY_REQUEST_API_VERSION_PARAMETER, QCUtility.API_VERSION);
    builder.appendQueryParameter(POLICY_REQUEST_DEVICE_TYPE_PARAMETER, POLICY_REQUEST_DEVICE_TYPE);

    String mcc = null;/*  w  w w.java2 s  . co  m*/
    try {
        TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tel != null) {
            mcc = tel.getNetworkCountryIso();
            if (mcc == null) {
                mcc = tel.getSimCountryIso();
            }
        }
    } catch (SecurityException ignored) {
    }

    if (mcc == null) {
        mcc = Locale.getDefault().getCountry();
    }
    if (mcc != null) {
        builder.appendQueryParameter(POLICY_REQUEST_DEVICE_COUNTRY, mcc);
    }

    if (apiKey != null) {
        builder.appendQueryParameter(POLICY_REQUEST_API_KEY_PARAMETER, apiKey);
    } else {
        builder.appendQueryParameter(POLICY_REQUEST_NETWORK_CODE_PARAMETER, networkCode);
        builder.appendQueryParameter(POLICY_REQUEST_PACKAGE_PARAMETER, packageName);
    }

    if (kidDirected) {
        builder.appendQueryParameter(POLICY_REQUEST_KID_DIRECTED_PARAMETER, "YES");
    }

    Uri builtURL = builder.build();
    if (builtURL != null) {
        return new QCPolicy(context, builtURL.toString());
    } else {
        QCLog.e(TAG, "Policy URL was not built correctly for some reason.  Should not happen");
        return null;
    }
}

From source file:tw.com.ksmt.cloud.libs.Utils.java

/**
 * Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
 *
 * @param context Context reference to get the TelephonyManager instance from
 * @return country code or null/*from  ww  w  .  j  a v  a  2s. c o  m*/
 */
public static String getCountryBySIM(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toUpperCase(Locale.US);
        } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toUpperCase(Locale.US);
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:android_network.hetnet.vpn_service.Util.java

public static boolean isInternational(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return (tm != null && tm.getSimCountryIso() != null
            && !tm.getSimCountryIso().equals(tm.getNetworkCountryIso()));
}

From source file:eu.faircode.netguard.Util.java

public static boolean isEU(Context context) {
    try {//from w w w  . j ava  2  s. c  o  m
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return (tm != null && isEU(tm.getSimCountryIso()) && isEU(tm.getNetworkCountryIso()));
    } catch (Throwable ignored) {
        return false;
    }
}

From source file:eu.faircode.netguard.Util.java

public static boolean isNational(Context context) {
    try {/*from   w  w  w.j  a v  a  2 s .  c om*/
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return (tm != null && tm.getSimCountryIso() != null
                && tm.getSimCountryIso().equals(tm.getNetworkCountryIso()));
    } catch (Throwable ignored) {
        return false;
    }
}

From source file:ca.psiphon.PsiphonTunnel.java

private static String getDeviceRegion(Context context) {
    String region = "";
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null) {
        region = telephonyManager.getSimCountryIso();
        if (region == null) {
            region = "";
        }//from   w  ww  .  j  ava 2 s  .c om
        if (region.length() == 0 && telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) {
            region = telephonyManager.getNetworkCountryIso();
            if (region == null) {
                region = "";
            }
        }
    }
    if (region.length() == 0) {
        Locale defaultLocale = Locale.getDefault();
        if (defaultLocale != null) {
            region = defaultLocale.getCountry();
        }
    }
    return region.toUpperCase(Locale.US);
}

From source file:se.goransson.messengerapp.MainActivity.java

protected void connect() {
    TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String localNbr = mTelephonyManager.getLine1Number();
    String localCode = mTelephonyManager.getNetworkCountryIso().toUpperCase(Locale.getDefault());

    if (localNbr != null && localCode != null) {
        phoneNbr = fixInternationalPhoneForSubscribe(getInternaltionalNumber(localNbr, localCode));
    }/*  ww  w .j  a  va  2  s.c  om*/

    if (phoneNbr != null) {
        client.setHost(getBroker());
        client.setPort(1883);
        client.setId(phoneNbr);
        client.setKeepAlive(7000);
        client.connect();
    } else {
        Toast.makeText(this, R.string.failed_phone, Toast.LENGTH_SHORT).show();
    }
}

From source file:org.totschnig.myexpenses.util.Utils.java

public static Currency getLocalCurrency() {
    Currency result = null;//from ww w  .j a v  a 2  s  .  c o  m
    TelephonyManager telephonyManager = (TelephonyManager) MyApplication.getInstance()
            .getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null) {
        try {
            String userCountry = telephonyManager.getNetworkCountryIso();
            if (TextUtils.isEmpty(userCountry)) {
                userCountry = telephonyManager.getSimCountryIso();
            }
            if (!TextUtils.isEmpty(userCountry)) {
                result = getSaveInstance(Currency.getInstance(new Locale("", userCountry)));
            }
        } catch (Exception e) {
            //fall back to currency from locale
        }
    }
    if (result == null) {
        try {
            //makeSure we know about the currency
            result = getSaveInstance(Currency.getInstance(Locale.getDefault()));
        } catch (IllegalArgumentException e) {
            result = Currency.getInstance("EUR");
        }
    }
    return result;
}