--- a/src/com/beem/project/beem/BeemApplication.java Tue Mar 31 22:21:42 2009 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java Tue Mar 31 20:46:06 2009 +0200
@@ -31,6 +31,7 @@
private Resources mPrivateResources;
private static BeemApplication mBeemApp;
private List<Message> mQueue = new LinkedList<Message>();
+ private boolean mIsConnected;
public static BeemApplication getApplication(Activity activity) {
if (mBeemApp == null) {
@@ -68,12 +69,13 @@
}
public synchronized void startBeemService() {
- if (mFacade == null) {
+ if (!mIsConnected) {
// Intent intent = new Intent(this, BeemService.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
mApplicationContext.startService(intent);
mApplicationContext.bindService(intent, mServConn, BIND_AUTO_CREATE);
+ mIsConnected = true;
}
}
@@ -82,7 +84,7 @@
@Override
public void onServiceDisconnected(ComponentName name) {
mFacade = null;
-
+ mIsConnected = false;
}
@Override
@@ -110,7 +112,7 @@
public void callWhenServiceConnected(Handler target, Runnable callback) {
Message msg = Message.obtain(target, callback);
- if (mFacade != null) {
+ if (!mIsConnected) {
msg.sendToTarget();
} else {
startBeemService();
--- a/src/com/beem/project/beem/ui/ContactList.java Tue Mar 31 22:21:42 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java Tue Mar 31 20:46:06 2009 +0200
@@ -6,145 +6,161 @@
import java.util.Map;
import android.app.ExpandableListActivity;
-import android.content.ComponentName;
-import android.content.Intent;
-import android.content.ServiceConnection;
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.graphics.drawable.Drawable;
import android.os.Bundle;
-import android.os.IBinder;
+import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
+import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
+import android.widget.TextView;
import com.beem.project.beem.BeemApplication;
-import com.beem.project.beem.BeemService;
import com.beem.project.beem.R;
import com.beem.project.beem.service.Contact;
-import com.beem.project.beem.service.aidl.IBeemConnectionListener;
-import com.beem.project.beem.service.aidl.IRoster;
-import com.beem.project.beem.service.aidl.IXMPPConnection;
import com.beem.project.beem.service.aidl.IXMPPFacade;
public class ContactList extends ExpandableListActivity {
- private static final String TAG = "CONTACTLIST_ACT";
-
- private IXMPPFacade mService = null;
-
- private BeemApplication mBeemApplication;
-
- @Override
- public void onCreate(Bundle saveBundle) {
- super.onCreate(saveBundle);
- mBeemApplication = BeemApplication.getApplication(this);
- mBeemApplication.startBeemService();
- mService = mBeemApplication.getXmppFacade();
- /* bindService(new Intent(this, BeemService.class), mConnection,
- BIND_AUTO_CREATE | BIND_DEBUG_UNBIND); */
- showContactList();
- }
-
- private void showContactList() {
- ExpandableListAdapter Adapter;
-
- List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
- List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
-
- for (int i = 0; i < 2; i++) {
- Map<String, String> curGroupMap = new HashMap<String, String>();
- groupData.add(curGroupMap);
- curGroupMap.put("NAME", "Group " + i);
-
- List<Map<String, String>> children = new ArrayList<Map<String, String>>();
- for (int j = 0; j < 5; j++) {
- Map<String, String> curChildMap = new HashMap<String, String>();
- children.add(curChildMap);
- curChildMap.put("NAOME CHILD", "Child " + j);
- }
- childData.add(children);
- }
-
- Adapter = new SimpleExpandableListAdapter(this,
- groupData, R.layout.contactlistgroup,
- new String[] {"NAME"}, new int[] {R.id.textgroup},
- childData, R.layout.contactlist,
- new String[] {"NAME CHILD"}, new int[] {R.id.textchild});
- setListAdapter(Adapter);
- }
-
- private ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- mService = IXMPPFacade.Stub.asInterface(service);
- try {
- IXMPPConnection con = mService.createConnection();
- con.addConnectionListener(new TestConnectionListener());
- mService.connectSync();
- Log.i("BEEM", "Connected !!!");
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- /*
- * mService.getGroupList(); mService.getContactList();
- */
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
-
- };
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- unbindService(mConnection);
- }
-
- private class TestConnectionListener extends IBeemConnectionListener.Stub {
+ private static final String TAG = "CONTACTLIST_ACT";
+ private IXMPPFacade mService = null;
+ private Handler mHandler;
+ private BeemApplication mBeemApplication;
@Override
- public void connectionClosed() throws RemoteException {
- // TODO Auto-generated method stub
-
- }
+ public void onCreate(Bundle saveBundle) {
+ super.onCreate(saveBundle);
+ mHandler = new Handler();
+ mBeemApplication = BeemApplication.getApplication(this);
+
- @Override
- public void connectionClosedOnError() throws RemoteException {
- // TODO Auto-generated method stub
-
}
-
+
@Override
- public void onConnect() throws RemoteException {
- // TODO Auto-generated method stub
- IRoster roster = mService.getRoster();
- for (Contact contact : roster.getContactList()) {
- Log.v(TAG,"Contact name " + contact.getJID() );
- }
- showContactList();
+ public void onStart() {
+ super.onStart();
+ mBeemApplication.startBeemService();
+ mBeemApplication.callWhenServiceConnected(mHandler, new Runnable() {
+ @Override
+ public void run() {
+ mService = mBeemApplication.getXmppFacade();
+ try {
+ showContactList(mService.getRoster().getContactList(),
+ mService.getRoster().getContactList());
+ } catch (RemoteException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+ });
}
- @Override
- public void reconnectingIn(int seconds) throws RemoteException {
- // TODO Auto-generated method stub
-
+ private void showContactList(List<Contact> listGroup,
+ List<Contact> listContact) {
+ ExpandableListAdapter Adapter;
+
+ List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
+ List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
+
+ if (listGroup.size() == 0)
+ listGroup.add(new Contact());
+ for (int i = 0; i < listGroup.size() ; i++) {
+ Map<String, String> curGroupMap = new HashMap<String, String>();
+
+ groupData.add(curGroupMap);
+ curGroupMap.put("NAME", "Default");
+
+ List<Map<String, String>> children = new ArrayList<Map<String, String>>();
+ for (int j = 0; j < listContact.size() ; ++j) {
+ Map<String, String> curChildMap = new HashMap<String, String>();
+ children.add(curChildMap);
+ curChildMap.put("NAME_CHILD", listContact.get(j).getJID());
+ curChildMap.put("MSG", "Taper votre message perso");
+ }
+ childData.add(children);
+ }
+
+ Adapter = new ContactExpandableListAdapter(this, groupData,
+ R.layout.contactlistgroup, new String[] { "NAME" },
+ new int[] { R.id.textgroup }, childData,
+ R.layout.contactlistcontact,
+ new String[] { "NAME_CHILD", "MSG" }, new int[] {
+ R.id.textchild1, R.id.textchild2, R.id.avatar });
+ setListAdapter(Adapter);
+ }
+
+ /**
+ * A simple adapter which allows you to bind data to specific Views defined
+ * within the layout of an Expandable Lists children (Implement
+ * getGroupView() to define the layout of parents)
+ */
+ public class ContactExpandableListAdapter extends
+ SimpleExpandableListAdapter {
+
+ private List<? extends List<? extends Map<String, ?>>> mChildData;
+ private String[] mChildFrom;
+ private int[] mChildTo;
+
+ public ContactExpandableListAdapter(Context context,
+ List<? extends Map<String, ?>> groupData, int groupLayout,
+ String[] groupFrom, int[] groupTo,
+ List<? extends List<? extends Map<String, ?>>> childData,
+ int childLayout, String[] childFrom, int[] childTo) {
+ super(context, groupData, groupLayout, groupFrom, groupTo,
+ childData, childLayout, childFrom, childTo);
+
+ mChildData = childData;
+ mChildFrom = childFrom;
+ mChildTo = childTo;
+
+ }
+
+ @Override
+ public View getChildView(int groupPosition, int childPosition,
+ boolean isLastChild, View convertView, ViewGroup parent) {
+
+ View v;
+ if (convertView == null) {
+ v = newChildView(isLastChild, parent);
+ } else {
+ v = convertView;
+ }
+ bindView(v, mChildData.get(groupPosition).get(childPosition),
+ mChildFrom, mChildTo, groupPosition, childPosition);
+ return v;
+ }
+
+ // This method binds my data to the Views specified in the child
+ // xmllayout
+ private void bindView(View view, Map<String, ?> data, String[] from,
+ int[] to, int groupPosition, int childPosition) {
+ // Apply TextViews
+ TextView v1 = (TextView) view.findViewById(to[0]);
+ if (v1 != null) {
+ Log.i("CONTACT LIST 1", (String) data.get(from[0]) + " "
+ + to[0]);
+ v1.setText((String) data.get(from[0]));
+ }
+ TextView v2 = (TextView) view.findViewById(to[1]);
+ if (v2 != null) {
+ Log.i("CONTACT LIST 2", (String) data.get(from[1]) + " "
+ + to[1]);
+ v2.setText((String) data.get(from[1]));
+ }
+ // Apply ImageView
+ ImageView imgV = (ImageView) view.findViewById(to[2]);
+ if (imgV != null) {
+ Drawable avatar = (Drawable) getResources().getDrawable(
+ R.drawable.avatar);
+ imgV.setImageDrawable(avatar);
+ }
+ }
}
- @Override
- public void reconnectionFailed() throws RemoteException {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void reconnectionSuccessful() throws RemoteException {
- // TODO Auto-generated method stub
-
- }
-
- }
-
}