If the Speed Dial contact only has one channel, place the call immediately.

If the disambig dialog would only show one option, just skip the dialog and
place the call directly. Don't save the number as a default entry though
because the contact could potentially change later (become duo reachable, get
another number, ect.)

Bug: 36841782
Test: SpeedDialIntegrationTest
PiperOrigin-RevId: 193555958
Change-Id: Icbb7e876b7a7c5aaa979980249bf074ec1d7a395
diff --git a/java/com/android/dialer/databasepopulator/ContactsPopulator.java b/java/com/android/dialer/databasepopulator/ContactsPopulator.java
index f22552d..f21e325 100644
--- a/java/com/android/dialer/databasepopulator/ContactsPopulator.java
+++ b/java/com/android/dialer/databasepopulator/ContactsPopulator.java
@@ -152,6 +152,7 @@
     Assert.isWorkerThread();
     ArrayList<ContentProviderOperation> operations = new ArrayList<>();
     addContact(SIMPLE_CONTACTS[0], operations);
+    addContact(SIMPLE_CONTACTS[3], operations);
     addContact(SIMPLE_CONTACTS[5], operations);
     try {
       context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
diff --git a/java/com/android/dialer/speeddial/SpeedDialFragment.java b/java/com/android/dialer/speeddial/SpeedDialFragment.java
index c45ec58..793169a 100644
--- a/java/com/android/dialer/speeddial/SpeedDialFragment.java
+++ b/java/com/android/dialer/speeddial/SpeedDialFragment.java
@@ -21,6 +21,8 @@
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.helper.ItemTouchHelper;
@@ -62,16 +64,12 @@
 public class SpeedDialFragment extends Fragment {
 
   private final SpeedDialHeaderListener headerListener = new SpeedDialFragmentHeaderListener();
-  private final FavoriteContactsListener favoritesListener = new SpeedDialFavoritesListener();
   private final SuggestedContactsListener suggestedListener = new SpeedDialSuggestedListener();
 
-  private View rootLayout;
   private ContextMenu contextMenu;
   private FrameLayout contextMenuBackground;
-  private ContextMenuItemListener contextMenuItemListener;
 
   private SpeedDialAdapter adapter;
-  private SpeedDialLayoutManager layoutManager;
   private SupportUiListener<ImmutableList<SpeedDialUiItem>> speedDialLoaderListener;
 
   /**
@@ -80,6 +78,8 @@
    */
   private boolean updateSpeedDialItemsOnResume = true;
 
+  private FavoriteContactsListener favoritesListener;
+
   public static SpeedDialFragment newInstance() {
     return new SpeedDialFragment();
   }
@@ -89,22 +89,7 @@
   public View onCreateView(
       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     LogUtil.enterBlock("SpeedDialFragment.onCreateView");
-    rootLayout = inflater.inflate(R.layout.fragment_speed_dial, container, false);
-
-    // Setup our RecyclerView
-    RecyclerView recyclerView = rootLayout.findViewById(R.id.speed_dial_recycler_view);
-    adapter =
-        new SpeedDialAdapter(getContext(), favoritesListener, suggestedListener, headerListener);
-    layoutManager = new SpeedDialLayoutManager(getContext(), 3 /* spanCount */);
-    layoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
-    recyclerView.setLayoutManager(layoutManager);
-    recyclerView.setAdapter(adapter);
-
-    // Setup drag and drop touch helper
-    ItemTouchHelper.Callback callback = new SpeedDialItemTouchHelperCallback(adapter);
-    ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
-    touchHelper.attachToRecyclerView(recyclerView);
-    adapter.setItemTouchHelper(touchHelper);
+    View rootLayout = inflater.inflate(R.layout.fragment_speed_dial, container, false);
 
     // Setup favorite contact context menu
     contextMenu = rootLayout.findViewById(R.id.favorite_contact_context_menu);
@@ -114,7 +99,31 @@
           contextMenu.hideMenu();
           contextMenuBackground.setVisibility(View.GONE);
         });
-    contextMenuItemListener = new SpeedDialContextMenuItemListener();
+
+    // Setup our RecyclerView
+    SpeedDialLayoutManager layoutManager =
+        new SpeedDialLayoutManager(getContext(), 3 /* spanCount */);
+    favoritesListener =
+        new SpeedDialFavoritesListener(
+            getActivity(),
+            getChildFragmentManager(),
+            rootLayout,
+            contextMenu,
+            contextMenuBackground,
+            new SpeedDialContextMenuItemListener(),
+            layoutManager);
+    adapter =
+        new SpeedDialAdapter(getContext(), favoritesListener, suggestedListener, headerListener);
+    layoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
+    RecyclerView recyclerView = rootLayout.findViewById(R.id.speed_dial_recycler_view);
+    recyclerView.setLayoutManager(layoutManager);
+    recyclerView.setAdapter(adapter);
+
+    // Setup drag and drop touch helper
+    ItemTouchHelper.Callback callback = new SpeedDialItemTouchHelperCallback(adapter);
+    ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
+    touchHelper.attachToRecyclerView(recyclerView);
+    adapter.setItemTouchHelper(touchHelper);
 
     speedDialLoaderListener =
         DialerExecutorComponent.get(getContext())
@@ -181,26 +190,56 @@
     }
   }
 
-  private final class SpeedDialFavoritesListener implements FavoriteContactsListener {
+  private static final class SpeedDialFavoritesListener implements FavoriteContactsListener {
+
+    private final FragmentActivity activity;
+    private final FragmentManager childFragmentManager;
+    private final View rootLayout;
+    private final ContextMenu contextMenu;
+    private final View contextMenuBackground;
+    private final ContextMenuItemListener contextMenuListener;
+    private final SpeedDialLayoutManager layoutManager;
+
+    SpeedDialFavoritesListener(
+        FragmentActivity activity,
+        FragmentManager childFragmentManager,
+        View rootLayout,
+        ContextMenu contextMenu,
+        View contextMenuBackground,
+        ContextMenuItemListener contextMenuListener,
+        SpeedDialLayoutManager layoutManager) {
+      this.activity = activity;
+      this.childFragmentManager = childFragmentManager;
+      this.rootLayout = rootLayout;
+      this.contextMenu = contextMenu;
+      this.contextMenuBackground = contextMenuBackground;
+      this.contextMenuListener = contextMenuListener;
+      this.layoutManager = layoutManager;
+    }
 
     @Override
     public void onAmbiguousContactClicked(SpeedDialUiItem speedDialUiItem) {
-      DisambigDialog.show(speedDialUiItem, getChildFragmentManager());
+      // If there is only one channel, skip the menu and place a call directly
+      if (speedDialUiItem.channels().size() == 1) {
+        onClick(speedDialUiItem.channels().get(0));
+        return;
+      }
+
+      DisambigDialog.show(speedDialUiItem, childFragmentManager);
     }
 
     @Override
     public void onClick(Channel channel) {
       if (channel.technology() == Channel.DUO) {
-        Logger.get(getContext())
+        Logger.get(activity)
             .logImpression(DialerImpression.Type.LIGHTBRINGER_VIDEO_REQUESTED_FOR_FAVORITE_CONTACT);
-        Intent intent =
-            DuoComponent.get(getContext()).getDuo().getIntent(getContext(), channel.number());
-        getActivity().startActivityForResult(intent, ActivityRequestCodes.DIALTACTS_DUO);
+        Intent intent = DuoComponent.get(activity).getDuo().getIntent(activity, channel.number());
+        activity.startActivityForResult(intent, ActivityRequestCodes.DIALTACTS_DUO);
         return;
       }
 
       PreCall.start(
-          getContext(),
+          activity,
           new CallIntentBuilder(channel.number(), CallInitiationType.Type.SPEED_DIAL)
               .setIsVideoCall(channel.isVideoTechnology()));
     }
@@ -208,7 +247,7 @@
     @Override
     public void showContextMenu(View view, SpeedDialUiItem speedDialUiItem) {
       layoutManager.setScrollEnabled(false);
-      contextMenu.showMenu(rootLayout, view, speedDialUiItem, contextMenuItemListener);
+      contextMenu.showMenu(rootLayout, view, speedDialUiItem, contextMenuListener);
     }
 
     @Override