Java Code Examples for android.telephony.TelephonyManager#createForSubscriptionId()

The following examples show how to use android.telephony.TelephonyManager#createForSubscriptionId() . 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: JNIUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 7 votes vote down vote up
public static String[] getCarrierInfo(){
	TelephonyManager tm=(TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
	if(Build.VERSION.SDK_INT>=24){
		tm=tm.createForSubscriptionId(SubscriptionManager.getDefaultDataSubscriptionId());
	}
	if(!TextUtils.isEmpty(tm.getNetworkOperatorName())){
		String mnc="", mcc="";
		String carrierID=tm.getNetworkOperator();
		if(carrierID!=null && carrierID.length()>3){
			mcc=carrierID.substring(0, 3);
			mnc=carrierID.substring(3);
		}
		return new String[]{tm.getNetworkOperatorName(), tm.getNetworkCountryIso().toUpperCase(), mcc, mnc};
	}
	return null;
}
 
Example 2
Source File: JNIUtilities.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public static String[] getCarrierInfo(){
	TelephonyManager tm=(TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
	if(Build.VERSION.SDK_INT>=24){
		tm=tm.createForSubscriptionId(SubscriptionManager.getDefaultDataSubscriptionId());
	}
	if(!TextUtils.isEmpty(tm.getNetworkOperatorName())){
		String mnc="", mcc="";
		String carrierID=tm.getNetworkOperator();
		if(carrierID!=null && carrierID.length()>3){
			mcc=carrierID.substring(0, 3);
			mnc=carrierID.substring(3);
		}
		return new String[]{tm.getNetworkOperatorName(), tm.getNetworkCountryIso().toUpperCase(), mcc, mnc};
	}
	return null;
}
 
Example 3
Source File: MultipathPolicyTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public MultipathTracker(Network network, NetworkCapabilities nc) {
    this.network = network;
    this.mNetworkCapabilities = new NetworkCapabilities(nc);
    try {
        subId = Integer.parseInt(
                ((StringNetworkSpecifier) nc.getNetworkSpecifier()).toString());
    } catch (ClassCastException | NullPointerException | NumberFormatException e) {
        throw new IllegalStateException(String.format(
                "Can't get subId from mobile network %s (%s): %s",
                network, nc, e.getMessage()));
    }

    TelephonyManager tele = mContext.getSystemService(TelephonyManager.class);
    if (tele == null) {
        throw new IllegalStateException(String.format("Missing TelephonyManager"));
    }
    tele = tele.createForSubscriptionId(subId);
    if (tele == null) {
        throw new IllegalStateException(String.format(
                "Can't get TelephonyManager for subId %d", subId));
    }

    subscriberId = tele.getSubscriberId();
    mNetworkTemplate = new NetworkTemplate(
            NetworkTemplate.MATCH_MOBILE, subscriberId, new String[] { subscriberId },
            null, NetworkStats.METERED_ALL, NetworkStats.ROAMING_ALL,
            NetworkStats.DEFAULT_NETWORK_NO);
    mUsageCallback = new UsageCallback() {
        @Override
        public void onThresholdReached(int networkType, String subscriberId) {
            if (DBG) Slog.d(TAG, "onThresholdReached for network " + network);
            mMultipathBudget = 0;
            updateMultipathBudget();
        }
    };

    updateMultipathBudget();
}
 
Example 4
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);
}