--- a/src/com/beem/project/beem/service/Contact.java Mon May 31 00:26:10 2010 +0200
+++ b/src/com/beem/project/beem/service/Contact.java Mon May 31 00:56:26 2010 +0200
@@ -79,20 +79,14 @@
private int mID;
private int mStatus;
- private String mJID;
+ private final String mJID;
private String mSelectedRes;
private String mMsgState;
private List<String> mRes;
- private List<String> mGroups;
+ private final List<String> mGroups = new ArrayList<String>();
private String mName;
/**
- * Constructor.
- */
- public Contact() {
- }
-
- /**
* Construct a contact from a parcel.
* @param in parcel to use for construction
*/
@@ -104,7 +98,6 @@
mName = in.readString();
mMsgState = in.readString();
mRes = new ArrayList<String>();
- mGroups = new ArrayList<String>();
in.readStringList(mRes);
in.readStringList(mGroups);
}
@@ -123,7 +116,6 @@
mSelectedRes = res;
if (!"".equals(res))
mRes.add(res);
- mGroups = new ArrayList<String>();
}
/**
@@ -143,7 +135,6 @@
String res = StringUtils.parseResource(enduri);
mSelectedRes = res;
mRes.add(res);
- mGroups = new ArrayList<String>();
}
/**
@@ -283,7 +274,8 @@
* @param groups the mGroups to set
*/
public void setGroups(List<String> groups) {
- this.mGroups = groups;
+ mGroups.clear();
+ mGroups.addAll(groups);
}
/**
@@ -295,14 +287,6 @@
}
/**
- * Set the Jabber ID of the contact.
- * @param jid the jabber ID to set
- */
- public void setJID(String jid) {
- mJID = jid;
- }
-
- /**
* Set the resource of the contact.
* @param resource to set.
*/
@@ -365,7 +349,6 @@
public void setStatus(PresenceAdapter presence) {
mStatus = presence.getStatus();
mMsgState = presence.getStatusText();
-
}
/**
@@ -423,4 +406,14 @@
build.append('/').append(mSelectedRes);
return build.toString();
}
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Contact) )
+ return false;
+ if (other == this)
+ return true;
+ Contact c = (Contact) other;
+ return c.getJID().equals(getJID());
+ }
}
--- a/src/com/beem/project/beem/ui/ContactList.java Mon May 31 00:26:10 2010 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java Mon May 31 00:56:26 2010 +0200
@@ -43,11 +43,14 @@
package com.beem.project.beem.ui;
import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Iterator;
import org.jivesoftware.smack.util.StringUtils;
@@ -58,8 +61,7 @@
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
-import android.graphics.BitmapFactory;
-import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -100,32 +102,31 @@
*/
public class ContactList extends Activity {
- // private static final String TAG = "CONTACTLIST_ACT";
private static final Intent SERVICE_INTENT = new Intent();
static {
SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
}
+ private static final int REQUEST_CODE = 1;
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 BeemBanner mAdapterBanner = new BeemBanner();
private final List<String> mListGroup = new ArrayList<String>();
private final Map<String, List<Contact>> mContactOnGroup = new HashMap<String, List<Contact>>();
+ private final BeemContactListOnClick mOnContactClick = new BeemContactListOnClick();
private final Handler mHandler = new Handler();
private final ServiceConnection mServConn = new BeemServiceConnection();
private final BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
private final ComparatorContactListByStatusAndName<Contact> mComparator =
new ComparatorContactListByStatusAndName<Contact>();
private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();
- private final Map<Integer, BitmapDrawable> mIconsMap = new HashMap<Integer, BitmapDrawable>();
private List<Contact> mListContact;
private IRoster mRoster;
- private String mCurGroup;
private Contact mSelectedContact;
private IXmppFacade mXmppFacade;
private SharedPreferences mSettings;
private LayoutInflater mInflater;
+ private BeemBanner mAdapterBanner;
private boolean mBinded;
/**
@@ -166,9 +167,6 @@
}
}
- /**
- * {@inheritDoc}
- */
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
@@ -184,9 +182,6 @@
menu.setHeaderTitle(mSelectedContact.getJID());
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean onContextItemSelected(MenuItem item) {
Intent in;
@@ -252,9 +247,6 @@
return super.onContextItemSelected(item);
}
- /**
- * {@inheritDoc}
- */
@Override
protected void onCreate(Bundle saveBundle) {
super.onCreate(saveBundle);
@@ -264,25 +256,19 @@
this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));
mInflater = getLayoutInflater();
- prepareIconsStatus();
+ mAdapterBanner = new BeemBanner(mInflater, mListGroup);
+ mListContact = new ArrayList<Contact>();
+ ListView listView = (ListView) findViewById(R.id.contactlist);
+ listView.setOnItemClickListener(mOnContactClick);
+ registerForContextMenu(listView);
+ listView.setAdapter(mAdapterContactList);
}
- /**
- * {@inheritDoc}
- */
@Override
protected void onResume() {
super.onResume();
- if (!mBinded) {
+ if (!mBinded)
mBinded = bindService(SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
- Log.d(TAG, "on resume bind = " + mBinded);
- } else {
- if (!mSettings.getBoolean("settings_key_hide_groups", false))
- buildBanner();
- else
- hideGroups();
- buildContactList();
- }
}
/**
@@ -306,10 +292,6 @@
mXmppFacade = null;
}
- /*
- * (non-Javadoc)
- * @see android.app.Activity#onDestroy()
- */
@Override
protected void onDestroy() {
super.onDestroy();
@@ -318,38 +300,15 @@
}
/**
- * Comparator Contact by Name.
- */
- @SuppressWarnings("unused")
- private class ComparatorContactListByName<T> implements Comparator<T> {
- /**
- * Constructor.
- */
- public ComparatorContactListByName() {
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int compare(T c1, T c2) {
- return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName());
- }
- }
-
- /**
* Comparator Contact by status and name.
*/
- private class ComparatorContactListByStatusAndName<T> implements Comparator<T> {
+ private static class ComparatorContactListByStatusAndName<T> implements Comparator<T> {
/**
* Constructor.
*/
public ComparatorContactListByStatusAndName() {
}
- /**
- * {@inheritDoc}
- */
@Override
public int compare(T c1, T c2) {
if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) {
@@ -364,30 +323,25 @@
/**
* Contact List construction.
*/
- private void buildContactList() {
- if (mCurGroup != null) {
- mListContact = mContactOnGroup.get(mCurGroup);
- }
+ private void buildContactList(String group) {
+ mListContact = mContactOnGroup.get(group);
+ Log.d(TAG, "buildContactList for group " + group);
sortBeemContactList();
- ListView listView = (ListView) findViewById(R.id.contactlist);
- listView.setOnItemClickListener(new BeemContactListOnClick());
- registerForContextMenu(listView);
- listView.setAdapter(mAdapterContactList);
}
/**
- * buildBanner.
+ * showGroups.
*/
- private void buildBanner() {
+ private void showGroups() {
ViewStub stub = (ViewStub) findViewById(R.id.contactlist_stub);
- if (stub != null)
- stub.inflate();
- else
+ if (stub != null) {
+ View v = stub.inflate();
+ Gallery g = (Gallery) v.findViewById(R.id.contactlist_banner);
+ g.setOnItemClickListener(new OnItemClickGroupName());
+ g.setAdapter(mAdapterBanner);
+ } else
((LinearLayout) findViewById(R.id.contactlist_groupstub)).setVisibility(View.VISIBLE);
- Gallery g = (Gallery) findViewById(R.id.contactlist_banner);
- g.setOnItemClickListener(new OnItemClickGroupName());
- g.setAdapter(mAdapterBanner);
}
/**
@@ -425,34 +379,18 @@
@Override
public void onItemClick(AdapterView<?> arg0, View v, int i, long l) {
- mCurGroup = mListGroup.get(i);
- buildContactList();
+ String group = mListGroup.get(i);
+ buildContactList(group);
}
}
/**
- * Prepare Bitmap Map.
- */
- private void prepareIconsStatus() {
- mIconsMap.put(Status.CONTACT_STATUS_AVAILABLE, new BitmapDrawable(BitmapFactory.decodeResource(getResources(),
- android.R.drawable.presence_online)));
- mIconsMap.put(Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT,
- new BitmapDrawable(BitmapFactory.decodeResource(getResources(), android.R.drawable.presence_online)));
- mIconsMap.put(Status.CONTACT_STATUS_AWAY,
- new BitmapDrawable(BitmapFactory.decodeResource(getResources(), android.R.drawable.presence_away)));
- mIconsMap.put(Status.CONTACT_STATUS_BUSY,
- new BitmapDrawable(BitmapFactory.decodeResource(getResources(), android.R.drawable.presence_busy)));
- mIconsMap.put(Status.CONTACT_STATUS_DISCONNECT, new BitmapDrawable(BitmapFactory.decodeResource(getResources(),
- android.R.drawable.presence_offline)));
- mIconsMap.put(Status.CONTACT_STATUS_UNAVAILABLE, new BitmapDrawable(BitmapFactory.decodeResource(getResources(),
- R.drawable.status_requested)));
- }
-
- /**
* Sort the contact list.
*/
private void sortBeemContactList() {
+ Log.d(TAG, "Sort ");
Collections.sort(mListContact, mComparator);
+ mAdapterContactList.notifyDataSetChanged();
}
/**
@@ -492,21 +430,20 @@
*/
@Override
public void onEntriesAdded(List<String> addresses) throws RemoteException {
+ boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
for (String newName : addresses) {
Contact c = mRoster.getContact(newName);
- mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
- if (c.getGroups().size() == 0)
- mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
- else {
+ if (!hideDisconnected || Status.statusOnline(c.getStatus())) {
+ mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
+ if (c.getGroups().size() == 0)
+ mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
for (String group : c.getGroups()) {
if (!mListGroup.contains(group)) {
mListGroup.add(mListGroup.size() - 1, group);
- List<Contact> tmplist = new ArrayList<Contact>();
+ List<Contact> tmplist = new LinkedList<Contact>();
mContactOnGroup.put(group, tmplist);
}
mContactOnGroup.get(group).add(c);
- if (group.equals(mCurGroup) && !mListContact.contains(c))
- mListContact.add(c);
}
}
}
@@ -518,20 +455,13 @@
*/
@Override
public void onEntriesDeleted(List<String> addresses) throws RemoteException {
+ Log.d(TAG, "onEntries deleted " + addresses);
for (String cToDelete : addresses) {
- for (Contact c : mListContact) {
- if (c.getJID().equals(cToDelete)) {
- for (String group : mListGroup) {
- mContactOnGroup.get(group).remove(c);
- if (mContactOnGroup.get(group).size() == 0) {
- //mContactOnGroup.remove(group);
- //mListGroup.remove(group);
- }
- }
- mListContact.remove(c);
- break;
- }
+ Contact contact = mRoster.getContact(cToDelete);
+ for (List<Contact> contactByGroups : mContactOnGroup.values()) {
+ contactByGroups.remove(contact);
}
+ cleanBannerGroup();
}
mListContact = mContactOnGroup.get(getString(R.string.contact_list_all_contact));
mHandler.post(new RunnableChange());
@@ -542,44 +472,67 @@
*/
@Override
public void onEntriesUpdated(List<String> addresses) throws RemoteException {
+ boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
for (String adr : addresses) {
Contact c = mRoster.getContact(adr);
- if (c.getGroups() != null) {
- if (mContactOnGroup.get(getString(R.string.contact_list_no_group)).contains(c)) {
- mContactOnGroup.get(getString(R.string.contact_list_no_group)).remove(c);
+ List<String> groups = c.getGroups();
+ for (Map.Entry<String, List<Contact>> entry : mContactOnGroup.entrySet()) {
+ List<Contact> contactByGroups = entry.getValue();
+ contactByGroups.remove(c);
+ }
+ if (!hideDisconnected || Status.statusOnline(c.getStatus())) {
+ mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
+ if (c.getGroups().size() == 0)
+ mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
+ for (String group : c.getGroups()) {
+ if (!mListGroup.contains(group)) {
+ mListGroup.add(mListGroup.size() - 1, group);
+ List<Contact> tmplist = new LinkedList<Contact>();
+ mContactOnGroup.put(group, tmplist);
+ }
+ mContactOnGroup.get(group).add(c);
}
}
}
+ cleanBannerGroup();
mHandler.post(new RunnableChange());
}
- /**
- * {@inheritDoc}
- */
@Override
public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
String from = presence.getFrom();
- boolean resfound = false;
- for (Contact curContact : mListContact) {
- if (curContact.getJID().equals(StringUtils.parseBareAddress(from))) {
- String pres = StringUtils.parseResource(from);
- for (String res : curContact.getMRes()) {
- if (res.equals(pres)) {
- resfound = true;
- break;
+ boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
+ Contact contact = mRoster.getContact(StringUtils.parseBareAddress(from));
+ for (Map.Entry<String, List<Contact>> entry : mContactOnGroup.entrySet()) {
+ List<Contact> contactByGroups = entry.getValue();
+ if (contactByGroups.contains(contact)) {
+ contactByGroups.remove(contact);
+ if (!hideDisconnected || Status.statusOnline(contact.getStatus())) {
+ contactByGroups.add(contact);
+ }
+ } else {
+ if (Status.statusOnline(contact.getStatus())) {
+ List<String> groups = contact.getGroups();
+ if (groups.contains(entry.getKey())) {
+ contactByGroups.add(contact);
}
}
- curContact.setStatus(mRoster.getPresence(StringUtils.parseBareAddress(presence.getFrom())));
- int status = presence.getStatus();
- if (!resfound && !Status.statusOnline(status))
- curContact.addRes(pres);
- else if (resfound && Status.statusOnline(status))
- curContact.delRes(pres);
- mHandler.post(new RunnableChange());
- return;
+ }
+ }
+ Log.d(TAG, "presence");
+ mHandler.post(new RunnableChange());
+ }
+
+ private void cleanBannerGroup() {
+ for (Iterator<String> it = mListGroup.iterator(); it.hasNext(); ){
+ String group = it.next();
+ if (mContactOnGroup.get(group).size() == 0) {
+ mContactOnGroup.remove(group);
+ it.remove();
}
}
}
+
}
/**
@@ -598,16 +551,7 @@
*/
@Override
public int getCount() {
- if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
- int res = 0;
- for (Contact c : mListContact) {
- if (Status.statusOnline(c.getStatus()))
- res++;
- }
- return res;
- } else {
- return mListContact.size();
- }
+ return mListContact.size();
}
/**
@@ -615,20 +559,7 @@
*/
@Override
public Object getItem(int position) {
- Contact c = null;
- if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
- int res = 0;
- for (Contact cur : mListContact) {
- if (res == position && Status.statusOnline(cur.getStatus())) {
- c = cur;
- break;
- }
- if (Status.statusOnline(cur.getStatus()))
- res++;
- }
- } else
- c = mListContact.get(position);
- return c;
+ return mListContact.get(position);
}
/**
@@ -648,19 +579,7 @@
if (convertView == null) {
v = mInflater.inflate(R.layout.contactlistcontact, null);
}
- Contact c = null;
- if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
- int res = 0;
- for (Contact cur : mListContact) {
- if (res == position && Status.statusOnline(cur.getStatus())) {
- c = cur;
- break;
- }
- if (Status.statusOnline(cur.getStatus()))
- res++;
- }
- } else
- c = mListContact.get(position);
+ Contact c = mListContact.get(position);
if (mRoster != null) {
try {
c = mRoster.getContact(c.getJID());
@@ -668,11 +587,7 @@
e.printStackTrace();
}
}
- if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false) && !Status.statusOnline(c.getStatus())) {
- v.setVisibility(View.GONE);
- } else {
- bindView(v, c);
- }
+ bindView(v, c);
return v;
}
@@ -685,7 +600,9 @@
if (curContact != null) {
TextView v = (TextView) view.findViewById(R.id.contactlistpseudo);
- v.setCompoundDrawablesWithIntrinsicBounds(mIconsMap.get(curContact.getStatus()), null, null, null);
+ LevelListDrawable mStatusDrawable = (LevelListDrawable) getResources().getDrawable(R.drawable.status_icon);
+ mStatusDrawable.setLevel(curContact.getStatus());
+ v.setCompoundDrawablesWithIntrinsicBounds(mStatusDrawable, null, null, null);
v.setText(curContact.getName());
v = (TextView) view.findViewById(R.id.contactlistmsgperso);
v.setText(curContact.getMsgState());
@@ -696,57 +613,45 @@
/**
* Adapter banner list.
*/
- private class BeemBanner extends BaseAdapter {
+ private static class BeemBanner extends BaseAdapter {
+ private List<String> mGroups;
+ private LayoutInflater mInflater;
+
/**
* Constructor.
*/
- public BeemBanner() {
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int getCount() {
- return mListGroup.size();
+ public BeemBanner(LayoutInflater inflater, List<String> groups) {
+ mGroups = groups;
+ mInflater = inflater;
}
- /**
- * {@inheritDoc}
- */
+ @Override
+ public int getCount() {
+ return mGroups.size();
+ }
+
@Override
public Object getItem(int position) {
- return mListGroup.get(position);
+ return mGroups.get(position);
}
- /**
- * {@inheritDoc}
- */
@Override
public long getItemId(int position) {
return position;
}
- /**
- * {@inheritDoc}
- */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
v = mInflater.inflate(R.layout.contactlist_group, null);
}
- ((TextView) v).setText(mListGroup.get(position));
+ ((TextView) v).setText(mGroups.get(position));
return v;
}
-
}
/**
- * Create dialog resend subscription.
- */
-
- /**
* The service connection used to connect to the Beem service.
*/
private class BeemServiceConnection implements ServiceConnection {
@@ -772,30 +677,13 @@
mListGroup.add(getString(R.string.contact_list_all_contact));
mListGroup.addAll(tmpGroupList);
mListGroup.add(getString(R.string.contact_list_no_group));
- mContactOnGroup.clear();
- if (tmpGroupList.size() > 0) {
- List<Contact> tmpNoGroup = new ArrayList<Contact>();
- for (String s : tmpGroupList) {
- List<Contact> tmpList = new ArrayList<Contact>();
- for (Contact c : tmpContactList) {
- if (c.getGroups().size() == 0 && !tmpNoGroup.contains(c))
- tmpNoGroup.add(c);
- else if (c.getGroups().contains(s))
- tmpList.add(c);
- }
- mContactOnGroup.put(s, tmpList);
- }
- mContactOnGroup.put(getString(R.string.contact_list_no_group), tmpNoGroup);
- } else {
- mContactOnGroup.put(getString(R.string.contact_list_no_group), tmpContactList);
- }
- mContactOnGroup.put(getString(R.string.contact_list_all_contact), tmpContactList);
- mCurGroup = getString(R.string.contact_list_all_contact);
+ assignContactToGroups(mRoster.getContactList(), tmpGroupList);
if (!mSettings.getBoolean("settings_key_hide_groups", false))
- buildBanner();
+ showGroups();
else
hideGroups();
- buildContactList();
+ String group = getString(R.string.contact_list_all_contact);
+ buildContactList(group);
}
} catch (RemoteException e) {
e.printStackTrace();
@@ -814,6 +702,7 @@
mListContact.clear();
mListGroup.clear();
mContactOnGroup.clear();
+ mBinded = false;
}
}
@@ -826,8 +715,35 @@
* Hide the groups view.
*/
private void hideGroups() {
- ViewStub stub = (ViewStub) findViewById(R.id.contactlist_stub);
- if (stub == null)
- ((LinearLayout) findViewById(R.id.contactlist_groupstub)).setVisibility(View.GONE);
+ View v = findViewById(R.id.contactlist_groupstub);
+ if (v != null)
+ v.setVisibility(View.GONE);
+ }
+
+ private void assignContactToGroups(List<Contact> contacts, List<String> groupNames) {
+ boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
+ mContactOnGroup.clear();
+ List<Contact> all = new LinkedList<Contact>();
+ List<Contact> noGroups = new LinkedList<Contact>();
+ for (String group: groupNames) {
+ mContactOnGroup.put(group, new LinkedList<Contact>());
+ }
+ for (Contact c : contacts) {
+ if (hideDisconnected && !Status.statusOnline(c.getStatus())) {
+ continue;
+ }
+ all.add(c);
+ List<String> groups = c.getGroups();
+ if (groups.size() == 0)
+ noGroups.add(c);
+ else {
+ for (String currentGroup : groups) {
+ List<Contact> contactsByGroups = mContactOnGroup.get(currentGroup);
+ contactsByGroups.add(c);
+ }
+ }
+ }
+ mContactOnGroup.put(getString(R.string.contact_list_no_group), noGroups);
+ mContactOnGroup.put(getString(R.string.contact_list_all_contact), all);
}
}