diff options
Diffstat (limited to 'java/com/android/dialer/telecom/TelecomUtil.java')
-rw-r--r-- | java/com/android/dialer/telecom/TelecomUtil.java | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/java/com/android/dialer/telecom/TelecomUtil.java b/java/com/android/dialer/telecom/TelecomUtil.java index 82b43835f..573bfe2d9 100644 --- a/java/com/android/dialer/telecom/TelecomUtil.java +++ b/java/com/android/dialer/telecom/TelecomUtil.java @@ -29,9 +29,12 @@ import android.telecom.PhoneAccount; import android.telecom.PhoneAccountHandle; import android.telecom.TelecomManager; import android.text.TextUtils; +import android.util.Pair; import com.android.dialer.common.LogUtil; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Performs permission checks before calling into TelecomManager. Each method is self-explanatory - @@ -45,6 +48,14 @@ public abstract class TelecomUtil { private static TelecomUtilImpl instance = new TelecomUtilImpl(); + /** + * Cache for {@link #isVoicemailNumber(Context, PhoneAccountHandle, String)}. Both + * PhoneAccountHandle and number are cached because multiple numbers might be mapped to true, and + * comparing with {@link #getVoicemailNumber(Context, PhoneAccountHandle)} will not suffice. + */ + private static final Map<Pair<PhoneAccountHandle, String>, Boolean> isVoicemailNumberCache = + new ConcurrentHashMap<>(); + @VisibleForTesting(otherwise = VisibleForTesting.NONE) public static void setInstanceForTesting(TelecomUtilImpl instanceForTesting) { instance = instanceForTesting; @@ -133,12 +144,27 @@ public abstract class TelecomUtil { return instance.isInCall(context); } + /** + * {@link TelecomManager#isVoiceMailNumber(PhoneAccountHandle, String)} takes about 10ms, which is + * way too slow for regular purposes. This method will cache the result for the life time of the + * process. The cache will not be invalidated, for example, if the voicemail number is changed by + * setting up apps like Google Voicemail, the result will be wrong. These events are rare. + */ public static boolean isVoicemailNumber( Context context, PhoneAccountHandle accountHandle, String number) { + if (TextUtils.isEmpty(number)) { + return false; + } + Pair<PhoneAccountHandle, String> cacheKey = new Pair<>(accountHandle, number); + if (isVoicemailNumberCache.containsKey(cacheKey)) { + return isVoicemailNumberCache.get(cacheKey); + } + boolean result = false; if (hasReadPhoneStatePermission(context)) { - return getTelecomManager(context).isVoiceMailNumber(accountHandle, number); + result = getTelecomManager(context).isVoiceMailNumber(accountHandle, number); } - return false; + isVoicemailNumberCache.put(cacheKey, result); + return result; } @Nullable |