Showing toast on the UI thread.

Apparently, it's not a good idea to kick off a toast from
a background thread.

Bug: 3308302
Change-Id: Ifbd3268ec06c9ebbfec14ff5cbea09ffda9e6bac
diff --git a/src/com/android/contacts/ContactSaveService.java b/src/com/android/contacts/ContactSaveService.java
index a926c78..c07dd68 100644
--- a/src/com/android/contacts/ContactSaveService.java
+++ b/src/com/android/contacts/ContactSaveService.java
@@ -33,6 +33,8 @@
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
 import android.os.RemoteException;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.AggregationExceptions;
@@ -505,14 +507,14 @@
         // Apply all aggregation exceptions as one batch
         try {
             resolver.applyBatch(ContactsContract.AUTHORITY, operations);
-            Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
+            showToast(R.string.contactsJoinedMessage);
             success = true;
         } catch (RemoteException e) {
             Log.e(TAG, "Failed to apply aggregation exception batch", e);
-            Toast.makeText(this, R.string.contactSavedErrorToast, Toast.LENGTH_LONG).show();
+            showToast(R.string.contactSavedErrorToast);
         } catch (OperationApplicationException e) {
             Log.e(TAG, "Failed to apply aggregation exception batch", e);
-            Toast.makeText(this, R.string.contactSavedErrorToast, Toast.LENGTH_LONG).show();
+            showToast(R.string.contactSavedErrorToast);
         }
 
         Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);
@@ -536,4 +538,17 @@
         builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
         operations.add(builder.build());
     }
+
+    /**
+     * Shows a toast on the UI thread.
+     */
+    private void showToast(final int message) {
+        new Handler(Looper.getMainLooper()).post(new Runnable() {
+
+            @Override
+            public void run() {
+                Toast.makeText(ContactSaveService.this, message, Toast.LENGTH_LONG).show();
+            }
+        });
+    }
 }