Inserting a contact / Android architecture

by brnzn » Thu, 01 Jan 2009 08:06:22 GMT

Sponsored Links
 I have a question on inserting a contact, but it's the general
principle I'm most interested in.  I'd really love to hear from one of
the Android Engineers on this, because currently I find some of the
design decisions in Android perplexing, and I'd like to understand why
it is as it is.

The most direct way to insert a contact seems to be to use
Contacts.People.createPersonInMyContactsGroup(...).  Something like

        ContentValues values = new ContentValues();
        values.put(People.NAME, "Bernie");


We're constructing the person entity as a map.  This brings up a few
potential problems:
1. How do we know what the keys should be?  By convention Android's
own classes (such as People) declare static constants to tell us what
those keys should be, but this is only a convention.
2. How do we know what datatype is appropriate for a given key?
There's nothing to stop you or warn you when you push the wrong type
of data in.
3. How do we know what combination of keys is valid?  If a given
entity has mandatory properties, there's nothing to stop you or warn
you if you forget to set some of those mandatory properties.
4. We're potentially allocating two objects for every value - one for
the value itself and one for the key.  Where we use static constants,
this is less of an issue.

Why does the API not provide a Person object that we can populate and
then pass in to be persisted?  Perhaps something like this:

        Person p = new Person("Bernie");
        p.addGroupMembership("My Contacts");

        getContentResolver().insert(Person.CONTENT_URI, p);

The Person class tells clearly us:
1. What values can be set (by the existence of bean properties)
2. What datatype each property is
3. What values are are mandatory (probably by virtue of them being
required in constructor calls)

Is it a classloading issue?  I'm guessing no because we could easily
overcome that by making Person Serializable or Parcelable, splitting
it out into a JAR to be linked into whatever app needs it.

So what then is the philosophy that has driven the architecture to be
this way?



Inserting a contact / Android architecture

by Mark Murphy » Thu, 01 Jan 2009 08:45:44 GMT


I can't speak for the core Android team, so I cannot say specifically
why they made the design decision.

However, given their repeated insistence that they will not break binary
interfaces with Android OS updates, my guess is that's at least part of
their rationale. The definition of a "contact" has potential to change
significantly. They may feel that a more generic interface will help
them expand the scope of contacts without changing existing uses of the API.

To draw an analogy, it's a variant on the strong-vs.-weak typing
argument you see when Rubyists and Javanauts square off. Strong typing
has definite benefits, in terms of compile-time validations and
potential for optimization. However, strong typing can also "get in the
way", which is why some folk prefer weakly-typed or type-inferred languages.

Anyway, that's my guess. Take it with a grain of salt. Preferably a
large grain of salt, maybe one cubic foot or so... ;-)

Mark Murphy (a Commons Guy) 
_The Busy Coder's Guide to Android Development_ Version 1.9 Published!


Sponsored Links

Other Threads

1. problem installing my app on real device

Hello, All

I am developing application using Eclipse. My friend has HTC Hero and
I am trying to run my application on it, but it says that the
application could not be installed.
I did the steps which are described in Android DevGuide. I.e. exported
my app with the generated key.
Am i right assuming that this is enough? As i understand eclipse signs
and aligns application automatically when selecting Export.

my app uses sdk version 3 i.e. 1.5

Thanks in advance.


2. R: How to build calculator?

It helps, but still some parts are missing, i.e. for example, or maybe I 
have done something wrong... which are the correct steps to get this program 
into eclipse?



3. Memory 8 gb tdk terbaca di spica

4. Emulator: Internet access via Proxy

5. Setting two setImeOptions() for a EditText

6. Boosting Kinerja G1 Space internal hh di dream

7. <WTA> Buat Desire user, ada yang sering HH reboot sendiri?