ContactList UI - Partial Sync contact with android provider
author"Vincent Veronis"
Fri, 10 Jun 2011 21:04:07 +0200
changeset 895 e43d5abca8b0
parent 894 c36af805487d
child 896 c2c6ee3d3c71
ContactList UI - Partial Sync contact with android provider
src/com/beem/project/beem/ui/ContactList.java
--- a/src/com/beem/project/beem/ui/ContactList.java	Fri Jun 10 20:26:24 2011 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Fri Jun 10 21:04:07 2011 +0200
@@ -122,7 +122,6 @@
 
     private static final String SETTINGS_HIDDEN_CONTACT = "settings_key_hidden_contact";
     private static final String TAG = "ContactList";
-    private final BeemContactList mAdapterContactList = new BeemContactList();
     private final List<String> mListGroup = new ArrayList<String>();
 
     /**
@@ -145,6 +144,7 @@
     private SharedPreferences mSettings;
     private LayoutInflater mInflater;
     private BeemBanner mAdapterBanner;
+    private BeemContactList mAdapterContactList;
     private boolean mBinded;
 
     private String mAccountName;
@@ -292,11 +292,17 @@
 
 	mInflater = getLayoutInflater();
 
-	final Cursor c = getContentResolver().query(ContactsContract.Groups.CONTENT_URI,
+	final Cursor cursorGroup = getContentResolver().query(ContactsContract.Groups.CONTENT_URI,
 	    new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }, null, null, null);
+	mAdapterBanner = new BeemBanner(this, R.layout.contactlist_group, cursorGroup,
+	    new String[] { ContactsContract.Groups.TITLE }, new int[] { R.id.contactlist_group });
 
-	mAdapterBanner = new BeemBanner(this, R.layout.contactlist_group, c,
-	    new String[] { ContactsContract.Groups.TITLE }, new int[] { R.id.contactlist_group });
+	final Cursor cursorContact = getContentResolver().query(
+	    ContactsContract.Contacts.CONTENT_URI,
+	    new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);
+	mAdapterContactList = new BeemContactList(this, R.layout.contactlistcontact, cursorContact, new String[] {
+	    ContactsContract.Contacts.DISPLAY_NAME}, new int[] { R.id.avatar,
+	    R.id.contactlistpseudo, R.id.contactlistmsgperso });
 
 	mListContact = new ArrayList<Contact>();
 	ListView listView = (ListView) findViewById(R.id.contactlist);
@@ -600,86 +606,97 @@
     /**
      * Adapter contact list.
      */
-    private class BeemContactList extends BaseAdapter implements Filterable {
-
-	private final ContactFilter mFilter;
+    private static class BeemContactList extends SimpleCursorAdapter implements Filterable {
 
-	/**
-	 * Constructor.
-	 */
-	public BeemContactList() {
-	    mFilter = new ContactFilter();
-	}
+	private Context mContext;
+	private int mLayout;
 
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public int getCount() {
-	    return mListContact.size();
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public Object getItem(int position) {
-	    return mListContact.get(position);
+	public BeemContactList(Context context, int layout, Cursor c, String[] from, int[] to) {
+	    super(context, layout, c, from, to);
+	    mContext = context;
+	    mLayout = layout;
 	}
 
-	/**
-	 * {@inheritDoc}
-	 */
 	@Override
-	public long getItemId(int position) {
-	    return mListContact.get(position).hashCode();
-	}
+	public View newView(Context context, Cursor cursor, ViewGroup parent) {
+	    Cursor c = getCursor();
+
+	    final LayoutInflater inflater = LayoutInflater.from(mContext);
+	    View v = inflater.inflate(mLayout, parent, false);
+	    String name = c.getString(c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
 
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public View getView(int position, View convertView, ViewGroup parent) {
-	    View v = convertView;
-	    if (convertView == null) {
-		v = mInflater.inflate(R.layout.contactlistcontact, null);
+	    TextView nameText = (TextView) v.findViewById(R.id.contactlistpseudo);
+	    if (nameText != null) {
+		nameText.setText(name);
 	    }
-	    Contact c = mListContact.get(position);
-	    if (mRoster != null) {
-		try {
-		    c = mRoster.getContact(c.getJID());
-		} catch (RemoteException e) {
-		    e.printStackTrace();
-		}
-	    }
-	    bindView(v, c);
+
+//	    String msgStatus = c.getString(c.getColumnIndex(ContactsContract.StatusUpdates.STATUS));
+//
+//	    TextView msgStatusText = (TextView) v.findViewById(R.id.contactlistmsgperso);
+//	    if (msgStatusText != null) {
+//		msgStatusText.setText(msgStatus);
+//	    }
+
 	    return v;
 	}
 
 	@Override
-	public Filter getFilter() {
-	    return mFilter;
+	public void bindView(View view, Context context, Cursor cursor) {
+	    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
+	    TextView nameText = (TextView) view.findViewById(R.id.contactlistpseudo);
+	    if (nameText != null) {
+		nameText.setText(name);
+	    }
+
+//	    String msgStatus = cursor.getString(cursor.getColumnIndex(ContactsContract.StatusUpdates.STATUS));
+//
+//	    TextView msgStatusText = (TextView) view.findViewById(R.id.contactlistmsgperso);
+//	    if (msgStatusText != null) {
+//		msgStatusText.setText(msgStatus);
+//	    }
 	}
 
-	/**
-	 * Adapte curContact to the view.
-	 * @param view the row view.
-	 * @param curContact the current contact.
-	 */
-	private void bindView(View view, Contact curContact) {
-	    if (curContact != null) {
-		TextView v = (TextView) view.findViewById(R.id.contactlistpseudo);
-		v.setText(curContact.getName());
-		v = (TextView) view.findViewById(R.id.contactlistmsgperso);
-		v.setText(curContact.getMsgState());
-		ImageView img = (ImageView) view.findViewById(R.id.avatar);
-		String avatarId = curContact.getAvatarId();
-		int contactStatus = curContact.getStatus();
-		Drawable avatar = getAvatarStatusDrawable(avatarId);
-		img.setImageDrawable(avatar);
-		img.setImageLevel(contactStatus);
-	    }
-	}
+	//	public View getView(int position, View convertView, ViewGroup parent) {
+	//	    View v = convertView;
+	//	    if (convertView == null) {
+	//		v = mInflater.inflate(R.layout.contactlistcontact, null);
+	//	    }
+	//	    Contact c = mListContact.get(position);
+	//	    if (mRoster != null) {
+	//		try {
+	//		    c = mRoster.getContact(c.getJID());
+	//		} catch (RemoteException e) {
+	//		    e.printStackTrace();
+	//		}
+	//	    }
+	//	    bindView(v, c);
+	//	    return v;
+	//	}
+
+	//	@Override
+	//	public Filter getFilter() {
+	//	    return mFilter;
+	//	}
+
+	//	/**
+	//	 * Adapte curContact to the view.
+	//	 * @param view the row view.
+	//	 * @param curContact the current contact.
+	//	 */
+	//	private void bindView(View view, Contact curContact) {
+	//	    if (curContact != null) {
+	//		TextView v = (TextView) view.findViewById(R.id.contactlistpseudo);
+	//		v.setText(curContact.getName());
+	//		v = (TextView) view.findViewById(R.id.contactlistmsgperso);
+	//		v.setText(curContact.getMsgState());
+	//		ImageView img = (ImageView) view.findViewById(R.id.avatar);
+	//		String avatarId = curContact.getAvatarId();
+	//		int contactStatus = curContact.getStatus();
+	//		Drawable avatar = getAvatarStatusDrawable(avatarId);
+	//		img.setImageDrawable(avatar);
+	//		img.setImageLevel(contactStatus);
+	//	    }
+	//	}
 
 	/**
 	 * Get a LayerDrawable containing the avatar and the status icon. The status icon will change with the level of
@@ -687,67 +704,67 @@
 	 * @param avatarId the avatar id to retrieve or null to get default
 	 * @return a LayerDrawable
 	 */
-	private Drawable getAvatarStatusDrawable(String avatarId) {
-	    Drawable avatarDrawable = null;
-	    if (avatarId != null) {
-		Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(avatarId).build();
-		InputStream in = null;
-		try {
-		    try {
-			in = getContentResolver().openInputStream(uri);
-			avatarDrawable = Drawable.createFromStream(in, avatarId);
-		    } finally {
-			if (in != null)
-			    in.close();
-		    }
-		} catch (IOException e) {
-		    Log.w(TAG, "Error while setting the avatar", e);
-		}
-	    }
-	    if (avatarDrawable == null)
-		avatarDrawable = getResources().getDrawable(R.drawable.beem_launcher_icon_silver);
-	    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.avatar_status);
-	    ld.setLayerInset(1, 36, 36, 0, 0);
-	    ld.setDrawableByLayerId(R.id.avatar, avatarDrawable);
-	    return ld;
-	}
+	//	private Drawable getAvatarStatusDrawable(String avatarId) {
+	//	    Drawable avatarDrawable = null;
+	//	    if (avatarId != null) {
+	//		Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(avatarId).build();
+	//		InputStream in = null;
+	//		try {
+	//		    try {
+	//			in = getContentResolver().openInputStream(uri);
+	//			avatarDrawable = Drawable.createFromStream(in, avatarId);
+	//		    } finally {
+	//			if (in != null)
+	//			    in.close();
+	//		    }
+	//		} catch (IOException e) {
+	//		    Log.w(TAG, "Error while setting the avatar", e);
+	//		}
+	//	    }
+	//	    if (avatarDrawable == null)
+	//		avatarDrawable = getResources().getDrawable(R.drawable.beem_launcher_icon_silver);
+	//	    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.avatar_status);
+	//	    ld.setLayerInset(1, 36, 36, 0, 0);
+	//	    ld.setDrawableByLayerId(R.id.avatar, avatarDrawable);
+	//	    return ld;
+	//	}
 
 	/**
 	 * A Filter which select Contact to display by searching in ther Jid.
 	 */
-	private class ContactFilter extends Filter {
-
-	    /**
-	     * Create a ContactFilter.
-	     */
-	    public ContactFilter() {
-	    }
-
-	    @Override
-	    protected Filter.FilterResults performFiltering(CharSequence constraint) {
-		Log.d(TAG, "performFiltering");
-		List<Contact> result = mListContact;
-		if (constraint.length() > 0) {
-		    result = new LinkedList<Contact>();
-		    for (Contact c : mContactOnGroup.get(mSelectedGroup)) {
-			if (c.getJID().contains(constraint))
-			    result.add(c);
-		    }
-		}
-		Filter.FilterResults fr = new Filter.FilterResults();
-		fr.values = result;
-		fr.count = result.size();
-		return fr;
-	    }
-
-	    @Override
-	    protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
-		Log.d(TAG, "publishResults");
-		List<Contact> contacts = (List<Contact>) results.values;
-		mListContact = contacts;
-		notifyDataSetChanged();
-	    }
-	}
+	//	private class ContactFilter extends Filter {
+	//
+	//	    /**
+	//	     * Create a ContactFilter.
+	//	     */
+	//	    public ContactFilter() {
+	//	    }
+	//
+	//	    @Override
+	//	    protected Filter.FilterResults performFiltering(CharSequence constraint) {
+	//		Log.d(TAG, "performFiltering");
+	//		List<Contact> result = mListContact;
+	//		if (constraint.length() > 0) {
+	//		    result = new LinkedList<Contact>();
+	//		    for (Contact c : mContactOnGroup.get(mSelectedGroup)) {
+	//			if (c.getJID().contains(constraint))
+	//			    result.add(c);
+	//		    }
+	//		}
+	//		Filter.FilterResults fr = new Filter.FilterResults();
+	//		fr.values = result;
+	//		fr.count = result.size();
+	//		return fr;
+	//	    }
+	//
+	//	    @Override
+	//	    protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
+	//		Log.d(TAG, "publishResults");
+	//		List<Contact> contacts = (List<Contact>) results.values;
+	//		mListContact = contacts;
+	//		notifyDataSetChanged();
+	//	    }
+	//	}
     }
 
     /**
@@ -952,8 +969,8 @@
 
 	@Override
 	public void onItemClick(AdapterView<?> arg0, View v, int i, long l) {
-//	    String group = mListGroup.get(i);
-//	    buildContactList(group);
+	    //	    String group = mListGroup.get(i);
+	    //	    buildContactList(group);
 	}
     }