Revision: 808
http://swingme.svn.sourceforge.net/swingme/?rev=808&view=rev
Author: janeroski
Date: 2010-04-19 16:24:31 +0000 (Mon, 19 Apr 2010)
Log Message:
-----------
PIM: Add new version that allows add/list of PIM in Android 1.6 and 2.1
Modified Paths:
--------------
AndroidME/AndroidManifest.xml
AndroidME/src_MIDP/javax/microedition/pim/ContactList.java
AndroidME/src_MIDP/javax/microedition/pim/PIM.java
AndroidME/src_MIDP/javax/microedition/pim/PIMItem.java
AndroidME/src_MIDP/javax/microedition/pim/PIMList.java
AndroidME/src_Test/net/yura/android/MainTest.java
Added Paths:
-----------
AndroidME/src_Android/net/yura/android/pim/AbstractPIMList.java
AndroidME/src_Android/net/yura/android/pim/AndroidPim.java
AndroidME/src_Android/net/yura/android/pim/ContactDao.java
AndroidME/src_Android/net/yura/android/pim/ContactEnumeration.java
AndroidME/src_Android/net/yura/android/pim/ContactFactory.java
AndroidME/src_Android/net/yura/android/pim/ContactImpl.java
AndroidME/src_Android/net/yura/android/pim/ContactListImpl.java
AndroidME/src_Android/net/yura/android/pim/Field.java
AndroidME/src_Android/net/yura/android/pim/FieldInfo.java
AndroidME/src_MIDP/javax/microedition/pim/UnsupportedFieldException.java
AndroidME/src_Test/net/yura/android/PimTest.java
Removed Paths:
-------------
AndroidME/src_Android/net/yura/android/pim/AndroidContactList.java
AndroidME/src_Android/net/yura/android/pim/AndroidPIMItem.java
AndroidME/src_Android/net/yura/android/pim/AndroidPIMList.java
AndroidME/src_Android/net/yura/android/pim/PreloadingAndroidPIMItem.java
Modified: AndroidME/AndroidManifest.xml
===================================================================
--- AndroidME/AndroidManifest.xml 2010-04-19 11:17:53 UTC (rev 807)
+++ AndroidME/AndroidManifest.xml 2010-04-19 16:24:31 UTC (rev 808)
@@ -21,4 +21,5 @@
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
+ <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
</manifest>
\ No newline at end of file
Added: AndroidME/src_Android/net/yura/android/pim/AbstractPIMList.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/AbstractPIMList.java (rev 0)
+++ AndroidME/src_Android/net/yura/android/pim/AbstractPIMList.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -0,0 +1,154 @@
+package net.yura.android.pim;
+
+import javax.microedition.pim.PIM;
+import javax.microedition.pim.PIMItem;
+import javax.microedition.pim.PIMList;
+import javax.microedition.pim.UnsupportedFieldException;
+
+/**
+ * This is an abstract class to unify common functionality of a PIMList
+ * @author rickyn
+ *
+ */
+public abstract class AbstractPIMList implements PIMList {
+
+ private FieldInfo[] fieldInfos;
+ private final int mode;
+ private final String name;
+
+ public AbstractPIMList(String name, int mode) {
+ this.name = name;
+ if(mode != PIM.READ_ONLY && mode != PIM.WRITE_ONLY && mode != PIM.READ_WRITE) {
+ throw new IllegalArgumentException("The mode '"+mode+"' is not supported.");
+ }
+ this.mode = mode;
+ }
+
+ public FieldInfo findFieldInfo(int fieldId) {
+ return findFieldInfo(fieldId,true);
+ }
+
+ public String getAttributeLabel(int attribute) {
+ switch(attribute) {
+ case PIMItem.ATTR_NONE: return "None";
+ default: throw new IllegalArgumentException("Attribute '"+attribute+"' is not valid.");
+ }
+ }
+
+ public int getFieldDataType(int fieldId) {
+ FieldInfo field = findFieldInfo(fieldId);
+ return field.type;
+ }
+
+ public String getFieldLabel(int fieldId) {
+ FieldInfo field = findFieldInfo(fieldId);
+ return field.label;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int[] getSupportedArrayElements(int stringArrayField) {
+ FieldInfo fieldInfo = findFieldInfo(stringArrayField);
+ return fieldInfo.supportedArrayElements;
+ }
+
+ public int[] getSupportedAttributes(int field) {
+ FieldInfo fieldInfo = findFieldInfo(field);
+ return fieldInfo.supportedAttributes;
+ }
+
+ public int[] getSupportedFields() {
+ int[] supportedFields = new int[this.fieldInfos.length];
+ for (int i = 0; i < this.fieldInfos.length; i++) {
+ supportedFields[i] = this.fieldInfos[i].pimId;
+ }
+ return supportedFields;
+ }
+
+ public boolean isSupportedArrayElement(int stringArrayField, int arrayElement) {
+ FieldInfo fieldInfo = findFieldInfo(stringArrayField);
+ if(fieldInfo.type != PIMItem.STRING_ARRAY) {
+ throw new IllegalArgumentException("The field with id '"+stringArrayField+"' is not of type 'PIMItem.STRING_ARRAY'.");
+ }
+ int numberOfValues = fieldInfo.supportedArrayElements.length;
+ for(int i = 0; i < numberOfValues; i++) {
+ if(arrayElement == fieldInfo.supportedArrayElements[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean isSupportedAttribute(int field, int attribute) {
+ FieldInfo fieldInfo = findFieldInfo(field);
+ int numberOfValues = fieldInfo.supportedAttributes.length;
+ for(int i = 0; i < numberOfValues; i++) {
+ if(attribute == fieldInfo.supportedAttributes[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean isSupportedField(int fieldId) {
+ FieldInfo field = findFieldInfo(fieldId,false);
+ if(field == null) {
+ return false;
+ }
+ return true;
+ }
+
+ public int maxValues(int field) {
+ return -1;
+ }
+
+ public int stringArraySize(int stringArrayField) {
+ FieldInfo fieldInfo = findFieldInfo(stringArrayField);
+ return fieldInfo.numberOfArrayElements;
+ }
+
+ /**
+ * This method throws a SecurityException if the list is not readable. This is the case if the list was opened in WRITE_ONLY mode.
+ */
+ protected void ensureListReadable() {
+ if(this.mode == PIM.WRITE_ONLY) {
+ throw new SecurityException("The list is only writeable.");
+ }
+ }
+
+ protected void ensureListWriteable() {
+ if(this.mode == PIM.READ_ONLY) {
+ throw new SecurityException("The list is only readable.");
+ }
+ }
+
+ /**
+ * This method will set the fields this list can handle.
+ * @param fieldInfos
+ */
+ protected void setFieldInfos(FieldInfo[] fieldInfos) {
+ this.fieldInfos = fieldInfos;
+ }
+
+ /**
+ *
+ * @param fieldId
+ * @param throwException
+ * @return May return null if no field with the given id is present and now exception should be thrown.
+ */
+ private FieldInfo findFieldInfo(int fieldId, boolean throwException) {
+ for (int i = 0; i < this.fieldInfos.length; i++) {
+ FieldInfo fieldInfo = this.fieldInfos[i];
+ if(fieldId == fieldInfo.pimId) {
+ return fieldInfo;
+ }
+ }
+ if(throwException) {
+ throw new UnsupportedFieldException("The field with id '"+fieldId+"' is not supported.");
+ }
+ return null;
+ }
+
+}
\ No newline at end of file
Deleted: AndroidME/src_Android/net/yura/android/pim/AndroidContactList.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/AndroidContactList.java 2010-04-19 11:17:53 UTC (rev 807)
+++ AndroidME/src_Android/net/yura/android/pim/AndroidContactList.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -1,287 +0,0 @@
-package net.yura.android.pim;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.microedition.midlet.MIDlet;
-import javax.microedition.pim.Contact;
-import javax.microedition.pim.ContactList;
-import javax.microedition.pim.PIMItem;
-
-import android.database.Cursor;
-import android.net.Uri;
-import android.provider.BaseColumns;
-import android.provider.Contacts;
-
-public class AndroidContactList extends AndroidPIMList implements ContactList
-{
- private static final Map<Integer, String> DEFAULT_COLUMN_MAPPINGS = new HashMap<Integer, String>();
- private static final Map<String, Class> DEFAULT_COLUMN_TYPES = new HashMap<String, Class>();
-
- static
- {
- DEFAULT_COLUMN_MAPPINGS.put( Contact.UID, BaseColumns._ID );
- DEFAULT_COLUMN_TYPES.put( BaseColumns._ID, String.class );
-
- DEFAULT_COLUMN_MAPPINGS.put( Contact.FORMATTED_NAME, Contacts.PeopleColumns.NAME );
- DEFAULT_COLUMN_TYPES.put( Contacts.PeopleColumns.NAME, String.class );
-
- //DEFAULT_COLUMN_MAPPINGS.put( Contact.TITLE, Contacts.PeopleColumns.TITLE );
-
- DEFAULT_COLUMN_MAPPINGS.put( Contact.TEL, Contacts.People.NUMBER );
- DEFAULT_COLUMN_TYPES.put( Contacts.People.NUMBER, String.class );
-
- //DEFAULT_COLUMN_MAPPINGS.put( Contact.EMAIL, Contacts.ContactMethodsColumns.DATA );
- }
-
- public AndroidContactList()
- {
- super(
- "Contacts",
- MIDlet.DEFAULT_MIDLET.getActivity().getContentResolver(),
- Contacts.People.CONTENT_URI,
- convert(
- DEFAULT_COLUMN_MAPPINGS.values(),
- new String[]{
- Contacts.People.PRIMARY_EMAIL_ID,
- Contacts.People.PRIMARY_PHONE_ID
- }
- )
- );
- }
-
- @Override
- protected PIMItem create( Cursor cursor )
- {
- AndroidContact contact = new AndroidContact( cursor );
-
- return contact;
- }
-
- private static class PhoneNumber
- {
- private String number;
- private int attributes;
-
- public PhoneNumber( String number, int attributes )
- {
- this.number = number;
- this.attributes = attributes;
- }
-
- public String getNumber() {
- return number;
- }
-
- public int getAttributes() {
- return attributes;
- }
-
-
- }
-
- private class AndroidContact extends PreloadingAndroidPIMItem implements Contact
- {
- private ArrayList<PhoneNumber> phoneNumbers;
- private String emailAddress;
-
- public AndroidContact( Cursor cursor )
- {
- super( cursor, DEFAULT_COLUMN_MAPPINGS, DEFAULT_COLUMN_TYPES );
-
- // look up the phone numbers for this person
-
- String id = this.getString( Contact.UID, 0 );
- String preferredPhoneId = this.getString(
- Contacts.People.PRIMARY_PHONE_ID
- );
- String preferredEmailId = this.getString(
- Contacts.People.PRIMARY_EMAIL_ID
- );
-
- Uri emailUri = Contacts.ContactMethods.CONTENT_URI;
- Cursor emailCursor = AndroidContactList.this.contentResolver.query(
- emailUri,
- new String[]{
- Contacts.ContactMethods._ID,
- Contacts.ContactMethods.TYPE,
- Contacts.ContactMethods.DATA
- },
- Contacts.ContactMethods.PERSON_ID+"=?",
- new String[]{ id },
- null
- );
- String emailAddress = null;
-
- int dataColumn = emailCursor.getColumnIndex( Contacts.ContactMethods.DATA );
- int emailTypeColumn = emailCursor.getColumnIndex( Contacts.ContactMethods.TYPE );
- int emailIdColumn = emailCursor.getColumnIndex( Contacts.ContactMethods._ID );
- while( emailCursor.moveToNext() )
- {
- String data = emailCursor.getString( dataColumn );
- String type = emailCursor.getString( emailTypeColumn );
- String emailId = emailCursor.getString( emailIdColumn );
- if( emailAddress == null || Contacts.ContactMethods.CONTENT_EMAIL_TYPE.equals( type ) )
- {
- emailAddress = data;
- }
- if( emailId != null && emailId.equals( preferredEmailId ) )
- {
- emailAddress = data;
- break;
- }
- }
- emailCursor.close();
- this.emailAddress = emailAddress;
-
- Uri phoneUri = Contacts.Phones.CONTENT_URI;
- Cursor phoneCursor = AndroidContactList.this.contentResolver.query(
- phoneUri,
- new String[]{
- Contacts.Phones._ID,
- Contacts.Phones.NUMBER,
- Contacts.Phones.TYPE,
- Contacts.Phones.PERSON_ID
- },
- Contacts.Phones.PERSON_ID+"=?",
- new String[]{ id },
- null
- );
-
- int numberColumn = phoneCursor.getColumnIndex( Contacts.Phones.NUMBER );
- int typeColumn = phoneCursor.getColumnIndex( Contacts.Phones.TYPE );
- int idColumn = phoneCursor.getColumnIndex( Contacts.Phones._ID );
-
- ArrayList<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
-
- while( phoneCursor.moveToNext() )
- {
- String number = phoneCursor.getString( numberColumn );
- int type = phoneCursor.getInt( typeColumn );
- String phoneId = phoneCursor.getString( idColumn );
- int attributes = 0;
- if( type == Contacts.Phones.TYPE_MOBILE )
- {
- attributes = attributes | Contact.ATTR_MOBILE;
- }
- if( type == Contacts.Phones.TYPE_WORK )
- {
- attributes = attributes | Contact.ATTR_WORK;
- }
- if( type == Contacts.Phones.TYPE_HOME )
- {
- attributes = attributes | Contact.ATTR_HOME;
- }
- if( phoneId != null && phoneId.equals( preferredPhoneId ) )
- {
- attributes = attributes | Contact.ATTR_PREFERRED;
- }
- PhoneNumber phoneNumber = new PhoneNumber( number, attributes );
- phoneNumbers.add( phoneNumber );
- }
- phoneCursor.close();
- this.phoneNumbers = phoneNumbers;
-
- }
-
- public int getPreferredIndex( int field )
- {
- return 0;
- }
-
- @Override
- public int countValues( int field )
- {
- int result;
- if( field == Contact.TEL )
- {
- result = this.phoneNumbers.size();
- }
- else
- {
- result = super.countValues( field );
- }
- return result;
- }
-
- @Override
- public int getAttributes( int field, int index )
- {
- int result;
- if( field == Contact.TEL )
- {
- result = this.phoneNumbers.get( index ).getAttributes();
- }
- else if( field == Contact.EMAIL )
- {
- result = this.emailAddress != null ? 1 : 0;
- }
- else
- {
- result = super.getAttributes( field, index );
- }
- return result;
- }
-
- @Override
- public String getString(int field, int index)
- {
- String result;
- if( field == Contact.TEL )
- {
- result = this.phoneNumbers.get( index ).getNumber();
- }
- else if( field == Contact.EMAIL )
- {
- result = this.emailAddress;
- }
- else
- {
- result = super.getString( field, index );
- }
- return result;
- }
-
- @Override
- public String[] getStringArray(int field, int index)
- {
- String[] result;
- if( field == Contact.NAME )
- {
- String fullName = getString( Contact.FORMATTED_NAME, 0 );
- int spaceIndex = fullName.indexOf( ' ' );
- String firstName;
- String lastName;
- String title = getString( Contact.TITLE, 0 );
- String prefix = null;
- String postfix = null;
- if( spaceIndex >= 0 )
- {
- firstName = fullName.substring( 0, spaceIndex );
- lastName = fullName.substring( spaceIndex + 1 );
- }
- else
- {
- firstName = fullName;
- lastName = null;
- }
- result = new String[]{ firstName, lastName, title, prefix, postfix };
- }
- else
- {
- result = super.getStringArray( field, index );
- }
- return result;
- }
-
-
- }
-
- // @Override
- public boolean isSupportedField(int fieldName) {
- return DEFAULT_COLUMN_MAPPINGS.containsKey( fieldName );
- }
-
-
-}
Deleted: AndroidME/src_Android/net/yura/android/pim/AndroidPIMItem.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/AndroidPIMItem.java 2010-04-19 11:17:53 UTC (rev 807)
+++ AndroidME/src_Android/net/yura/android/pim/AndroidPIMItem.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -1,81 +0,0 @@
-package net.yura.android.pim;
-
-import java.util.Map;
-
-import javax.microedition.pim.PIMItem;
-
-import android.database.Cursor;
-
-public class AndroidPIMItem implements PIMItem
-{
- private Cursor cursor;
- private Map<Integer, String> columnMappings;
-
- public AndroidPIMItem( Cursor cursor, Map<Integer, String> columnMappings )
- {
- this.cursor = cursor;
- this.columnMappings = columnMappings;
-
- }
-
-// @Override
- public int countValues( int field )
- {
- return 1;
- }
-
-// @Override
- public int getAttributes( int field, int index )
- {
- return 0;
- }
-
- public long getDate( String columnName ) {
- return this.cursor.getLong( this.cursor.getColumnIndex( columnName ) );
- }
-
-// @Override
- public long getDate( int field, int index )
- {
- return getDate( getColumnName(field) );
- }
-
- public String getString( String columnName )
- {
- return this.cursor.getString( this.cursor.getColumnIndex( columnName ) );
- }
-
-// @Override
- public String getString( int field, int index )
- {
- return this.getString( getColumnName( field ) );
- }
-
- public String[] getStringArray( String columnName ) {
- return new String[]{ getString( columnName ) };
- }
-
-// @Override
- public String[] getStringArray(int field, int index)
- {
- return getStringArray( this.getColumnName( field ) );
- }
-
- protected String getColumnName( int field )
- {
- return this.columnMappings.get( field );
- }
-
- protected int getColumnIndex( int field )
- {
- String columnName = getColumnName( field );
- if( columnName != null )
- {
- return this.cursor.getColumnIndex( columnName );
- }
- else
- {
- throw new NullPointerException( "no column name for field "+field );
- }
- }
-}
Deleted: AndroidME/src_Android/net/yura/android/pim/AndroidPIMList.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/AndroidPIMList.java 2010-04-19 11:17:53 UTC (rev 807)
+++ AndroidME/src_Android/net/yura/android/pim/AndroidPIMList.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -1,125 +0,0 @@
-package net.yura.android.pim;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Enumeration;
-
-import javax.microedition.pim.PIMException;
-import javax.microedition.pim.PIMItem;
-import javax.microedition.pim.PIMList;
-
-import android.content.ContentResolver;
-import android.database.Cursor;
-import android.net.Uri;
-
-public abstract class AndroidPIMList implements PIMList
-{
- public static final String[] convert( Collection<String> values )
- {
- return convert( values, new String[ 0 ] );
- }
-
- public static final String[] convert( Collection<String> values, String[] additional )
- {
- String[] names = new String[ values.size() + additional.length ];
- System.arraycopy( additional, 0, names, 0, additional.length );
- int pos = additional.length;
- for( String value : values )
- {
- names[ pos ] = value;
- pos++;
- }
- return names;
- }
-
- private String name;
- private String[] columnNames;
- protected ContentResolver contentResolver;
- private Uri contentURI;
- private Collection<Cursor> cursors;
-
- protected AndroidPIMList(
- String name,
- ContentResolver contentResolver,
- Uri contentURI,
- String[] columnNames )
- {
- this.name = name;
- this.contentResolver = contentResolver;
- this.contentURI = contentURI;
- this.columnNames = columnNames;
- this.cursors = new ArrayList<Cursor>();
-
- }
-
-// @Override
- public void close() throws PIMException
- {
- for( Cursor cursor : this.cursors )
- {
- cursor.close();
- }
- this.cursors.clear();
- }
-
- public String getName() {
- return this.name;
- }
-
-// @Override
- public Enumeration items() throws PIMException
- {
-
- Cursor cursor = this.contentResolver.query(
- this.contentURI,
- this.columnNames,
- null,
- null,
- null
- );
- if( cursor == null )
- {
- StringBuilder columnNames = new StringBuilder();
- for( int i=0; i<this.columnNames.length; i++ )
- {
- String columnName = this.columnNames[ i ];
- columnNames.append( columnName );
- if( i < this.columnNames.length-1 )
- {
- columnNames.append( ", " );
- }
- }
- throw new PIMException( "invalid query "+this.contentURI.toString()+" "+columnNames );
- }
- this.cursors.add( cursor );
- return new AndroidPIMListEnumeration( cursor );
- }
-
- protected abstract PIMItem create( Cursor cursor );
-
- private class AndroidPIMListEnumeration implements Enumeration
- {
- private Cursor cursor;
-
- public AndroidPIMListEnumeration( Cursor cursor )
- {
- this.cursor = cursor;
- cursor.moveToFirst();
- }
-
-// @Override
- public boolean hasMoreElements()
- {
- return !this.cursor.isAfterLast();
- }
-
-// @Override
- public Object nextElement()
- {
- PIMItem item = AndroidPIMList.this.create( this.cursor );
- this.cursor.moveToNext();
- return item;
- }
-
- }
-}
Added: AndroidME/src_Android/net/yura/android/pim/AndroidPim.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/AndroidPim.java (rev 0)
+++ AndroidME/src_Android/net/yura/android/pim/AndroidPim.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -0,0 +1,76 @@
+package net.yura.android.pim;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Hashtable;
+
+import javax.microedition.pim.PIM;
+import javax.microedition.pim.PIMException;
+import javax.microedition.pim.PIMItem;
+import javax.microedition.pim.PIMList;
+
+public class AndroidPim extends PIM {
+
+ public static final String DEFAULT_PIMLIST_NAME_CONTACTS = "contacts";
+ private static Hashtable<Integer, ContactListImpl> contactListInstances = new Hashtable<Integer, ContactListImpl>();
+
+ @Override
+ public PIMItem[] fromSerialFormat(InputStream is, String enc) throws PIMException, UnsupportedEncodingException {
+ throw new UnsupportedEncodingException("At the moment no encoding is supported.");
+ }
+
+ @Override
+ public String[] listPIMLists(int pimListType) {
+ switch(pimListType) {
+ case PIM.CONTACT_LIST:
+ return new String[] {DEFAULT_PIMLIST_NAME_CONTACTS};
+ default:
+ return new String[0];
+ }
+ }
+
+ @Override
+ public PIMList openPIMList(int pimListType, int mode) throws PIMException {
+ switch(pimListType) {
+ case PIM.CONTACT_LIST:
+ return openPIMList(pimListType, mode,DEFAULT_PIMLIST_NAME_CONTACTS);
+ default:
+ throw new PIMException("The pimListType '"+pimListType+"' is not supported.");
+ }
+
+ }
+
+ @Override
+ public PIMList openPIMList(int pimListType, int mode, String name) throws PIMException {
+ switch(pimListType) {
+ case PIM.CONTACT_LIST:
+ if( ! DEFAULT_PIMLIST_NAME_CONTACTS.equals(name)) {
+ throw new PIMException("A PIMList with name '"+name+"' and type '"+pimListType+"' does not exist.");
+ }
+
+ ContactListImpl contactList = contactListInstances.get(mode);
+ if (contactList == null) {
+//JP: The list is being re-used... Once open in one mode, it can no longer change... at least have one per mode...
+// contactListInstance = new ContactListImpl(name,mode);
+
+ contactList = new ContactListImpl(name, mode);
+ contactListInstances.put(mode, contactList);
+ }
+ return contactList;
+ default:
+ throw new PIMException("The pimListType '"+pimListType+"' is not supported.");
+ }
+ }
+
+ @Override
+ public String[] supportedSerialFormats(int pimListType) {
+ return new String[0];
+ }
+
+ @Override
+ public void toSerialFormat(PIMItem item, OutputStream os, String enc, String dataFormat) throws PIMException, UnsupportedEncodingException {
+ throw new UnsupportedEncodingException("At the moment no encoding is supported.");
+ }
+
+}
Added: AndroidME/src_Android/net/yura/android/pim/ContactDao.java
===================================================================
--- AndroidME/src_Android/net/yura/android/pim/ContactDao.java (rev 0)
+++ AndroidME/src_Android/net/yura/android/pim/ContactDao.java 2010-04-19 16:24:31 UTC (rev 808)
@@ -0,0 +1,212 @@
+package net.yura.android.pim;
+
+import java.util.Enumeration;
+
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.Contacts;
+import android.provider.Contacts.People;
+import javax.microedition.pim.Contact;
+
+import net.yura.android.AndroidMeMIDlet;
+
+/**
+ * This Data Access Object will manage JavaME PIM contact objects and talks to the sqlite3 database on android.
+ * It uses API level 4 and not the new ContactsContract.
+ * @author rickyn
+ *
+ */
+public class ContactDao {
+
+ private final ContactListImpl contactListImpl;
+ private final ContentResolver contentResolver;
+
+ public ContactDao(ContactListImpl contactListImpl) {
+ this.contactListImpl = contactListImpl;
+ this.contentResolver = AndroidMeMIDlet.DEFAULT_ACTIVITY.getContentResolver();
+ }
+
+ private void addNonEmptyString(StringBuffer buffer, String str) {
+ if (str != null) {
+ if (buffer.length() > 0) {
+ buffer.append(' ');
+ }
+ buffer.append(str);
+ }
+ }
+
+ public void persist(ContactImpl contact) {
+ final boolean isNew = contact.isNew();
+ final Uri personUri;
+ final ContentValues values = new ContentValues();
+
+ // Update the name
+ values.clear();
+ StringBuffer buffer = new StringBuffer();
+ String[] names = contact.getStringArray(Contact.NAME, 0);
+
+ addNonEmptyString(buffer, names[Contact.NAME_PREFIX]);
+ addNonEmptyString(buffer, names[Contact.NAME_GIVEN]);
+ addNonEmptyString(buffer, names[Contact.NAME_FAMILY]);
+ addNonEmptyString(buffer, names[Contact.NAME_SUFFIX]);
+ addNonEmptyString(buffer, names[Contact.NAME_OTHER]);
+
+ values.put(People.NAME, buffer.toString());
+
+ // Update the note
+ int numberOfNotes = contact.countValues(Contact.NOTE);
+ if(numberOfNotes > 0) {
+ String name = contact.getString(Contact.NOTE, 0);
+ values.put(People.NOTES, name);
+ }
+
+ if(isNew) {
+ personUri = this.contentResolver.insert(People.CONTENT_URI, values);
+ long id = ContentUris.parseId(personUri);
+ // insert this contact into "My Contacts" group in order to see it in the Contact viewer
+ try {
+ Contacts.People.addToMyContactsGroup(this.contentResolver, id);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ } else {
+ long id = contact.getId();
+ personUri = ContentUris.withAppendedId(People.CONTENT_URI, id);
+ this.contentResolver.update(personUri, values, null, null);
+ }
+
+ // Update the address.
+ int numberOfAddresses = contact.countValues(Contact.ADDR);
+ for(int i = 0; i < numberOfAddresses; i++) {
+ Uri addressUri = Uri.withAppendedPath(personUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
+ values.clear();
+ values.put(Contacts.ContactMethods.KIND,new Integer(Contacts.KIND_POSTAL));
+
+ int attributes = contact.getAttributes(Contact.ADDR, i);
+ int type = convertAttrToAddressType(attributes);
+ values.put(Contacts.ContactMethods.TYPE,new Integer(type));
+
+ String[] addressElements = contact.getStringArray(Contact.ADDR, i);
+ buffer = new StringBuffer();
+ if (addressElements[Contact.ADDR_POBOX] != null) {
+ buffer.append("PoBox ");
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_POBOX]);
+ }
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_STREET]);
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_POSTALCODE]);
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_LOCALITY]);
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_COUNTRY]);
+ addNonEmptyString(buffer, addressElements[Contact.ADDR_EXTRA]);
+
+ values.put(Contacts.ContactMethods.DATA,buffer.toString());
+ this.contentResolver.insert(addressUri, values);
+ }
+
+ int numberOfEMails = contact.countValues(Contact.EMAIL);
+ for(int i = 0; i < numberOfEMails; i++) {
+ Uri addressUri = Uri.withAppendedPath(personUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
+ values.clear();
+ buffer = new StringBuffer();
+ buffer.append(contact.getString(Contact.EMAIL, i));
+ values.put(Contacts.ContactMethods.DATA,buffer.toString());
+
+ values.put(Contacts.ContactMethods.KIND,new Integer(Contacts.KIND_EMAIL));
+
+ int attributes = contact.getAttributes(Contact.EMAIL, i);
+ int type = convertAttrToAddressType(attributes);
+ values.put(Contacts.ContactMethods.TYPE,new Integer(type));
+
+ this.contentResolver.insert(addressUri, values);
+ }
+
+ // Update the telephone number.
+ int numberOfTelephoneNumbers = contact.countValues(Contact.TEL);
+ for (int i = 0; i < numberOfTelephoneNumbers; i++) {
+ Uri phoneUri = Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);
+ values.clear();
+ String telephoneNumber = contact.getString(Contact.TEL, i);
+ values.put(Contacts.People.Phones.NUMBER,telephoneNumber);
+
+ int attributes = contact.getAttributes(Contact.TEL, i);
+ int type = convertAttrToTelType(attributes);
+ values.put(Contacts.ContactMethods.TYPE,new Integer(type));
+
+ this.contentResolver.insert(phoneUri, values);
+ }
+ }
+
+ private int convertAttrToAddressType(int attribute) {
+ if((attribute & Contact.ATTR_HOME) == Contact.ATTR_HOME){
+ return Contacts.ContactMethods.TYPE_HOME;
+ }
+ if((attribute & Contact.ATTR_WORK) == Contact.ATTR_WORK){
+ return Contacts.ContactMethods.TYPE_WORK;
+ }
+ if((attribute & Contact.ATTR_OTHER) == Contact.ATTR_OTHER){
+ return Contacts.ContactMethods.TYPE_OTHER;
+ }
+ return Contacts.ContactMethods.TYPE_OTHER;
+ }
+
+ private int convertAttrToTelType(int attribute) {
+ if((attribute & Contact.ATTR_MOBILE) == Contact.ATTR_MOBILE){
+ return Contacts.People.Phones.TYPE_MOBILE;
+ }
+ if((attribute & Contact.ATTR_FAX) == Contact.ATTR_FAX){
+ if((attribute & Contact.ATTR_HOME) == Contact.ATTR_HOME){
+ return Contacts.People.Phones.TYPE_FAX_HOME;
+ }
+ if((attribute & Contact.ATTR_WORK) == Contact.ATTR_WORK){
+ return Contacts.People.Phones.TYPE_FAX_WORK;
+ }
+ return Contacts.People.Phones.TYPE_FAX_WORK;
+ }
+ if((attribute & Contact.ATTR_HOME) == Contact.ATTR_HOME){
+ return Contacts.People.Phones.TYPE_HOME;
+ }
+ if((attribute & Contact.ATTR_WORK) == Contact.ATTR_WORK){
+ return Contacts.People.Phones.TYPE_WORK;
+ }
+ if((attribute & Contact.ATTR_PAGER) == Contact.ATTR_PAGER){
+ return Contacts.People.Phones.TYPE_PAGER;
+ }
+ if((attribute & Contact.ATTR_OTHER) == Contact.ATTR_OTHER){
+ return Contacts.People.Phones.TYPE_OTHER;
+ }
+ return Contacts.People.Phones.TYPE_OTHER;
+ }
+
+ publi...
[truncated message content] |