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
this:

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

        Contacts.People.createPersonInMyContactsGroup(
                        getContentResolver(),
                        values);

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?


brnzn

--~--~---------~--~----~------------~-------~--~----~



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)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.9 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. How to properly use PreferenceActivity.addPreferencesFromIntent(Intent)

Hi all!
Does anyone know how to properly use the
PreferenceActivity.addPreferencesFromIntent(Intent) method?!

Android reference (http://developer.android.com/reference/android/
preference/PreferenceActivity.html) says: "To specify an Intent to
query Activities that each have preferences, use
addPreferencesFromIntent(Intent). Each Activity can specify meta-data
in the manifest (via the key METADATA_KEY_PREFERENCES) that points to
an XML resource. These XML resources will be inflated into a single
preference hierarchy and shown by this activity"

So I created a PreferenceActivity overriding onCreate() as follow:
protected void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 addPreferencesFromIntent(new Intent("mypackage.MYKEY"));
}

Then, I added the following lines into the AndroidManifest file:
<activity android:name=".AnotherActivity">
...
<intent-filter>
<action android:name="mypackage.MYKEY" />
</intent-filter>
<meta-data android:name="android.preference" android:resource="@xml/
settings" />
</activity>

and xml/settings.xml contains
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
android" android:key="settings">
 <CheckBoxPreference android:key="1-check-box" android:title="1. Check
Box" />
 <EditTextPreference android:key="2-edit-text" android:title="2. Edit
Text" />
</PreferenceScreen>

Now, all is showed and the CheckBoxPreference works fine but when I
click to the EditTextPrefence the following exception occurs:
"android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application"
...
It seems to be related to the dialog that should appear but that is
never shown.

Any ideas?!
Thanks in advance,
Francesco

--~--~---------~--~----~------------~-------~--~----~

2. Accelerometer: Fixed Sample Rate Requirements

Hi there,

I require the following in order to get my accelerometer based
application to work:

 1. a reliable sample rate of ~20Hz (can be faster - I can sub-sample)
 2. a guarantee of service even when the phone goes to sleep

I have coded up a background service to test the performance of the
accelerometer.  I use the SensorEventListener interface and record the
timestamps of sensor events in the onSensorChanged() handler.  I have
noticed the following:

 - if I use a PARTIAL_WAKE_LOCK the device stops calling
onSensorChanged() whenever the screen goes to sleep
 - if I use any other wake-lock the device stops calling
onSensorChanged() when I press the power button
 - when the device is awake and onSensorChanged() is being called, I
see a varied sample rate for SENSOR_DELAY_FASTEST (and this is without
doing anything in the onSensorChanged() function, apart from setting a
time-between-calls variable, i.e. no I/O)

I should mention that my test device is an HTC Hero.

So, my question: is it possible to achieve my requirements?  Have I
missed something?  For example, is it possible to poll the sensor,
waking up reliably using the AlarmManager?

Many thanks in advance!

--~--~---------~--~----~------------~-------~--~----~

3. How to display list of images in ListView in android?

4. Mobile Developer Roles in Toronto

5. Using custom collection of words to be used as SoftKeyboard candidates

6. taking pictures without seeing the preview on screen

7. How to play music album?