Hi Dmitri,

Thanks for you info regarding the new API - it was r4eally helpful.
I'm trying to use it to create new contact but I constantly have
problems.
Do you know if this is the final version of the API ? Or maybe it's
still not finish thus that's why we don't get any real documentation
or code examples for it ?


On Nov 3, 2:24 am, Dmitri Plotnikov <dplotni...@google.com> wrote:
> You can always delegate contact creation to the Contacts app using the
> ContactsContract.Intents.UI.Insert intent with extras. This will show the
> edit UI.
>
> If you want to explicitly create the contact by yourself, that's now a bit
> tricky because Android 2.0 support multiple accounts.
>
> First of all, you will need to figure out which account you want to create
> the contact in. Get a list of all available accounts from AccountManager:
>
> AccountManager am = AccountManager.get(getContext());
> Account[] accounts = am.getAccounts();
>
> Also, get a list of all sync adapters and find the ones that support
> contacts:
>
> SyncAdapterType[] syncs
> = ContentResolver.getContentService().getSyncAdapterTypes();
>
> for (SyncAdapterType sync : syncs) {
>      if (ContactsContract.AUTHORITY.equals(sync.authority) &&
> sync.supportsUploading()) {
>           contactAccountTypes.add(sync.accountType);
>      }
>
> }
>
> Now you have a list of all accounts and a list of account types that support
> contacts.  So here's your account list:
>
> for (Account acct: accounts) {
>    if (contactAccountTypes.contains(acct.type)) {
>       contactAccounts.add(account);
>    }
>
> }
>
> If the contactAccounts list contains nothing - use accountType = null and
> accountName = null
> If it contains exactly one account, use it.
> If it contains multiple accounts, build a dialog and ask the user which
> account to use.
>
> From here on it gets easier.
>
> Let's start with a more traditional method.  Insert a raw contact first:
>
> ContentValues values = new ContentValues();
> values.put(RawContacts.ACCOUNT_TYPE, accountType);
> values.put(RawContacts.ACCOUNT_NAME, accountName);
> Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI,
> values);
> long rawContactId = ContentUris.parseId(rawContactUri);
>
> Then insert the name:
>
> values.clear();
> values.put(Data.RAW_CONTACT_ID, rawContactId);
> values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
> values.put(StructuredName.DISPLAY_NAME, "Some Body");
> getContentResolver().insert(Data.CONTENT_URI, values);
>
> You are done.
>
> Now here's a much better way to do the same.  Use the
> new ContentProviderOperation API, which will ensure that the raw contact and
> its name are inserted at the same time.
>
> ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
> ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
>         .withValue(RawContacts.ACCOUNT_TYPE, accountType)
>         .withValue(RawContacts.ACCOUNT_NAME, accountName)
>         .build());
>
> ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
>         .withValueBackReference(Data.RAW_CONTACT_ID, 0)
>         .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
>         .withValue(StructuredName.DISPLAY_NAME, "Some Body")
>         .build());
>
> getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
>
> I hope this helps.
> - Dmitri
>
> On Mon, Nov 2, 2009 at 4:23 PM, jak. <koda...@gmail.com> wrote:
> > Hello,
>
> > I'm currently working on porting our Android app to 2.0 but I'm having
> > a rather hard time figuring out what is required to interact with the
> > new Contacts API.
> > I'm using reflection to decide whether the new API is available, if it
> > is I attempt to use the new features, otherwise I fall back to our old
> > methods.
>
> > However, I'm having a hard time finding analogs to the old
> > functionality in the new API.
> > For example in the past I was adding contacts to the database from an
> > external text source by creating a ContentValues object, filling it
> > with information on the contact and then adding it with a call to:
> > Contacts.People.createPersonInMyContactsGroup(...);
>
> > i.e.:
> > ...
>
> > ContentValues personValues = new ContentValues();
> > personValues.put(Contacts.People.NAME, "Some Body");
> > Uri personUri = Contacts.People.createPersonInMyContactsGroup
> > (curContext().getContentResolver(), personValues);
>
> > ...
> > How can I achieve the same goal in the new API?
>
> > I appreciate all the hard work going into improving the APIs but I
> > must admit I'm a bit frustrated by the lack of documentation and
> > examples.
> > I realize we have plenty of Javadocs on developer.android.com to
> > reference, but those only really show us what the new interfaces are.
> > I'm having a really hard time finding any discussion in terms of how
> > the new API is intended to be used.
>
> > Any help would be greatly appreciated!
> > Thanks!
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to