Merge "Fixing filtering contacts by account"
diff --git a/src/com/android/contacts/vcard/CancelImportActivity.java b/src/com/android/contacts/vcard/CancelImportActivity.java
index 22de0da..50e6e99 100644
--- a/src/com/android/contacts/vcard/CancelImportActivity.java
+++ b/src/com/android/contacts/vcard/CancelImportActivity.java
@@ -45,10 +45,6 @@
 
     private class CustomConnection implements ServiceConnection {
         private Messenger mMessenger;
-        public void doBindService() {
-            bindService(new Intent(CancelImportActivity.this,
-                    VCardService.class), this, Context.BIND_AUTO_CREATE);
-        }
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
             mMessenger = new Messenger(service);
@@ -61,6 +57,8 @@
             } catch (RemoteException e) {
                 Log.e(LOG_TAG, "RemoteException is thrown when trying to send request");
                 CancelImportActivity.this.showDialog(R.string.fail_reason_unknown);
+            } finally {
+                CancelImportActivity.this.unbindService(this);
             }
         }
         @Override
@@ -72,7 +70,8 @@
     private class RequestCancelImportListener implements DialogInterface.OnClickListener {
         @Override
         public void onClick(DialogInterface dialog, int which) {
-            mConnection.doBindService();
+            bindService(new Intent(CancelImportActivity.this,
+                    VCardService.class), mConnection, Context.BIND_AUTO_CREATE);
         }
     }
 
diff --git a/src/com/android/contacts/vcard/ImportProcessor.java b/src/com/android/contacts/vcard/ImportProcessor.java
index 6a7dbe6..89b6be6 100644
--- a/src/com/android/contacts/vcard/ImportProcessor.java
+++ b/src/com/android/contacts/vcard/ImportProcessor.java
@@ -68,7 +68,7 @@
 
         public synchronized void tryBind() {
             if (!mContext.bindService(new Intent(mContext, VCardService.class),
-                    mConnection, Context.BIND_AUTO_CREATE)) {
+                    this, Context.BIND_AUTO_CREATE)) {
                 throw new RuntimeException("Failed to bind to VCardService.");
             }
             mBound = true;
@@ -76,7 +76,7 @@
 
         public synchronized void tryUnbind() {
             if (mBound) {
-                mContext.unbindService(mConnection);
+                mContext.unbindService(this);
             } else {
                 // TODO: Not graceful.
                 Log.w(LOG_TAG, "unbind() is tried while ServiceConnection is not bound yet");
@@ -105,9 +105,6 @@
     }
 
     private final Context mContext;
-
-    private final ImportProcessorConnection mConnection = new ImportProcessorConnection();
-
     private ContentResolver mResolver;
     private NotificationManager mNotificationManager;
 
@@ -157,8 +154,6 @@
 
     public ImportProcessor(final Context context) {
         mContext = context;
-
-        mConnection.tryBind();
     }
 
     /**
@@ -210,12 +205,13 @@
      */
     private void process() {
         if (!mReadyForRequest) {
-            mConnection.tryUnbind();
             throw new RuntimeException(
                     "process() is called before request being pushed "
                     + "or after this object's finishing its processing.");
         }
 
+        final ImportProcessorConnection connection = new ImportProcessorConnection();
+        connection.tryBind();
         try {
             while (!mCanceled) {
                 final ImportRequest parameter;
@@ -233,12 +229,12 @@
             // Currenty we don't have an appropriate way to let users see all URIs imported.
             // Instead, we show one only when there's just one created uri.
             doFinishNotification(mCreatedUris.size() > 0 ? mCreatedUris.get(0) : null);
-            mConnection.sendFinishNotification();
+            connection.sendFinishNotification();
         } finally {
             // TODO: verify this works fine.
             mReadyForRequest = false;  // Just in case.
             mNotifier.resetTotalCount();
-            mConnection.tryUnbind();
+            connection.tryUnbind();
         }
     }
 
diff --git a/src/com/android/contacts/vcard/ImportVCardActivity.java b/src/com/android/contacts/vcard/ImportVCardActivity.java
index 01ff01a..d66a09a 100644
--- a/src/com/android/contacts/vcard/ImportVCardActivity.java
+++ b/src/com/android/contacts/vcard/ImportVCardActivity.java
@@ -133,11 +133,6 @@
         private boolean mNeedToCallFinish = false;
         private boolean mDisconnectAndFinishDone = false;
 
-        public void doBindService() {
-            bindService(new Intent(ImportVCardActivity.this,
-                    VCardService.class), this, Context.BIND_AUTO_CREATE);
-        }
-
         /**
          * Tries to unbind this connection and call {@link ImportVCardActivity#finish()}.
          * When timing is not appropriate, this object remembers this call and
@@ -227,8 +222,6 @@
         }
     }
 
-    private final CustomConnection mConnection = new CustomConnection();
-
     private static class VCardFile {
         private final String mName;
         private final String mCanonicalPath;
@@ -322,9 +315,11 @@
             final ContentResolver resolver = context.getContentResolver();
             String errorMessage = null;
             mWakeLock.acquire();
+            final CustomConnection connection = new CustomConnection();
+            bindService(new Intent(ImportVCardActivity.this,
+                    VCardService.class), connection, Context.BIND_AUTO_CREATE);
             try {
                 clearOldCache();
-                mConnection.doBindService();
 
                 final int length = mSourceUris.length;
                 // Uris given from caller applications may not be opened twice: consider when
@@ -352,7 +347,7 @@
                     if (mCanceled) {
                         return;
                     }
-                    mConnection.requestSend(parameter);
+                    connection.requestSend(parameter);
                 }
             } catch (OutOfMemoryError e) {
                 Log.e(LOG_TAG, "OutOfMemoryError");
@@ -368,7 +363,7 @@
             } finally {
                 mWakeLock.release();
                 mProgressDialogForCacheVCard.dismiss();
-                mConnection.tryDisconnectAndFinish();
+                connection.tryDisconnectAndFinish();
             }
         }
 
@@ -1017,12 +1012,6 @@
         finish();
     }
 
-    @Override
-    protected void onDestroy() {
-        super.onDestroy();
-        mConnection.tryDisconnectAndFinish();
-    }
-
     /**
      * Scans vCard in external storage (typically SDCard) and tries to import it.
      * - When there's no SDCard available, an error dialog is shown.