Check if telephony service is null before making a call.

Bug: 138866013
Test: mma
Change-Id: I125850a8a66aeb2ee09547e6cf573fd23bdcdfc5
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3b63333..a214911 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -34,6 +34,10 @@
     <string name="error_contact_deleted">This contact might have been deleted.</string>
     <!-- Toast text when user has inputted an invalid phone number [CHAR LIMIT=NONE] -->
     <string name="error_invalid_phone_number">Can\'t dial this number. Check it and try again.</string>
+    <!-- Toast text when telephony service is not ready [CHAR LIMIT=NONE] -->
+    <string name="error_telephony_not_available">
+        Phone call is not available. Please try again later.
+    </string>
 
     <!-- Decline an in coming call [CHAR LIMIT=20] -->
     <string name="decline_call">Decline</string>
diff --git a/src/com/android/car/dialer/telecom/UiCallManager.java b/src/com/android/car/dialer/telecom/UiCallManager.java
index b0dbe81..956121c 100644
--- a/src/com/android/car/dialer/telecom/UiCallManager.java
+++ b/src/com/android/car/dialer/telecom/UiCallManager.java
@@ -26,6 +26,7 @@
 import android.content.ServiceConnection;
 import android.net.Uri;
 import android.os.IBinder;
+import android.os.ServiceManager;
 import android.telecom.Call;
 import android.telecom.CallAudioState;
 import android.telecom.CallAudioState.CallAudioRoute;
@@ -39,6 +40,7 @@
 import com.android.car.dialer.log.L;
 import com.android.car.telephony.common.TelecomUtils;
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.ITelephony;
 
 import com.google.i18n.phonenumbers.PhoneNumberUtil;
 import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
@@ -280,8 +282,22 @@
         if (isValidNumber(number)) {
             Uri uri = Uri.fromParts("tel", number, null);
             L.d(TAG, "android.telecom.TelecomManager#placeCall: %s", number);
-            mTelecomManager.placeCall(uri, null);
-            return true;
+
+            // Check if ITelephony is present before placing a call to avoid crash. This is a
+            // temporary fix where system performance is the root cause of the availability of
+            // the service.
+            // TODO(b/138866013): revert when the system is stable and is able to bring up the
+            // ITelephony in time.
+            ITelephony telephony = ITelephony.Stub.asInterface(
+                    ServiceManager.getService(Context.TELEPHONY_SERVICE));
+            if (telephony != null) {
+                mTelecomManager.placeCall(uri, null);
+                return true;
+            }
+
+            Toast.makeText(mContext, R.string.error_telephony_not_available,
+                    Toast.LENGTH_SHORT).show();
+            return false;
         } else {
             L.d(TAG, "invalid number dialed", number);
             Toast.makeText(mContext, R.string.error_invalid_phone_number,
diff --git a/src/com/android/car/dialer/ui/contact/ContactDetailsViewHolder.java b/src/com/android/car/dialer/ui/contact/ContactDetailsViewHolder.java
index 55ebf93..27e113d 100644
--- a/src/com/android/car/dialer/ui/contact/ContactDetailsViewHolder.java
+++ b/src/com/android/car/dialer/ui/contact/ContactDetailsViewHolder.java
@@ -94,15 +94,12 @@
             mText.setTextAppearance(R.style.TextAppearance_ContactDetailsListSubtitle);
         }
 
-        mCallActionView.setOnClickListener(v -> placeCall(phoneNumber));
+        mCallActionView.setOnClickListener(
+                v -> UiCallManager.get().placeCall(phoneNumber.getRawNumber()));
         mFavoriteActionView.setActivated(phoneNumber.isFavorite());
         mFavoriteActionView.setOnClickListener(v -> {
             mPhoneNumberPresenter.onClick(contact, phoneNumber);
             mFavoriteActionView.setActivated(!mFavoriteActionView.isActivated());
         });
     }
-
-    private void placeCall(PhoneNumber number) {
-        UiCallManager.get().placeCall(number.getRawNumber());
-    }
 }
diff --git a/tests/robotests/src/com/android/car/dialer/telecom/UiCallManagerTest.java b/tests/robotests/src/com/android/car/dialer/telecom/UiCallManagerTest.java
index ea5e315..9c64a11 100644
--- a/tests/robotests/src/com/android/car/dialer/telecom/UiCallManagerTest.java
+++ b/tests/robotests/src/com/android/car/dialer/telecom/UiCallManagerTest.java
@@ -32,6 +32,7 @@
 import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.IBinder;
 import android.telecom.CallAudioState;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
@@ -39,6 +40,8 @@
 import com.android.car.dialer.CarDialerRobolectricTestRunner;
 import com.android.car.dialer.R;
 import com.android.car.dialer.TestDialerApplication;
+import com.android.car.dialer.testutils.ShadowServiceManagerOverride;
+import com.android.internal.telephony.ITelephony;
 
 import org.junit.After;
 import org.junit.Before;
@@ -48,6 +51,7 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
 import org.robolectric.shadow.api.Shadow;
 import org.robolectric.shadows.ShadowContextImpl;
 import org.robolectric.shadows.ShadowToast;
@@ -55,6 +59,7 @@
 import java.util.List;
 
 @RunWith(CarDialerRobolectricTestRunner.class)
+@Config(shadows = ShadowServiceManagerOverride.class)
 public class UiCallManagerTest {
 
     private static final String TEL_SCHEME = "tel";
@@ -65,6 +70,10 @@
     private TelecomManager mMockTelecomManager;
     @Mock
     private InCallServiceImpl mMockInCallService;
+    @Mock
+    private IBinder mMockBinder;
+    @Mock
+    private ITelephony mMockITelephony;
 
     @Before
     public void setup() {
@@ -74,6 +83,10 @@
 
         ShadowContextImpl shadowContext = Shadow.extract(((Application) mContext).getBaseContext());
         shadowContext.setSystemService(Context.TELECOM_SERVICE, mMockTelecomManager);
+
+        when(mMockBinder.queryLocalInterface(
+                "com.android.internal.telephony.ITelephony")).thenReturn(mMockITelephony);
+        ShadowServiceManagerOverride.addService(Context.TELEPHONY_SERVICE, mMockBinder);
     }
 
     private void initUiCallManager() {
diff --git a/tests/robotests/src/com/android/car/dialer/testutils/ShadowServiceManagerOverride.java b/tests/robotests/src/com/android/car/dialer/testutils/ShadowServiceManagerOverride.java
new file mode 100644
index 0000000..101a25d
--- /dev/null
+++ b/tests/robotests/src/com/android/car/dialer/testutils/ShadowServiceManagerOverride.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2019 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.car.dialer.testutils;
+
+import android.os.IBinder;
+import android.os.ServiceManager;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+import org.robolectric.shadows.ShadowServiceManager;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Derived from {@link org.robolectric.shadows.ShadowServiceManager}.
+ *
+ * Needed for {@link com.android.car.dialer.telecom.UiCallManagerTest} which needs to set up the
+ * ITelephony service.
+ */
+
+@Implements(value = ServiceManager.class)
+public class ShadowServiceManagerOverride extends ShadowServiceManager {
+    private static final Map<String, IBinder> EXTRA_SERVICES = new HashMap<>();
+
+    @Implementation
+    public static IBinder getService(String name) {
+        IBinder iBinder = ShadowServiceManager.getService(name);
+        if (iBinder == null) {
+            return EXTRA_SERVICES.get(name);
+        }
+        return iBinder;
+    }
+
+    /** Sets the service with {@param name} to {@param service}. */
+    @Implementation
+    public static void addService(String name, IBinder service) {
+        ShadowServiceManager.addService(name, service);
+        EXTRA_SERVICES.put(name, service);
+    }
+
+}