Don't copy contact list when sort

Sorting contacts creates a copy of contact list to solve a crash when
TelecomActivity being created twice that two tasks sort the same list at
the same time. Reverting the change since the issue of TelecomActivity
being created twice has been resolved. Also changes to use class static
Executor to prevent such crashes in the future.

Bug: 170610268
Test: manually, test with huge contact list and switching tabs
Change-Id: I7937928d18dcaacc500f0da08358ea1bde6f2f65
diff --git a/src/com/android/car/dialer/ui/contact/ContactListViewModel.java b/src/com/android/car/dialer/ui/contact/ContactListViewModel.java
index fcdbc1b..f09472b 100644
--- a/src/com/android/car/dialer/ui/contact/ContactListViewModel.java
+++ b/src/com/android/car/dialer/ui/contact/ContactListViewModel.java
@@ -34,7 +34,6 @@
 import com.android.car.telephony.common.Contact;
 import com.android.car.telephony.common.InMemoryPhoneBook;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
@@ -74,12 +73,13 @@
 
     private static class SortedContactListLiveData
             extends MediatorLiveData<Pair<Integer, List<Contact>>> {
+        // Class static to make sure only one task is sorting contacts at one time.
+        private static ExecutorService sExecutorService = Executors.newSingleThreadExecutor();
 
         private final LiveData<List<Contact>> mContactListLiveData;
         private final SharedPreferencesLiveData mPreferencesLiveData;
         private final Context mContext;
 
-        private final ExecutorService mExecutorService;
         private Future<?> mRunnableFuture;
 
         private SortedContactListLiveData(Context context,
@@ -88,7 +88,6 @@
             mContext = context;
             mContactListLiveData = contactListLiveData;
             mPreferencesLiveData = sharedPreferencesLiveData;
-            mExecutorService = Executors.newSingleThreadExecutor();
 
             addSource(mPreferencesLiveData, trigger -> onSortOrderChanged());
             addSource(mContactListLiveData, this::sortContacts);
@@ -120,13 +119,10 @@
             Integer sortMethod = contactSortingInfo.second;
 
             Runnable runnable = () -> {
-                // Make a copy of the contact list to avoid the same list being sorted at the same
-                // time since the ViewModel is not single instance but the contact LiveData is.
-                List<Contact> contactListCopy = new ArrayList<>(contactList);
-                Collections.sort(contactListCopy, comparator);
-                postValue(new Pair<>(sortMethod, contactListCopy));
+                Collections.sort(contactList, comparator);
+                postValue(new Pair<>(sortMethod, contactList));
             };
-            mRunnableFuture = mExecutorService.submit(runnable);
+            mRunnableFuture = sExecutorService.submit(runnable);
         }
 
         @Override