Notification fix 2.

Bug: 2733143
Change-Id: I369ca66f4fdb4d29fb20b8d8c451e6f1cf1e5f22
diff --git a/src/com/android/contacts/vcard/ExportProcessor.java b/src/com/android/contacts/vcard/ExportProcessor.java
index 6a3dd86..7e7c7c8 100644
--- a/src/com/android/contacts/vcard/ExportProcessor.java
+++ b/src/com/android/contacts/vcard/ExportProcessor.java
@@ -195,6 +195,7 @@
                     return;
                 }
                 doProgressNotification(uri, total, current);
+                current++;
             }
         } finally {
             if (composer != null) {
@@ -221,27 +222,24 @@
         final String message =
                 mService.getString(R.string.exporting_contact_list_message, uri);
 
+        final RemoteViews remoteViews = new RemoteViews(mService.getPackageName(),
+                R.layout.status_bar_ongoing_event_progress_bar);
+        remoteViews.setTextViewText(R.id.description, message);
+        remoteViews.setProgressBar(R.id.progress_bar, total, current, (total == -1));
+
+        final String percentage = mService.getString(R.string.percentage,
+                String.valueOf((current * 100)/total));
+        remoteViews.setTextViewText(R.id.progress_text, percentage);
+        remoteViews.setImageViewResource(R.id.appIcon, android.R.drawable.stat_sys_upload);
+
         final Notification notification = new Notification();
         notification.icon = android.R.drawable.stat_sys_upload;
         notification.flags |= Notification.FLAG_ONGOING_EVENT;
         notification.tickerText = title;
-
-        final RemoteViews remoteView = new RemoteViews(mService.getPackageName(),
-                R.layout.status_bar_ongoing_event_progress_bar);
-        remoteView.setTextViewText(R.id.description, message);
-        remoteView.setProgressBar(R.id.progress_bar, total, current, (total == -1));
-
-        final String percentage = mService.getString(R.string.percentage,
-                String.valueOf((current * 100)/total));
-        remoteView.setTextViewText(R.id.progress_text, percentage);
-        remoteView.setImageViewResource(R.id.appIcon, android.R.drawable.stat_sys_download);
-        notification.contentView = remoteView;
-
-        notification.setLatestEventInfo(mService, title, message, null);
-        final Intent intent = new Intent(mService, ContactsListActivity.class);
+        notification.contentView = remoteViews;
         notification.contentIntent =
-                PendingIntent.getActivity(mService, 0, intent, 0);
-        PendingIntent.getActivity(mService, 0, intent, 0);
+                PendingIntent.getActivity(mService, 0,
+                        new Intent(mService, ContactsListActivity.class), 0);
         mNotificationManager.notify(VCardService.EXPORT_NOTIFICATION_ID, notification);
     }
 
diff --git a/src/com/android/contacts/vcard/ExportVCardActivity.java b/src/com/android/contacts/vcard/ExportVCardActivity.java
index 081feb1..43ae858 100644
--- a/src/com/android/contacts/vcard/ExportVCardActivity.java
+++ b/src/com/android/contacts/vcard/ExportVCardActivity.java
@@ -69,21 +69,14 @@
     private int mFileIndexMinimum;
     private int mFileIndexMaximum;
     private String mFileNameExtension;
-    private String mVCardTypeStr;
     private Set<String> mExtensionsToConsider;
 
-    private ProgressDialog mProgressDialog;
-    private String mExportingFileName;
-
-    private Handler mHandler = new Handler();
-
-    // Used temporaly when asking users to confirm the file name
+    // Used temporarily when asking users to confirm the file name
     private String mTargetFileName;
 
-    // String for storing error reason temporaly.
+    // String for storing error reason temporarily.
     private String mErrorReason;
 
-    private ActualExportThread mActualExportThread;
 
     private class CustomConnection implements ServiceConnection {
         private Messenger mMessenger;
@@ -126,7 +119,9 @@
                     }
                     sendMessage(parameter);
                 }
+
                 unbindService(this);
+                finish();
             }
         }
 
@@ -139,7 +134,6 @@
                 // the Service via NullPointerException;
                 mPendingRequests = null;
                 mMessenger = null;
-                finish();
             }
         }
     }
@@ -198,118 +192,6 @@
         }
     }
 
-    private class ActualExportThread extends Thread
-            implements DialogInterface.OnCancelListener {
-        private PowerManager.WakeLock mWakeLock;
-        private boolean mCanceled = false;
-
-        public ActualExportThread(String fileName) {
-            mExportingFileName = fileName;
-            PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
-            mWakeLock = powerManager.newWakeLock(
-                    PowerManager.SCREEN_DIM_WAKE_LOCK |
-                    PowerManager.ON_AFTER_RELEASE, LOG_TAG);
-        }
-
-        @Override
-        public void run() {
-            boolean shouldCallFinish = true;
-            mWakeLock.acquire();
-            VCardComposer composer = null;
-            try {
-                OutputStream outputStream = null;
-                try {
-                    outputStream = new FileOutputStream(mExportingFileName);
-                } catch (FileNotFoundException e) {
-                    final String errorReason =
-                        getString(R.string.fail_reason_could_not_open_file,
-                                mExportingFileName, e.getMessage());
-                    shouldCallFinish = false;
-                    mHandler.post(new ErrorReasonDisplayer(errorReason));
-                    return;
-                }
-
-                final int vcardType = VCardConfig.getVCardTypeFromString(mVCardTypeStr);
-                composer = new VCardComposer(ExportVCardActivity.this, vcardType, true);
-
-                // for testing
-                // int vcardType = (VCardConfig.VCARD_TYPE_V21_GENERIC |
-                // VCardConfig.FLAG_USE_QP_TO_PRIMARY_PROPERTIES);
-                // composer = new VCardComposer(ExportVCardActivity.this, vcardType, true);
-
-                composer.addHandler(composer.new HandlerForOutputStream(outputStream));
-
-                if (!composer.init()) {
-                    final String errorReason = composer.getErrorReason();
-                    Log.e(LOG_TAG, "initialization of vCard composer failed: " + errorReason);
-                    final String translatedErrorReason =
-                            translateComposerError(errorReason);
-                    mHandler.post(new ErrorReasonDisplayer(
-                            getString(R.string.fail_reason_could_not_initialize_exporter,
-                                    translatedErrorReason)));
-                    shouldCallFinish = false;
-                    return;
-                }
-
-                final int size = composer.getCount();
-
-                if (size == 0) {
-                    mHandler.post(new ErrorReasonDisplayer(
-                            getString(R.string.fail_reason_no_exportable_contact)));
-                    shouldCallFinish = false;
-                    return;
-                }
-
-                mProgressDialog.setProgressNumberFormat(
-                        getString(R.string.exporting_contact_list_progress));
-                mProgressDialog.setMax(size);
-                mProgressDialog.setProgress(0);
-
-                while (!composer.isAfterLast()) {
-                    if (mCanceled) {
-                        return;
-                    }
-                    if (!composer.createOneEntry()) {
-                        final String errorReason = composer.getErrorReason();
-                        Log.e(LOG_TAG, "Failed to read a contact: " + errorReason);
-                        final String translatedErrorReason =
-                            translateComposerError(errorReason);
-                        mHandler.post(new ErrorReasonDisplayer(
-                                getString(R.string.fail_reason_error_occurred_during_export,
-                                        translatedErrorReason)));
-                        shouldCallFinish = false;
-                        return;
-                    }
-                    mProgressDialog.incrementProgressBy(1);
-                }
-            } finally {
-                if (composer != null) {
-                    composer.terminate();
-                }
-                mWakeLock.release();
-                mProgressDialog.dismiss();
-                if (shouldCallFinish && !isFinishing()) {
-                    finish();
-                }
-            }
-        }
-
-        @Override
-        public void finalize() {
-            if (mWakeLock != null && mWakeLock.isHeld()) {
-                mWakeLock.release();
-            }
-        }
-
-        public void cancel() {
-            mCanceled = true;
-        }
-
-        public void onCancel(DialogInterface dialog) {
-            cancel();
-        }
-    }
-
     private String translateComposerError(String errorMessage) {
         Resources resources = getResources();
         if (VCardComposer.FAILURE_REASON_FAILED_TO_GET_DATABASE_INFO.equals(errorMessage)) {
@@ -331,7 +213,6 @@
         mFileNamePrefix = getString(R.string.config_export_file_prefix);
         mFileNameSuffix = getString(R.string.config_export_file_suffix);
         mFileNameExtension = getString(R.string.config_export_file_extension);
-        mVCardTypeStr = getString(R.string.config_export_vcard_type);
 
         mExtensionsToConsider = new HashSet<String>();
         mExtensionsToConsider.add(mFileNameExtension);
@@ -379,20 +260,6 @@
                 .setPositiveButton(android.R.string.ok, mCancelListener);
                 return builder.create();
             }
-            case R.id.dialog_exporting_vcard: {
-                if (mProgressDialog == null) {
-                    String title = getString(R.string.exporting_contact_list_title);
-                    String message = getString(R.string.exporting_contact_list_message,
-                            mExportingFileName);
-                    mProgressDialog = new ProgressDialog(ExportVCardActivity.this);
-                    mProgressDialog.setTitle(title);
-                    mProgressDialog.setMessage(message);
-                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-                    mProgressDialog.setOnCancelListener(mActualExportThread);
-                    mActualExportThread.start();
-                }
-                return mProgressDialog;
-            }
         }
         return super.onCreateDialog(id, bundle);
     }
@@ -412,11 +279,6 @@
     @Override
     protected void onStop() {
         super.onStop();
-        if (mActualExportThread != null) {
-            // The Activity is no longer visible. Stop the thread.
-            mActualExportThread.cancel();
-            mActualExportThread = null;
-        }
 
         if (!isFinishing()) {
             finish();
@@ -531,13 +393,6 @@
             .create();
     }
 
-    public void cancelExport() {
-        if (mActualExportThread != null) {
-            mActualExportThread.cancel();
-            mActualExportThread = null;
-        }
-    }
-
     public String getErrorReason() {
         return mErrorReason;
     }
diff --git a/src/com/android/contacts/vcard/ImportProcessor.java b/src/com/android/contacts/vcard/ImportProcessor.java
index 73d5dff..f4b5f5a 100644
--- a/src/com/android/contacts/vcard/ImportProcessor.java
+++ b/src/com/android/contacts/vcard/ImportProcessor.java
@@ -260,9 +260,8 @@
             intent = null;
         }
 
-        final PendingIntent pendingIntent =
-                PendingIntent.getActivity(mService, 0, intent, 0);
-        notification.setLatestEventInfo(mService, title, "", pendingIntent);
+        notification.setLatestEventInfo(mService, title, "",
+                PendingIntent.getActivity(mService, 0, intent, 0));
         mNotificationManager.notify(VCardService.IMPORT_NOTIFICATION_ID, notification);
     }
 
diff --git a/src/com/android/contacts/vcard/ImportProgressNotifier.java b/src/com/android/contacts/vcard/ImportProgressNotifier.java
index e16980f..e6f1037 100644
--- a/src/com/android/contacts/vcard/ImportProgressNotifier.java
+++ b/src/com/android/contacts/vcard/ImportProgressNotifier.java
@@ -20,7 +20,6 @@
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
-import android.util.Log;
 import android.widget.RemoteViews;
 
 import com.android.contacts.ContactsListActivity;
@@ -61,17 +60,20 @@
         // TODO: should not create this every time?
         final RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(),
                 R.layout.status_bar_ongoing_event_progress_bar);
+
         final String title = mContext.getString(R.string.reading_vcard_title);
+
         String totalCountString;
         synchronized (this) {
             totalCountString = String.valueOf(mTotalCount);
         }
-        final String text = mContext.getString(R.string.progress_notifier_message,
+        final String description = mContext.getString(R.string.progress_notifier_message,
                 String.valueOf(mCurrentCount),
                 totalCountString,
                 contactStruct.getDisplayName());
 
-        remoteViews.setTextViewText(R.id.description, text);
+        remoteViews.setTextViewText(R.id.title, title);
+        remoteViews.setTextViewText(R.id.description, description);
         remoteViews.setProgressBar(R.id.progress_bar, mTotalCount, mCurrentCount,
                 mTotalCount == -1);
         final String percentage =
@@ -83,11 +85,9 @@
 
         final Notification notification = new Notification();
         notification.icon = android.R.drawable.stat_sys_download;
-        notification.tickerText = text;
         notification.flags |= Notification.FLAG_ONGOING_EVENT;
-
+        notification.tickerText = description;
         notification.contentView = remoteViews;
-
         notification.contentIntent =
                 PendingIntent.getActivity(mContext, 0,
                         new Intent(mContext, ContactsListActivity.class), 0);
diff --git a/src/com/android/contacts/vcard/ImportVCardActivity.java b/src/com/android/contacts/vcard/ImportVCardActivity.java
index 1ae9d3f..ec63a96 100644
--- a/src/com/android/contacts/vcard/ImportVCardActivity.java
+++ b/src/com/android/contacts/vcard/ImportVCardActivity.java
@@ -254,7 +254,7 @@
      * {@link VCardService} to access all the contents in given Uris, some of
      * which may not be accessible from other components due to permission problem.
      * (Activity which gives the Uri may allow only this Activity to access that content,
-     * not the ohter components like {@link VCardService}.
+     * not the other components like {@link VCardService}.
      *
      * We also allow the Service to happen to exit during the vCard import procedure.
      */