author | Da Risk <darisk972@gmail.com> |
Fri, 20 Mar 2009 23:21:05 +0100 | |
changeset 32 | 4d26854143e2 |
parent 31 | 85faac00d92c |
child 33 | 0e65d5f55d2f |
permissions | -rw-r--r-- |
17 | 1 |
/** |
2 |
* |
|
3 |
*/ |
|
4 |
package com.beem.project.beem; |
|
5 |
||
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
6 |
import java.util.Collection; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
7 |
import java.util.HashSet; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
8 |
import java.util.Set; |
18 | 9 |
|
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
10 |
import org.jivesoftware.smack.Roster; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
11 |
import org.jivesoftware.smack.RosterListener; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
12 |
import org.jivesoftware.smack.packet.Presence; |
17 | 13 |
|
14 |
import android.app.Notification; |
|
15 |
import android.app.NotificationManager; |
|
16 |
import android.app.PendingIntent; |
|
17 |
import android.app.Service; |
|
18 |
import android.content.Intent; |
|
19
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
19 |
import android.content.SharedPreferences; |
17 | 20 |
import android.os.IBinder; |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
21 |
import android.os.Looper; |
18 | 22 |
import android.os.RemoteException; |
17 | 23 |
import android.widget.Toast; |
24 |
||
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
25 |
import com.beem.project.beem.service.XMPPConnectionAdapter; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
26 |
import com.beem.project.beem.service.aidl.IXMPPConnection; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
27 |
import com.beem.project.beem.service.aidl.IXMPPFacade; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
28 |
|
17 | 29 |
/** |
30 |
* @author darisk |
|
32 | 31 |
* |
17 | 32 |
*/ |
33 |
public class BeemService extends Service { |
|
34 |
||
35 |
private NotificationManager notificationManager; |
|
36 |
||
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
37 |
private IXMPPConnection connection; |
19
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
38 |
private SharedPreferences settings; |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
39 |
private String mLogin; |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
40 |
private String mPassword; |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
41 |
private String mHost; |
18 | 42 |
private IXMPPFacade.Stub bind = new IXMPPFacade.Stub() { |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
43 |
|
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
44 |
@Override |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
45 |
public IXMPPConnection getXMPPConnection() throws RemoteException { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
46 |
return connection; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
47 |
} |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
48 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
49 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
50 |
@Override |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
51 |
public void disconnect() throws RemoteException { |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
52 |
connection.disconnect(); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
53 |
} |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
54 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
55 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
56 |
@Override |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
57 |
public void connectAsync() throws RemoteException { |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
58 |
connection.connectAsync(mLogin, mPassword, "BEEM"); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
59 |
} |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
60 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
61 |
|
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
62 |
@Override |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
63 |
public void connectSync() throws RemoteException { |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
64 |
connection.connectSync(mLogin, mPassword, "BEEM"); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
65 |
} |
18 | 66 |
}; |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
67 |
|
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
68 |
/* |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
69 |
* (non-Javadoc) |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
70 |
* |
17 | 71 |
* @see android.app.Service#onBind(android.content.Intent) |
72 |
*/ |
|
73 |
@Override |
|
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
74 |
public IBinder onBind(Intent intent) { |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
75 |
showBasicNotification(R.string.BeemServiceCreated); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
76 |
return bind; |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
77 |
// to forbid a client to bind |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
78 |
// return null; |
17 | 79 |
} |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
80 |
|
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
81 |
@Override |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
82 |
public void onCreate() { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
83 |
super.onCreate(); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
84 |
settings = getSharedPreferences(getString(R.string.PreferenceFileName), |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
85 |
MODE_PRIVATE); |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
86 |
mLogin = settings.getString(getString(R.string.PreferenceLoginKey), ""); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
87 |
mPassword = settings.getString( |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
88 |
getString(R.string.PreferencePasswordKey), ""); |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
89 |
mHost = settings.getString(getString(R.string.PreferenceHostKey), ""); |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
90 |
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
91 |
connection = new XMPPConnectionAdapter("10.0.2.2"); // address |
17 | 92 |
} |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
93 |
|
19
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
94 |
@Override |
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
95 |
public void onStart(Intent intent, int startId) { |
31
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
96 |
try { |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
97 |
connection.connectSync(mLogin, mPassword, "BEEM"); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
98 |
} catch (RemoteException e) { |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
99 |
// TODO Auto-generated catch block |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
100 |
e.printStackTrace(); |
85faac00d92c
Asynchronous service almost perfect.
Da Risk <darisk972@gmail.com>
parents:
29
diff
changeset
|
101 |
} |
17 | 102 |
} |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
103 |
|
19
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
104 |
@Override |
17 | 105 |
public void onDestroy() { |
19
213b84d2e743
Improve the service. It connect with a xmpp server running on the
Da Risk <darisk972@gmail.com>
parents:
18
diff
changeset
|
106 |
closeConnection(); |
29
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
107 |
showBasicNotification(R.string.BeemServiceDestroyed); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
108 |
} |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
109 |
|
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
110 |
private void showBasicNotification(int stringResource) { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
111 |
String text = (String) getText(stringResource); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
112 |
Notification notif = new Notification(R.drawable.logo, text, System |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
113 |
.currentTimeMillis()); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
114 |
notif.defaults = Notification.DEFAULT_ALL; |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
115 |
notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity( |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
116 |
this, 0, new Intent(), 0)); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
117 |
notificationManager.notify(stringResource, notif); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
118 |
} |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
119 |
|
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
120 |
private void closeConnection() { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
121 |
if (connection != null) |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
122 |
try { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
123 |
connection.disconnect(); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
124 |
} catch (RemoteException e) { |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
125 |
// TODO Auto-generated catch block |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
126 |
e.printStackTrace(); |
a49d1556772c
Starting work on the Service interface.
Da Risk <darisk972@gmail.com>
parents:
19
diff
changeset
|
127 |
} |
17 | 128 |
} |
129 |
||
130 |
} |