Copy directory list into new list to prevent modifications.

Since lists are passed by reference, if the directory list passed into
RemoteContactsCursorLoader was modified before #lodInBackground was called, the
directory list would change and crash when compared to the list of cursors.

Since directory lists are small, there shouldn't be any memory issues with
copying the list.

Additionally, this CL adds some logging to the new Search Fragment.

Bug: 66902071,66902062
Test: RemoteContactsCursorLoaderTest
PiperOrigin-RevId: 171063035
Change-Id: Id2faa542805da4167fc7045e6fbe71d02c644ab6
diff --git a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
index 47a4ee6..b06f9c3 100644
--- a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
+++ b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
@@ -165,6 +165,7 @@
 
   @Override
   public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
+    LogUtil.i("NewSearchFragment.onCreateLoader", "loading cursor: " + id);
     if (id == CONTACTS_LOADER_ID) {
       return new SearchContactsCursorLoader(getContext(), query);
     } else if (id == NEARBY_PLACES_LOADER_ID) {
@@ -187,6 +188,7 @@
 
   @Override
   public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
+    LogUtil.i("NewSearchFragment.onLoadFinished", "Loader finished: " + loader);
     if (cursor != null
         && !(loader instanceof RemoteDirectoriesCursorLoader)
         && !(cursor instanceof SearchCursor)) {
@@ -218,8 +220,14 @@
 
   @Override
   public void onLoaderReset(Loader<Cursor> loader) {
-    adapter.clear();
-    recyclerView.setAdapter(null);
+    LogUtil.i("NewSearchFragment.onLoaderReset", "Loader reset: " + loader);
+    if (loader instanceof SearchContactsCursorLoader) {
+      adapter.setContactsCursor(null);
+    } else if (loader instanceof NearbyPlacesCursorLoader) {
+      adapter.setNearbyPlacesCursor(null);
+    } else if (loader instanceof RemoteContactsCursorLoader) {
+      adapter.setRemoteContactsCursor(null);
+    }
   }
 
   public void setQuery(String query, CallInitiationType.Type callInitiationType) {
diff --git a/java/com/android/dialer/searchfragment/remote/RemoteContactsCursor.java b/java/com/android/dialer/searchfragment/remote/RemoteContactsCursor.java
index 5d80a45..e9e83c1 100644
--- a/java/com/android/dialer/searchfragment/remote/RemoteContactsCursor.java
+++ b/java/com/android/dialer/searchfragment/remote/RemoteContactsCursor.java
@@ -60,7 +60,10 @@
   public static RemoteContactsCursor newInstance(
       Context context, Cursor[] cursors, List<Directory> directories) {
     Assert.checkArgument(
-        cursors.length == directories.size(), "Directories and cursors must be the same size.");
+        cursors.length == directories.size(),
+        "Directories (%d) and cursors (%d) must be the same size.",
+        directories.size(),
+        cursors.length);
     Cursor[] cursorsWithHeaders = insertHeaders(context, cursors, directories);
     if (cursorsWithHeaders.length > 0) {
       return new RemoteContactsCursor(cursorsWithHeaders);
diff --git a/java/com/android/dialer/searchfragment/remote/RemoteContactsCursorLoader.java b/java/com/android/dialer/searchfragment/remote/RemoteContactsCursorLoader.java
index 771b7f1..37695be 100644
--- a/java/com/android/dialer/searchfragment/remote/RemoteContactsCursorLoader.java
+++ b/java/com/android/dialer/searchfragment/remote/RemoteContactsCursorLoader.java
@@ -28,6 +28,7 @@
 import android.text.TextUtils;
 import com.android.dialer.searchfragment.common.Projections;
 import com.android.dialer.searchfragment.remote.RemoteDirectoriesCursorLoader.Directory;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -58,7 +59,7 @@
         null,
         Phone.SORT_KEY_PRIMARY);
     this.query = query;
-    this.directories = directories;
+    this.directories = new ArrayList<>(directories);
     cursors = new Cursor[directories.size()];
   }