Example usage for android.telephony SubscriptionManager from

List of usage examples for android.telephony SubscriptionManager from

Introduction

In this page you can find the example usage for android.telephony SubscriptionManager from.

Prototype

@Deprecated
public static SubscriptionManager from(Context context) 

Source Link

Usage

From source file:hmatalonga.greenhub.models.SimCard.java

/**
 * Experimental call to retrieve sim operator names by subscription ids.
 *
 * @param context Application context/*from  www .ja  v a2s. c om*/
 * @return SIM operator name/names with ";" as a delimiter for many.
 */
private static String getSIMOperators(final Context context) {

    String operators = "";

    if (!PermissionsUtils.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
        return operators;
    }

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        List<SubscriptionInfo> subscriptions = SubscriptionManager.from(context)
                .getActiveSubscriptionInfoList();
        if (subscriptions != null && subscriptions.size() > 0) {
            for (SubscriptionInfo info : subscriptions) {
                int subId = info.getSubscriptionId();
                String operator = getSimOperatorNameForSubscription(context, subId);
                if (operator != null && operator.length() > 0) {
                    operators += operator + ";";
                }
            }
            // Remove last delimiter
            if (operators.length() > 1) {
                operators = operators.substring(0, operators.length() - 1);
            }
        }
    }
    return operators;
}

From source file:com.android.settings.sim.SimSelectNotification.java

@Override
public void onReceive(Context context, Intent intent) {
    final TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
    final int numSlots = telephonyManager.getSimCount();

    // Do not create notifications on single SIM devices or when provisioning i.e. Setup Wizard.
    // or User selection of fallback user preference is disabled.
    if (numSlots < 2 || !Utils.isDeviceProvisioned(context)
            || !SystemProperties.getBoolean("persist.radio.aosp_usr_pref_sel", false)) {
        Log.d(TAG, " no of slots " + numSlots + " provision = " + Utils.isDeviceProvisioned(context));
        return;//from  w w w .  j ava2  s .  co m
    }

    // Cancel any previous notifications
    cancelNotification(context);

    // If sim state is not ABSENT or LOADED then ignore
    String simStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
    if (!(IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(simStatus)
            || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(simStatus))) {
        Log.d(TAG, "sim state is not Absent or Loaded");
        return;
    } else {
        Log.d(TAG, "simstatus = " + simStatus);
    }

    int state;
    for (int i = 0; i < numSlots; i++) {
        state = telephonyManager.getSimState(i);
        if (!(state == TelephonyManager.SIM_STATE_ABSENT || state == TelephonyManager.SIM_STATE_READY
                || state == TelephonyManager.SIM_STATE_UNKNOWN)) {
            Log.d(TAG, "All sims not in valid state yet");
            return;
        }
    }

    List<SubscriptionInfo> sil = subscriptionManager.getActiveSubscriptionInfoList();
    if (sil == null || sil.size() < 1) {
        Log.d(TAG, "Subscription list is empty");
        return;
    }

    // Clear defaults for any subscriptions which no longer exist
    subscriptionManager.clearDefaultsForInactiveSubIds();

    boolean dataSelected = SubscriptionManager
            .isUsableSubIdValue(SubscriptionManager.getDefaultDataSubscriptionId());
    boolean smsSelected = SubscriptionManager
            .isUsableSubIdValue(SubscriptionManager.getDefaultSmsSubscriptionId());

    // If data and sms defaults are selected, dont show notification (Calls default is optional)
    if (dataSelected && smsSelected) {
        Log.d(TAG, "Data & SMS default sims are selected. No notification");
        return;
    }

    // Create a notification to tell the user that some defaults are missing
    createNotification(context);

    if (sil.size() == 1) {
        // If there is only one subscription, ask if user wants to use if for everything
        Intent newIntent = new Intent(context, SimDialogActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.PREFERRED_PICK);
        newIntent.putExtra(SimDialogActivity.PREFERRED_SIM, sil.get(0).getSimSlotIndex());
        context.startActivity(newIntent);
    } else if (!dataSelected) {
        // If there are mulitple, ensure they pick default data
        Intent newIntent = new Intent(context, SimDialogActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK);
        context.startActivity(newIntent);
    }
}

From source file:com.android.settings.sim.SimBootReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    mContext = context;/*ww  w.java  2 s .c  o  m*/
    mSubscriptionManager = SubscriptionManager.from(mContext);
    mSharedPreferences = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

    mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
}

From source file:com.android.settings.datausage.CellDataPreference.java

@Override
public void setTemplate(NetworkTemplate template, int subId, NetworkServices services) {
    if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        throw new IllegalArgumentException("CellDataPreference needs a SubscriptionInfo");
    }/* w  w  w  .j  ava2  s.  co  m*/
    mSubscriptionManager = SubscriptionManager.from(getContext());
    mTelephonyManager = TelephonyManager.from(getContext());
    if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        mSubId = subId;
        setKey(getKey() + subId);
    }
    updateChecked();
}

From source file:com.mediatek.mms.ext.DefaultMmsConfigExt.java

/**
 * M: For OM Version: check whethor has usim card.
 * @param context the context./*from  w  ww .j a  va2 s.co  m*/
 * @return ture: has usim; false: not.
 */
private boolean hasUSIMInserted(Context context) {
    if (context == null) {
        return false;
    }
    int[] ids = SubscriptionManager.from(context).getActiveSubscriptionIdList();
    if (ids != null && ids.length > 0) {
        for (int subId : ids) {
            if (isUSimType(subId)) {
                Log.d(TAG, "[hasUSIMInserted]: true");
                return true;
            }
        }
    }
    Log.d(TAG, "[hasUSIMInserted]: false");
    return false;
}

From source file:com.master.metehan.filtereagle.Util.java

public static boolean isInternational(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && hasPhoneStatePermission(context)) {
        int dataSubId;
        try {//from  w w  w . j a v a  2  s  .c o  m
            dataSubId = Settings.Global.getInt(context.getContentResolver(), "multi_sim_data_call", -1);
        } catch (Throwable ignored) {
            dataSubId = -1;
        }
        if (dataSubId >= 0) {
            SubscriptionManager sm = SubscriptionManager.from(context);
            SubscriptionInfo si = sm.getActiveSubscriptionInfo(dataSubId);
            if (si != null && si.getCountryIso() != null)
                try {
                    Method getNetworkCountryIso = tm.getClass().getMethod("getNetworkCountryIsoForSubscription",
                            int.class);
                    getNetworkCountryIso.setAccessible(true);
                    String networkCountryIso = (String) getNetworkCountryIso.invoke(tm, dataSubId);
                    Log.d(TAG, "SIM=" + si.getCountryIso() + " network=" + networkCountryIso);
                    return !si.getCountryIso().equals(networkCountryIso);
                } catch (Throwable ex) {
                    Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    sendCrashReport(ex, context);
                }
        }
    }

    return (tm == null || tm.getSimCountryIso() == null
            || !tm.getSimCountryIso().equals(tm.getNetworkCountryIso()));
}

From source file:com.mediatek.mms.ext.DefaultMmsConfigExt.java

@Override
public boolean isAllowDRWhenRoaming(Context context, long subId) {
    Log.d(TAG, "DefaultMmsConfigExt, isAllowDRWhenRoaming() subId = " + subId);
    if (!MTK_CT6M_SUPPORT) {
        Log.d(TAG, "DefaultMmsConfigExt, CT6M NOT SUPPORT");
        return true;
    }/*from  ww  w .j  a  va  2  s  . c  om*/
    if (!isUSimType((int) subId)) {
        return true;
    }
    int simCount = SubscriptionManager.from(context).getActiveSubscriptionInfoCount();
    if (simCount <= 0) {
        Log.e(TAG, "isAllowDRWhenRoaming(): Wrong subId!");
        return false;
    }
    TelephonyManagerEx telephonyManagerEx = TelephonyManagerEx.getDefault();
    boolean isRoaming = false;
    isRoaming = telephonyManagerEx.isNetworkRoaming(SubscriptionManager.getSlotId((int) subId));
    Log.d(TAG, "isAllowDRWhenRoaming() isRoaming: " + isRoaming);
    return !isRoaming;
}

From source file:com.android.mms.ui.MessageUtils.java

public static boolean isSmsSubIdActive(Context context, int subId) {
    List<SubscriptionInfo> subinfoList = SubscriptionManager.from(context).getActiveSubscriptionInfoList();
    for (SubscriptionInfo subInfo : subinfoList) {
        if (subInfo.getSubscriptionId() == subId) {
            return true;
        }// w w w .j  av  a2 s . c om
    }
    return false;
}

From source file:com.master.metehan.filtereagle.Util.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
public static String getSubscriptionInfo(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
        return "Not supported";
    if (!hasPhoneStatePermission(context))
        return "No permission";

    StringBuilder sb = new StringBuilder();
    SubscriptionManager sm = SubscriptionManager.from(context);
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    sb.append("Slots ").append(sm.getActiveSubscriptionInfoCount()).append('/')
            .append(sm.getActiveSubscriptionInfoCountMax()).append("\r\n");

    int dataSubId;
    try {/*from   www  .j  a va  2 s.  c om*/
        dataSubId = Settings.Global.getInt(context.getContentResolver(), "multi_sim_data_call", -1);
    } catch (Throwable ignored) {
        dataSubId = -1;
    }

    Method getNetworkCountryIso = null;
    Method getNetworkOperator = null;
    Method getNetworkOperatorName = null;
    Method getDataEnabled = null;
    try {
        getNetworkCountryIso = tm.getClass().getMethod("getNetworkCountryIsoForSubscription", int.class);
        getNetworkOperator = tm.getClass().getMethod("getNetworkOperatorForSubscription", int.class);
        getNetworkOperatorName = tm.getClass().getMethod("getNetworkOperatorName", int.class);
        getDataEnabled = tm.getClass().getMethod("getDataEnabled", int.class);

        getNetworkCountryIso.setAccessible(true);
        getNetworkOperator.setAccessible(true);
        getNetworkOperatorName.setAccessible(true);
        getDataEnabled.setAccessible(true);
    } catch (NoSuchMethodException ex) {
        Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    List<SubscriptionInfo> subscriptions = sm.getActiveSubscriptionInfoList();
    if (subscriptions != null)
        for (SubscriptionInfo si : subscriptions) {
            sb.append("SIM ").append(si.getSimSlotIndex() + 1).append('/').append(si.getSubscriptionId())
                    .append(' ').append(si.getCountryIso()).append('/').append(si.getMcc()).append(si.getMnc())
                    .append(' ').append(si.getCarrierName())
                    .append(si.getDataRoaming() == SubscriptionManager.DATA_ROAMING_ENABLE ? " R" : "")
                    .append(si.getSubscriptionId() == dataSubId ? " *" : "").append("\r\n");
            if (getNetworkCountryIso != null && getNetworkOperator != null && getNetworkOperatorName != null
                    && getDataEnabled != null)
                try {
                    sb.append("Network ").append(si.getSimSlotIndex() + 1).append('/')
                            .append(si.getSubscriptionId()).append(' ')
                            .append(getNetworkCountryIso.invoke(tm, si.getSubscriptionId())).append('/')
                            .append(getNetworkOperator.invoke(tm, si.getSubscriptionId())).append(' ')
                            .append(getNetworkOperatorName.invoke(tm, si.getSubscriptionId()))
                            .append(sm.isNetworkRoaming(si.getSubscriptionId()) ? " R" : "").append(' ')
                            .append(String.format("%B", getDataEnabled.invoke(tm, si.getSubscriptionId())))
                            .append("\r\n");
                } catch (IllegalAccessException | InvocationTargetException ex) {
                    Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
        }

    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);

    return sb.toString();
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
public static String getSubscriptionInfo(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
        return "Not supported";
    if (!hasPhoneStatePermission(context))
        return "No permission";

    StringBuilder sb = new StringBuilder();
    SubscriptionManager sm = SubscriptionManager.from(context);

    sb.append("Slots ").append(sm.getActiveSubscriptionInfoCount()).append('/')
            .append(sm.getActiveSubscriptionInfoCountMax()).append("\r\n");

    int dataid = -1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        dataid = sm.getDefaultDataSubscriptionId();

    int voiceid = -1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        voiceid = sm.getDefaultVoiceSubscriptionId();

    List<SubscriptionInfo> subscriptions = sm.getActiveSubscriptionInfoList();
    if (subscriptions != null)
        for (SubscriptionInfo si : subscriptions)
            sb.append("SIM ").append(si.getSimSlotIndex() + 1).append('/').append(si.getSubscriptionId())
                    .append(' ').append(si.getCountryIso()).append('/').append(si.getMcc()).append(si.getMnc())
                    .append(' ').append(si.getCarrierName())
                    .append(si.getSubscriptionId() == dataid ? " D" : "")
                    .append(si.getSubscriptionId() == voiceid ? " V" : "")
                    .append(si.getDataRoaming() == SubscriptionManager.DATA_ROAMING_ENABLE ? " R" : "")
                    .append("\r\n");

    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);//from  w ww.  ja  v a  2 s  .  co m

    return sb.toString();
}