2011年5月20日金曜日

AndroidStudyMemo=Opens vcf attachments in emails and let user create a contact

received a mail with a vCard attachment but can't open it, this simple application that just opens the contacts creator with the information and let the user create a new contact or add to an existing one.

 

Card-catcher is (and should be) invisible, it just waits for opening an attachment and opens the add contact thing with the info from the vCard. But after adding the contact, the back key goes back to the form of Card-catcher.

 

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="se.rende.cardcatcher" android:versionCode="1" android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

        <activity android:name=".CardCatcher"

                  android:label="@string/app_name">

            <intent-filter>

                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.VIEW" />

                <data android:mimeType="text/x-vcard" />

            </intent-filter>

        </activity>

 

    </application>

    <uses-sdk android:minSdkVersion="7" />

 

</manifest>

 

 

 

 

package se.rende.cardcatcher;

 

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.provider.ContactsContract.RawContacts;

import android.widget.TextView;

 

/**

 * Handles vCard attachments or browser resources. Opens Contacts with option to add as new contact or to existing contact.

 *

 * @author Dag Rende

 */

public class CardCatcher extends Activity {

    private static final String[] phoneTagTable = {ContactsContract.Intents.Insert.PHONE, ContactsContract.Intents.Insert.SECONDARY_PHONE, ContactsContract.Intents.Insert.TERTIARY_PHONE};

    private static final String[] emailTagTable = {ContactsContract.Intents.Insert.EMAIL, ContactsContract.Intents.Insert.SECONDARY_EMAIL, ContactsContract.Intents.Insert.TERTIARY_EMAIL};

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        try {

                        if (getIntent().getScheme().equals("content")) {

                                InputStream is = getContentResolver().openInputStream(getIntent().getData());

                                BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                                PrintWriter pw = new PrintWriter(baos);

                                

                                Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);

                                i.setType(RawContacts.CONTENT_ITEM_TYPE);

 

                                Pattern fnPattern = Pattern.compile("FN:(.+)$");

                                int phoneIndex = 0;

                                Pattern phonePattern = Pattern.compile("TEL;CELL:(.+)$");

                                int emailIndex = 0;

                                Pattern emailPattern = Pattern.compile("EMAIL;INTERNET:(.+)$");

 

                                while (true) {

                                        String line = reader.readLine();

                                        if (line == null) {

                                                break;

                                        }

                                        pw.println(line);

                                       

                                        Matcher matcher = fnPattern.matcher(line);

                                        if (matcher.matches()) {

                                                String name = matcher.group(1);

                                                pw.println("name=" + name);

                                                i.putExtra(ContactsContract.Intents.Insert.NAME, name);                        

                                        }

                                       

                                        matcher = phonePattern.matcher(line);

                                        if (matcher.matches() && phoneIndex < phoneTagTable.length) {

                                                String match = matcher.group(1);

                                                pw.println("phone=" + match);

                                                i.putExtra(phoneTagTable[phoneIndex++], match);                        

                                        }

 

                                        matcher = emailPattern.matcher(line);

                                        if (matcher.matches() && emailIndex < emailTagTable.length) {

                                                String match = matcher.group(1);

                                                pw.println("mail=" + match);

                                                i.putExtra(emailTagTable[emailIndex++], match);                

                                        }

                                }

                                reader.close();

                                pw.close();

                               

                                TextView tv = (TextView) findViewById(R.id.tv);

                                tv.setText(baos.toString());

                                

                                startActivity(i);

                        }

                } catch (FileNotFoundException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }

    }

   

    @Override

    protected void onResume() {

        super.onResume();

        // don't show this view when back from contacts app

        finish();

    }

}

 

 

0 件のコメント:

コメントを投稿