Handle NFC vCard import errors

Show a notification and toast when errors happen during an
NFC vCard import similar to ImportVCardActivity.

Bug: 30110121
Change-Id: Idf23a2091e386959956cec8e1491fd0b4a891f01
diff --git a/src/com/android/contacts/common/vcard/NfcImportVCardActivity.java b/src/com/android/contacts/common/vcard/NfcImportVCardActivity.java
index 0634df4..6093405 100644
--- a/src/com/android/contacts/common/vcard/NfcImportVCardActivity.java
+++ b/src/com/android/contacts/common/vcard/NfcImportVCardActivity.java
@@ -17,6 +17,8 @@
 package com.android.contacts.common.vcard;
 
 import android.app.Activity;
+import android.app.Notification;
+import android.app.NotificationManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -27,9 +29,11 @@
 import android.nfc.NfcAdapter;
 import android.os.AsyncTask;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
 import android.provider.ContactsContract.RawContacts;
 import android.util.Log;
+import android.widget.Toast;
 
 import com.android.contacts.common.R;
 import com.android.contacts.common.activity.RequestPermissionsActivity;
@@ -59,6 +63,12 @@
 
     private NdefRecord mRecord;
     private AccountWithDataSet mAccount;
+    private Handler mHandler = new Handler();
+
+    /**
+     * Notification id used when error happened before sending an import request to VCardServer.
+     */
+    private static final int FAILURE_NOTIFICATION_ID = 1;
 
     /* package */ class ImportTask extends AsyncTask<VCardService, Void, ImportRequest> {
         @Override
@@ -81,6 +91,10 @@
 
         @Override
         public void onPostExecute(ImportRequest request) {
+            if (request == null) {
+                // Finish the activity in case of error so it doesn't stay in view.
+                finish();
+            }
             unbindService(NfcImportVCardActivity.this);
         }
     }
@@ -111,6 +125,8 @@
                     parser.addInterpreter(detector);
                     parser.parse(is);
                 } catch (VCardVersionException e2) {
+                    Log.e(TAG, "vCard with unsupported version.");
+                    showFailureNotification(R.string.fail_reason_not_supported);
                     return null;
                 }
             } finally {
@@ -120,14 +136,16 @@
                 }
             }
         } catch (IOException e) {
-            Log.e(TAG, "Failed reading vcard data", e);
+            Log.e(TAG, "Failed reading vCard data", e);
+            showFailureNotification(R.string.fail_reason_io_error);
             return null;
         } catch (VCardNestedException e) {
             Log.w(TAG, "Nested Exception is found (it may be false-positive).");
             // Go through without throwing the Exception, as we may be able to detect the
             // version before it
         } catch (VCardException e) {
-            Log.e(TAG, "Error parsing vcard", e);
+            Log.e(TAG, "Error parsing vCard", e);
+            showFailureNotification(R.string.fail_reason_not_supported);
             return null;
         }
 
@@ -242,7 +260,8 @@
             Log.i(TAG, "Late import failure -- ignoring");
             return;
         }
-        // TODO: report failure
+        showFailureNotification(R.string.vcard_import_request_rejected_message);
+        finish();
     }
 
     @Override
@@ -269,4 +288,22 @@
     public void onComplete() {
         // do nothing
     }
+
+    /* package */ void showFailureNotification(int reasonId) {
+        final NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+        final Notification notification =
+                NotificationImportExportListener.constructImportFailureNotification(
+                        this,
+                        getString(reasonId));
+        notificationManager.notify(NotificationImportExportListener.FAILURE_NOTIFICATION_TAG,
+                FAILURE_NOTIFICATION_ID, notification);
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                Toast.makeText(NfcImportVCardActivity.this,
+                        getString(R.string.vcard_import_failed), Toast.LENGTH_LONG).show();
+            }
+        });
+    }
 }