Support vcard share: receiving a vcf file in contacts activity

Automatically accept and import a vcard into contacts, after receiving ACTION_VIEW intent
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b54d417..cb6c077 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -427,6 +427,15 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".VCardActivity"
+            android:theme="@android:style/Theme.NoTitleBar" >
+             <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <data android:mimeType="text/x-vcard" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".ImportVCardActivity"
             android:theme="@style/BackgroundOnly" />
 
diff --git a/res/layout/vcardshare_main.xml b/res/layout/vcardshare_main.xml
new file mode 100644
index 0000000..4361cfe
--- /dev/null
+++ b/res/layout/vcardshare_main.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    >
+</LinearLayout>
diff --git a/src/com/android/contacts/ImportVCardActivity.java b/src/com/android/contacts/ImportVCardActivity.java
index b306528..d785736 100644
--- a/src/com/android/contacts/ImportVCardActivity.java
+++ b/src/com/android/contacts/ImportVCardActivity.java
@@ -48,6 +48,7 @@
 import android.text.TextUtils;
 import android.text.style.RelativeSizeSpan;
 import android.util.Log;
+import android.net.Uri;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -112,6 +113,8 @@
 
     private String mErrorMessage;
 
+    private boolean mNeedReview = false;
+
     private class DialogDisplayer implements Runnable {
         private final int mResId;
         public DialogDisplayer(int resId) {
@@ -271,6 +274,19 @@
                 if (shouldCallFinish && !isFinishing()) {
                     if (mErrorFileNameList == null || mErrorFileNameList.isEmpty()) {
                         finish();
+                        // TODO: Send out ACTION_EDIT intent here to review the
+                        // incoming contact
+                        if (mNeedReview) {
+                            mNeedReview = false;
+                            Log.v("importVCardActivity", "Prepare to review the imported contact");
+                            Uri uri = null; // TODO: need get the uri of the
+                                            // incoming contact
+                            Intent editIntent = new Intent(Intent.ACTION_EDIT, uri);
+                            if (editIntent != null) {
+                                //startActivity(editIntent);
+                            }
+
+                        }
                     } else {
                         StringBuilder builder = new StringBuilder();
                         boolean first = true;
@@ -697,9 +713,20 @@
             Log.e(LOG_TAG, "intent does not exist");
         }
 
-        startImportVCardFromSdCard();
+        final String action = intent.getAction();
+        final Uri path = intent.getData();
+        Log.v(LOG_TAG, "action = " + action + " ; path = " + path);
+        if (Intent.ACTION_VIEW.equals(action)) {
+            mNeedReview = true;
+            // Import the file directly and then go to EDIT screen
+            if (path.toString().toLowerCase().endsWith(".vcf")) {
+                String cannonicalPath = path.getPath();
+                importOneVCardFromSDCard(cannonicalPath);
+            }
+        } else {
+            startImportVCardFromSdCard();
+        }
     }
-
     @Override
     protected Dialog onCreateDialog(int resId) {
         switch (resId) {
diff --git a/src/com/android/contacts/VCardActivity.java b/src/com/android/contacts/VCardActivity.java
new file mode 100644
index 0000000..ef04be6
--- /dev/null
+++ b/src/com/android/contacts/VCardActivity.java
@@ -0,0 +1,62 @@
+//package com.motorola.vcardshare;
+
+package com.android.contacts;
+
+import java.util.List;
+import com.android.contacts.model.Sources;
+import com.android.contacts.util.AccountSelectionUtil;
+import android.accounts.Account;
+import android.app.Activity;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.util.Log;
+
+public class VCardActivity extends Activity {
+    private static final String TAG = "VCardActivity";
+    public static final int IMPORT_TYPE_GOOGLE = 0;
+    public static final int IMPORT_TYPE_EXCHANGE = 1;
+
+    /** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.vcardshare_main);
+
+        Intent intent = getIntent();
+        String action = intent.getAction();
+        Log.v(TAG, "action is " + action);
+        Uri path = intent.getData();
+        Log.v(TAG, "path is " + path);
+        String mimeType = intent.getType();
+        Log.v(TAG, "mimeType is " + mimeType);
+
+        if (action.equals(Intent.ACTION_VIEW)) {
+
+            AccountSelectionUtil.mVCardShare = true;
+            AccountSelectionUtil.mPath = path;
+            handleImportRequest(R.string.import_from_sdcard);
+        } else {
+            Log.w(TAG, "VcardActivity not handle such action: " + action);
+        }
+        finish();
+    }
+    
+    private void handleImportRequest(int resId) {
+        // There's three possibilities:
+        // - more than one accounts -> ask the user
+        // - just one account -> use the account without asking the user
+        // - no account -> use phone-local storage without asking the user
+        final Sources sources = Sources.getInstance(this);
+        final List<Account> accountList = sources.getAccounts(true);
+        final int size = accountList.size();
+        Log.v(TAG, "account num =  " + size);
+
+        if (size > 1) {
+            // showDialog(resId);
+            AccountSelectionUtil.getSelectAccountDialog(this, resId);
+        }
+
+        AccountSelectionUtil.doImport(this, resId, (size == 1 ? accountList.get(0) : null));
+    }
+}
diff --git a/src/com/android/contacts/util/AccountSelectionUtil.java b/src/com/android/contacts/util/AccountSelectionUtil.java
index cf83581..e32ef69 100644
--- a/src/com/android/contacts/util/AccountSelectionUtil.java
+++ b/src/com/android/contacts/util/AccountSelectionUtil.java
@@ -35,6 +35,7 @@
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.TextView;
+import android.net.Uri;
 
 import java.util.List;
 
@@ -45,6 +46,10 @@
     // TODO: maybe useful for EditContactActivity.java...
     private static final String LOG_TAG = "AccountSelectionUtil";
 
+    public static boolean mVCardShare = false;
+
+    public static Uri mPath;
+
     private static class AccountSelectedListener
             implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
 
@@ -165,6 +170,13 @@
             importIntent.putExtra("account_name", account.name);
             importIntent.putExtra("account_type", account.type);
         }
+
+        if (mVCardShare) {
+            importIntent.setAction(Intent.ACTION_VIEW);
+            importIntent.setData(mPath);
+        }
+        mVCardShare = false;
+        mPath = null;
         context.startActivity(importIntent);
     }
 }