#15737: Add move contact adapter in contact fragment
diff --git a/src/com/savoirfairelinux/sflphone/client/CallElementList.java b/src/com/savoirfairelinux/sflphone/client/CallElementList.java
index 1ef0548..19df055 100644
--- a/src/com/savoirfairelinux/sflphone/client/CallElementList.java
+++ b/src/com/savoirfairelinux/sflphone/client/CallElementList.java
@@ -71,7 +71,7 @@
 public class CallElementList extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
 {
         ContactManager mContactManager;
-	CallElementAdapter mAdapter;
+	CursorAdapter mAdapter;
 	String mCurFilter;
 
 	// These are the Contacts rows that we will retrieve.
@@ -161,56 +161,42 @@
 	 * A CursorAdapter that creates and update call elements using corresponding contact infos.
 	 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
 	 */
-	public static class CallElementAdapter extends ArrayAdapter
+	public static class CallElementAdapter extends CursorAdapter
 	{
 		private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
-                private ContactManager mContactManager;
                 private Context mContext;
 
-                public CallElementAdapter(Context context)
+                public CallElementAdapter(Context context, Cursor c)
                 {
-                        super(context, R.layout.call_element);
-                        mContactManager = new ContactManager(context);
-                }
-
-		public CallElementAdapter(Context context, ContactManager manager)
-		{
-			super(context, R.layout.call_element, manager.getContactList());
-                        mContactManager = manager;
+                        super(context, c, 0);
                         mContext = context;
-		}
+                }
 
                 @Override
-                public View getView(int position, View convertView, ViewGroup parent)
+                public View newView(Context context, Cursor cursor, ViewGroup parent)
                 {
-                    View rowView = convertView;
-                    CallElementView callElementView = null;
-
-                    if(rowView == null) {
-                        LayoutInflater inflater = LayoutInflater.from(mContext);
-                        final long contact_id = 0;
-                        rowView = inflater.inflate(R.layout.call_element, parent, false);
-                        infos_fetcher.execute(new InfosLoader(mContext, rowView, contact_id));
-
-                        callElementView = new CallElementView();
-                        callElementView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
-                        callElementView.button = (Button) rowView.findViewById(R.id.button2);
-                        callElementView.photo = (ImageView) rowView.findViewById(R.id.photo);
-                        callElementView.displayName = (TextView) rowView.findViewById(R.id.display_name);
-                        callElementView.phones = (TextView) rowView.findViewById(R.id.phones);
-
-                        rowView.setTag(callElementView);
-                    } else {
-                        callElementView = (CallElementView) rowView.getTag();
-                    }
-
-                    CallContact c = mContactManager.getContact(position);
-                    callElementView.displayName.setText(c.getDisplayName());
-                    callElementView.phones.setText(c.getPhone());
-
-                    return rowView;
+                        LayoutInflater inflater = LayoutInflater.from(context);
+                        View v = inflater.inflate(R.layout.call_element, parent, false);
+                        bindView(v, context, cursor);
+                        return v;
                 }
 
+                @Override
+                public void bindView(final View view, Context context, Cursor cursor)
+                {
+                        final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
+                        final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
+                        //final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
+                        //final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
+
+                        TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
+                        display_name_txt.setText(display_name);
+
+                        ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
+                        photo_view.setVisibility(View.GONE);
+ 
+                        infos_fetcher.execute(new InfosLoader(context, view, contact_id));
+                }
 	};
 
         public static class CallElementView
@@ -236,15 +222,15 @@
 
 
 		// Create an empty adapter we will use to display the loaded data.
-		mAdapter = new CallElementAdapter(getActivity());
-		setListAdapter(mAdapter);
+		// mAdapter = new CallElementAdapter(getActivity(), null);
+		// setListAdapter(mAdapter);
 
 		// Start out with a progress indicator.
 		//setListShown(false);
 
 		// Prepare the loader.  Either re-connect with an existing one,
 		// or start a new one.
-		getLoaderManager().initLoader(0, null, this);
+		// getLoaderManager().initLoader(0, null, this);
 	}
 
 	@Override
@@ -287,7 +273,7 @@
 	public boolean onQueryTextSubmit(String query)
 	{
 		// Don't care about this.
-		return true;
+		return false;
 	}
 
 	@Override
@@ -338,7 +324,7 @@
 	{
 		// Swap the new cursor in.  (The framework will take care of closing the
 		// old cursor once we return.)
-		// mAdapter.swapCursor(data);
+		mAdapter.swapCursor(data);
 
 		// The list should now be shown.
 		/*
@@ -355,6 +341,6 @@
 		// This is called when the last Cursor provided to onLoadFinished()
 		// above is about to be closed.  We need to make sure we are no
 		// longer using it.
-		// mAdapter.swapCursor(null);
+		mAdapter.swapCursor(null);
 	}
 }
diff --git a/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java b/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
index 6e83c37..784f21f 100644
--- a/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
+++ b/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
@@ -1,7 +1,7 @@
 /*
  *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
  *
- *  Author: Adrien Beraud <adrien.beraud@gmail.com>
+ *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -32,35 +32,48 @@
 
 import android.app.ListFragment;
 import android.app.LoaderManager;
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.Context;
 import android.content.CursorLoader;
 import android.content.Loader;
 import android.database.Cursor;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Bundle;
+import android.provider.*;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.CommonDataKinds.SipAddress;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Profile;
 import android.text.TextUtils;
-import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.CursorAdapter;
+import android.widget.ImageView;
 import android.widget.ListView;
+import android.widget.TextView;
 import android.widget.SearchView;
 import android.widget.SearchView.OnQueryTextListener;
 import android.util.Log;
+import java.io.InputStream;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.List;
+import java.util.ArrayList;
 
 import com.savoirfairelinux.sflphone.R;
 
 public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
 {
-    ContactManager mContactManager;
-    CallElementList.CallElementAdapter mAdapter;
+    ContactElementAdapter mAdapter;
+    Manager mManager;
     String mCurFilter;
 
     // These are the Contacts rows that we will retrieve.
@@ -69,6 +82,100 @@
     static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
     static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
 
+    public static class InfosLoader implements Runnable
+    {
+        private View view;
+        private long cid;
+        private ContentResolver cr;
+
+        public InfosLoader(Context context, View element, long contact_id)
+        {
+            cid = contact_id;
+            cr = context.getContentResolver();
+            view = element;
+        }
+
+        public static Bitmap loadContactPhoto(ContentResolver cr, long id) 
+        {
+            Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
+            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
+            if (input == null) {
+                return null;
+            }
+            return BitmapFactory.decodeStream(input);
+        }
+
+        @Override
+        public void run()
+        {
+            final Bitmap photo_bmp = loadContactPhoto(cr, cid);
+
+            Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,	
+                                        CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
+                                        new String[] { Long.toString(cid) },
+                                        null);
+
+            final List<String> numbers = new ArrayList<String>();
+            while (phones.moveToNext()) {
+                String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
+                // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
+                numbers.add(number);
+            }
+            phones.close();
+
+            final Bitmap bmp = photo_bmp;
+            view.post(new Runnable()
+            {
+                @Override
+                public void run()
+                {
+                }
+            });
+        }
+    }
+
+    public static class ContactElementAdapter extends CursorAdapter
+    {
+        private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
+
+        public ContactElementAdapter(Context context, Cursor c)
+        {
+            super(context, c, 0);
+        }
+
+        @Override
+        public View newView(Context context, Cursor cursor, ViewGroup parent)
+        {
+            LayoutInflater inflater = LayoutInflater.from(context);
+            View v = inflater.inflate(R.layout.call_element, parent, false);
+            bindView(v, context, cursor);
+            return v;
+        }
+
+        @Override
+        public void bindView(final View view, Context context, Cursor cursor)
+        {
+            final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
+            final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
+            // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
+            // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
+
+            TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
+            display_name_txt.setText(display_name);
+
+            ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
+            photo_view.setVisibility(View.GONE);
+
+            infos_fetcher.execute(new InfosLoader(context, view, contact_id));
+        }
+    };
+
+    public ContactListFragment(Manager manager)
+    {
+        super();
+        mManager = manager;
+    }
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState)
     {
@@ -77,10 +184,10 @@
         // In order to onCreateOptionsMenu be called 
         setHasOptionsMenu(true);
 
-        mContactManager = new ContactManager(getActivity());
-
-        mAdapter = new CallElementList.CallElementAdapter(getActivity(), mContactManager);
+        mAdapter = new ContactElementAdapter(getActivity(), null);
         setListAdapter(mAdapter);
+
+        getLoaderManager().initLoader(0, null, this);
     }
 
     @Override
@@ -105,12 +212,15 @@
     public void onListItemClick(ListView l, View v, int position, long id)
     {
         // Insert desired behavior here.
-        CallContact contact = mContactManager.getContact(position);
+/*
         Log.i("ContactListFragment", "Contact clicked: " + contact.getDisplayName());
 
         SipCall call = SipCall.getCallInstance(contact);
         Log.i("ConatctListFragment", "OK");
         Log.i("ContactListFragment", "Number of calls " + SipCall.getNbCalls());
+
+        mManager.callmanagerJNI.placeCall("IP2IP", "CALL1234", "192.168.40.35");
+*/
     }
 
     @Override
@@ -164,11 +274,13 @@
     public void onLoadFinished(Loader<Cursor> loader, Cursor data)
     {
         // Swap the new cursor in.
+        mAdapter.swapCursor(data);
     }
 
     @Override
     public void onLoaderReset(Loader<Cursor> loader)
     {
         // Thi is called when the last Cursor provided to onLoadFinished 
+        mAdapter.swapCursor(null);
     }
 }
diff --git a/src/com/savoirfairelinux/sflphone/client/ContactManager.java b/src/com/savoirfairelinux/sflphone/client/ContactManager.java
index a0d9e25..13bfe18 100644
--- a/src/com/savoirfairelinux/sflphone/client/ContactManager.java
+++ b/src/com/savoirfairelinux/sflphone/client/ContactManager.java
@@ -34,6 +34,7 @@
 import android.content.Context;
 import android.database.Cursor;
 import android.provider.ContactsContract;
+import android.provider.ContactsContract.Contacts;
 import android.util.Log;
 import java.util.ArrayList;
 
@@ -43,6 +44,9 @@
     Context mContext;
     ArrayList<CallContact> contactList;
 
+    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
+                                                                       Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
+
     public ContactManager(Context context)
     {
         mCount = 0;
@@ -56,7 +60,7 @@
     {
         ContentResolver resolver = mContext.getContentResolver();
 
-        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
+        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, null, null, null);
         while(cursor.moveToNext()) {
             mCount++;
             String displayName = "";
diff --git a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
index 900dfde..f36e48a 100644
--- a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
+++ b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
@@ -231,7 +231,7 @@
 			
 			switch (i) {
                         case 0:
-                                fragment = new ContactListFragment();
+                                fragment = new ContactListFragment(manager);
                                 break;
 			case 1:
 				fragment = new CallElementList();
diff --git a/src/com/savoirfairelinux/sflphone/client/SipCall.java b/src/com/savoirfairelinux/sflphone/client/SipCall.java
index a27387c..cab3bfa 100644
--- a/src/com/savoirfairelinux/sflphone/client/SipCall.java
+++ b/src/com/savoirfairelinux/sflphone/client/SipCall.java
@@ -58,13 +58,10 @@
 
     public static SipCall getCallInstance(CallContact contact)
     {
-        Log.i(TAG, "Get call instance for " + contact.getDisplayName()); 
         if(CallList.isEmpty())
             return new SipCall(contact);
        
         for(SipCall sipcall : CallList) {
-            Log.i(TAG, "Searching...");
-
             if(sipcall.mContact.getDisplayName().equals(contact.getDisplayName())) {
                 return sipcall;
             }
@@ -78,6 +75,11 @@
         return CallList.size();
     }
 
+    public void placeCall()
+    {
+        
+    }
+
     public void answer()
     {