Merge "Transfer VM fragment logic to presenter." into mnc-dev
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 56d5ad1..502b845 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -585,7 +585,7 @@
     public void onClick(View view) {
         switch (view.getId()) {
             case R.id.floating_action_button:
-                if (mListsFragment.getTabPosition() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
+                if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
                     DialerUtils.startActivityWithErrorToast(
                             this,
                             IntentUtil.getNewContactIntent(),
@@ -1162,25 +1162,25 @@
 
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
-        position = mListsFragment.getRtlPosition(position);
-        // Only scroll the button when the first tab is selected. The button should scroll from
-        // the middle to right position only on the transition from the first tab to the second
-        // tab.
-        // If the app is in RTL mode, we need to check against the second tab, rather than the
-        // first. This is because if we are scrolling between the first and second tabs, the
-        // viewpager will report that the starting tab position is 1 rather than 0, due to the
-        // reversal of the order of the tabs.
-        final boolean isLayoutRtl = DialerUtils.isRtl();
-        final boolean shouldScrollButton = position == (isLayoutRtl
-                ? ListsFragment.TAB_INDEX_RECENTS : ListsFragment.TAB_INDEX_SPEED_DIAL);
-        if (shouldScrollButton && !mIsLandscape) {
-            mFloatingActionButtonController.onPageScrolled(
-                    isLayoutRtl ? 1 - positionOffset : positionOffset);
-        } else if (position != ListsFragment.TAB_INDEX_SPEED_DIAL) {
+        int tabIndex = mListsFragment.getCurrentTabIndex();
+
+        // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
+        // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
+        // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
+        boolean isRtl = DialerUtils.isRtl();
+        if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
+            mFloatingActionButtonController.onPageScrolled(positionOffset);
+        } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
+            mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
+        } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
             mFloatingActionButtonController.onPageScrolled(1);
         }
+    }
 
-        if (position == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
+    @Override
+    public void onPageSelected(int position) {
+        int tabIndex = mListsFragment.getCurrentTabIndex();
+        if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
             mFloatingActionButtonController.changeIcon(
                     getResources().getDrawable(R.drawable.ic_person_add_24dp),
                     getResources().getString(R.string.search_shortcut_create_new_contact));
@@ -1192,10 +1192,6 @@
     }
 
     @Override
-    public void onPageSelected(int position) {
-    }
-
-    @Override
     public void onPageScrollStateChanged(int state) {
     }
 
@@ -1239,7 +1235,7 @@
      */
     private void updateFloatingActionButtonControllerAlignment(boolean animate) {
         int align = (!mIsLandscape &&
-                mListsFragment.getTabPosition() == ListsFragment.TAB_INDEX_SPEED_DIAL) ?
+                mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) ?
                 FloatingActionButtonController.ALIGN_MIDDLE :
                         FloatingActionButtonController.ALIGN_END;
         mFloatingActionButtonController.align(align, 0 /* offsetX */, 0 /* offsetY */, animate);
diff --git a/src/com/android/dialer/list/ListsFragment.java b/src/com/android/dialer/list/ListsFragment.java
index 0ac6b1a..a0e443c 100644
--- a/src/com/android/dialer/list/ListsFragment.java
+++ b/src/com/android/dialer/list/ListsFragment.java
@@ -62,9 +62,6 @@
     // Oldest recents entry to display is 2 weeks old.
     private static final long OLDEST_RECENTS_DATE = 1000L * 60 * 60 * 24 * 14;
 
-    private static final String KEY_LAST_DISMISSED_CALL_SHORTCUT_DATE =
-            "key_last_dismissed_call_shortcut_date";
-
     public interface HostInterface {
         public ActionBarController getActionBarController();
     }
@@ -92,18 +89,7 @@
     /**
      * The position of the currently selected tab.
      */
-    private int mTabPosition = TAB_INDEX_SPEED_DIAL;
-
-    /**
-     * Call shortcuts older than this date (persisted in shared preferences) will not show up in
-     * at the top of the screen
-     */
-    private long mLastCallShortcutDate = 0;
-
-    /**
-     * The date of the current call shortcut that is showing on screen.
-     */
-    private long mCurrentCallShortcutDate = 0;
+    private int mTabIndex = TAB_INDEX_SPEED_DIAL;
 
     public class ViewPagerAdapter extends FragmentPagerAdapter {
         public ViewPagerAdapter(FragmentManager fm) {
@@ -182,12 +168,9 @@
     public void onResume() {
         Trace.beginSection(TAG + " onResume");
         super.onResume();
-        final SharedPreferences prefs = getActivity().getSharedPreferences(
-                DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
-        mLastCallShortcutDate = prefs.getLong(KEY_LAST_DISMISSED_CALL_SHORTCUT_DATE, 0);
         mActionBar = getActivity().getActionBar();
         if (getUserVisibleHint()) {
-            sendScreenViewForPosition(mViewPager.getCurrentItem());
+            sendScreenViewForCurrentPosition();
         }
 
         // Fetch voicemail status to determine if we should show the voicemail tab.
@@ -208,7 +191,7 @@
         mViewPager = (ViewPager) parentView.findViewById(R.id.lists_pager);
         mViewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
         mViewPager.setAdapter(mViewPagerAdapter);
-        mViewPager.setOffscreenPageLimit(TAB_COUNT_DEFAULT - 1);
+        mViewPager.setOffscreenPageLimit(TAB_COUNT_WITH_VOICEMAIL - 1);
         mViewPager.setOnPageChangeListener(this);
         mViewPager.setCurrentItem(getRtlPosition(TAB_INDEX_SPEED_DIAL));
 
@@ -245,6 +228,8 @@
 
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+        mTabIndex = getRtlPosition(position);
+
         final int count = mOnPageChangeListeners.size();
         for (int i = 0; i < count; i++) {
             mOnPageChangeListeners.get(i).onPageScrolled(position, positionOffset,
@@ -254,13 +239,13 @@
 
     @Override
     public void onPageSelected(int position) {
-        mTabPosition = getRtlPosition(position);
+        mTabIndex = getRtlPosition(position);
 
         final int count = mOnPageChangeListeners.size();
         for (int i = 0; i < count; i++) {
             mOnPageChangeListeners.get(i).onPageSelected(position);
         }
-        sendScreenViewForPosition(position);
+        sendScreenViewForCurrentPosition();
     }
 
     @Override
@@ -293,8 +278,8 @@
         return false;
     }
 
-    public int getTabPosition() {
-        return mTabPosition;
+    public int getCurrentTabIndex() {
+        return mTabIndex;
     }
 
     public void showRemoveView(boolean show) {
@@ -316,7 +301,7 @@
         return mRemoveView;
     }
 
-    public int getRtlPosition(int position) {
+    private int getRtlPosition(int position) {
         if (DialerUtils.isRtl()) {
             return mViewPagerAdapter.getCount() - 1 - position;
         }
@@ -324,15 +309,12 @@
     }
 
     public void sendScreenViewForCurrentPosition() {
-        sendScreenViewForPosition(mViewPager.getCurrentItem());
-    }
-
-    private void sendScreenViewForPosition(int position) {
         if (!isResumed()) {
             return;
         }
+
         String fragmentName;
-        switch (getRtlPosition(position)) {
+        switch (getCurrentTabIndex()) {
             case TAB_INDEX_SPEED_DIAL:
                 fragmentName = SpeedDialFragment.class.getSimpleName();
                 break;
diff --git a/src/com/android/dialer/list/RegularSearchListAdapter.java b/src/com/android/dialer/list/RegularSearchListAdapter.java
index 6c70458..2be8a1d 100644
--- a/src/com/android/dialer/list/RegularSearchListAdapter.java
+++ b/src/com/android/dialer/list/RegularSearchListAdapter.java
@@ -22,6 +22,7 @@
 
 import com.android.contacts.common.CallUtil;
 import com.android.contacts.common.list.DirectoryPartition;
+import com.android.contacts.common.util.PhoneNumberHelper;
 import com.android.dialer.calllog.ContactInfo;
 import com.android.dialer.service.CachedNumberLookupService;
 import com.android.dialer.service.CachedNumberLookupService.CachedContactInfo;
@@ -30,6 +31,7 @@
  * List adapter to display regular search results.
  */
 public class RegularSearchListAdapter extends DialerPhoneNumberListAdapter {
+    private boolean mIsQuerySipAddress;
 
     public RegularSearchListAdapter(Context context) {
         super(context);
@@ -67,12 +69,24 @@
     }
 
     @Override
+    public String getFormattedQueryString() {
+        if (mIsQuerySipAddress) {
+            // Return unnormalized SIP address
+            return getQueryString();
+        }
+        return super.getFormattedQueryString();
+    }
+
+    @Override
     public void setQueryString(String queryString) {
         // Don't show actions if the query string contains a letter.
         final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString())
                 && hasDigitsInQueryString();
+        // Email addresses that could be SIP addresses are an exception.
+        mIsQuerySipAddress = PhoneNumberHelper.isUriNumber(queryString);
         boolean changed = false;
-        changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL, showNumberShortcuts);
+        changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL,
+                showNumberShortcuts || mIsQuerySipAddress);
         changed |= setShortcutEnabled(SHORTCUT_SEND_SMS_MESSAGE, showNumberShortcuts);
         changed |= setShortcutEnabled(SHORTCUT_MAKE_VIDEO_CALL,
                 showNumberShortcuts && CallUtil.isVideoEnabled(getContext()));