#16843: Launch SFLphone service in SFLphoneHome activity creation
diff --git a/src/com/savoirfairelinux/sflphone/client/AccountSelectionDialog.java b/src/com/savoirfairelinux/sflphone/client/AccountSelectionDialog.java
new file mode 100644
index 0000000..d3b32ae
--- /dev/null
+++ b/src/com/savoirfairelinux/sflphone/client/AccountSelectionDialog.java
@@ -0,0 +1,78 @@
+/*
+ *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
+ *
+ *  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
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *  Additional permission under GNU GPL version 3 section 7:
+ *
+ *  If you modify this program, or any covered work, by linking or
+ *  combining it with the OpenSSL project's OpenSSL library (or a
+ *  modified version of that library), containing parts covered by the
+ *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
+ *  grants you additional permission to convey the resulting work.
+ *  Corresponding Source for a non-source form of such a combination
+ *  shall include the source code for the parts of OpenSSL used as well
+ *  as that of the covered work.
+ */
+package com.savoirfairelinux.sflphone.client;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+import java.util.ArrayList;
+
+public class AccountSelectionDialog extends AlertDialog
+{
+    Context mContext;
+    ListView mListView;
+    ArrayAdapter mListAdapter;
+    ArrayList<String> mItems;
+
+    public AccountSelectionDialog(Context context, ArrayList<String> items)
+    {
+        super(context);
+        mContext = context;
+        mItems = items;
+    } 
+
+    private DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() 
+    {
+        public void onClick(DialogInterface dialog, int which) {
+        }
+    };
+
+    public void onCreate(Bundle savedInstanceState)
+    {
+        mListView = new ListView(mContext);
+        mListAdapter = new ArrayAdapter(mContext, android.R.layout.simple_expandable_list_item_1, mItems.toArray());
+        mListView.setAdapter(mListAdapter);
+        setContentView(mListView);
+    }
+
+    public Dialog onCreateDialog(Bundle savedInstanceState)
+    {
+        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
+        builder.setTitle("Account Selection");
+
+        return builder.create();
+    }
+}
diff --git a/src/com/savoirfairelinux/sflphone/client/CallElementList.java b/src/com/savoirfairelinux/sflphone/client/CallElementList.java
index 0fd1c32..47f6af0 100644
--- a/src/com/savoirfairelinux/sflphone/client/CallElementList.java
+++ b/src/com/savoirfairelinux/sflphone/client/CallElementList.java
@@ -51,6 +51,7 @@
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.RemoteException;
 import android.provider.*;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
@@ -65,6 +66,7 @@
 import java.util.ArrayList;
 
 import com.savoirfairelinux.sflphone.R;
+import com.savoirfairelinux.sflphone.service.ISipService;
 
 /**
  * Main list of Call Elements.
@@ -76,6 +78,7 @@
     ContactManager mContactManager;
     ArrayAdapter mAdapter;
     String mCurFilter;
+    private ISipService service;
 
     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
                                                                        Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
@@ -221,6 +224,17 @@
         protected TextView phones; 
     }
 
+    public CallElementList(ISipService s)
+    {
+        super();
+        service = s;
+    }
+
+    public void setService(ISipService s)
+    {
+        service = s;
+    }
+
     public void addCall(SipCall c)
     {
         Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
@@ -304,13 +318,41 @@
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.));
         View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
+
+        Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
+        accountSelectionButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                /*
+                ArrayList<String> list = new ArrayList<String>();
+                list.add("IP2IP");
+                list.add("SFL-181");
+                AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
+                accountSelectionDialog.show();
+                */
+                try {
+                    if(service != null) {
+                        ArrayList<String> list = (ArrayList<String>)service.getAccountList();
+                        if(list != null) {
+                            AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
+                            accountSelectionDialog.show();
+                        }
+                        else { Log.i(TAG, "Could not get account list"); }
+                    }
+                    else { Log.i(TAG, "Could not get service"); }
+                }
+                catch (RemoteException e) {
+                    Log.e(TAG, "Remote exception", e);
+                }
+            }
+        });
+
         return inflatedView;
     }
 	
     public void onListItemClick(ListView l, View v, int position, long id)
     {
         // Insert desired behavior here.
-        Log.i("CallElementList", "Item clicked: " + id);
+        Log.i(TAG, "Item clicked: " + id);
         SipCall call = (SipCall) mAdapter.getItem(position);
         call.hangup(); 
     }
diff --git a/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java b/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
index f2239df..3968c6f 100644
--- a/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
+++ b/src/com/savoirfairelinux/sflphone/client/ContactListFragment.java
@@ -45,6 +45,7 @@
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.RemoteException;
 import android.provider.*;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
@@ -57,6 +58,7 @@
 import android.view.MenuInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
 import android.widget.CursorAdapter;
 import android.widget.ImageView;
 import android.widget.ListView;
@@ -75,6 +77,7 @@
 import java.util.ArrayList;
 
 import com.savoirfairelinux.sflphone.R;
+import com.savoirfairelinux.sflphone.service.ISipService;
 
 public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
 {
@@ -82,6 +85,7 @@
     ContactElementAdapter mAdapter;
     Context mContext;
     String mCurFilter;
+    private ISipService service;
 
     // These are the Contacts rows that we will retrieve.
     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
@@ -177,16 +181,22 @@
         }
     };
 
-    public ContactListFragment()
+    public ContactListFragment(ISipService s)
     {
         super();
-        mContext = getActivity();
+        service = s;
+    }
+
+    public void setService(ISipService s)
+    {
+        service = s;
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState)
     {
         super.onActivityCreated(savedInstanceState);
+        mContext = getActivity();
 
         // In order to onCreateOptionsMenu be called 
         setHasOptionsMenu(true);
@@ -255,7 +265,23 @@
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
-        return inflater.inflate(R.layout.call_element_list, container, false);
+        View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
+
+        Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
+        accountSelectionButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                try {
+                    ArrayList<String> list = (ArrayList<String>)service.getAccountList();
+                    AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
+                    accountSelectionDialog.show();
+                }
+                catch (RemoteException e) {
+                    Log.e(TAG, "Remote exception", e);
+                }
+            }
+        });
+
+        return inflatedView;
     }
 
     @Override
diff --git a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
index 9c9214e..694d6ac 100644
--- a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
+++ b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
@@ -97,10 +97,25 @@
 
     final private int[] icon_res_id = {R.drawable.ic_tab_call, R.drawable.ic_tab_call, R.drawable.ic_tab_history, R.drawable.ic_tab_play_selected};
 
+    // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
+
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
+
+        if (!serviceIsOn) {
+            Log.i(TAG, "starting SipService");
+            startSipService();
+        }
+
+        // Bind to LocalService
+        if (!mBound) {
+            Log.i(TAG, "onStart: Binding service...");
+            Intent intent = new Intent(this, SipService.class);
+            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
+        }
+
         setContentView(R.layout.activity_sflphone_home);
 
         mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
@@ -147,23 +162,12 @@
         animation.setRepeatCount(Animation.INFINITE);
         // Reverse
         animation.setRepeatMode(Animation.REVERSE);
-
-        if (!serviceIsOn) {
-            Log.i(TAG, "starting SipService");
-            startSipService();
-        }
     }
 
     @Override
     protected void onStart() {
         Log.i(TAG, "onStart");
         super.onStart();
-        // Bind to LocalService
-        if (!mBound) {
-            Log.i(TAG, "onStart: Binding service...");
-            Intent intent = new Intent(this, SipService.class);
-            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
-        }
     }
 
     /* user gets back to the activity, e.g. through task manager */
@@ -189,17 +193,17 @@
     @Override
     protected void onStop() {
         super.onStop();
+    }
+
+    /* activity finishes itself or is being killed by the system */
+    @Override
+    protected void onDestroy() {
         /* stop the service, if no other bound user, no need to check if it is running */
         if (mBound) {
             Log.i(TAG, "onStop: Unbinding service...");
             unbindService(mConnection);
             mBound = false;
         }
-    }
-
-    /* activity finishes itself or is being killed by the system */
-    @Override
-    protected void onDestroy() {
         Log.i(TAG, "onDestroy: stopping SipService...");
         stopService(new Intent(this, SipService.class));
         serviceIsOn = false;
@@ -214,6 +218,8 @@
                 IBinder binder) {
             service = ISipService.Stub.asInterface(binder);
             mBound = true;
+            mContactListFragment.setService(service);
+            mCallElementList.setService(service);
             Log.d(TAG, "Service connected");
         }
 
@@ -309,11 +315,11 @@
 
             switch (i) {
             case 0:
-                mContactListFragment = new ContactListFragment();
+                mContactListFragment = new ContactListFragment(service);
                 fragment = mContactListFragment;
                 break;
             case 1:
-                mCallElementList = new CallElementList();
+                mCallElementList = new CallElementList(service);
                 SipCall.setCallElementList(mCallElementList);
                 fragment = mCallElementList;
                 break;