Improve code style
author"Vincent Veronis"
Sun, 08 May 2011 12:04:31 +0200
changeset 888 9ffd19d0249c
parent 887 d9a0565ccacc
child 889 45bbdf20aa9d
Improve code style
AndroidManifest.xml
src/com/beem/project/beem/account/SyncAdapterService.java
--- a/AndroidManifest.xml	Sun May 08 11:35:51 2011 +0200
+++ b/AndroidManifest.xml	Sun May 08 12:04:31 2011 +0200
@@ -55,16 +55,7 @@
 				android:label="Beem Connection">
 				<action
 					android:name="com.beem.project.beem.service.XmppConnectionAdapter.CONNECTION_CLOSED" />
-			</intent-filter>
-			<intent-filter
-				android:label="Beem Account">
-				<action
-					android:name="android.intent.action.VIEW" />
-				<category
-					android:name="android.intent.category.DEFAULT" />
-				<data
-					android:mimeType="vnd.android.cursor.item/vnd.com.beem.project.beem.android.profile" />
-			</intent-filter>
+			</intent-filter>			
 		</activity>
 		<activity
 			android:name=".ui.ChangeStatus"
--- a/src/com/beem/project/beem/account/SyncAdapterService.java	Sun May 08 11:35:51 2011 +0200
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Sun May 08 12:04:31 2011 +0200
@@ -74,7 +74,7 @@
 import com.beem.project.beem.BeemConnection;
 
 /**
- * Class to integrate beem in android's account
+ * Class to integrate beem in android's account.
  * @author marseille
  */
 public class SyncAdapterService extends Service {
@@ -91,11 +91,17 @@
 	super();
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void onCreate() {
 	mContext = SyncAdapterService.this;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public IBinder onBind(Intent intent) {
 	IBinder ret = null;
@@ -104,6 +110,10 @@
 
     }
 
+    /**
+     * Get syncAdapter instance.
+     * @return sync adapter
+     */
     private SyncAdapter getSyncAdapter() {
 	if (mSyncAdapter == null)
 	    mSyncAdapter = new SyncAdapter(this, true);
@@ -111,7 +121,7 @@
     }
 
     /**
-     * Method to sync Beem roster with Android
+     * Method to sync Beem roster with Android account.
      * @param context context
      * @param account account
      * @param extras extras
@@ -144,7 +154,11 @@
 	con.disconnect();
     }
 
-    private static void executeOperation(ArrayList<ContentProviderOperation> ops) {
+    /**
+     * Method to execute content provider operation.
+     * @param ops
+     */
+    private static void executeOperation(final ArrayList<ContentProviderOperation> ops) {
 	try {
 	    mContentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
 	} catch (RemoteException e) {
@@ -154,6 +168,11 @@
 	}
     }
 
+    /**
+     * Roster sync method.
+     * @param r The roster to sync
+     * @param a The account related
+     */
     private static void manageRoster(final Roster r, final Account a) {
 	ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 	for (RosterEntry entry : r.getEntries()) {
@@ -163,20 +182,18 @@
 	    if (ops.size() > 100)
 		executeOperation(ops);
 	}
-	// TODO: dont know how to ttest
-	//	for (RosterGroup group : r.getGroups()) {
-	//	    if (group != null)
-	//		manageGroup(ops, a, group);
-	//	    if (ops.size() > 0)
-	//		    executeOperation(ops);
-	//	}
 	if (ops.size() > 0)
 	    executeOperation(ops);
     }
 
+    /**
+     * RosterEntry sync method.
+     * @param ops The content provider operation
+     * @param account The account related
+     * @param entry The roster entry to sync
+     */
     private static void manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
 	long rawContactID = getRawContactID(account.name, entry.getUser());
-	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
 	Log.i(TAG, "Sync Contact : " + entry.getUser() + " RawContactID : " + rawContactID);
 	if (rawContactID == -1) { // Not found in database, add new
 	    ContentValues values = new ContentValues();
@@ -186,17 +203,57 @@
 	    Uri rawContactUri = mContentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
 	    rawContactID = ContentUris.parseId(rawContactUri);
 	    values.clear();
-	    //Insert Name values
-	    ContentProviderOperation.Builder builder = ContentProviderOperation
-		.newInsert(ContactsContract.Data.CONTENT_URI);
+	    ContentProviderOperation.Builder builder = addUpdateStructuredName(entry, rawContactID, true);
+	    ops.add(builder.build());
+	    builder = addUpdateIm(entry, rawContactID, true);
+	    ops.add(builder.build());
+	} else { // Found, update
+	    ContentProviderOperation.Builder builder = addUpdateStructuredName(entry, rawContactID, false);
+	    ops.add(builder.build());
+	}
+    }
+
+    /**
+     * Method to insert or update structured name informations.
+     * @param entry The roster entry to sync
+     * @param rawContactID The contact ID in the android database
+     * @param isInsert Insert boolean
+     * @return
+     */
+    private static ContentProviderOperation.Builder addUpdateStructuredName(RosterEntry entry, long rawContactID,
+	boolean isInsert) {
+	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
+	ContentProviderOperation.Builder builder;
+	if (isInsert) {
+	    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
 	    builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
 	    builder.withValue(ContactsContract.Data.MIMETYPE,
 		ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
 	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, rawContactID);
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, displayName);
-	    ops.add(builder.build());
+	} else {
+	    builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
+	    builder.withSelection(
+		ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID + " =? AND "
+		    + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
+		    + "'", new String[] { String.valueOf(rawContactID) });
+	}
+	builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
+	builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);
+	builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, displayName);
+	return builder;
+    }
+
+    /**
+     * Method to insert or update IM informations.
+     * @param entry The roster entry to sync
+     * @param rawContactID The contact ID in the android database
+     * @param isInsert Insert boolean
+     * @return
+     */
+    private static ContentProviderOperation.Builder addUpdateIm(RosterEntry entry, long rawContactID, boolean isInsert) {
+	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
+	ContentProviderOperation.Builder builder;
+	if (isInsert) {
 	    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
 	    builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
 	    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
@@ -204,23 +261,18 @@
 	    builder.withValue(ContactsContract.CommonDataKinds.Im.DATA1, displayName);
 	    builder.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL,
 		ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER);
-	    ops.add(builder.build());
-	} else { // Found, update
-	    // Update Name values
-	    ContentProviderOperation.Builder builder = ContentProviderOperation
-		.newUpdate(ContactsContract.Data.CONTENT_URI);
-	    builder.withSelection(
-		ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID + " =? AND "
-		    + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
-		    + "'", new String[] { String.valueOf(rawContactID)});
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);
-	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, displayName);
-	    ops.add(builder.build());
+	} else {
+	    builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
 	}
+	return builder;
     }
-   
 
+    /**
+     * Get contact ID from android database.
+     * @param account The account related
+     * @param jid The jid related
+     * @return ID in the database of the jid
+     */
     private static long getRawContactID(String account, String jid) {
 	long authorId = -1;
 	final Cursor c = mContentResolver.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {