Merge "Fix dialer monkey NPEs" into mnc-dev
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 2370739..546a646 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -545,6 +545,14 @@
         prepareVoiceSearchButton();
         mDialerDatabaseHelper.startSmartDialUpdateThread();
         mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
+
+        if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
+            int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
+            if (index < mListsFragment.getTabCount()) {
+                mListsFragment.showTab(index);
+            }
+        }
+
         Trace.endSection();
     }
 
@@ -907,11 +915,6 @@
         mStateSaved = false;
         displayFragment(newIntent);
 
-        if (newIntent.hasExtra(EXTRA_SHOW_TAB)) {
-            mListsFragment.showTab(
-                    getIntent().getIntExtra(EXTRA_SHOW_TAB, mListsFragment.TAB_INDEX_SPEED_DIAL));
-        }
-
         invalidateOptionsMenu();
     }
 
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index 83e5594..5108472 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -361,10 +361,13 @@
 
     @Override
     public void changeCursor(Cursor cursor) {
-        // Data has changed; reset so that the first call log list item will be expanded.
-        mHasBoundFirstView = false;
-        mCurrentlyExpandedPosition = RecyclerView.NO_POSITION;
-        mCurrentlyExpandedRowId = NO_EXPANDED_LIST_ITEM;
+        // Don't auto-expand the first item for the voicemail list fragment since that will
+        // trigger an unwanted voicemail download and playback.
+        if (mVoicemailPlaybackPresenter == null) {
+            mHasBoundFirstView = false;
+            mCurrentlyExpandedPosition = RecyclerView.NO_POSITION;
+            mCurrentlyExpandedRowId = NO_EXPANDED_LIST_ITEM;
+        }
 
         super.changeCursor(cursor);
     }
@@ -578,8 +581,10 @@
             // In case ViewHolders were added/removed, update the expanded position if the rowIds
             // match so that we can restore the correct expanded state on rebind.
             mCurrentlyExpandedPosition = position;
-        } else if (!mHasBoundFirstView) {
+        } else if (!mHasBoundFirstView && mVoicemailPlaybackPresenter == null) {
             // Expand the first view when loading the call log to expose the actions.
+            // Don't auto-expand the first item for the voicemail list fragment since that will
+            // trigger an unwanted voicemail download and playback.
             mCurrentlyExpandedRowId = views.rowId;
             mCurrentlyExpandedPosition = position;
             mHasBoundFirstView = true;
diff --git a/src/com/android/dialer/list/ListsFragment.java b/src/com/android/dialer/list/ListsFragment.java
index e45da0c..33c9776 100644
--- a/src/com/android/dialer/list/ListsFragment.java
+++ b/src/com/android/dialer/list/ListsFragment.java
@@ -347,6 +347,10 @@
         return mRemoveView;
     }
 
+    public int getTabCount() {
+        return mViewPagerAdapter.getCount();
+    }
+
     private int getRtlPosition(int position) {
         if (DialerUtils.isRtl()) {
             return mViewPagerAdapter.getCount() - 1 - position;
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
index 4597d35..19f7145 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
@@ -521,7 +521,9 @@
             mIsPrepared = false;
         }
 
-        mView.onPlaybackError();
+        if (mView != null) {
+            mView.onPlaybackError();
+        }
 
         mPosition = 0;
         mIsPlaying = false;
@@ -536,7 +538,9 @@
 
         // Reset the seekbar position to the beginning.
         mPosition = 0;
-        mView.setClipPosition(0, mDuration.get());
+        if (mView != null) {
+            mView.setClipPosition(0, mDuration.get());
+        }
     }
 
     @Override
@@ -610,7 +614,9 @@
         mPosition = mMediaPlayer.getCurrentPosition();
         Log.d(TAG, "Paused playback at " + mPosition + ".");
 
-        mView.onPlaybackStopped();
+        if (mView != null) {
+            mView.onPlaybackStopped();
+        }
         mAudioManager.abandonAudioFocus(this);
 
         mActivity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);