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