/** Check ISO country by MCC to see if phone is roaming in same registered country */
 protected boolean inSameCountry(String operatorNumeric) {
   if (TextUtils.isEmpty(operatorNumeric) || (operatorNumeric.length() < 5)) {
     // Not a valid network
     return false;
   }
   final String homeNumeric = getHomeOperatorNumeric();
   if (TextUtils.isEmpty(homeNumeric) || (homeNumeric.length() < 5)) {
     // Not a valid SIM MCC
     return false;
   }
   boolean inSameCountry = true;
   final String networkMCC = operatorNumeric.substring(0, 3);
   final String homeMCC = homeNumeric.substring(0, 3);
   final String networkCountry = MccTable.countryCodeForMcc(Integer.parseInt(networkMCC));
   final String homeCountry = MccTable.countryCodeForMcc(Integer.parseInt(homeMCC));
   if (networkCountry.isEmpty() || homeCountry.isEmpty()) {
     // Not a valid country
     return false;
   }
   inSameCountry = homeCountry.equals(networkCountry);
   if (inSameCountry) {
     return inSameCountry;
   }
   // special same country cases
   if ("us".equals(homeCountry) && "vi".equals(networkCountry)) {
     inSameCountry = true;
   } else if ("vi".equals(homeCountry) && "us".equals(networkCountry)) {
     inSameCountry = true;
   }
   return inSameCountry;
 }
  /* Sets the default subscription. If only one phone instance is active that
   * subscription is set as default subscription. If both phone instances
   * are active the first instance "0" is set as default subscription
   */
  public static void setDefaultSubscription(int subId) {
    SystemProperties.set(PROPERTY_DEFAULT_SUBSCRIPTION, Integer.toString(subId));
    int phoneId = SubscriptionController.getInstance().getPhoneId(subId);

    synchronized (sLockProxyPhones) {
      // Set the default phone in base class
      if (phoneId >= 0 && phoneId < sProxyPhones.length) {
        sProxyPhone = sProxyPhones[phoneId];
        sCommandsInterface = sCommandsInterfaces[phoneId];
        sMadeDefaults = true;
      }
    }

    // Update MCC MNC device configuration information
    String defaultMccMnc = TelephonyManager.getDefault().getSimOperatorNumericForPhone(phoneId);
    if (DBG) Rlog.d(LOG_TAG, "update mccmnc=" + defaultMccMnc);
    MccTable.updateMccMncConfiguration(sContext, defaultMccMnc, false);

    // Broadcast an Intent for default sub change
    Intent intent = new Intent(TelephonyIntents.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
    intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
    SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
    Rlog.d(
        LOG_TAG,
        "setDefaultSubscription : " + subId + " Broadcasting Default Subscription Changed...");
    sContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
  }
 protected void updateCarrierMccMncConfiguration(String newOp, String oldOp, Context context) {
   // if we have a change in operator, notify wifi (even to/from none)
   if (((newOp == null) && (TextUtils.isEmpty(oldOp) == false))
       || ((newOp != null) && (newOp.equals(oldOp) == false))) {
     log("update mccmnc=" + newOp + " fromServiceState=true");
     MccTable.updateMccMncConfiguration(context, newOp, true);
   }
 }
  private void handleSimLoaded(int slotId) {
    logd("handleSimStateLoadedInternal: slotId: " + slotId);

    // The SIM should be loaded at this state, but it is possible in cases such as SIM being
    // removed or a refresh RESET that the IccRecords could be null. The right behavior is to
    // not broadcast the SIM loaded.
    IccRecords records = mPhone[slotId].getIccCard().getIccRecords();
    if (records == null) { // Possibly a race condition.
      logd("onRecieve: IccRecords null");
      return;
    }
    if (records.getIccId() == null) {
      logd("onRecieve: IccID null");
      return;
    }
    mIccId[slotId] = records.getIccId();

    if (isAllIccIdQueryDone()) {
      updateSubscriptionInfoByIccId();
    }

    int subId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
    int[] subIds = SubscriptionController.getInstance().getSubId(slotId);
    if (subIds != null) { // Why an array?
      subId = subIds[0];
    }

    if (SubscriptionManager.isValidSubscriptionId(subId)) {
      String operator = records.getOperatorNumeric();
      if (operator != null) {
        if (subId == SubscriptionController.getInstance().getDefaultSubId()) {
          MccTable.updateMccMncConfiguration(mContext, operator, false);
        }
        SubscriptionController.getInstance().setMccMnc(operator, subId);
      } else {
        logd("EVENT_RECORDS_LOADED Operator name is null");
      }
      TelephonyManager tm = TelephonyManager.getDefault();
      String msisdn = tm.getLine1NumberForSubscriber(subId);
      ContentResolver contentResolver = mContext.getContentResolver();

      if (msisdn != null) {
        ContentValues number = new ContentValues(1);
        number.put(SubscriptionManager.NUMBER, msisdn);
        contentResolver.update(
            SubscriptionManager.CONTENT_URI,
            number,
            SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "=" + Long.toString(subId),
            null);
      }

      SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
      String nameToSet;
      String simCarrierName = tm.getSimOperatorNameForSubscription(subId);
      ContentValues name = new ContentValues(1);

      if (subInfo != null
          && subInfo.getNameSource() != SubscriptionManager.NAME_SOURCE_USER_INPUT) {
        if (!TextUtils.isEmpty(simCarrierName)) {
          nameToSet = simCarrierName;
        } else {
          nameToSet = "CARD " + Integer.toString(slotId + 1);
        }
        name.put(SubscriptionManager.DISPLAY_NAME, nameToSet);
        logd("sim name = " + nameToSet);
        contentResolver.update(
            SubscriptionManager.CONTENT_URI,
            name,
            SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "=" + Long.toString(subId),
            null);
      }

      /* Update preferred network type and network selection mode on SIM change.
       * Storing last subId in SharedPreference for now to detect SIM change. */
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
      int storedSubId = sp.getInt(CURR_SUBID + slotId, -1);

      if (storedSubId != subId) {
        int networkType = RILConstants.PREFERRED_NETWORK_MODE;

        // Set the modem network mode
        mPhone[slotId].setPreferredNetworkType(networkType, null);
        Settings.Global.putInt(
            mPhone[slotId].getContext().getContentResolver(),
            Settings.Global.PREFERRED_NETWORK_MODE + subId,
            networkType);

        // Only support automatic selection mode on SIM change.
        mPhone[slotId].getNetworkSelectionMode(
            obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, new Integer(slotId)));

        // Update stored subId
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(CURR_SUBID + slotId, subId);
        editor.apply();
      }
    } else {
      logd("Invalid subId, could not update ContentResolver");
    }

    // Update set of enabled carrier apps now that the privilege rules may have changed.
    CarrierAppUtils.disableCarrierAppsUntilPrivileged(
        mContext.getOpPackageName(),
        mPackageManager,
        TelephonyManager.getDefault(),
        mCurrentlyActiveUserId);

    broadcastSimStateChanged(slotId, IccCardConstants.INTENT_VALUE_ICC_LOADED, null);
    updateCarrierServices(slotId, IccCardConstants.INTENT_VALUE_ICC_LOADED);
  }