Java Code Examples for android.telephony.SubscriptionManager#getActiveSubscriptionInfoList()

The following examples show how to use android.telephony.SubscriptionManager#getActiveSubscriptionInfoList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SettingsFragment.java    From sms-ticket with Apache License 2.0 7 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private void fillDualSimList(PreferenceScreen preferenceScreen) {
    PreferenceCategory category = (PreferenceCategory) preferenceScreen.findPreference("sms_category");
    ListPreference preference = (ListPreference) category.findPreference(Preferences.DUALSIM_SIM);
    List<String> simIds = new ArrayList<>();
    List<String> simNames = new ArrayList<>();
    simIds.add(String.valueOf(Preferences.VALUE_DEFAULT_SIM));
    simNames.add(getString(R.string.sim_default));
    SubscriptionManager subscriptionManager = SubscriptionManager.from(getActivity());
    for (SubscriptionInfo subscriptionInfo : subscriptionManager.getActiveSubscriptionInfoList()) {
        simIds.add(String.valueOf(subscriptionInfo.getSubscriptionId()));
        simNames.add(getString(R.string.sim_name, subscriptionInfo.getSimSlotIndex() + 1, subscriptionInfo
            .getDisplayName()));
    }
    preference.setEntries(simNames.toArray(new String[simNames.size()]));
    preference.setEntryValues(simIds.toArray(new String[simIds.size()]));
    preference.setDefaultValue(String.valueOf(Preferences.VALUE_DEFAULT_SIM));
    preference.setSummary(preference.getEntry());
}
 
Example 2
Source File: SimChangedReceiver.java    From Silence with GNU General Public License v3.0 7 votes vote down vote up
private static String getDeviceSubscriptions(Context context) {
  if (Build.VERSION.SDK_INT < 22) return "1";

  SubscriptionManager    subscriptionManager = SubscriptionManager.from(context);
  List<SubscriptionInfo> activeSubscriptions = subscriptionManager.getActiveSubscriptionInfoList();

  if (activeSubscriptions == null) return "1";

  String[] subscriptions = new String[activeSubscriptions.size()];
  for(int i=0; i<activeSubscriptions.size(); i++){
    subscriptions[i] = Integer.toString(activeSubscriptions.get(i).getSubscriptionId());
  }

  Arrays.sort(subscriptions);

  return joinString(subscriptions);
}
 
Example 3
Source File: DialerActivity.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
public static void call(final CharSequence number, final Context context, final boolean directly) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1 && !directly && BPrefs.get(context).getBoolean(BPrefs.DUAL_SIM_KEY, BPrefs.DUAL_SIM_DEFAULT_VALUE)) {
            final SubscriptionManager subscriptionManager = (SubscriptionManager) context
                    .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
            if (activeSubscriptionInfoList != null && (activeSubscriptionInfoList.size() > 1)) {
                final CharSequence[] simNames = new CharSequence[activeSubscriptionInfoList.size()];
                for (int i = 0; i < activeSubscriptionInfoList.size(); i++) {
                    simNames[i] = activeSubscriptionInfoList.get(i).getDisplayName();
                }
                BDB.from(context)
                        .addFlag(BDialog.FLAG_OK | BDialog.FLAG_CANCEL)
                        .setTitle(R.string.choose_sim)
                        .setSubText(R.string.choose_sim_subtext)
                        .setOptions(simNames)
                        .setPositiveButtonListener(params -> {
                            call(number, context, activeSubscriptionInfoList.get((Integer) params[0]));
                            return true;
                        }).show();
            } else
                call(number, context, null);
        } else
            call(number, context, null);
    } else
        BaldToast.error(context);

}
 
Example 4
Source File: SimCardUtils.java    From MobileInfo with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"NewApi"})
private List<SubscriptionInfo> getSubInfoList(Context context) {
    SubscriptionManager subscriptionManager = SubscriptionManager.from(context.getApplicationContext());
    List list = null;
    if (subscriptionManager != null) {
        list = subscriptionManager.getActiveSubscriptionInfoList();
    }

    return list;
}
 
Example 5
Source File: MainActivity.java    From Msgs with MIT License 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
private void sendSms(final int which,String phone,String context) {
    SubscriptionInfo sInfo = null;

    final SubscriptionManager sManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

    List<SubscriptionInfo> list = sManager.getActiveSubscriptionInfoList();

    if (list.size() == 2) {
        // 双卡
        sInfo = list.get(which);
    } else {
        // 单卡
        sInfo = list.get(0);
    }

    if (sInfo != null) {
        int subId = sInfo.getSubscriptionId();
        SmsManager manager = SmsManager.getSmsManagerForSubscriptionId(subId);

        if (!TextUtils.isEmpty(phone)) {
            ArrayList<String> messageList =manager.divideMessage(context);
            for(String text:messageList){
                manager.sendTextMessage(phone, null, text, null, null);
            }
            Toast.makeText(this, "信息正在发送,请稍候", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(this, "无法正确的获取SIM卡信息,请稍候重试",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
 
Example 6
Source File: SubscriptionHelper.java    From BlackList with Apache License 2.0 5 votes vote down vote up
@Nullable
public static List<SubscriptionInfo> getSubscriptions(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(context);
        return sm.getActiveSubscriptionInfoList();
    }

    return null;
}
 
Example 7
Source File: BuilderSimCard.java    From SmsScheduler with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RadioGroup build() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
        return getView();
    }
    SubscriptionManager subscriptionManager = (SubscriptionManager) activity.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    if (null == subscriptionManager) {
        return getView();
    }
    if (subscriptionManager.getActiveSubscriptionInfoCount() < 2) {
        sms.setSubscriptionId(subscriptionManager.getActiveSubscriptionInfoList().get(0).getSubscriptionId());
        return getView();
    }
    getView().setVisibility(View.VISIBLE);
    List<Pair<Integer, String>> simCards = new ArrayList<>();
    for (SubscriptionInfo info: subscriptionManager.getActiveSubscriptionInfoList()) {
        simCards.add(new Pair<>(info.getSubscriptionId(), info.getCarrierName().toString()));
    }
    RadioButton radio1 = getView().findViewById(R.id.radio_sim1);
    RadioButton radio2 = getView().findViewById(R.id.radio_sim2);
    prepareRadioButton(radio1, simCards.get(0));
    prepareRadioButton(radio2, simCards.get(1));
    if (!radio1.isChecked() && !radio2.isChecked()) {
        radio1.setChecked(true);
    }
    return getView();
}
 
Example 8
Source File: Phone.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves network operator names from subscription manager.
 * NOTE: Requires SDK level 22 or above
 *
 * @param context
 * @return
 */
private static String getNetworkOperators(Context context) {
    String operator = "";

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

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
        if (subscriptionManager != null) {
            List<SubscriptionInfo> subscriptions =
                    subscriptionManager.getActiveSubscriptionInfoList();
            if (subscriptions != null) {
                for (SubscriptionInfo info : subscriptions) {
                    CharSequence carrierName = info.getCarrierName();
                    if (carrierName != null && carrierName.length() > 0) {
                        operator += carrierName + ";";
                    }
                }
                // Remove last delimiter
                if (operator.length() >= 1) {
                    operator = operator.substring(0, operator.length() - 1);
                }
            }
        }
    }
    return operator;
}
 
Example 9
Source File: Util.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
@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);

    return sb.toString();
}
 
Example 10
Source File: CollectorService.java    From TowerCollector with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    Timber.d("onCreate(): Creating service");
    MyApplication.startBackgroundTask(this);
    // get managers
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    TelephonyManager defaultTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (MobileUtils.isApi24MultiSimCompatible()) { // multi-sim, multiple listeners
        try {
            SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            List<SubscriptionInfo> activeSubscriptions = subscriptionManager.getActiveSubscriptionInfoList();
            if (activeSubscriptions == null || activeSubscriptions.isEmpty()) {
                // fallback in case subscriptions state is unknown or inactive
                telephonyTriples.add(new TelephonyTriple(defaultTelephonyManager));
            } else {
                for (SubscriptionInfo subscription : activeSubscriptions) {
                    TelephonyManager telephonyManager = defaultTelephonyManager.createForSubscriptionId(subscription.getSubscriptionId());
                    telephonyTriples.add(new TelephonyTriple(telephonyManager));
                }
                // if due to some stupid bug active subscription don't have telephony manager
                if (telephonyTriples.isEmpty()) {
                    telephonyTriples.add(new TelephonyTriple(defaultTelephonyManager));
                }
            }
        } catch (SecurityException ex) {
            Timber.e(ex, "onCreate(): phone permission is denied");
            stopSelf();
            return;
        }
    } else { // single-sim, single listener
        telephonyTriples.add(new TelephonyTriple(defaultTelephonyManager));
    }
    boolean hideNotification = MyApplication.getPreferencesProvider().getHideCollectorNotification();
    notificationHelper = new CollectorNotificationHelper(this, hideNotification);
    // create notification
    startStats = MeasurementsDatabase.getInstance(MyApplication.getApplication()).getMeasurementsStatistics();
    startTime = lastLocationObtainedTime = System.currentTimeMillis();
    EventBus.getDefault().register(this);
    // register receiver
    registerReceiver(stopRequestBroadcastReceiver, new IntentFilter(BROADCAST_INTENT_STOP_SERVICE));
    registerReceiver(batteryStatusBroadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
    registerReceiver(locationModeOrProvidersChanged, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
    registerReceiver(locationModeOrProvidersChanged, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
    Notification notification = notificationHelper.createNotification(notificationManager, getGpsStatusNotificationText(getGpsStatus()));
    // start as foreground service to prevent from killing
    startForeground(NOTIFICATION_ID, notification);
}
 
Example 11
Source File: RNSimDataModule.java    From react-native-sim-data with MIT License 4 votes vote down vote up
@Override
public Map<String, Object> getConstants() {

  final Map<String, Object> constants = new HashMap<>();

  try {
    TelephonyManager telManager = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE);

    SubscriptionManager manager = (SubscriptionManager) this.reactContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subscriptionInfos = manager.getActiveSubscriptionInfoList();

    int sub = 0;
    for (SubscriptionInfo subInfo : subscriptionInfos) {
      CharSequence carrierName = subInfo.getCarrierName();
      String countryIso        = subInfo.getCountryIso();
      int dataRoaming          = subInfo.getDataRoaming();  // 1 is enabled ; 0 is disabled
      CharSequence displayName = subInfo.getDisplayName();
      String iccId             = subInfo.getIccId();
      int mcc                  = subInfo.getMcc();
      int mnc                  = subInfo.getMnc();
      String number            = subInfo.getNumber();
      int simSlotIndex         = subInfo.getSimSlotIndex();
      int subscriptionId       = subInfo.getSubscriptionId();
      boolean networkRoaming   = telManager.isNetworkRoaming();
      String deviceId          = telManager.getDeviceId(simSlotIndex);
      //String deviceId          = telManager.getImei(simSlotIndex) || telManager.getMeid(simSlotIndex);

      constants.put("carrierName" + sub, carrierName.toString());
      constants.put("displayName" + sub, displayName.toString());
      constants.put("countryCode" + sub, countryIso);
      constants.put("mcc" + sub, mcc);
      constants.put("mnc" + sub, mnc);
      constants.put("isNetworkRoaming" + sub, networkRoaming);
      constants.put("isDataRoaming"    + sub, (dataRoaming == 1));
      constants.put("simSlotIndex"     + sub, simSlotIndex);
      constants.put("phoneNumber"      + sub, number);
      constants.put("deviceId"         + sub, deviceId);
      constants.put("simSerialNumber"  + sub, iccId);
      constants.put("subscriptionId"   + sub, subscriptionId);
      sub++;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

  return constants;
}
 
Example 12
Source File: PhoneStateScanner.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint("InlinedApi")
void connect() {
    //PPApplication.logE("PhoneStateScanner.connect", "xxx");
    boolean isPowerSaveMode = DataWrapper.isPowerSaveMode(context);
    if (/*PPApplication.*/isPowerSaveMode && ApplicationPreferences.applicationEventMobileCellsScanInPowerSaveMode.equals("2"))
        // start scanning in power save mode is not allowed
        return;

    /*if (PPApplication.logEnabled()) {
        PPApplication.logE("PhoneStateScanner.connect", "telephonyManager=" + telephonyManager);
        PPApplication.logE("PhoneStateScanner.connect", "FEATURE_TELEPHONY=" + PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY));
        PPApplication.logE("PhoneStateScanner.connect", "checkLocation=" + Permissions.checkLocation(context.getApplicationContext()));
    }*/

    if ((telephonyManager != null) &&
            PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY) &&
            Permissions.checkLocation(context.getApplicationContext())) {
        boolean simIsReady = false;
        if (Build.VERSION.SDK_INT < 26) {
            if (telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY)
                // sim card is ready
                simIsReady = true;
        } else {
            if (Permissions.checkPhone(context.getApplicationContext())) {
                SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
                //SubscriptionManager.from(context);
                if (mSubscriptionManager != null) {
                    List<SubscriptionInfo> subscriptionList = null;
                    try {
                        // Loop through the subscription list i.e. SIM list.
                        subscriptionList = mSubscriptionManager.getActiveSubscriptionInfoList();
                    } catch (SecurityException e) {
                        PPApplication.recordException(e);
                    }
                    if (subscriptionList != null) {
                        for (int i = 0; i < subscriptionList.size();/*mSubscriptionManager.getActiveSubscriptionInfoCountMax();*/ i++) {
                            // Get the active subscription ID for a given SIM card.
                            SubscriptionInfo subscriptionInfo = subscriptionList.get(i);
                            if (subscriptionInfo != null) {
                                int slotIndex = subscriptionInfo.getSimSlotIndex();
                                if (telephonyManager.getSimState(slotIndex) == TelephonyManager.SIM_STATE_READY) {
                                    // sim card is ready
                                    simIsReady = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (simIsReady) {
            //PPApplication.logE("PhoneStateScanner.connect", "telephonyManager.listen");
            telephonyManager.listen(this,
                    //  PhoneStateListener.LISTEN_CALL_STATE
                    PhoneStateListener.LISTEN_CELL_INFO // Requires API 17
                            //| PhoneStateListener.LISTEN_CELL_LOCATION
                            //| PhoneStateListener.LISTEN_DATA_ACTIVITY
                            //| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
                            | PhoneStateListener.LISTEN_SERVICE_STATE
                    //| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
                    //| PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
                    //| PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
            );
            //checkLocationEnabled();
        }
    }
    startAutoRegistration(context, true);
}