#14652: add android service with test button
diff --git a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
index 900dfde..0b66831 100644
--- a/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
+++ b/src/com/savoirfairelinux/sflphone/client/SFLPhoneHome.java
@@ -57,11 +57,13 @@
 import android.view.animation.AlphaAnimation;
 import android.view.animation.Animation;
 import android.view.animation.LinearInterpolator;
+import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageButton;
 import android.widget.TextView;
 
 import com.savoirfairelinux.sflphone.R;
+import com.savoirfairelinux.sflphone.service.SipService;
 
 public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
 {
@@ -73,9 +75,11 @@
 	/* default callID */
 	static String callID = "007";
 	static boolean callOnGoing = false;
+    static boolean serviceIsOn = false;
 	private String incomingCallID = "";
         private static final int REQUEST_CODE_PREFERENCES = 1;
 	ImageButton buttonCall, buttonHangup;
+	Button buttonService;
 
 	/**
 	 * The {@link ViewPager} that will host the section contents.
@@ -323,6 +327,8 @@
 	@Override
     public void onClick(View view)
     {
+        buttonService = (Button) findViewById(R.id.buttonService);
+        
     	switch (view.getId()) {
     	case R.id.buttonCall:
     		TextView textView = (TextView) findViewById(R.id.editAccountID);
@@ -384,6 +390,18 @@
     		Manager.managerImpl.setPath("");
     		Manager.managerImpl.init("");
     		break;
+    	case R.id.buttonService:
+    	    if (!serviceIsOn) {
+    	        startService(new Intent(this, SipService.class));
+    	        serviceIsOn = true;
+    	        buttonService.setText("disable Service");
+    	    }
+    	    else {
+                stopService(new Intent(this, SipService.class));
+    	        serviceIsOn = false;
+    	        buttonService.setText("enable Service");
+            }
+    	    break;
     	case R.id.buttonCallVoid:
     		Manager.callVoid();
         	break;
diff --git a/src/com/savoirfairelinux/sflphone/service/SipService.java b/src/com/savoirfairelinux/sflphone/service/SipService.java
new file mode 100644
index 0000000..822dbc4
--- /dev/null
+++ b/src/com/savoirfairelinux/sflphone/service/SipService.java
@@ -0,0 +1,99 @@
+package com.savoirfairelinux.sflphone.service;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.Binder;
+import android.os.IBinder;
+import android.util.Log;
+import android.widget.Toast;
+
+import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
+
+public class SipService extends Service {
+
+    static final String TAG = "SipService";
+    static final int DELAY = 5000; /* 5 sec */
+    private boolean runFlag = false;
+    private SipServiceThread sipServiceThread;
+    private SFLphoneApplication sflphone;
+    private final IBinder mBinder = new LocalBinder();
+
+    /* called once by startService() */
+    @Override
+    public void onCreate() {
+        Log.i(TAG, "onCreated");
+        super.onCreate();
+        this.sflphone = (SFLphoneApplication) getApplication();
+        this.sipServiceThread = new SipServiceThread();
+        Log.i(TAG, "onCreated");
+    }
+
+    /* called for each startService() */
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        Log.i(TAG, "onStarted");
+        super.onStartCommand(intent, flags, startId);
+//        if(intent != null) {
+//            Parcelable p = intent.getParcelableExtra(ServiceConstants.EXTRA_OUTGOING_ACTIVITY);
+//            Log.i(TAG, "unmarshalled outgoing_activity");
+//        }
+        this.runFlag = true;
+        this.sipServiceThread.start();
+        this.sflphone.setServiceRunning(true);
+        Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
+        
+        Log.i(TAG, "onStarted");
+        return START_STICKY; /* started and stopped explicitly */
+    }
+
+    @Override
+    public void onDestroy() {
+        /* called once by stopService() */
+        super.onDestroy();
+        this.runFlag = false;
+        this.sipServiceThread.interrupt();
+        this.sipServiceThread = null;
+        this.sflphone.setServiceRunning(false);
+        Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
+        
+        Log.i(TAG, "onDestroyed");
+    }
+
+    @Override
+    public IBinder onBind(Intent arg0) {
+        Log.i(TAG, "onBound");
+        return mBinder;
+    }
+
+    /**
+     * Class used for the client Binder.  Because we know this service always
+     * runs in the same process as its clients, we don't need to deal with IPC.
+     */
+    public class LocalBinder extends Binder {
+        SipService getService() {
+            // Return this instance of LocalService so clients can call public methods
+            return SipService.this;
+        }
+    }
+
+    private class SipServiceThread extends Thread {
+        
+        public SipServiceThread() {
+            super("sipServiceThread");
+        }
+        
+        @Override
+        public void run() {
+            SipService sipService = SipService.this;
+            while(sipService.runFlag) {
+                try {
+                    //Log.i(TAG, "SipService thread running...");
+                    Thread.sleep(DELAY);
+                } catch (InterruptedException e) {
+                    sipService.runFlag = false;
+                    Log.w(TAG, "service thread interrupted!");
+                }
+            }
+        }
+    }
+}