summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/telecom
diff options
context:
space:
mode:
authortwyen <twyen@google.com>2017-12-12 18:40:11 -0800
committerCopybara-Service <copybara-piper@google.com>2017-12-12 22:51:11 -0800
commita4745bddb3a012c826225df313820ccd8a68455d (patch)
treea3b6f7552bd6e66f28dda2c303249511a498d6be /java/com/android/dialer/telecom
parent19be6707405171fdecb86c04f49a7006e2d64057 (diff)
Move TelecomCallUtil to com.android.dialer
Since PhoneLookup exposes Call, more common access to the utility is required. Bug: 70355819 Test: TelecomCallUtilTest PiperOrigin-RevId: 178847628 Change-Id: I6cf55ad4e3566596b7b2e8cffb5a1614e6640a8b
Diffstat (limited to 'java/com/android/dialer/telecom')
-rw-r--r--java/com/android/dialer/telecom/TelecomCallUtil.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/java/com/android/dialer/telecom/TelecomCallUtil.java b/java/com/android/dialer/telecom/TelecomCallUtil.java
new file mode 100644
index 000000000..acec49851
--- /dev/null
+++ b/java/com/android/dialer/telecom/TelecomCallUtil.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.telecom;
+
+import android.content.Context;
+import android.net.Uri;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.WorkerThread;
+import android.telecom.Call;
+import android.telecom.PhoneAccountHandle;
+import android.telephony.PhoneNumberUtils;
+import android.telephony.SubscriptionInfo;
+import android.text.TextUtils;
+import com.android.dialer.common.Assert;
+import com.android.dialer.common.LogUtil;
+import com.google.common.base.Optional;
+import java.util.Locale;
+
+/**
+ * Class to provide a standard interface for obtaining information from the underlying
+ * android.telecom.Call. Much of this should be obtained through the incall.Call, but on occasion we
+ * need to interact with the telecom.Call directly (eg. call blocking, before the incall.Call has
+ * been created).
+ */
+public class TelecomCallUtil {
+
+ /** Returns Whether the call handle is an emergency number. */
+ public static boolean isEmergencyCall(@NonNull Call call) {
+ Assert.isNotNull(call);
+ Uri handle = call.getDetails().getHandle();
+ return PhoneNumberUtils.isEmergencyNumber(handle == null ? "" : handle.getSchemeSpecificPart());
+ }
+
+ /**
+ * Returns The phone number which the {@code Call} is currently connected, or {@code null} if the
+ * number is not available.
+ */
+ @Nullable
+ public static String getNumber(@Nullable Call call) {
+ if (call == null) {
+ return null;
+ }
+ if (call.getDetails().getGatewayInfo() != null) {
+ return call.getDetails().getGatewayInfo().getOriginalAddress().getSchemeSpecificPart();
+ }
+ Uri handle = getHandle(call);
+ return handle == null ? null : handle.getSchemeSpecificPart();
+ }
+
+ /**
+ * Returns The handle (e.g., phone number) to which the {@code Call} is currently connected, or
+ * {@code null} if the number is not available.
+ */
+ @Nullable
+ public static Uri getHandle(@Nullable Call call) {
+ return call == null ? null : call.getDetails().getHandle();
+ }
+
+ /**
+ * Normalizes the number of the {@code call} to E.164. If the country code is missing in the
+ * number the SIM's country will be used. Only removes non-dialable digits if the country code is
+ * missing.
+ */
+ @WorkerThread
+ public static Optional<String> getNormalizedNumber(Context appContext, Call call) {
+ Assert.isWorkerThread();
+ PhoneAccountHandle phoneAccountHandle = call.getDetails().getAccountHandle();
+ Optional<SubscriptionInfo> subscriptionInfo =
+ TelecomUtil.getSubscriptionInfo(appContext, phoneAccountHandle);
+ String rawNumber = getNumber(call);
+ if (TextUtils.isEmpty(rawNumber)) {
+ return Optional.absent();
+ }
+ String normalizedNumber = PhoneNumberUtils.normalizeNumber(rawNumber);
+ if (TextUtils.isEmpty(normalizedNumber)) {
+ return Optional.absent();
+ }
+ String countryCode =
+ subscriptionInfo.isPresent() ? subscriptionInfo.get().getCountryIso() : null;
+ if (countryCode == null) {
+ LogUtil.w(
+ "PhoneLookupHistoryRecorder.getNormalizedNumber",
+ "couldn't find a country code for call");
+ return Optional.of(normalizedNumber);
+ }
+
+ String e164Number =
+ PhoneNumberUtils.formatNumberToE164(rawNumber, countryCode.toUpperCase(Locale.US));
+ return e164Number == null ? Optional.of(normalizedNumber) : Optional.of(e164Number);
+ }
+}