blob: b8f116f7435a9b87036f918a7b9b5a006ce1e354 [file] [log] [blame]
Emeric Vigiereaf2c492012-09-19 14:38:20 -04001package com.savoirfairelinux.sflphone.service;
2
3import android.app.Service;
4import android.content.Intent;
5import android.os.Binder;
6import android.os.IBinder;
7import android.util.Log;
8import android.widget.Toast;
9
10import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
11
12public class SipService extends Service {
13
14 static final String TAG = "SipService";
15 static final int DELAY = 5000; /* 5 sec */
16 private boolean runFlag = false;
17 private SipServiceThread sipServiceThread;
18 private SFLphoneApplication sflphone;
19 private final IBinder mBinder = new LocalBinder();
20
21 /* called once by startService() */
22 @Override
23 public void onCreate() {
24 Log.i(TAG, "onCreated");
25 super.onCreate();
26 this.sflphone = (SFLphoneApplication) getApplication();
27 this.sipServiceThread = new SipServiceThread();
28 Log.i(TAG, "onCreated");
29 }
30
31 /* called for each startService() */
32 @Override
33 public int onStartCommand(Intent intent, int flags, int startId) {
34 Log.i(TAG, "onStarted");
35 super.onStartCommand(intent, flags, startId);
36// if(intent != null) {
37// Parcelable p = intent.getParcelableExtra(ServiceConstants.EXTRA_OUTGOING_ACTIVITY);
38// Log.i(TAG, "unmarshalled outgoing_activity");
39// }
40 this.runFlag = true;
41 this.sipServiceThread.start();
42 this.sflphone.setServiceRunning(true);
43 Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
44
45 Log.i(TAG, "onStarted");
46 return START_STICKY; /* started and stopped explicitly */
47 }
48
49 @Override
50 public void onDestroy() {
51 /* called once by stopService() */
52 super.onDestroy();
53 this.runFlag = false;
54 this.sipServiceThread.interrupt();
55 this.sipServiceThread = null;
56 this.sflphone.setServiceRunning(false);
57 Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
58
59 Log.i(TAG, "onDestroyed");
60 }
61
62 @Override
63 public IBinder onBind(Intent arg0) {
64 Log.i(TAG, "onBound");
65 return mBinder;
66 }
67
68 /**
69 * Class used for the client Binder. Because we know this service always
70 * runs in the same process as its clients, we don't need to deal with IPC.
71 */
72 public class LocalBinder extends Binder {
73 SipService getService() {
74 // Return this instance of LocalService so clients can call public methods
75 return SipService.this;
76 }
77 }
78
79 private class SipServiceThread extends Thread {
80
81 public SipServiceThread() {
82 super("sipServiceThread");
83 }
84
85 @Override
86 public void run() {
Emeric Vigier12d61d82012-09-19 15:08:18 -040087 Log.i(TAG, "SipService thread running...");
Emeric Vigiereaf2c492012-09-19 14:38:20 -040088 SipService sipService = SipService.this;
89 while(sipService.runFlag) {
90 try {
Emeric Vigiereaf2c492012-09-19 14:38:20 -040091 Thread.sleep(DELAY);
92 } catch (InterruptedException e) {
93 sipService.runFlag = false;
94 Log.w(TAG, "service thread interrupted!");
95 }
96 }
97 }
98 }
99}