diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java index 11e5625f8538f..c398ad82e0a95 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java @@ -94,8 +94,7 @@ public class MobileSignalController extends SignalController< mPhone = phone; mDefaults = defaults; mSubscriptionInfo = info; - mPhoneStateListener = new MobilePhoneStateListener(info.getSubscriptionId(), - receiverLooper); + mPhoneStateListener = new MobilePhoneStateListener(receiverLooper); mNetworkNameSeparator = getStringIfExists(R.string.status_bar_network_name_separator); mNetworkNameDefault = getStringIfExists( com.android.internal.R.string.lockscreen_carrier_default); @@ -574,8 +573,8 @@ public class MobileSignalController extends SignalController< } class MobilePhoneStateListener extends PhoneStateListener { - public MobilePhoneStateListener(int subId, Looper looper) { - super(subId, looper); + public MobilePhoneStateListener(Looper looper) { + super(looper); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java index faf63c8382593..950f07d5cffa7 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java @@ -662,8 +662,9 @@ public class NetworkControllerImpl extends BroadcastReceiver cachedControllers.remove(subId); } else { MobileSignalController controller = new MobileSignalController(mContext, mConfig, - mHasMobileDataFeature, mPhone, mCallbackHandler, - this, subscriptions.get(i), mSubDefaults, mReceiverHandler.getLooper()); + mHasMobileDataFeature, mPhone.createForSubscriptionId(subId), + mCallbackHandler, this, subscriptions.get(i), + mSubDefaults, mReceiverHandler.getLooper()); controller.setUserSetupComplete(mUserSetup); mMobileSignalControllers.put(subId, controller); if (subscriptions.get(i).getSimSlotIndex() == 0) { @@ -1049,7 +1050,8 @@ public class NetworkControllerImpl extends BroadcastReceiver SubscriptionInfo info = new SubscriptionInfo(id, "", simSlotIndex, "", "", 0, 0, "", 0, null, null, null, "", false, null, null); MobileSignalController controller = new MobileSignalController(mContext, - mConfig, mHasMobileDataFeature, mPhone, mCallbackHandler, this, info, + mConfig, mHasMobileDataFeature, + mPhone.createForSubscriptionId(info.getSubscriptionId()), mCallbackHandler, this, info, mSubDefaults, mReceiverHandler.getLooper()); mMobileSignalControllers.put(id, controller); controller.getState().userSetup = true; diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/NetworkControllerBaseTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/NetworkControllerBaseTest.java index ce5bfced961be..616b46a6d316c 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/NetworkControllerBaseTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/NetworkControllerBaseTest.java @@ -130,6 +130,7 @@ public class NetworkControllerBaseTest extends SysuiTestCase { when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true); when(mMockCm.getDefaultNetworkCapabilitiesForUser(0)).thenReturn( new NetworkCapabilities[] { mNetCapabilities }); + when(mMockTm.createForSubscriptionId(anyInt())).thenReturn(mMockTm); mSignalStrength = mock(SignalStrength.class); mServiceState = mock(ServiceState.class); diff --git a/telephony/java/android/telephony/PhoneStateListener.java b/telephony/java/android/telephony/PhoneStateListener.java index 373c5d27eec85..3ce28a46a029f 100644 --- a/telephony/java/android/telephony/PhoneStateListener.java +++ b/telephony/java/android/telephony/PhoneStateListener.java @@ -38,6 +38,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.Executor; +import dalvik.system.VMRuntime; + /** * A listener class for monitoring changes in specific telephony states * on the device, including service state, signal strength, message @@ -400,8 +402,12 @@ public class PhoneStateListener { @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) public PhoneStateListener(Integer subId) { this(subId, Looper.myLooper()); + if (subId != null && VMRuntime.getRuntime().getTargetSdkVersion() + >= Build.VERSION_CODES.Q) { + throw new IllegalArgumentException("PhoneStateListener with subId: " + + subId + " is not supported, use default constructor"); + } } - /** * Create a PhoneStateListener for the Phone using the specified subscription * and non-null Looper. @@ -410,6 +416,11 @@ public class PhoneStateListener { @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) public PhoneStateListener(Integer subId, Looper looper) { this(subId, new HandlerExecutor(new Handler(looper))); + if (subId != null && VMRuntime.getRuntime().getTargetSdkVersion() + >= Build.VERSION_CODES.Q) { + throw new IllegalArgumentException("PhoneStateListener with subId: " + + subId + " is not supported, use default constructor"); + } } /** diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java index 27fb1d06f2738..4d8fa57a2cc96 100644 --- a/telephony/java/android/telephony/TelephonyManager.java +++ b/telephony/java/android/telephony/TelephonyManager.java @@ -108,6 +108,8 @@ import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; +import dalvik.system.VMRuntime; + /** * Provides access to information about the telephony services on * the device. Applications can use the methods in this class to @@ -4869,18 +4871,22 @@ public class TelephonyManager { * Registers a listener object to receive notification of changes * in specified telephony states. *
- * To register a listener, pass a {@link PhoneStateListener} - * and specify at least one telephony state of interest in - * the events argument. + * To register a listener, pass a {@link PhoneStateListener} and specify at least one telephony + * state of interest in the events argument. * - * At registration, and when a specified telephony state - * changes, the telephony manager invokes the appropriate - * callback method on the listener object and passes the - * current (updated) values. + * At registration, and when a specified telephony state changes, the telephony manager invokes + * the appropriate callback method on the listener object and passes the current (updated) + * values. *
- * To unregister a listener, pass the listener object and set the - * events argument to + * To un-register a listener, pass the listener object and set the events argument to * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). + * + * If this TelephonyManager object has been created with {@link #createForSubscriptionId}, + * applies to the given subId. Otherwise, applies to + * {@link SubscriptionManager#getDefaultSubscriptionId()}. To listen events for multiple subIds, + * pass a separate listener object to each TelephonyManager object created with + * {@link #createForSubscriptionId}. + * * Note: if you call this method while in the middle of a binder transaction, you must * call {@link android.os.Binder#clearCallingIdentity()} before calling this method. A * {@link SecurityException} will be thrown otherwise. @@ -4895,17 +4901,26 @@ public class TelephonyManager { if (mContext == null) return; try { boolean notifyNow = (getITelephony() != null); - // If the listener has not explicitly set the subId (for example, created with the - // default constructor), replace the subId so it will listen to the account the - // telephony manager is created with. - if (listener.mSubId == null) { - listener.mSubId = mSubId; - } - ITelephonyRegistry registry = getTelephonyRegistry(); if (registry != null) { - registry.listenForSubscriber(listener.mSubId, getOpPackageName(), + int subId; + // subId from phonestatelistner is deprecated Q on forward, use the subId from + // TelephonyManager instance. + if (VMRuntime.getRuntime().getTargetSdkVersion() >= Build.VERSION_CODES.Q + || listener.mSubId == null) { + subId = mSubId; + } else { + subId = listener.mSubId; + } + + registry.listenForSubscriber(subId, getOpPackageName(), listener.callback, events, notifyNow); + // TODO: remove this once we remove PhoneStateListener constructor with subId. + if (events == PhoneStateListener.LISTEN_NONE) { + listener.mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; + } else { + listener.mSubId = subId; + } } else { Rlog.w(TAG, "telephony registry not ready."); }