Implement SIM selection dialog

Bug: 64216442,64214592,64213352
Test: N/A
PiperOrigin-RevId: 176424724
Change-Id: I1709156098a14ac3bd35d98b913e7b881fcd9b2b
diff --git a/java/com/android/contacts/common/res/layout/default_account_checkbox.xml b/java/com/android/contacts/common/res/layout/default_account_checkbox.xml
index b7c0cf6..1e76b74 100644
--- a/java/com/android/contacts/common/res/layout/default_account_checkbox.xml
+++ b/java/com/android/contacts/common/res/layout/default_account_checkbox.xml
@@ -15,22 +15,36 @@
 -->
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-  android:id="@+id/default_account_checkbox_layout"
-  android:layout_width="match_parent"
-  android:layout_height="wrap_content"
-  android:padding="4dp"
-  android:orientation="vertical">
-  <CheckBox
-    android:id="@+id/default_account_checkbox_view"
+    android:id="@+id/default_account_checkbox_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:layout_marginBottom="20dip"
-    android:layout_marginLeft="13dip"
-    android:paddingStart="15dip"
+    android:padding="4dp"
+    android:paddingStart="0dp"
+    android:paddingEnd="24dp"
     android:gravity="center"
-    android:text="@string/set_default_account"
-    android:textAlignment="viewStart"
-    android:textAppearance="?android:attr/textAppearanceSmall"
-    android:textColor="@color/dialer_secondary_text_color"
+    android:minHeight="56dp"
+    android:orientation="horizontal"
+    android:focusable="true">
+  <FrameLayout
+      android:layout_width="72dp"
+      android:layout_height="wrap_content">
+    <CheckBox
+        android:id="@+id/default_account_checkbox_view"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:checked="false"/>
     />
+  </FrameLayout>
+  <TextView
+      android:id="@+id/default_account_checkbox_text"
+      android:layout_width="0dp"
+      android:layout_height="wrap_content"
+      android:layout_weight="1"
+      android:text="@string/set_default_account"
+      android:textAlignment="viewStart"
+      android:textAppearance="?android:attr/textAppearanceSmall"
+      android:textColor="@color/dialer_secondary_text_color"
+      />
+
 </LinearLayout>
diff --git a/java/com/android/contacts/common/res/layout/select_account_list_item.xml b/java/com/android/contacts/common/res/layout/select_account_list_item.xml
index fbd31e5..8f7cc70 100644
--- a/java/com/android/contacts/common/res/layout/select_account_list_item.xml
+++ b/java/com/android/contacts/common/res/layout/select_account_list_item.xml
@@ -16,41 +16,53 @@
 
 <!-- Layout of a single item in the InCallUI Account Chooser Dialog. -->
 <com.android.contacts.common.widget.ActivityTouchLinearLayout
-  xmlns:android="http://schemas.android.com/apk/res/android"
-  android:layout_width="match_parent"
-  android:layout_height="wrap_content"
-  android:padding="8dp"
-  android:orientation="horizontal">
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="64dp"
+    android:padding="8dp"
+    android:paddingStart="24dp"
+    android:paddingEnd="24dp"
+    android:orientation="horizontal"
+    android:gravity="center">
 
   <ImageView
-    android:id="@+id/icon"
-    android:layout_width="48dp"
-    android:layout_height="48dp"
-    android:scaleType="center"/>
+      android:id="@+id/icon"
+      android:layout_width="24dp"
+      android:layout_height="24dp"
+      android:scaleType="center"/>
 
   <LinearLayout
-    android:id="@+id/text"
-    android:layout_width="0dp"
-    android:layout_height="wrap_content"
-    android:layout_weight="1"
-    android:layout_marginStart="8dp"
-    android:gravity="start|center_vertical"
-    android:orientation="vertical">
-    <TextView
-      android:id="@+id/label"
-      android:layout_width="match_parent"
+      android:id="@+id/text"
+      android:layout_width="0dp"
       android:layout_height="wrap_content"
-      android:includeFontPadding="false"
-      android:textAppearance="?android:attr/textAppearanceMedium"
-      android:textColor="@color/dialer_primary_text_color"/>
+      android:layout_weight="1"
+      android:paddingStart="24dp"
+      android:gravity="start|center_vertical"
+      android:orientation="vertical">
     <TextView
-      android:id="@+id/number"
-      android:layout_width="match_parent"
-      android:layout_height="wrap_content"
-      android:includeFontPadding="false"
-      android:maxLines="1"
-      android:textAppearance="?android:attr/textAppearanceSmall"
-      android:visibility="gone"/>
+        android:id="@+id/label"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:includeFontPadding="false"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textColor="@color/dialer_primary_text_color"/>
+    <TextView
+        android:id="@+id/number"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:includeFontPadding="false"
+        android:maxLines="1"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:visibility="gone"/>
+    <TextView
+        android:id="@+id/hint"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:includeFontPadding="false"
+        android:maxLines="1"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:visibility="gone"/>
   </LinearLayout>
 
 </com.android.contacts.common.widget.ActivityTouchLinearLayout>
diff --git a/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java b/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java
index e21fded..3c9d926 100644
--- a/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java
+++ b/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java
@@ -55,12 +55,15 @@
 
   private static final String ARG_TITLE_RES_ID = "title_res_id";
   private static final String ARG_CAN_SET_DEFAULT = "can_set_default";
+  private static final String ARG_SET_DEFAULT_RES_ID = "set_default_res_id";
   private static final String ARG_ACCOUNT_HANDLES = "account_handles";
   private static final String ARG_IS_DEFAULT_CHECKED = "is_default_checked";
   private static final String ARG_LISTENER = "listener";
   private static final String ARG_CALL_ID = "call_id";
+  private static final String ARG_HINTS = "hints";
 
   private List<PhoneAccountHandle> mAccountHandles;
+  private List<String> mHints;
   private boolean mIsSelected;
   private boolean mIsDefaultChecked;
   private SelectPhoneAccountListener mListener;
@@ -78,7 +81,7 @@
       SelectPhoneAccountListener listener,
       @Nullable String callId) {
     return newInstance(
-        R.string.select_account_dialog_title, false, accountHandles, listener, callId);
+        R.string.select_account_dialog_title, false, 0, accountHandles, listener, callId, null);
   }
 
   /**
@@ -88,15 +91,22 @@
    * @param titleResId The resource ID for the string to use in the title of the dialog.
    * @param canSetDefault {@code true} if the dialog should include an option to set the selection
    *     as the default. False otherwise.
+   * @param setDefaultResId The resource ID for the string to use in the "set as default" checkbox
    * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
    * @param listener The listener for the results of the account selection.
+   * @param callId The callId to be passed back to the listener in {@link
+   *     SelectPhoneAccountListener#EXTRA_CALL_ID}
+   * @param hints Additional information to be shown underneath the phone account to help user
+   *     choose. Index must match {@code accountHandles}
    */
   public static SelectPhoneAccountDialogFragment newInstance(
       int titleResId,
       boolean canSetDefault,
+      int setDefaultResId,
       List<PhoneAccountHandle> accountHandles,
       SelectPhoneAccountListener listener,
-      @Nullable String callId) {
+      @Nullable String callId,
+      @Nullable List<String> hints) {
     ArrayList<PhoneAccountHandle> accountHandlesCopy = new ArrayList<>();
     if (accountHandles != null) {
       accountHandlesCopy.addAll(accountHandles);
@@ -105,9 +115,13 @@
     final Bundle args = new Bundle();
     args.putInt(ARG_TITLE_RES_ID, titleResId);
     args.putBoolean(ARG_CAN_SET_DEFAULT, canSetDefault);
+    args.putInt(ARG_SET_DEFAULT_RES_ID, setDefaultResId);
     args.putParcelableArrayList(ARG_ACCOUNT_HANDLES, accountHandlesCopy);
     args.putParcelable(ARG_LISTENER, listener);
     args.putString(ARG_CALL_ID, callId);
+    if (hints != null) {
+      args.putStringArrayList(ARG_HINTS, new ArrayList<>(hints));
+    }
     fragment.setArguments(args);
     fragment.setListener(listener);
     return fragment;
@@ -140,6 +154,7 @@
     boolean canSetDefault = getArguments().getBoolean(ARG_CAN_SET_DEFAULT);
     mAccountHandles = getArguments().getParcelableArrayList(ARG_ACCOUNT_HANDLES);
     mListener = getArguments().getParcelable(ARG_LISTENER);
+    mHints = getArguments().getStringArrayList(ARG_HINTS);
     if (savedInstanceState != null) {
       mIsDefaultChecked = savedInstanceState.getBoolean(ARG_IS_DEFAULT_CHECKED);
     }
@@ -173,7 +188,7 @@
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     ListAdapter selectAccountListAdapter =
         new SelectAccountListAdapter(
-            builder.getContext(), R.layout.select_account_list_item, mAccountHandles);
+            builder.getContext(), R.layout.select_account_list_item, mAccountHandles, mHints);
 
     AlertDialog dialog =
         builder
@@ -189,10 +204,17 @@
               LayoutInflater.from(builder.getContext())
                   .inflate(R.layout.default_account_checkbox, null);
 
-      CheckBox cb = (CheckBox) checkboxLayout.findViewById(R.id.default_account_checkbox_view);
-      cb.setOnCheckedChangeListener(checkListener);
-      cb.setChecked(mIsDefaultChecked);
+      CheckBox checkBox = checkboxLayout.findViewById(R.id.default_account_checkbox_view);
+      checkBox.setOnCheckedChangeListener(checkListener);
+      checkBox.setChecked(mIsDefaultChecked);
 
+      TextView textView = checkboxLayout.findViewById(R.id.default_account_checkbox_text);
+      int setDefaultResId =
+          getArguments().getInt(ARG_SET_DEFAULT_RES_ID, R.string.set_default_account);
+      textView.setText(setDefaultResId);
+      textView.setOnClickListener((view) -> checkBox.performClick());
+      checkboxLayout.setOnClickListener((view) -> checkBox.performClick());
+      checkboxLayout.setContentDescription(getString(setDefaultResId));
       dialog.getListView().addFooterView(checkboxLayout);
     }
 
@@ -248,10 +270,15 @@
   private static class SelectAccountListAdapter extends ArrayAdapter<PhoneAccountHandle> {
 
     private int mResId;
+    private final List<String> mHints;
 
     SelectAccountListAdapter(
-        Context context, int resource, List<PhoneAccountHandle> accountHandles) {
+        Context context,
+        int resource,
+        List<PhoneAccountHandle> accountHandles,
+        @Nullable List<String> hints) {
       super(context, resource, accountHandles);
+      mHints = hints;
       mResId = resource;
     }
 
@@ -269,6 +296,7 @@
         holder = new ViewHolder();
         holder.labelTextView = (TextView) rowView.findViewById(R.id.label);
         holder.numberTextView = (TextView) rowView.findViewById(R.id.number);
+        holder.hintTextView = rowView.findViewById(R.id.hint);
         holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
         rowView.setTag(holder);
       } else {
@@ -294,6 +322,18 @@
       }
       holder.imageView.setImageDrawable(
           PhoneAccountCompat.createIconDrawable(account, getContext()));
+      if (mHints != null && position < mHints.size()) {
+        String hint = mHints.get(position);
+        if (TextUtils.isEmpty(hint)) {
+          holder.hintTextView.setVisibility(View.GONE);
+        } else {
+          holder.hintTextView.setVisibility(View.VISIBLE);
+          holder.hintTextView.setText(hint);
+        }
+      } else {
+        holder.hintTextView.setVisibility(View.GONE);
+      }
+
       return rowView;
     }
 
@@ -301,6 +341,7 @@
 
       TextView labelTextView;
       TextView numberTextView;
+      TextView hintTextView;
       ImageView imageView;
     }
   }
diff --git a/java/com/android/dialer/precall/impl/CallingAccountSelector.java b/java/com/android/dialer/precall/impl/CallingAccountSelector.java
index d46e317..fe3a5b6 100644
--- a/java/com/android/dialer/precall/impl/CallingAccountSelector.java
+++ b/java/com/android/dialer/precall/impl/CallingAccountSelector.java
@@ -48,6 +48,7 @@
 import com.android.dialer.preferredsim.suggestion.SimSuggestionComponent;
 import com.android.dialer.preferredsim.suggestion.SuggestionProvider.Suggestion;
 import com.google.common.base.Optional;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
@@ -158,22 +159,54 @@
       PendingAction pendingAction,
       @Nullable String dataId,
       @Nullable String number,
-      @Nullable Suggestion unusedSuggestion) { // TODO(twyen): incoporate suggestion in dialog
+      @Nullable Suggestion suggestion) {
     Assert.isMainThread();
+    List<PhoneAccountHandle> phoneAccountHandles =
+        coordinator
+            .getActivity()
+            .getSystemService(TelecomManager.class)
+            .getCallCapablePhoneAccounts();
     selectPhoneAccountDialogFragment =
         SelectPhoneAccountDialogFragment.newInstance(
             R.string.pre_call_select_phone_account,
             dataId != null /* canSetDefault */,
-            coordinator
-                .getActivity()
-                .getSystemService(TelecomManager.class)
-                .getCallCapablePhoneAccounts(),
+            R.string.pre_call_select_phone_account_remember,
+            phoneAccountHandles,
             new SelectedListener(coordinator, pendingAction, dataId, number),
-            null /* call ID */);
+            null /* call ID */,
+            buildHint(coordinator.getActivity(), phoneAccountHandles, suggestion));
     selectPhoneAccountDialogFragment.show(
         coordinator.getActivity().getFragmentManager(), TAG_CALLING_ACCOUNT_SELECTOR);
   }
 
+  @Nullable
+  private static List<String> buildHint(
+      Context context,
+      List<PhoneAccountHandle> phoneAccountHandles,
+      @Nullable Suggestion suggestion) {
+    if (suggestion == null) {
+      return null;
+    }
+    List<String> hints = new ArrayList<>();
+    for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
+      if (!phoneAccountHandle.equals(suggestion.phoneAccountHandle)) {
+        hints.add(null);
+        continue;
+      }
+      switch (suggestion.reason) {
+        case INTRA_CARRIER:
+          hints.add(context.getString(R.string.pre_call_select_phone_account_hint_intra_carrier));
+          break;
+        case FREQUENT:
+          hints.add(context.getString(R.string.pre_call_select_phone_account_hint_frequent));
+          break;
+        default:
+          throw Assert.createAssertionFailException("unexpected reason " + suggestion.reason);
+      }
+    }
+    return hints;
+  }
+
   @MainThread
   @Override
   public void onDiscard() {
diff --git a/java/com/android/dialer/precall/impl/res/values/strings.xml b/java/com/android/dialer/precall/impl/res/values/strings.xml
index 8943946..0d30ac9 100644
--- a/java/com/android/dialer/precall/impl/res/values/strings.xml
+++ b/java/com/android/dialer/precall/impl/res/values/strings.xml
@@ -19,4 +19,16 @@
   multiple SIMs [CHAR LIMIT=40]-->
   <string name="pre_call_select_phone_account">Call with</string>
 
+  <!-- Checkbox label when selecting a SIM when calling a contact, to use the selected SIM for the
+   same contact and never ask again [CHAR LIMIT=40]-->
+  <string name="pre_call_select_phone_account_remember">Remember this choice</string>
+
+  <!-- Hint text under a SIM when selecting SIM to call, indicating the SIM is on the same carrier
+   as the outgoing call.[CHAR LIMIT=40]-->
+  <string name="pre_call_select_phone_account_hint_intra_carrier">Same carrier</string>
+
+  <!-- Hint text under a SIM when selecting SIM to call, indicating user often use the SIM to call
+  the contact.[CHAR LIMIT=40]-->
+  <string name="pre_call_select_phone_account_hint_frequent">You use often</string>
+
 </resources>
\ No newline at end of file
diff --git a/java/com/android/incallui/InCallActivityCommon.java b/java/com/android/incallui/InCallActivityCommon.java
index e8588a6..d2aae48 100644
--- a/java/com/android/incallui/InCallActivityCommon.java
+++ b/java/com/android/incallui/InCallActivityCommon.java
@@ -524,9 +524,11 @@
         SelectPhoneAccountDialogFragment.newInstance(
             R.string.select_phone_account_for_calls,
             true,
+            0,
             phoneAccountHandles,
             selectAccountListener,
-            waitingForAccountCall.getId());
+            waitingForAccountCall.getId(),
+            null);
     selectPhoneAccountDialogFragment.show(
         inCallActivity.getFragmentManager(), TAG_SELECT_ACCOUNT_FRAGMENT);
     return true;