Fixed several screen rotation related bugs.

Change-Id: I12a34ec81267ab68f3d86c64cfaa30118caebbba
diff --git a/src/com/android/contacts/list/ContactBrowseListFragment.java b/src/com/android/contacts/list/ContactBrowseListFragment.java
index de72b69..c09dc47 100644
--- a/src/com/android/contacts/list/ContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/ContactBrowseListFragment.java
@@ -82,6 +82,7 @@
         }
 
         mSelectedContactUri = savedState.getParcelable(KEY_SELECTED_URI);
+        parseSelectedContactUri();
     }
 
     @Override
@@ -135,24 +136,7 @@
                 || (mSelectedContactUri != null && !mSelectedContactUri.equals(uri))) {
             mSelectedContactUri = uri;
 
-            if (mSelectedContactUri != null) {
-                if (!mSelectedContactUri.toString()
-                        .startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) {
-                    throw new IllegalStateException(
-                            "Contact list contains a non-lookup URI: " + mSelectedContactUri);
-                }
-
-                String directoryParam =
-                    mSelectedContactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
-                mSelectedContactDirectoryId = TextUtils.isEmpty(directoryParam)
-                        ? Directory.DEFAULT
-                        : Long.parseLong(directoryParam);
-                mSelectedContactLookupKey =
-                        Uri.encode(mSelectedContactUri.getPathSegments().get(2));
-            } else {
-                mSelectedContactDirectoryId = Directory.DEFAULT;
-                mSelectedContactLookupKey = null;
-            }
+            parseSelectedContactUri();
 
             // Configure the adapter to show the selection based on the lookup key extracted
             // from the URI
@@ -163,6 +147,27 @@
         }
     }
 
+    private void parseSelectedContactUri() {
+        if (mSelectedContactUri != null) {
+            if (!mSelectedContactUri.toString()
+                    .startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) {
+                throw new IllegalStateException(
+                        "Contact list contains a non-lookup URI: " + mSelectedContactUri);
+            }
+
+            String directoryParam =
+                mSelectedContactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
+            mSelectedContactDirectoryId = TextUtils.isEmpty(directoryParam)
+                    ? Directory.DEFAULT
+                    : Long.parseLong(directoryParam);
+            mSelectedContactLookupKey =
+                    Uri.encode(mSelectedContactUri.getPathSegments().get(2));
+        } else {
+            mSelectedContactDirectoryId = Directory.DEFAULT;
+            mSelectedContactLookupKey = null;
+        }
+    }
+
     @Override
     protected void configureAdapter() {
         super.configureAdapter();
diff --git a/src/com/android/contacts/list/ContactEntryListAdapter.java b/src/com/android/contacts/list/ContactEntryListAdapter.java
index aaf80f8..ef0807a 100644
--- a/src/com/android/contacts/list/ContactEntryListAdapter.java
+++ b/src/com/android/contacts/list/ContactEntryListAdapter.java
@@ -222,20 +222,17 @@
 
         // TODO preserve the order of partition to match those of the cursor
         // Phase I: add new directories
-        try {
-            while (cursor.moveToNext()) {
-                long id = cursor.getLong(idColumnIndex);
-                directoryIds.add(id);
-                if (getPartitionByDirectoryId(id) == -1) {
-                    DirectoryPartition partition = new DirectoryPartition(false, true);
-                    partition.setDirectoryId(id);
-                    partition.setDirectoryType(cursor.getString(directoryTypeColumnIndex));
-                    partition.setDisplayName(cursor.getString(displayNameColumnIndex));
-                    addPartition(partition);
-                }
+        cursor.moveToPosition(-1);
+        while (cursor.moveToNext()) {
+            long id = cursor.getLong(idColumnIndex);
+            directoryIds.add(id);
+            if (getPartitionByDirectoryId(id) == -1) {
+                DirectoryPartition partition = new DirectoryPartition(false, true);
+                partition.setDirectoryId(id);
+                partition.setDirectoryType(cursor.getString(directoryTypeColumnIndex));
+                partition.setDisplayName(cursor.getString(displayNameColumnIndex));
+                addPartition(partition);
             }
-        } finally {
-            cursor.close();
         }
 
         // Phase II: remove deleted directories
@@ -257,10 +254,7 @@
     @Override
     public void changeCursor(int partitionIndex, Cursor cursor) {
         if (partitionIndex >= getPartitionCount()) {
-            // There is no partition for this data - just drop it on the ground
-            if (cursor != null) {
-                cursor.close();
-            }
+            // There is no partition for this data
             return;
         }
 
diff --git a/src/com/android/contacts/widget/CompositeCursorAdapter.java b/src/com/android/contacts/widget/CompositeCursorAdapter.java
index 6465a21..c6aa775 100644
--- a/src/com/android/contacts/widget/CompositeCursorAdapter.java
+++ b/src/com/android/contacts/widget/CompositeCursorAdapter.java
@@ -108,14 +108,10 @@
     }
 
     /**
-     * Removes cursors for all partitions, closing them as necessary.
+     * Removes cursors for all partitions.
      */
     public void clearPartitions() {
         for (int i = 0; i < mSize; i++) {
-            Cursor cursor = mPartitions[i].cursor;
-            if (cursor != null && !cursor.isClosed()) {
-                cursor.close();
-            }
             mPartitions[i].cursor = null;
         }
         invalidate();