Show gateway UI when dialing via a gateway

Gateway information is not available in the Call object.  Inside
CallCardPresenter, we use this to show the user dialing information when
dialing out through a gateway.  This includes a "Calling via..." notice
above the call state label and the gateway number.

bug: 10393622
Change-Id: Iff1247cd592b0730bb7cfc6d16bd8f60b47f3737
diff --git a/InCallUI/res/layout/primary_call_info.xml b/InCallUI/res/layout/primary_call_info.xml
index 251ded3..681519b 100644
--- a/InCallUI/res/layout/primary_call_info.xml
+++ b/InCallUI/res/layout/primary_call_info.xml
@@ -110,24 +110,67 @@
         </GridLayout>
         <!-- End of call_banner -->
 
+        <LinearLayout android:id="@+id/supplementary_info_container"
+            style="@style/PrimaryCallInfoSupplementaryInfoContainer"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_vertical|right"
+            android:orientation="vertical"
+            android:background="@color/incall_secondary_info_background"
+            android:animateLayoutChanges="true">
 
-        <!-- The "call state label": In some states, this shows a special
-             indication like "Dialing" or "Incoming call" or "Call ended".
-             It's unused for the normal case of an active ongoing call. -->
-        <TextView android:id="@+id/callStateLabel"
-                  android:layout_width="match_parent"
-                  android:layout_height="wrap_content"
-                  android:paddingTop="@dimen/provider_info_top_bottom_padding"
-                  android:paddingBottom="@dimen/provider_info_top_bottom_padding"
-                  android:paddingStart="@dimen/call_banner_side_padding"
-                  android:paddingEnd="@dimen/call_banner_side_padding"
-                  android:gravity="end"
-                  android:textAppearance="?android:attr/textAppearanceSmall"
-                  android:textColor="@color/incall_call_banner_text_color"
-                  android:textAllCaps="true"
-                  android:background="@color/incall_secondary_info_background"
-                  android:singleLine="true"
-                  android:ellipsize="end"/>
+            <!-- Shown when a gateway provider is used during any outgoing call. -->
+            <LinearLayout android:id="@+id/providerInfo"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:paddingTop="@dimen/provider_info_top_bottom_padding"
+                android:paddingBottom="@dimen/provider_info_top_bottom_padding"
+                android:paddingStart="@dimen/call_banner_side_padding"
+                android:paddingEnd="@dimen/call_banner_side_padding"
+                android:gravity="end"
+                android:orientation="horizontal"
+                android:background="@android:color/transparent"
+                android:visibility="gone">
+                <TextView android:id="@+id/providerLabel"
+                    android:layout_width="0px"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="6"
+                    android:textAppearance="?android:attr/textAppearanceSmall"
+                    android:textAllCaps="true"
+                    android:textColor="@color/incall_call_banner_text_color"
+                    android:singleLine="true"
+                    android:ellipsize="marquee" />
+                <TextView android:id="@+id/providerAddress"
+                    android:layout_width="0px"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="4"
+                    android:gravity="end"
+                    android:paddingStart="8dp"
+                    android:textAppearance="?android:attr/textAppearanceSmall"
+                    android:textAllCaps="true"
+                    android:textColor="@color/incall_call_banner_text_color"
+                    android:singleLine="true"
+                    android:ellipsize="middle" />
+            </LinearLayout>
+
+            <!-- The "call state label": In some states, this shows a special
+                 indication like "Dialing" or "Incoming call" or "Call ended".
+                 It's unused for the normal case of an active ongoing call. -->
+            <TextView android:id="@+id/callStateLabel"
+                      android:layout_width="match_parent"
+                      android:layout_height="wrap_content"
+                      android:paddingTop="@dimen/provider_info_top_bottom_padding"
+                      android:paddingBottom="@dimen/provider_info_top_bottom_padding"
+                      android:paddingStart="@dimen/call_banner_side_padding"
+                      android:paddingEnd="@dimen/call_banner_side_padding"
+                      android:gravity="end"
+                      android:textAppearance="?android:attr/textAppearanceSmall"
+                      android:textColor="@color/incall_call_banner_text_color"
+                      android:textAllCaps="true"
+                      android:background="@color/incall_secondary_info_background"
+                      android:singleLine="true"
+                      android:ellipsize="end"/>
+        </LinearLayout>
 
     </LinearLayout>
 
diff --git a/InCallUI/res/values/styles.xml b/InCallUI/res/values/styles.xml
index 415db36..4570653 100644
--- a/InCallUI/res/values/styles.xml
+++ b/InCallUI/res/values/styles.xml
@@ -152,7 +152,7 @@
     <style name="PrimaryCallInfoPrimaryCallBanner">
         <item name="android:layout_alignParentTop">true</item>
     </style>
-    <style name="PrimaryCallInfoSecondaryInfoContainer">
+    <style name="PrimaryCallInfoSupplementaryInfoContainer">
         <item name="android:layout_below">@id/primary_call_banner</item>
     </style>
 
diff --git a/InCallUI/src/com/android/incallui/CallCardFragment.java b/InCallUI/src/com/android/incallui/CallCardFragment.java
index 65a75d7..da87f99 100644
--- a/InCallUI/src/com/android/incallui/CallCardFragment.java
+++ b/InCallUI/src/com/android/incallui/CallCardFragment.java
@@ -46,6 +46,9 @@
     private TextView mCallStateLabel;
     private ImageView mPhoto;
     private TextView mElapsedTime;
+    private View mProviderInfo;
+    private TextView mProviderLabel;
+    private TextView mProviderNumber;
 
     // Secondary caller info
     private ViewStub mSecondaryCallInfo;
@@ -69,7 +72,7 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        getPresenter().init(ServiceFactory.newPhoneNumberService(getActivity()));
+        getPresenter().init(getActivity(), ServiceFactory.newPhoneNumberService(getActivity()));
     }
 
     @Override
@@ -93,6 +96,9 @@
         mPhoto = (ImageView) view.findViewById(R.id.photo);
         mCallStateLabel = (TextView) view.findViewById(R.id.callStateLabel);
         mElapsedTime = (TextView) view.findViewById(R.id.elapsedTime);
+        mProviderInfo = view.findViewById(R.id.providerInfo);
+        mProviderLabel = (TextView) view.findViewById(R.id.providerLabel);
+        mProviderNumber = (TextView) view.findViewById(R.id.providerAddress);
     }
 
     @Override
@@ -122,7 +128,8 @@
 
     @Override
     public void setPrimary(String number, String name, boolean nameIsNumber, String label,
-            Drawable photo, boolean isConference) {
+            Drawable photo, boolean isConference, String gatewayLabel, String gatewayNumber) {
+        Log.d(this, "Setting primary call [" + gatewayLabel + "][" + gatewayNumber + "]");
 
         if (isConference) {
             name = getView().getResources().getString(R.string.card_title_conf_call);
@@ -139,6 +146,15 @@
             mPhoneNumber.setTextDirection(View.TEXT_DIRECTION_LTR);
         }
 
+        // Set any gateway information
+        if (!TextUtils.isEmpty(gatewayLabel) && !TextUtils.isEmpty(gatewayNumber)) {
+            mProviderLabel.setText(gatewayLabel);
+            mProviderNumber.setText(gatewayNumber);
+            mProviderInfo.setVisibility(View.VISIBLE);
+        } else {
+            mProviderInfo.setVisibility(View.GONE);
+        }
+
         // Set direction of the name field
 
         // set the name field.
diff --git a/InCallUI/src/com/android/incallui/CallCardPresenter.java b/InCallUI/src/com/android/incallui/CallCardPresenter.java
index 5ab2529..f233d78 100644
--- a/InCallUI/src/com/android/incallui/CallCardPresenter.java
+++ b/InCallUI/src/com/android/incallui/CallCardPresenter.java
@@ -16,6 +16,9 @@
 
 package com.android.incallui;
 
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.graphics.drawable.Drawable;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -54,9 +57,9 @@
     private ContactCacheEntry mPrimaryContactInfo;
     private ContactCacheEntry mSecondaryContactInfo;
     private CallTimer mCallTimer;
+    private Context mContext;
 
     public CallCardPresenter() {
-
         // create the call timer
         mCallTimer = new CallTimer(new Runnable() {
             @Override
@@ -66,7 +69,8 @@
         });
     }
 
-    public void init(PhoneNumberService phoneNumberService) {
+    public void init(Context context, PhoneNumberService phoneNumberService) {
+        mContext = context;
         mPhoneNumberService = phoneNumberService;
     }
 
@@ -266,11 +270,14 @@
             final String name = getNameForCall(mPrimaryContactInfo);
             final String number = getNumberForCall(mPrimaryContactInfo);
             final boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
+            final String gatewayLabel = getGatewayLabel();
+            final String gatewayNumber = getGatewayNumber();
             ui.setPrimary(number, name, nameIsNumber, mPrimaryContactInfo.label,
-                    mPrimaryContactInfo.photo, mPrimary.isConferenceCall());
+                    mPrimaryContactInfo.photo, mPrimary.isConferenceCall(), gatewayLabel,
+                    gatewayNumber);
         } else {
             // reset to nothing (like at end of call)
-            ui.setPrimary(null, null, false, null, null, false);
+            ui.setPrimary(null, null, false, null, null, false, null, null);
         }
 
     }
@@ -300,6 +307,41 @@
         }
     }
 
+    /**
+     * Returns the gateway number for any existing outgoing call.
+     */
+    private String getGatewayNumber() {
+        if (hasOutgoingGatewayCall()) {
+            return mPrimary.getGatewayNumber();
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns the label for the gateway app for any existing outgoing call.
+     */
+    private String getGatewayLabel() {
+        if (hasOutgoingGatewayCall() && getUi() != null) {
+            final PackageManager pm = mContext.getPackageManager();
+            try {
+                final ApplicationInfo info = pm.getApplicationInfo(mPrimary.getGatewayPackage(), 0);
+                return mContext.getString(R.string.calling_via_template,
+                        pm.getApplicationLabel(info).toString());
+            } catch (PackageManager.NameNotFoundException e) {
+            }
+        }
+        return null;
+    }
+
+    private boolean hasOutgoingGatewayCall() {
+        // We only display the gateway information while DIALING so return false for any othe
+        // call state.
+        return (mPrimary.getState() == Call.State.DIALING &&
+                !TextUtils.isEmpty(mPrimary.getGatewayNumber()) &&
+                !TextUtils.isEmpty(mPrimary.getGatewayPackage()));
+    }
+
     private void fetchImage(final String url) {
         if (url != null) {
             new AsyncTask<Void, Void, Bitmap>() {
@@ -374,7 +416,7 @@
     public interface CallCardUi extends Ui {
         void setVisible(boolean on);
         void setPrimary(String number, String name, boolean nameIsNumber, String label,
-                Drawable photo, boolean isConference);
+                Drawable photo, boolean isConference, String gatewayLabel, String gatewayNumber);
         void setSecondary(boolean show, String name, String label, Drawable photo);
         void setCallState(int state, Call.DisconnectCause cause, boolean bluetoothOn);
         void setPrimaryCallElapsedTime(boolean show, String duration);