diff options
author | Daniel Norman <danielnorman@google.com> | 2020-11-30 11:41:57 -0800 |
---|---|---|
committer | Daniel Norman <danielnorman@google.com> | 2020-12-02 11:41:53 -0800 |
commit | ac442e0398fe7344f66cbbd4d2af15b1bfdbf46b (patch) | |
tree | 4d4da7e239e222c053b5d48121dd6a88d05c0ba2 /telecomm/java/android/telecom/TelecomManager.java | |
parent | b1975c51b1d4ef7cf7dbab5e4b9bf6072947852c (diff) | |
parent | acf29f85fdc2832e52d9dc9ee416c8835f54f132 (diff) |
Merge SP1A.201130.001
Change-Id: Idcf943792aaa73080b26e2294f0569061502c85d
Diffstat (limited to 'telecomm/java/android/telecom/TelecomManager.java')
-rw-r--r-- | telecomm/java/android/telecom/TelecomManager.java | 531 |
1 files changed, 304 insertions, 227 deletions
diff --git a/telecomm/java/android/telecom/TelecomManager.java b/telecomm/java/android/telecom/TelecomManager.java index 0953808c603d..7710e418add1 100644 --- a/telecomm/java/android/telecom/TelecomManager.java +++ b/telecomm/java/android/telecom/TelecomManager.java @@ -32,6 +32,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.IBinder; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; @@ -42,6 +43,7 @@ import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; +import com.android.internal.annotations.GuardedBy; import com.android.internal.telecom.ITelecomService; import java.lang.annotation.Retention; @@ -961,6 +963,15 @@ public class TelecomManager { private static final String TAG = "TelecomManager"; + + /** Cached service handles, cleared by resetServiceCache() at death */ + private static final Object CACHE_LOCK = new Object(); + + @GuardedBy("CACHE_LOCK") + private static ITelecomService sTelecomService; + @GuardedBy("CACHE_LOCK") + private static final DeathRecipient SERVICE_DEATH = new DeathRecipient(); + private final Context mContext; private final ITelecomService mTelecomServiceOverride; @@ -1015,13 +1026,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) { - try { - if (isServiceConnected()) { - return getTelecomService().getDefaultOutgoingPhoneAccount(uriScheme, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getDefaultOutgoingPhoneAccount(uriScheme, mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getDefaultOutgoingPhoneAccount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getDefaultOutgoingPhoneAccount", e); } return null; } @@ -1041,13 +1053,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public @Nullable PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { - try { - if (isServiceConnected()) { - return getTelecomService().getUserSelectedOutgoingPhoneAccount( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getUserSelectedOutgoingPhoneAccount( mContext.getOpPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getUserSelectedOutgoingPhoneAccount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getUserSelectedOutgoingPhoneAccount", e); } return null; } @@ -1063,12 +1076,13 @@ public class TelecomManager { @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) @SystemApi public void setUserSelectedOutgoingPhoneAccount(@Nullable PhoneAccountHandle accountHandle) { - try { - if (isServiceConnected()) { - getTelecomService().setUserSelectedOutgoingPhoneAccount(accountHandle); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.setUserSelectedOutgoingPhoneAccount(accountHandle); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#setUserSelectedOutgoingPhoneAccount"); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#setUserSelectedOutgoingPhoneAccount"); } } @@ -1082,13 +1096,14 @@ public class TelecomManager { * @see SubscriptionManager#getDefaultVoiceSubscriptionId() */ public PhoneAccountHandle getSimCallManager() { - try { - if (isServiceConnected()) { - return getTelecomService().getSimCallManager( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getSimCallManager( SubscriptionManager.getDefaultSubscriptionId()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getSimCallManager"); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getSimCallManager"); } return null; } @@ -1104,12 +1119,13 @@ public class TelecomManager { * @see SubscriptionManager#getActiveSubscriptionInfoList() */ public @Nullable PhoneAccountHandle getSimCallManagerForSubscription(int subscriptionId) { - try { - if (isServiceConnected()) { - return getTelecomService().getSimCallManager(subscriptionId); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getSimCallManager(subscriptionId); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getSimCallManager"); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getSimCallManager"); } return null; } @@ -1127,12 +1143,13 @@ public class TelecomManager { */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 119305590) public PhoneAccountHandle getSimCallManager(int userId) { - try { - if (isServiceConnected()) { - return getTelecomService().getSimCallManagerForUser(userId); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getSimCallManagerForUser(userId); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getSimCallManagerForUser"); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getSimCallManagerForUser"); } return null; } @@ -1169,13 +1186,14 @@ public class TelecomManager { android.Manifest.permission.READ_PHONE_STATE }) public List<PhoneAccountHandle> getPhoneAccountsSupportingScheme(String uriScheme) { - try { - if (isServiceConnected()) { - return getTelecomService().getPhoneAccountsSupportingScheme(uriScheme, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getPhoneAccountsSupportingScheme(uriScheme, mContext.getOpPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsSupportingScheme", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsSupportingScheme", e); } return new ArrayList<>(); } @@ -1210,13 +1228,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public List<PhoneAccountHandle> getSelfManagedPhoneAccounts() { - try { - if (isServiceConnected()) { - return getTelecomService().getSelfManagedPhoneAccounts(mContext.getOpPackageName(), + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getSelfManagedPhoneAccounts(mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getSelfManagedPhoneAccounts()", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getSelfManagedPhoneAccounts()", e); } return new ArrayList<>(); } @@ -1235,14 +1254,15 @@ public class TelecomManager { @RequiresPermission(READ_PRIVILEGED_PHONE_STATE) public @NonNull List<PhoneAccountHandle> getCallCapablePhoneAccounts( boolean includeDisabledAccounts) { - try { - if (isServiceConnected()) { - return getTelecomService().getCallCapablePhoneAccounts(includeDisabledAccounts, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getCallCapablePhoneAccounts(includeDisabledAccounts, mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getCallCapablePhoneAccounts(" + + includeDisabledAccounts + ")", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getCallCapablePhoneAccounts(" + - includeDisabledAccounts + ")", e); } return new ArrayList<>(); } @@ -1259,12 +1279,13 @@ public class TelecomManager { @SuppressLint("RequiresPermission") @Deprecated public List<PhoneAccountHandle> getPhoneAccountsForPackage() { - try { - if (isServiceConnected()) { - return getTelecomService().getPhoneAccountsForPackage(mContext.getPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getPhoneAccountsForPackage(mContext.getPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsForPackage", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getPhoneAccountsForPackage", e); } return null; } @@ -1277,12 +1298,13 @@ public class TelecomManager { * @return The {@link PhoneAccount} object. */ public PhoneAccount getPhoneAccount(PhoneAccountHandle account) { - try { - if (isServiceConnected()) { - return getTelecomService().getPhoneAccount(account); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getPhoneAccount(account); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getPhoneAccount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getPhoneAccount", e); } return null; } @@ -1295,12 +1317,13 @@ public class TelecomManager { */ @SystemApi public int getAllPhoneAccountsCount() { - try { - if (isServiceConnected()) { - return getTelecomService().getAllPhoneAccountsCount(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getAllPhoneAccountsCount(); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountsCount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountsCount", e); } return 0; } @@ -1313,12 +1336,13 @@ public class TelecomManager { */ @SystemApi public List<PhoneAccount> getAllPhoneAccounts() { - try { - if (isServiceConnected()) { - return getTelecomService().getAllPhoneAccounts(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getAllPhoneAccounts(); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccounts", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccounts", e); } return Collections.EMPTY_LIST; } @@ -1331,12 +1355,13 @@ public class TelecomManager { */ @SystemApi public List<PhoneAccountHandle> getAllPhoneAccountHandles() { - try { - if (isServiceConnected()) { - return getTelecomService().getAllPhoneAccountHandles(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getAllPhoneAccountHandles(); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountHandles", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#getAllPhoneAccountHandles", e); } return Collections.EMPTY_LIST; } @@ -1356,12 +1381,13 @@ public class TelecomManager { * @param account The complete {@link PhoneAccount}. */ public void registerPhoneAccount(PhoneAccount account) { - try { - if (isServiceConnected()) { - getTelecomService().registerPhoneAccount(account); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.registerPhoneAccount(account); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#registerPhoneAccount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#registerPhoneAccount", e); } } @@ -1371,12 +1397,13 @@ public class TelecomManager { * @param accountHandle A {@link PhoneAccountHandle} for the {@link PhoneAccount} to unregister. */ public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { - try { - if (isServiceConnected()) { - getTelecomService().unregisterPhoneAccount(accountHandle); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.unregisterPhoneAccount(accountHandle); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#unregisterPhoneAccount", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#unregisterPhoneAccount", e); } } @@ -1397,12 +1424,13 @@ public class TelecomManager { @SystemApi @SuppressLint("RequiresPermission") public void clearAccounts() { - try { - if (isServiceConnected()) { - getTelecomService().clearAccounts(mContext.getPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.clearAccounts(mContext.getPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#clearAccounts", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#clearAccounts", e); } } @@ -1411,12 +1439,15 @@ public class TelecomManager { * @hide */ public void clearAccountsForPackage(String packageName) { - try { - if (isServiceConnected() && !TextUtils.isEmpty(packageName)) { - getTelecomService().clearAccounts(packageName); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + if (!TextUtils.isEmpty(packageName)) { + service.clearAccounts(packageName); + } + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#clearAccountsForPackage", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#clearAccountsForPackage", e); } } @@ -1429,12 +1460,13 @@ public class TelecomManager { @SystemApi @SuppressLint("RequiresPermission") public ComponentName getDefaultPhoneApp() { - try { - if (isServiceConnected()) { - return getTelecomService().getDefaultPhoneApp(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getDefaultPhoneApp(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get the default phone app.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get the default phone app.", e); } return null; } @@ -1446,12 +1478,13 @@ public class TelecomManager { * selected as the default dialer. */ public String getDefaultDialerPackage() { - try { - if (isServiceConnected()) { - return getTelecomService().getDefaultDialerPackage(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getDefaultDialerPackage(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get the default dialer package name.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get the default dialer package name.", e); } return null; } @@ -1467,13 +1500,14 @@ public class TelecomManager { @SystemApi @RequiresPermission(READ_PRIVILEGED_PHONE_STATE) public @Nullable String getDefaultDialerPackage(@NonNull UserHandle userHandle) { - try { - if (isServiceConnected()) { - return getTelecomService().getDefaultDialerPackageForUser( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getDefaultDialerPackageForUser( userHandle.getIdentifier()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get the default dialer package name.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get the default dialer package name.", e); } return null; } @@ -1500,12 +1534,13 @@ public class TelecomManager { android.Manifest.permission.MODIFY_PHONE_STATE, android.Manifest.permission.WRITE_SECURE_SETTINGS}) public boolean setDefaultDialer(@Nullable String packageName) { - try { - if (isServiceConnected()) { - return getTelecomService().setDefaultDialer(packageName); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.setDefaultDialer(packageName); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to set the default dialer.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to set the default dialer.", e); } return false; } @@ -1517,12 +1552,13 @@ public class TelecomManager { * preloaded. */ public @Nullable String getSystemDialerPackage() { - try { - if (isServiceConnected()) { - return getTelecomService().getSystemDialerPackage(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getSystemDialerPackage(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get the system dialer package name.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get the system dialer package name.", e); } return null; } @@ -1536,13 +1572,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { - try { - if (isServiceConnected()) { - return getTelecomService().isVoiceMailNumber(accountHandle, number, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isVoiceMailNumber(accountHandle, number, mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling ITelecomService#isVoiceMailNumber.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling ITelecomService#isVoiceMailNumber.", e); } return false; } @@ -1556,13 +1593,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public String getVoiceMailNumber(PhoneAccountHandle accountHandle) { - try { - if (isServiceConnected()) { - return getTelecomService().getVoiceMailNumber(accountHandle, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getVoiceMailNumber(accountHandle, mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling ITelecomService#hasVoiceMailNumber.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling ITelecomService#hasVoiceMailNumber.", e); } return null; } @@ -1587,13 +1625,14 @@ public class TelecomManager { android.Manifest.permission.READ_PHONE_NUMBERS }, conditional = true) public String getLine1Number(PhoneAccountHandle accountHandle) { - try { - if (isServiceConnected()) { - return getTelecomService().getLine1Number(accountHandle, + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getLine1Number(accountHandle, mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling ITelecomService#getLine1Number.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling ITelecomService#getLine1Number.", e); } return null; } @@ -1607,13 +1646,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public boolean isInCall() { - try { - if (isServiceConnected()) { - return getTelecomService().isInCall(mContext.getOpPackageName(), + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isInCall(mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling isInCall().", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling isInCall().", e); } return false; } @@ -1628,15 +1668,16 @@ public class TelecomManager { * companion app; {@code false} otherwise. */ public boolean hasCompanionInCallServiceAccess() { - try { - if (isServiceConnected()) { - return getTelecomService().hasCompanionInCallServiceAccess( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.hasCompanionInCallServiceAccess( mContext.getOpPackageName()); - } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling hasCompanionInCallServiceAccess().", e); - if (!isSystemProcess()) { - e.rethrowAsRuntimeException(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling hasCompanionInCallServiceAccess().", e); + if (!isSystemProcess()) { + e.rethrowAsRuntimeException(); + } } } return false; @@ -1655,13 +1696,14 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) public boolean isInManagedCall() { - try { - if (isServiceConnected()) { - return getTelecomService().isInManagedCall(mContext.getOpPackageName(), + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isInManagedCall(mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException calling isInManagedCall().", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException calling isInManagedCall().", e); } return false; } @@ -1684,12 +1726,13 @@ public class TelecomManager { */ @SystemApi public @CallState int getCallState() { - try { - if (isServiceConnected()) { - return getTelecomService().getCallState(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getCallState(); + } catch (RemoteException e) { + Log.d(TAG, "RemoteException calling getCallState().", e); } - } catch (RemoteException e) { - Log.d(TAG, "RemoteException calling getCallState().", e); } return TelephonyManager.CALL_STATE_IDLE; } @@ -1706,12 +1749,13 @@ public class TelecomManager { android.Manifest.permission.READ_PHONE_STATE }) public boolean isRinging() { - try { - if (isServiceConnected()) { - return getTelecomService().isRinging(mContext.getOpPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isRinging(mContext.getOpPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get ringing state of phone app.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get ringing state of phone app.", e); } return false; } @@ -1734,12 +1778,13 @@ public class TelecomManager { @RequiresPermission(Manifest.permission.ANSWER_PHONE_CALLS) @Deprecated public boolean endCall() { - try { - if (isServiceConnected()) { - return getTelecomService().endCall(mContext.getPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.endCall(mContext.getPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#endCall", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#endCall", e); } return false; } @@ -1760,12 +1805,13 @@ public class TelecomManager { {Manifest.permission.ANSWER_PHONE_CALLS, Manifest.permission.MODIFY_PHONE_STATE}) @Deprecated public void acceptRingingCall() { - try { - if (isServiceConnected()) { - getTelecomService().acceptRingingCall(mContext.getPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.acceptRingingCall(mContext.getPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#acceptRingingCall", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#acceptRingingCall", e); } } @@ -1781,13 +1827,14 @@ public class TelecomManager { {Manifest.permission.ANSWER_PHONE_CALLS, Manifest.permission.MODIFY_PHONE_STATE}) @Deprecated public void acceptRingingCall(int videoState) { - try { - if (isServiceConnected()) { - getTelecomService().acceptRingingCallWithVideoState( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.acceptRingingCallWithVideoState( mContext.getPackageName(), videoState); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#acceptRingingCallWithVideoState", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#acceptRingingCallWithVideoState", e); } } @@ -1811,12 +1858,13 @@ public class TelecomManager { */ @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public void silenceRinger() { - try { - if (isServiceConnected()) { - getTelecomService().silenceRinger(mContext.getOpPackageName()); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.silenceRinger(mContext.getOpPackageName()); + } catch (RemoteException e) { + Log.e(TAG, "Error calling ITelecomService#silenceRinger", e); } - } catch (RemoteException e) { - Log.e(TAG, "Error calling ITelecomService#silenceRinger", e); } } @@ -1828,13 +1876,14 @@ public class TelecomManager { android.Manifest.permission.READ_PHONE_STATE }) public boolean isTtySupported() { - try { - if (isServiceConnected()) { - return getTelecomService().isTtySupported(mContext.getOpPackageName(), + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isTtySupported(mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get TTY supported state.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get TTY supported state.", e); } return false; } @@ -1852,13 +1901,14 @@ public class TelecomManager { @SystemApi @RequiresPermission(READ_PRIVILEGED_PHONE_STATE) public @TtyMode int getCurrentTtyMode() { - try { - if (isServiceConnected()) { - return getTelecomService().getCurrentTtyMode(mContext.getOpPackageName(), + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.getCurrentTtyMode(mContext.getOpPackageName(), mContext.getAttributionTag()); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException attempting to get the current TTY mode.", e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException attempting to get the current TTY mode.", e); } return TTY_MODE_OFF; } @@ -1894,8 +1944,9 @@ public class TelecomManager { * {@link ConnectionService#onCreateIncomingConnection}. */ public void addNewIncomingCall(PhoneAccountHandle phoneAccount, Bundle extras) { - try { - if (isServiceConnected()) { + ITelecomService service = getTelecomService(); + if (service != null) { + try { if (extras != null && extras.getBoolean(EXTRA_IS_HANDOVER) && mContext.getApplicationContext().getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.O_MR1) { @@ -1903,11 +1954,10 @@ public class TelecomManager { "acceptHandover for API > O-MR1"); return; } - getTelecomService().addNewIncomingCall( - phoneAccount, extras == null ? new Bundle() : extras); + service.addNewIncomingCall(phoneAccount, extras == null ? new Bundle() : extras); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException adding a new incoming call: " + phoneAccount, e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException adding a new incoming call: " + phoneAccount, e); } } @@ -1942,13 +1992,14 @@ public class TelecomManager { */ public void addNewIncomingConference(@NonNull PhoneAccountHandle phoneAccount, @NonNull Bundle extras) { - try { - if (isServiceConnected()) { - getTelecomService().addNewIncomingConference( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.addNewIncomingConference( phoneAccount, extras == null ? new Bundle() : extras); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException adding a new incoming conference: " + phoneAccount, e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException adding a new incoming conference: " + phoneAccount, e); } } @@ -1965,13 +2016,14 @@ public class TelecomManager { */ @SystemApi public void addNewUnknownCall(PhoneAccountHandle phoneAccount, Bundle extras) { - try { - if (isServiceConnected()) { - getTelecomService().addNewUnknownCall( + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.addNewUnknownCall( phoneAccount, extras == null ? new Bundle() : extras); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException adding a new unknown call: " + phoneAccount, e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException adding a new unknown call: " + phoneAccount, e); } } @@ -2392,12 +2444,13 @@ public class TelecomManager { */ public void acceptHandover(Uri srcAddr, @VideoProfile.VideoState int videoState, PhoneAccountHandle destAcct) { - try { - if (isServiceConnected()) { - getTelecomService().acceptHandover(srcAddr, videoState, destAcct); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.acceptHandover(srcAddr, videoState, destAcct); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException acceptHandover: " + e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException acceptHandover: " + e); } } @@ -2411,13 +2464,14 @@ public class TelecomManager { @SystemApi @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public boolean isInEmergencyCall() { - try { - if (isServiceConnected()) { - return getTelecomService().isInEmergencyCall(); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + return service.isInEmergencyCall(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException isInEmergencyCall: " + e); + return false; } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException isInEmergencyCall: " + e); - return false; } return false; } @@ -2429,12 +2483,13 @@ public class TelecomManager { * @hide */ public void handleCallIntent(Intent intent, String callingPackageProxy) { - try { - if (isServiceConnected()) { - getTelecomService().handleCallIntent(intent, callingPackageProxy); + ITelecomService service = getTelecomService(); + if (service != null) { + try { + service.handleCallIntent(intent, callingPackageProxy); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException handleCallIntent: " + e); } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException handleCallIntent: " + e); } } @@ -2446,14 +2501,36 @@ public class TelecomManager { if (mTelecomServiceOverride != null) { return mTelecomServiceOverride; } - return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE)); + if (sTelecomService == null) { + ITelecomService temp = ITelecomService.Stub.asInterface( + ServiceManager.getService(Context.TELECOM_SERVICE)); + synchronized (CACHE_LOCK) { + if (sTelecomService == null && temp != null) { + try { + sTelecomService = temp; + sTelecomService.asBinder().linkToDeath(SERVICE_DEATH, 0); + } catch (Exception e) { + sTelecomService = null; + } + } + } + } + return sTelecomService; } - private boolean isServiceConnected() { - boolean isConnected = getTelecomService() != null; - if (!isConnected) { - Log.w(TAG, "Telecom Service not found."); + private static class DeathRecipient implements IBinder.DeathRecipient { + @Override + public void binderDied() { + resetServiceCache(); + } + } + + private static void resetServiceCache() { + synchronized (CACHE_LOCK) { + if (sTelecomService != null) { + sTelecomService.asBinder().unlinkToDeath(SERVICE_DEATH, 0); + sTelecomService = null; + } } - return isConnected; } } |