src/com/beem/project/beem/ui/ContactList.java
changeset 757 1a5cb28a1c4d
parent 754 151841f2b1a1
child 758 565942fdf426
equal deleted inserted replaced
756:c86eb188de1c 757:1a5cb28a1c4d
    41 
    41 
    42 */
    42 */
    43 package com.beem.project.beem.ui;
    43 package com.beem.project.beem.ui;
    44 
    44 
    45 import java.util.ArrayList;
    45 import java.util.ArrayList;
       
    46 import java.util.LinkedList;
    46 import java.util.Collections;
    47 import java.util.Collections;
    47 import java.util.Comparator;
    48 import java.util.Comparator;
    48 import java.util.HashMap;
    49 import java.util.HashMap;
    49 import java.util.List;
    50 import java.util.List;
    50 import java.util.Map;
    51 import java.util.Map;
       
    52 import java.util.Map.Entry;
       
    53 import java.util.Set;
       
    54 import java.util.Iterator;
    51 
    55 
    52 import org.jivesoftware.smack.util.StringUtils;
    56 import org.jivesoftware.smack.util.StringUtils;
    53 
    57 
    54 import android.app.Activity;
    58 import android.app.Activity;
    55 import android.app.Dialog;
    59 import android.app.Dialog;
    56 import android.content.ComponentName;
    60 import android.content.ComponentName;
    57 import android.content.Intent;
    61 import android.content.Intent;
    58 import android.content.IntentFilter;
    62 import android.content.IntentFilter;
    59 import android.content.ServiceConnection;
    63 import android.content.ServiceConnection;
    60 import android.content.SharedPreferences;
    64 import android.content.SharedPreferences;
       
    65 import android.content.Context;
    61 import android.graphics.drawable.LevelListDrawable;
    66 import android.graphics.drawable.LevelListDrawable;
    62 import android.os.Bundle;
    67 import android.os.Bundle;
    63 import android.os.Handler;
    68 import android.os.Handler;
    64 import android.os.IBinder;
    69 import android.os.IBinder;
    65 import android.os.RemoteException;
    70 import android.os.RemoteException;
    97 /**
   102 /**
    98  * The contact list activity displays the roster of the user.
   103  * The contact list activity displays the roster of the user.
    99  */
   104  */
   100 public class ContactList extends Activity {
   105 public class ContactList extends Activity {
   101 
   106 
   102     // private static final String TAG = "CONTACTLIST_ACT";
       
   103     private static final Intent SERVICE_INTENT = new Intent();
   107     private static final Intent SERVICE_INTENT = new Intent();
   104     static {
   108     static {
   105 	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
   109 	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
   106     }
   110     }
   107 
   111 
   108     private static final int REQUEST_CODE = 1;
   112     private static final int REQUEST_CODE = 1;
   109     private static final String SETTINGS_HIDDEN_CONTACT = "settings_key_hidden_contact";
   113     private static final String SETTINGS_HIDDEN_CONTACT = "settings_key_hidden_contact";
   110     private static final String TAG = "ContactList";
   114     private static final String TAG = "ContactList";
   111     private final BeemContactList mAdapterContactList = new BeemContactList();
   115     private final BeemContactList mAdapterContactList = new BeemContactList();
   112     private final BeemBanner mAdapterBanner = new BeemBanner();
       
   113     private final List<String> mListGroup = new ArrayList<String>();
   116     private final List<String> mListGroup = new ArrayList<String>();
   114     private final Map<String, List<Contact>> mContactOnGroup = new HashMap<String, List<Contact>>();
   117     private final Map<String, List<Contact>> mContactOnGroup = new HashMap<String, List<Contact>>();
       
   118     private final BeemContactListOnClick mOnContactClick = new BeemContactListOnClick();
   115     private final Handler mHandler = new Handler();
   119     private final Handler mHandler = new Handler();
   116     private final ServiceConnection mServConn = new BeemServiceConnection();
   120     private final ServiceConnection mServConn = new BeemServiceConnection();
   117     private final BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
   121     private final BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
   118     private final ComparatorContactListByStatusAndName<Contact> mComparator =
   122     private final ComparatorContactListByStatusAndName<Contact> mComparator =
   119 	new ComparatorContactListByStatusAndName<Contact>();
   123 	new ComparatorContactListByStatusAndName<Contact>();
   120     private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();
   124     private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();
   121     private List<Contact> mListContact;
   125     private List<Contact> mListContact;
   122     private IRoster mRoster;
   126     private IRoster mRoster;
   123     private String mCurGroup;
       
   124     private Contact mContact;
   127     private Contact mContact;
   125     private IXmppFacade mXmppFacade;
   128     private IXmppFacade mXmppFacade;
   126     private SharedPreferences mSettings;
   129     private SharedPreferences mSettings;
   127     private LayoutInflater mInflater;
   130     private LayoutInflater mInflater;
       
   131     private BeemBanner mAdapterBanner;
       
   132     private boolean mBinded;
   128 
   133 
   129     /**
   134     /**
   130      * Constructor.
   135      * Constructor.
   131      */
   136      */
   132     public ContactList() {
   137     public ContactList() {
   162 	    default:
   167 	    default:
   163 		return false;
   168 		return false;
   164 	}
   169 	}
   165     }
   170     }
   166 
   171 
   167     /**
       
   168      * {@inheritDoc}
       
   169      */
       
   170     @Override
   172     @Override
   171     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   173     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   172 	super.onCreateContextMenu(menu, v, menuInfo);
   174 	super.onCreateContextMenu(menu, v, menuInfo);
   173 	MenuInflater inflater = getMenuInflater();
   175 	MenuInflater inflater = getMenuInflater();
   174 	inflater.inflate(R.menu.contactlist_context, menu);
   176 	inflater.inflate(R.menu.contactlist_context, menu);
   180 	    e.printStackTrace();
   182 	    e.printStackTrace();
   181 	}
   183 	}
   182 	menu.setHeaderTitle(mContact.getJID());
   184 	menu.setHeaderTitle(mContact.getJID());
   183     }
   185     }
   184 
   186 
   185     /**
       
   186      * {@inheritDoc}
       
   187      */
       
   188     @Override
   187     @Override
   189     public boolean onContextItemSelected(MenuItem item) {
   188     public boolean onContextItemSelected(MenuItem item) {
   190 	Intent in;
   189 	Intent in;
   191 	boolean result;
   190 	boolean result;
   192 	if (mContact != null) {
   191 	if (mContact != null) {
   248 	    return result;
   247 	    return result;
   249 	}
   248 	}
   250 	return super.onContextItemSelected(item);
   249 	return super.onContextItemSelected(item);
   251     }
   250     }
   252 
   251 
   253     /*
       
   254      * (non-Javadoc)
       
   255      * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
       
   256      */
       
   257     @Override
   252     @Override
   258     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   253     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   259 	super.onActivityResult(requestCode, resultCode, data);
   254 	super.onActivityResult(requestCode, resultCode, data);
   260 	if (requestCode == REQUEST_CODE) {
   255 	if (requestCode == REQUEST_CODE) {
   261 	    if (resultCode == RESULT_OK) {
   256 	    if (resultCode == RESULT_OK) {
   264 		finish();
   259 		finish();
   265 	    }
   260 	    }
   266 	}
   261 	}
   267     }
   262     }
   268 
   263 
   269     /**
       
   270      * {@inheritDoc}
       
   271      */
       
   272     @Override
   264     @Override
   273     protected void onCreate(Bundle saveBundle) {
   265     protected void onCreate(Bundle saveBundle) {
   274 	super.onCreate(saveBundle);
   266 	super.onCreate(saveBundle);
   275 	mSettings = PreferenceManager.getDefaultSharedPreferences(this);
   267 	mSettings = PreferenceManager.getDefaultSharedPreferences(this);
   276 	setContentView(R.layout.contactlist);
   268 	setContentView(R.layout.contactlist);
   277 
   269 
   278 	this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));
   270 	this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));
   279 
   271 
   280 	mInflater = getLayoutInflater();
   272 	mInflater = getLayoutInflater();
   281     }
   273 	mAdapterBanner = new BeemBanner(mInflater, mListGroup);
   282 
   274 	mListContact = new ArrayList<Contact>();
   283     /**
   275 	ListView listView = (ListView) findViewById(R.id.contactlist);
   284      * {@inheritDoc}
   276 	listView.setOnItemClickListener(mOnContactClick);
   285      */
   277 	registerForContextMenu(listView);
       
   278 	listView.setAdapter(mAdapterContactList);
       
   279     }
       
   280 
   286     @Override
   281     @Override
   287     protected void onResume() {
   282     protected void onResume() {
   288 	super.onResume();
   283 	super.onResume();
   289 	if (mXmppFacade == null)
   284 	if (!mBinded)
   290 	    bindService(SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
   285 	    mBinded = bindService(SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
   291 	else {
       
   292 	    if (!mSettings.getBoolean("settings_key_hide_groups", false))
       
   293 		buildBanner();
       
   294 	    else
       
   295 		hideGroups();
       
   296 	    buildContactList();
       
   297 	}
       
   298     }
   286     }
   299 
   287 
   300     /**
   288     /**
   301      * {@inheritDoc}
   289      * {@inheritDoc}
   302      */
   290      */
   310 	    }
   298 	    }
   311 	} catch (RemoteException e) {
   299 	} catch (RemoteException e) {
   312 	    Log.d("ContactList", "Remote exception", e);
   300 	    Log.d("ContactList", "Remote exception", e);
   313 	}
   301 	}
   314 	unbindService(mServConn);
   302 	unbindService(mServConn);
       
   303 	mBinded = false;
   315 	mXmppFacade = null;
   304 	mXmppFacade = null;
   316     }
   305     }
   317 
   306 
   318     /*
       
   319      * (non-Javadoc)
       
   320      * @see android.app.Activity#onDestroy()
       
   321      */
       
   322     @Override
   307     @Override
   323     protected void onDestroy() {
   308     protected void onDestroy() {
   324 	super.onDestroy();
   309 	super.onDestroy();
   325 	this.unregisterReceiver(mReceiver);
   310 	this.unregisterReceiver(mReceiver);
   326 	Log.e(TAG, "onDestroy activity");
   311 	Log.e(TAG, "onDestroy activity");
   327     }
   312     }
   328 
   313 
   329     /**
   314     /**
   330      * Comparator Contact by Name.
   315      * Comparator Contact by status and name.
   331      */
   316      */
   332     @SuppressWarnings("unused")
   317     private static class ComparatorContactListByStatusAndName<T> implements Comparator<T> {
   333     private class ComparatorContactListByName<T> implements Comparator<T> {
       
   334 	/**
   318 	/**
   335 	 * Constructor.
   319 	 * Constructor.
   336 	 */
   320 	 */
   337 	public ComparatorContactListByName() {
       
   338 	}
       
   339 
       
   340 	/**
       
   341 	 * {@inheritDoc}
       
   342 	 */
       
   343 	@Override
       
   344 	public int compare(T c1, T c2) {
       
   345 	    return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName());
       
   346 	}
       
   347     }
       
   348 
       
   349     /**
       
   350      * Comparator Contact by status and name.
       
   351      */
       
   352     private class ComparatorContactListByStatusAndName<T> implements Comparator<T> {
       
   353 	/**
       
   354 	 * Constructor.
       
   355 	 */
       
   356 	public ComparatorContactListByStatusAndName() {
   321 	public ComparatorContactListByStatusAndName() {
   357 	}
   322 	}
   358 
   323 
   359 	/**
       
   360 	 * {@inheritDoc}
       
   361 	 */
       
   362 	@Override
   324 	@Override
   363 	public int compare(T c1, T c2) {
   325 	public int compare(T c1, T c2) {
   364 	    if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) {
   326 	    if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) {
   365 		return 1;
   327 		return 1;
   366 	    } else if (((Contact) c1).getStatus() > ((Contact) c2).getStatus()) {
   328 	    } else if (((Contact) c1).getStatus() > ((Contact) c2).getStatus()) {
   371     }
   333     }
   372 
   334 
   373     /**
   335     /**
   374      * Contact List construction.
   336      * Contact List construction.
   375      */
   337      */
   376     private void buildContactList() {
   338     private void buildContactList(String group) {
   377 	if (mCurGroup != null) {
   339 	mListContact = mContactOnGroup.get(group);
   378 	    mListContact = mContactOnGroup.get(mCurGroup);
   340 	Log.d(TAG, "buildContactList for group " + group);
   379 	}
       
   380 	sortBeemContactList();
   341 	sortBeemContactList();
   381 	ListView listView = (ListView) findViewById(R.id.contactlist);
   342     }
   382 	listView.setOnItemClickListener(new BeemContactListOnClick());
   343 
   383 	registerForContextMenu(listView);
   344     /**
   384 	listView.setAdapter(mAdapterContactList);
   345      * showGroups.
   385     }
   346      */
   386 
   347     private void showGroups() {
   387     /**
       
   388      * buildBanner.
       
   389      */
       
   390     private void buildBanner() {
       
   391 
   348 
   392 	ViewStub stub = (ViewStub) findViewById(R.id.contactlist_stub);
   349 	ViewStub stub = (ViewStub) findViewById(R.id.contactlist_stub);
   393 	if (stub != null)
   350 	if (stub != null) {
   394 	    stub.inflate();
   351 	    View v = stub.inflate();
   395 	else
   352 	    Gallery g = (Gallery) v.findViewById(R.id.contactlist_banner);
       
   353 	    g.setOnItemClickListener(new OnItemClickGroupName());
       
   354 	    g.setAdapter(mAdapterBanner);
       
   355 	} else
   396 	    ((LinearLayout) findViewById(R.id.contactlist_groupstub)).setVisibility(View.VISIBLE);
   356 	    ((LinearLayout) findViewById(R.id.contactlist_groupstub)).setVisibility(View.VISIBLE);
   397 	Gallery g = (Gallery) findViewById(R.id.contactlist_banner);
       
   398 	g.setOnItemClickListener(new OnItemClickGroupName());
       
   399 	g.setAdapter(mAdapterBanner);
       
   400     }
   357     }
   401 
   358 
   402     /**
   359     /**
   403      * Event simple click on item of the contact list.
   360      * Event simple click on item of the contact list.
   404      */
   361      */
   432 	public OnItemClickGroupName() {
   389 	public OnItemClickGroupName() {
   433 	}
   390 	}
   434 
   391 
   435 	@Override
   392 	@Override
   436 	public void onItemClick(AdapterView<?> arg0, View v, int i, long l) {
   393 	public void onItemClick(AdapterView<?> arg0, View v, int i, long l) {
   437 	    mCurGroup = mListGroup.get(i);
   394 	    String group = mListGroup.get(i);
   438 	    buildContactList();
   395 	    buildContactList(group);
   439 	}
   396 	}
   440     }
   397     }
   441 
   398 
   442     /**
   399     /**
   443      * Sort the contact list.
   400      * Sort the contact list.
   444      */
   401      */
   445     private void sortBeemContactList() {
   402     private void sortBeemContactList() {
       
   403 	Log.d(TAG, "Sort ");
   446 	Collections.sort(mListContact, mComparator);
   404 	Collections.sort(mListContact, mComparator);
       
   405 	mAdapterContactList.notifyDataSetChanged();
   447     }
   406     }
   448 
   407 
   449     /**
   408     /**
   450      * Listener on service event.
   409      * Listener on service event.
   451      */
   410      */
   481 	/**
   440 	/**
   482 	 * {@inheritDoc}
   441 	 * {@inheritDoc}
   483 	 */
   442 	 */
   484 	@Override
   443 	@Override
   485 	public void onEntriesAdded(List<String> addresses) throws RemoteException {
   444 	public void onEntriesAdded(List<String> addresses) throws RemoteException {
       
   445 	    boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
   486 	    for (String newName : addresses) {
   446 	    for (String newName : addresses) {
   487 		Contact c = mRoster.getContact(newName);
   447 		Contact c = mRoster.getContact(newName);
   488 		mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
   448 		if (!hideDisconnected || Status.statusOnline(c.getStatus())) {
   489 		if (c.getGroups().size() == 0)
   449 		    mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
   490 		    mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
   450 		    if (c.getGroups().size() == 0)
   491 		else {
   451 			mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
   492 		    for (String group : c.getGroups()) {
   452 		    for (String group : c.getGroups()) {
   493 			if (!mListGroup.contains(group)) {
   453 			if (!mListGroup.contains(group)) {
   494 			    mListGroup.add(mListGroup.size() - 1, group);
   454 			    mListGroup.add(mListGroup.size() - 1, group);
   495 			    List<Contact> tmplist = new ArrayList<Contact>();
   455 			    List<Contact> tmplist = new LinkedList<Contact>();
   496 			    mContactOnGroup.put(group, tmplist);
   456 			    mContactOnGroup.put(group, tmplist);
   497 			}
   457 			}
   498 			mContactOnGroup.get(group).add(c);
   458 			mContactOnGroup.get(group).add(c);
   499 			if (group.equals(mCurGroup) && !mListContact.contains(c))
       
   500 			    mListContact.add(c);
       
   501 		    }
   459 		    }
   502 		}
   460 		}
   503 	    }
   461 	    }
   504 	    mHandler.post(new RunnableChange());
   462 	    mHandler.post(new RunnableChange());
   505 	}
   463 	}
   507 	/**
   465 	/**
   508 	 * {@inheritDoc}
   466 	 * {@inheritDoc}
   509 	 */
   467 	 */
   510 	@Override
   468 	@Override
   511 	public void onEntriesDeleted(List<String> addresses) throws RemoteException {
   469 	public void onEntriesDeleted(List<String> addresses) throws RemoteException {
       
   470 	    Log.d(TAG, "onEntries deleted " + addresses);
   512 	    for (String cToDelete : addresses) {
   471 	    for (String cToDelete : addresses) {
   513 		for (Contact c : mListContact) {
   472 		Contact contact = mRoster.getContact(cToDelete);
   514 		    if (c.getJID().equals(cToDelete)) {
   473 		for (List<Contact> contactByGroups : mContactOnGroup.values()) {
   515 			for (String group : mListGroup) {
   474 		    contactByGroups.remove(contact);
   516 			    mContactOnGroup.get(group).remove(c);
   475 		}
   517 			    if (mContactOnGroup.get(group).size() == 0) {
   476 		cleanBannerGroup();
   518 				//mContactOnGroup.remove(group);
       
   519 				//mListGroup.remove(group);
       
   520 			    }
       
   521 			}
       
   522 			mListContact.remove(c);
       
   523 			break;
       
   524 		    }
       
   525 		}
       
   526 	    }
   477 	    }
   527 	    mListContact = mContactOnGroup.get(getString(R.string.contact_list_all_contact));
   478 	    mListContact = mContactOnGroup.get(getString(R.string.contact_list_all_contact));
   528 	    mHandler.post(new RunnableChange());
   479 	    mHandler.post(new RunnableChange());
   529 	}
   480 	}
   530 
   481 
   531 	/**
   482 	/**
   532 	 * {@inheritDoc}
   483 	 * {@inheritDoc}
   533 	 */
   484 	 */
   534 	@Override
   485 	@Override
   535 	public void onEntriesUpdated(List<String> addresses) throws RemoteException {
   486 	public void onEntriesUpdated(List<String> addresses) throws RemoteException {
       
   487 	    boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
   536 	    for (String adr : addresses) {
   488 	    for (String adr : addresses) {
   537 		Contact c = mRoster.getContact(adr);
   489 		Contact c = mRoster.getContact(adr);
   538 		if (c.getGroups() != null) {
   490 		List<String> groups = c.getGroups();
   539 		    if (mContactOnGroup.get(getString(R.string.contact_list_no_group)).contains(c)) {
   491 		for (Map.Entry<String, List<Contact>> entry : mContactOnGroup.entrySet()) {
   540 			mContactOnGroup.get(getString(R.string.contact_list_no_group)).remove(c);
   492 		    List<Contact> contactByGroups = entry.getValue();
       
   493 		    contactByGroups.remove(c);
       
   494 		}
       
   495 		if (!hideDisconnected || Status.statusOnline(c.getStatus())) {
       
   496 		    mContactOnGroup.get(getString(R.string.contact_list_all_contact)).add(c);
       
   497 		    if (c.getGroups().size() == 0)
       
   498 			mContactOnGroup.get(getString(R.string.contact_list_no_group)).add(c);
       
   499 		    for (String group : c.getGroups()) {
       
   500 			if (!mListGroup.contains(group)) {
       
   501 			    mListGroup.add(mListGroup.size() - 1, group);
       
   502 			    List<Contact> tmplist = new LinkedList<Contact>();
       
   503 			    mContactOnGroup.put(group, tmplist);
       
   504 			}
       
   505 			mContactOnGroup.get(group).add(c);
   541 		    }
   506 		    }
   542 		}
   507 		}
   543 	    }
   508 	    }
       
   509 	    cleanBannerGroup();
   544 	    mHandler.post(new RunnableChange());
   510 	    mHandler.post(new RunnableChange());
   545 	}
   511 	}
   546 
   512 
   547 	/**
       
   548 	 * {@inheritDoc}
       
   549 	 */
       
   550 	@Override
   513 	@Override
   551 	public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
   514 	public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
   552 	    String from = presence.getFrom();
   515 	    String from = presence.getFrom();
   553 	    boolean resfound = false;
   516 	    boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
   554 	    int foundIdx = 0;
       
   555 	    boolean found = false;
       
   556 	    Contact contact = mRoster.getContact(StringUtils.parseBareAddress(from));
   517 	    Contact contact = mRoster.getContact(StringUtils.parseBareAddress(from));
   557 	    for (int i = 0; i < mListContact.size(); i++) {
   518 	    for (Map.Entry<String, List<Contact>> entry : mContactOnGroup.entrySet()) {
   558 		Contact curContact = mListContact.get(i);
   519 		List<Contact> contactByGroups = entry.getValue();
   559 		if (curContact.getJID().equals(contact.getJID())) {
   520 		if (contactByGroups.contains(contact)) {
   560 		    found = true;
   521 		    contactByGroups.remove(contact);
   561 		    foundIdx = i;
   522 		    if (!hideDisconnected || Status.statusOnline(contact.getStatus())) {
   562 		    break;
   523 			contactByGroups.add(contact);
   563 		}
   524 		    }
   564 	    }
   525 		} else {
   565 	    if (found){
   526 		    if (Status.statusOnline(contact.getStatus())) {
   566 		mListContact.set(foundIdx, contact);
   527 			List<String> groups = contact.getGroups();
   567 	    } else {
   528 			if (groups.contains(entry.getKey())) {
   568 		mListContact.add(contact);
   529 			    contactByGroups.add(contact);
   569 	    }
   530 			}
       
   531 		    }
       
   532 		}
       
   533 	    }
       
   534 	    Log.d(TAG, "presence");
   570 	    mHandler.post(new RunnableChange());
   535 	    mHandler.post(new RunnableChange());
   571 	}
   536 	}
       
   537 
       
   538 	private void cleanBannerGroup() {
       
   539 	    for (Iterator<String> it = mListGroup.iterator(); it.hasNext(); ){
       
   540 		String group = it.next();
       
   541 		if (mContactOnGroup.get(group).size() == 0) {
       
   542 		    mContactOnGroup.remove(group);
       
   543 		    it.remove();
       
   544 		}
       
   545 	    }
       
   546 	}
       
   547 
   572     }
   548     }
   573 
   549 
   574     /**
   550     /**
   575      * Adapter contact list.
   551      * Adapter contact list.
   576      */
   552      */
   585 	/**
   561 	/**
   586 	 * {@inheritDoc}
   562 	 * {@inheritDoc}
   587 	 */
   563 	 */
   588 	@Override
   564 	@Override
   589 	public int getCount() {
   565 	public int getCount() {
   590 	    if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
   566 	    return mListContact.size();
   591 		int res = 0;
       
   592 		for (Contact c : mListContact) {
       
   593 		    if (Status.statusOnline(c.getStatus()))
       
   594 			res++;
       
   595 		}
       
   596 		return res;
       
   597 	    } else {
       
   598 		return mListContact.size();
       
   599 	    }
       
   600 	}
   567 	}
   601 
   568 
   602 	/**
   569 	/**
   603 	 * {@inheritDoc}
   570 	 * {@inheritDoc}
   604 	 */
   571 	 */
   605 	@Override
   572 	@Override
   606 	public Object getItem(int position) {
   573 	public Object getItem(int position) {
   607 	    Contact c = null;
   574 	    return mListContact.get(position);
   608 	    if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
       
   609 		int res = 0;
       
   610 		for (Contact cur : mListContact) {
       
   611 		    if (res == position && Status.statusOnline(cur.getStatus())) {
       
   612 			c = cur;
       
   613 			break;
       
   614 		    }
       
   615 		    if (Status.statusOnline(cur.getStatus()))
       
   616 			res++;
       
   617 		}
       
   618 	    } else
       
   619 		c = mListContact.get(position);
       
   620 	    return c;
       
   621 	}
   575 	}
   622 
   576 
   623 	/**
   577 	/**
   624 	 * {@inheritDoc}
   578 	 * {@inheritDoc}
   625 	 */
   579 	 */
   635 	public View getView(int position, View convertView, ViewGroup parent) {
   589 	public View getView(int position, View convertView, ViewGroup parent) {
   636 	    View v = convertView;
   590 	    View v = convertView;
   637 	    if (convertView == null) {
   591 	    if (convertView == null) {
   638 		v = mInflater.inflate(R.layout.contactlistcontact, null);
   592 		v = mInflater.inflate(R.layout.contactlistcontact, null);
   639 	    }
   593 	    }
   640 	    Contact c = null;
   594 	    Contact c = mListContact.get(position);
   641 	    if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false)) {
       
   642 		int res = 0;
       
   643 		for (Contact cur : mListContact) {
       
   644 		    if (res == position && Status.statusOnline(cur.getStatus())) {
       
   645 			c = cur;
       
   646 			break;
       
   647 		    }
       
   648 		    if (Status.statusOnline(cur.getStatus()))
       
   649 			res++;
       
   650 		}
       
   651 	    } else
       
   652 		c = mListContact.get(position);
       
   653 	    if (mRoster != null) {
   595 	    if (mRoster != null) {
   654 		try {
   596 		try {
   655 		    c = mRoster.getContact(c.getJID());
   597 		    c = mRoster.getContact(c.getJID());
   656 		} catch (RemoteException e) {
   598 		} catch (RemoteException e) {
   657 		    e.printStackTrace();
   599 		    e.printStackTrace();
   658 		}
   600 		}
   659 	    }
   601 	    }
   660 	    if (mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false) && !Status.statusOnline(c.getStatus())) {
   602 	    bindView(v, c);
   661 		v.setVisibility(View.GONE);
       
   662 	    } else {
       
   663 		bindView(v, c);
       
   664 	    }
       
   665 	    return v;
   603 	    return v;
   666 	}
   604 	}
   667 
   605 
   668 	/**
   606 	/**
   669 	 * Adapte curContact to the view.
   607 	 * Adapte curContact to the view.
   685     }
   623     }
   686 
   624 
   687     /**
   625     /**
   688      * Adapter banner list.
   626      * Adapter banner list.
   689      */
   627      */
   690     private class BeemBanner extends BaseAdapter {
   628     private static class BeemBanner extends BaseAdapter {
       
   629 	private List<String> mGroups;
       
   630 	private LayoutInflater mInflater;
       
   631 
   691 	/**
   632 	/**
   692 	 * Constructor.
   633 	 * Constructor.
   693 	 */
   634 	 */
   694 	public BeemBanner() {
   635 	public BeemBanner(LayoutInflater inflater, List<String> groups) {
   695 	}
   636 	    mGroups = groups;
   696 
   637 	    mInflater = inflater;
   697 	/**
   638 	}
   698 	 * {@inheritDoc}
   639 
   699 	 */
       
   700 	@Override
   640 	@Override
   701 	public int getCount() {
   641 	public int getCount() {
   702 	    return mListGroup.size();
   642 	    return mGroups.size();
   703 	}
   643 	}
   704 
   644 
   705 	/**
       
   706 	 * {@inheritDoc}
       
   707 	 */
       
   708 	@Override
   645 	@Override
   709 	public Object getItem(int position) {
   646 	public Object getItem(int position) {
   710 	    return mListGroup.get(position);
   647 	    return mGroups.get(position);
   711 	}
   648 	}
   712 
   649 
   713 	/**
       
   714 	 * {@inheritDoc}
       
   715 	 */
       
   716 	@Override
   650 	@Override
   717 	public long getItemId(int position) {
   651 	public long getItemId(int position) {
   718 	    return position;
   652 	    return position;
   719 	}
   653 	}
   720 
   654 
   721 	/**
       
   722 	 * {@inheritDoc}
       
   723 	 */
       
   724 	@Override
   655 	@Override
   725 	public View getView(int position, View convertView, ViewGroup parent) {
   656 	public View getView(int position, View convertView, ViewGroup parent) {
   726 	    View v = convertView;
   657 	    View v = convertView;
   727 	    if (convertView == null) {
   658 	    if (convertView == null) {
   728 		v = mInflater.inflate(R.layout.contactlist_group, null);
   659 		v = mInflater.inflate(R.layout.contactlist_group, null);
   729 	    }
   660 	    }
   730 	    ((TextView) v).setText(mListGroup.get(position));
   661 	    ((TextView) v).setText(mGroups.get(position));
   731 	    return v;
   662 	    return v;
   732 	}
   663 	}
   733 
   664     }
   734     }
       
   735 
       
   736     /**
       
   737      * Create dialog resend subscription.
       
   738      */
       
   739 
   665 
   740     /**
   666     /**
   741      * The service connection used to connect to the Beem service.
   667      * The service connection used to connect to the Beem service.
   742      */
   668      */
   743     private class BeemServiceConnection implements ServiceConnection {
   669     private class BeemServiceConnection implements ServiceConnection {
   752 	public void onServiceConnected(ComponentName name, IBinder service) {
   678 	public void onServiceConnected(ComponentName name, IBinder service) {
   753 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   679 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   754 	    try {
   680 	    try {
   755 		mRoster = mXmppFacade.getRoster();
   681 		mRoster = mXmppFacade.getRoster();
   756 		if (mRoster != null) {
   682 		if (mRoster != null) {
       
   683 		    Log.d(TAG, "add roster listenet");
   757 		    mRoster.addRosterListener(mBeemRosterListener);
   684 		    mRoster.addRosterListener(mBeemRosterListener);
   758 		    List<Contact> tmpContactList = mRoster.getContactList();
   685 		    List<Contact> tmpContactList = mRoster.getContactList();
   759 		    List<String> tmpGroupList = mRoster.getGroupsNames();
   686 		    List<String> tmpGroupList = mRoster.getGroupsNames();
   760 		    Collections.sort(tmpGroupList);
   687 		    Collections.sort(tmpGroupList);
   761 		    if (mListGroup.size() > 0)
   688 		    if (mListGroup.size() > 0)
   762 			mListGroup.clear();
   689 			mListGroup.clear();
   763 		    mListGroup.add(getString(R.string.contact_list_all_contact));
   690 		    mListGroup.add(getString(R.string.contact_list_all_contact));
   764 		    mListGroup.addAll(tmpGroupList);
   691 		    mListGroup.addAll(tmpGroupList);
   765 		    mListGroup.add(getString(R.string.contact_list_no_group));
   692 		    mListGroup.add(getString(R.string.contact_list_no_group));
   766 		    mContactOnGroup.clear();
   693 		    assignContactToGroups(mRoster.getContactList(), tmpGroupList);
   767 		    if (tmpGroupList.size() > 0) {
       
   768 			List<Contact> tmpNoGroup = new ArrayList<Contact>();
       
   769 			for (String s : tmpGroupList) {
       
   770 			    List<Contact> tmpList = new ArrayList<Contact>();
       
   771 			    for (Contact c : tmpContactList) {
       
   772 				if (c.getGroups().size() == 0 && !tmpNoGroup.contains(c))
       
   773 				    tmpNoGroup.add(c);
       
   774 				else if (c.getGroups().contains(s))
       
   775 				    tmpList.add(c);
       
   776 			    }
       
   777 			    mContactOnGroup.put(s, tmpList);
       
   778 			}
       
   779 			mContactOnGroup.put(getString(R.string.contact_list_no_group), tmpNoGroup);
       
   780 		    } else {
       
   781 			mContactOnGroup.put(getString(R.string.contact_list_no_group), tmpContactList);
       
   782 		    }
       
   783 		    mContactOnGroup.put(getString(R.string.contact_list_all_contact), tmpContactList);
       
   784 		    mCurGroup = getString(R.string.contact_list_all_contact);
       
   785 		    if (!mSettings.getBoolean("settings_key_hide_groups", false))
   694 		    if (!mSettings.getBoolean("settings_key_hide_groups", false))
   786 			buildBanner();
   695 			showGroups();
   787 		    else
   696 		    else
   788 			hideGroups();
   697 			hideGroups();
   789 		    buildContactList();
   698 		    String group = getString(R.string.contact_list_all_contact);
       
   699 		    buildContactList(group);
   790 		}
   700 		}
   791 	    } catch (RemoteException e) {
   701 	    } catch (RemoteException e) {
   792 		e.printStackTrace();
   702 		e.printStackTrace();
   793 	    }
   703 	    }
   794 	}
   704 	}
   803 	    mXmppFacade = null;
   713 	    mXmppFacade = null;
   804 	    mRoster = null;
   714 	    mRoster = null;
   805 	    mListContact.clear();
   715 	    mListContact.clear();
   806 	    mListGroup.clear();
   716 	    mListGroup.clear();
   807 	    mContactOnGroup.clear();
   717 	    mContactOnGroup.clear();
       
   718 	    mBinded = false;
   808 	}
   719 	}
   809     }
   720     }
   810 
   721 
   811     @Override
   722     @Override
   812     protected void finalize() {
   723     protected void finalize() {
   815 
   726 
   816     /**
   727     /**
   817      * Hide the groups view.
   728      * Hide the groups view.
   818      */
   729      */
   819     private void hideGroups() {
   730     private void hideGroups() {
   820 	ViewStub stub = (ViewStub) findViewById(R.id.contactlist_stub);
   731 	View v = findViewById(R.id.contactlist_groupstub);
   821 	if (stub == null)
   732 	if (v != null)
   822 	    ((LinearLayout) findViewById(R.id.contactlist_groupstub)).setVisibility(View.GONE);
   733 	    v.setVisibility(View.GONE);
       
   734     }
       
   735 
       
   736     private void assignContactToGroups(List<Contact> contacts, List<String> groupNames) {
       
   737 	boolean hideDisconnected = mSettings.getBoolean(SETTINGS_HIDDEN_CONTACT, false);
       
   738 	mContactOnGroup.clear();
       
   739 	List<Contact> all = new LinkedList<Contact>();
       
   740 	List<Contact> noGroups = new LinkedList<Contact>();
       
   741 	for (Contact c : contacts) {
       
   742 	    if (hideDisconnected && !Status.statusOnline(c.getStatus())) {
       
   743 		continue;
       
   744 	    }
       
   745 	    all.add(c);
       
   746 	    List<String> groups = c.getGroups();
       
   747 	    if (groups.size() == 0)
       
   748 		noGroups.add(c);
       
   749 	    else {
       
   750 		for (String currentGroup : groups) {
       
   751 		    List<Contact> contactsByGroups = mContactOnGroup.get(currentGroup);
       
   752 		    if (contactsByGroups == null) {
       
   753 			contactsByGroups = new LinkedList<Contact>();
       
   754 			mContactOnGroup.put(currentGroup, contactsByGroups);
       
   755 		    }
       
   756 		    contactsByGroups.add(c);
       
   757 		}
       
   758 	    }
       
   759 	}
       
   760 	mContactOnGroup.put(getString(R.string.contact_list_no_group), noGroups);
       
   761 	mContactOnGroup.put(getString(R.string.contact_list_all_contact), all);
   823     }
   762     }
   824 }
   763 }