From 05f9112d15f506ab8960ee4ae586565d9a023e09 Mon Sep 17 00:00:00 2001 From: Ta-wei Yen Date: Thu, 10 Nov 2016 12:11:56 -0800 Subject: [PATCH] Add createForPhoneAccountHandle() and getServicetate() Respect TelephonyManager's subId when listening to phone state TelecomManager perfers to use a PhoneAccountHandle to represent a account but TelephonyManager prefers subscription IDs. This CL added createForPhoneAccountHandle() in TelephonyManager so telephony values can be queried from it. Currently the ServiceState can be retrieved through registering a PhoneStatelistener, but it is a callback which the handler cannot be specified. getServiceState() is added to allow the ServiceState to be polled. While TelphonyManager has createForSubscriptionId(), listen() does not respect the subId on the manager created from it, and will always use the hidden subId on the listener, which is always the default subId through public API. After this CL, the default subId on the listener will null. TelephonyManager use its' own subId if the listener does not set the subId to something else. Bug: 32637799 Bug: 32414216 Test: cts-tradefed run cts-dev --module CtsTelephonyTestCases Change-Id: I9995e4da1573cf1f6b6e4acf2daf7a538fb60d5f --- api/current.txt | 2 + api/system-current.txt | 2 + api/test-current.txt | 2 + .../android/telephony/PhoneStateListener.java | 10 ++--- .../android/telephony/TelephonyManager.java | 45 +++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/api/current.txt b/api/current.txt index 9cfbbc7d4ef3f..03e3f3619231b 100644 --- a/api/current.txt +++ b/api/current.txt @@ -37211,6 +37211,7 @@ package android.telephony { public class TelephonyManager { method public boolean canChangeDtmfToneLength(); + method public android.telephony.TelephonyManager createForPhoneAccountHandle(android.telecom.PhoneAccountHandle); method public android.telephony.TelephonyManager createForSubscriptionId(int); method public java.util.List getAllCellInfo(); method public int getCallState(); @@ -37233,6 +37234,7 @@ package android.telephony { method public int getNetworkType(); method public int getPhoneCount(); method public int getPhoneType(); + method public android.telephony.ServiceState getServiceState(); method public java.lang.String getSimCountryIso(); method public java.lang.String getSimOperator(); method public java.lang.String getSimOperatorName(); diff --git a/api/system-current.txt b/api/system-current.txt index fab5e00919234..057f1afa9036b 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -40150,6 +40150,7 @@ package android.telephony { method public boolean canChangeDtmfToneLength(); method public int checkCarrierPrivilegesForPackage(java.lang.String); method public int checkCarrierPrivilegesForPackageAnyPhone(java.lang.String); + method public android.telephony.TelephonyManager createForPhoneAccountHandle(android.telecom.PhoneAccountHandle); method public android.telephony.TelephonyManager createForSubscriptionId(int); method public void dial(java.lang.String); method public boolean disableDataConnectivity(); @@ -40187,6 +40188,7 @@ package android.telephony { method public int getNetworkType(); method public int getPhoneCount(); method public int getPhoneType(); + method public android.telephony.ServiceState getServiceState(); method public java.lang.String getSimCountryIso(); method public java.lang.String getSimOperator(); method public java.lang.String getSimOperatorName(); diff --git a/api/test-current.txt b/api/test-current.txt index 7a58e1e17177a..85b5ab8d04f6c 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -37288,6 +37288,7 @@ package android.telephony { public class TelephonyManager { method public boolean canChangeDtmfToneLength(); + method public android.telephony.TelephonyManager createForPhoneAccountHandle(android.telecom.PhoneAccountHandle); method public android.telephony.TelephonyManager createForSubscriptionId(int); method public java.util.List getAllCellInfo(); method public int getCallState(); @@ -37310,6 +37311,7 @@ package android.telephony { method public int getNetworkType(); method public int getPhoneCount(); method public int getPhoneType(); + method public android.telephony.ServiceState getServiceState(); method public java.lang.String getSimCountryIso(); method public java.lang.String getSimOperator(); method public java.lang.String getSimOperatorName(); diff --git a/telephony/java/android/telephony/PhoneStateListener.java b/telephony/java/android/telephony/PhoneStateListener.java index bb2b4478433d9..32f487bb5dbed 100644 --- a/telephony/java/android/telephony/PhoneStateListener.java +++ b/telephony/java/android/telephony/PhoneStateListener.java @@ -233,7 +233,7 @@ public class PhoneStateListener { * @hide */ /** @hide */ - protected int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; + protected Integer mSubId; private final Handler mHandler; @@ -242,7 +242,7 @@ public class PhoneStateListener { * This class requires Looper.myLooper() not return null. */ public PhoneStateListener() { - this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, Looper.myLooper()); + this(null, Looper.myLooper()); } /** @@ -251,7 +251,7 @@ public class PhoneStateListener { * @hide */ public PhoneStateListener(Looper looper) { - this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, looper); + this(null, looper); } /** @@ -260,7 +260,7 @@ public class PhoneStateListener { * own non-null Looper use PhoneStateListener(int subId, Looper looper) below. * @hide */ - public PhoneStateListener(int subId) { + public PhoneStateListener(Integer subId) { this(subId, Looper.myLooper()); } @@ -269,7 +269,7 @@ public class PhoneStateListener { * and non-null Looper. * @hide */ - public PhoneStateListener(int subId, Looper looper) { + public PhoneStateListener(Integer subId, Looper looper) { if (DBG) log("ctor: subId=" + subId + " looper=" + looper); mSubId = subId; mHandler = new Handler(looper) { diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java index 79130e4b16ec1..c6ed04c7bdb6c 100644 --- a/telephony/java/android/telephony/TelephonyManager.java +++ b/telephony/java/android/telephony/TelephonyManager.java @@ -255,6 +255,22 @@ public class TelephonyManager { return new TelephonyManager(mContext, subId); } + /** + * Create a new TelephonyManager object pinned to the subscription ID associated with the given + * phone account. + * + * @return a TelephonyManager that uses the given phone account for all calls, or {@code null} + * if the phone account does not correspond to a valid subscription ID. + */ + @Nullable + public TelephonyManager createForPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) { + int subId = getSubIdForPhoneAccountHandle(phoneAccountHandle); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { + return null; + } + return new TelephonyManager(mContext, subId); + } + /** {@hide} */ public boolean isMultiSimEnabled() { return (multiSimConfig.equals("dsds") || multiSimConfig.equals("dsda") || @@ -2783,6 +2799,12 @@ 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; + } sRegistry.listenForSubscriber(listener.mSubId, getOpPackageName(), listener.callback, events, notifyNow); } catch (RemoteException ex) { @@ -5146,6 +5168,19 @@ public class TelephonyManager { return retval; } + private int getSubIdForPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) { + int retval = SubscriptionManager.INVALID_SUBSCRIPTION_ID; + try { + ITelecomService service = getTelecomService(); + if (service != null) { + retval = getSubIdForPhoneAccount(service.getPhoneAccount(phoneAccountHandle)); + } + } catch (RemoteException e) { + } + + return retval; + } + /** * Resets telephony manager settings back to factory defaults. * @@ -5194,6 +5229,16 @@ public class TelephonyManager { result.send(0, null); } + /** + * Returns the current {@link ServiceState} information. + * + *

Requires Permission: + * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} + */ + public ServiceState getServiceState() { + return getServiceStateForSubscriber(getSubId()); + } + /** * Returns the service state information on specified subscription. Callers require * either READ_PRIVILEGED_PHONE_STATE or READ_PHONE_STATE to retrieve the information.