Improve muc and sync management
authorVincent V. <marseille@beem-project.com>
Wed, 18 Apr 2012 15:46:21 +0200
changeset 941 2777c49a84b0
parent 940 b8e23e8da2f7
child 942 e74b3604b3a1
Improve muc and sync management
src/com/beem/project/beem/BeemSync.java
src/com/beem/project/beem/account/SyncAdapterService.java
--- a/src/com/beem/project/beem/BeemSync.java	Tue Apr 10 11:32:31 2012 +0200
+++ b/src/com/beem/project/beem/BeemSync.java	Wed Apr 18 15:46:21 2012 +0200
@@ -144,7 +144,7 @@
 	    executeOperation(ops);
     }
 
-    private void manageGroup(ArrayList<ContentProviderOperation> ops, Account account, RosterGroup group) {
+    public void manageGroup(ArrayList<ContentProviderOperation> ops, Account account, RosterGroup group) {
 	Log.i(TAG, "Sync group : " + group.getName() + " " + group.getEntryCount());
 	long rawGroupID = getRawGroupID(account.name, group.getName());
 	if (rawGroupID == -1) {
@@ -164,7 +164,7 @@
      * @param entry The roster entry to sync
      * @return The raw contact ID
      */
-    private long manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
+    public long manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
 	long rawContactID = getRawContactID(account.name, entry.getUser());
 	Log.i(TAG, "Sync Contact : " + entry.getUser() + " RawContactID : " + rawContactID);
 	if (rawContactID == -1) { // Not found in database, add new
@@ -334,30 +334,22 @@
 	    Log.e(TAG, "BookmarkManager", e);
 	}
 	for (BookmarkedConference bookmarkedConference : list) {
-	    ContentValues values = new ContentValues();
-	    values.put(MUCs.NAME, bookmarkedConference.getName());
-	    values.put(MUCs.JID, bookmarkedConference.getJid());
-	    values.put(MUCs.AUTO_JOIN, bookmarkedConference.isAutoJoin());
-	    values.put(MUCs.SHARED, bookmarkedConference.isShared());
-	    values.put(MUCs.NICKNAME, bookmarkedConference.getNickname());
-	    values.put(MUCs.PASSWORD, bookmarkedConference.getPassword());
-	    values.put(MUCs.ACCOUNT_NAME, accoutname);
-	    values.put(MUCs.ACCOUNT_TYPE, BeemApplication.BEEM_PACKAGE);
-	    Log.e(TAG, "MUC " + bookmarkedConference.getName());
-	    mContext.getContentResolver().insert(MUCs.CONTENT_URI, values);
-
-	    //	    if (bookmarkedConference.isAutoJoin()) {
-	    //		MultiUserChat muc2 = new MultiUserChat(xmppCo, bookmarkedConference.getJid());
-	    //
-	    //		DiscussionHistory history = new DiscussionHistory();
-	    //		history.setMaxStanzas(5);
-	    //		try {
-	    //		    muc2.join(bookmarkedConference.getNickname(), bookmarkedConference.getPassword(), history,
-	    //			SmackConfiguration.getPacketReplyTimeout());
-	    //		} catch (XMPPException e) {
-	    //		    Log.e(TAG, "MUC Join Problem", e);
-	    //		}
-	    //	    }
+	    Cursor c = mContext.getContentResolver().query(MUCs.CONTENT_URI,
+		new String[] { MUCs._ID, MUCs.NAME, MUCs.JID },
+		MUCs.ACCOUNT_NAME + "=? AND " + MUCs.ACCOUNT_TYPE + "=? AND " + MUCs.JID + "=?",
+		new String[] { accoutname, BeemApplication.BEEM_PACKAGE, bookmarkedConference.getJid() }, null);
+	    if (c.getCount() == 0) {
+		ContentValues values = new ContentValues();
+		values.put(MUCs.NAME, bookmarkedConference.getName());
+		values.put(MUCs.JID, bookmarkedConference.getJid());
+		values.put(MUCs.AUTO_JOIN, bookmarkedConference.isAutoJoin());
+		values.put(MUCs.SHARED, bookmarkedConference.isShared());
+		values.put(MUCs.NICKNAME, bookmarkedConference.getNickname());
+		values.put(MUCs.PASSWORD, bookmarkedConference.getPassword());
+		values.put(MUCs.ACCOUNT_NAME, accoutname);
+		values.put(MUCs.ACCOUNT_TYPE, BeemApplication.BEEM_PACKAGE);
+		mContext.getContentResolver().insert(MUCs.CONTENT_URI, values);
+	    } // TODO: Else update
 	}
     }
 
@@ -413,15 +405,17 @@
      */
     private void addVirtualGroup(ArrayList<ContentProviderOperation> ops, Account account, String groupName,
 	String systemID) {
-	ContentProviderOperation.Builder builder = ContentProviderOperation
-	    .newInsert(ContactsContract.Groups.CONTENT_URI);
-	builder.withValue(ContactsContract.Groups.ACCOUNT_NAME, account.name);
-	builder.withValue(ContactsContract.Groups.ACCOUNT_TYPE, account.type);
-	if (systemID != null)
-	    builder.withValue(ContactsContract.Groups.SYSTEM_ID, systemID);
-	builder.withValue(ContactsContract.Groups.TITLE, groupName);
+	if (getRawGroupID(account.name, groupName) == -1) {
+	    ContentProviderOperation.Builder builder = ContentProviderOperation
+		.newInsert(ContactsContract.Groups.CONTENT_URI);
+	    builder.withValue(ContactsContract.Groups.ACCOUNT_NAME, account.name);
+	    builder.withValue(ContactsContract.Groups.ACCOUNT_TYPE, account.type);
+	    if (systemID != null)
+		builder.withValue(ContactsContract.Groups.SYSTEM_ID, systemID);
+	    builder.withValue(ContactsContract.Groups.TITLE, groupName);
 
-	ops.add(builder.build());
+	    ops.add(builder.build());
+	}
     }
 
 }
--- a/src/com/beem/project/beem/account/SyncAdapterService.java	Tue Apr 10 11:32:31 2012 +0200
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Wed Apr 18 15:46:21 2012 +0200
@@ -103,38 +103,37 @@
      */
     public static void performSync(Context context, final Account account, Bundle extras, String authority,
 	ContentProviderClient provider, SyncResult syncResult) throws OperationCanceledException {
-	Log.i(TAG, "performSync: " + account.toString());	
-	Intent intent = new Intent(BeemIntent.ACTION_SYNC );
-	intent.putExtra(BeemIntent.EXTRA_ACCOUNT, account);
-//	IntentFilter filter = new IntentFilter(BeemIntent.ACTION_CONNECTED);
-//	filter.addAction(BeemIntent.ACTION_DISCONNECTED);
-//	registerReceiver(mReceiver, filter);
+	Log.i(TAG, "performSync: " + account.toString());
+	Intent intent = new Intent(BeemIntent.ACTION_SYNC);
+	intent.putExtra(BeemIntent.EXTRA_ACCOUNT, account.name);
+	//	IntentFilter filter = new IntentFilter(BeemIntent.ACTION_CONNECTED);
+	//	filter.addAction(BeemIntent.ACTION_DISCONNECTED);
+	//	registerReceiver(mReceiver, filter);
 	context.startService(intent);
-	
-//	mContentResolver = context.getContentResolver();
+
+	//	mContentResolver = context.getContentResolver();
 
-//
-//	//TODO: Get BeemService connectino support
-//	//TODO: Get resource information
-//	BeemConnection beemco = new BeemConnection(mContext.getSharedPreferences(account.name, MODE_PRIVATE), null);
-//	//if (!BeemService.getIsLaunch())
-//	//beemco.setNoPresence();
-//	XMPPConnection con = new XMPPConnection(beemco.getConnectionConfiguration());
-//	Roster roster = null;
-//	try {
-//	    con.connect();
-//	    //SharedPreferences sp = context.getSharedPreferences(account.name, MODE_PRIVATE);	    
-//	    con.login(beemco.getLogin(), beemco.getPassword(), "beem sync adapter");
-//	    roster = con.getRoster();
-//	} catch (XMPPException e) {
-//	    Log.e(TAG, "Error while connecting with syncAdapter", e);
-//	} catch (IllegalStateException e) {
-//	    Log.e(TAG, "Not connected to server", e);
-//	}
-//	if (roster != null)
-//	    manageRoster(roster, account);
-//	con.disconnect();
+	//
+	//	//TODO: Get BeemService connectino support
+	//	//TODO: Get resource information
+	//	BeemConnection beemco = new BeemConnection(mContext.getSharedPreferences(account.name, MODE_PRIVATE), null);
+	//	//if (!BeemService.getIsLaunch())
+	//	//beemco.setNoPresence();
+	//	XMPPConnection con = new XMPPConnection(beemco.getConnectionConfiguration());
+	//	Roster roster = null;
+	//	try {
+	//	    con.connect();
+	//	    //SharedPreferences sp = context.getSharedPreferences(account.name, MODE_PRIVATE);	    
+	//	    con.login(beemco.getLogin(), beemco.getPassword(), "beem sync adapter");
+	//	    roster = con.getRoster();
+	//	} catch (XMPPException e) {
+	//	    Log.e(TAG, "Error while connecting with syncAdapter", e);
+	//	} catch (IllegalStateException e) {
+	//	    Log.e(TAG, "Not connected to server", e);
+	//	}
+	//	if (roster != null)
+	//	    manageRoster(roster, account);
+	//	con.disconnect();
     }
 
-
 }