Nextrek
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
package nextrek.services;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public abstract class ServiceConnector {
final static private String TAG = "ServiceConnector";
/**
* Command to the service to register a client, receiving callbacks
* from the service. The Message's replyTo field must be a Messenger of
* the client where callbacks should be sent.
*/
static final int MSG_REGISTER_CLIENT = 1;
/**
* Command to the service to unregister a client, ot stop receiving callbacks
* from the service. The Message's replyTo field must be a Messenger of
* the client as previously given with MSG_REGISTER_CLIENT.
*/
static final int MSG_UNREGISTER_CLIENT = 2;
/**
* Command to service to add a photo to the queue. This can be sent to the
* service to supply a new value, and will be sent by the service to
* any registered clients with the new value.
*/
static final int MSG_SEND = 3;
/** Keeps track of all current registered clients. */
static ArrayList<Messenger> mClients = new ArrayList<Messenger>();
public ServiceConnector() {
}
/**
* Handler of incoming messages from clients.
*/
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REGISTER_CLIENT:
// don't add duplicates!!!
synchronized (mClients) {
if (!mClients.contains(msg.replyTo)) {
mClients.add(msg.replyTo);
}
}
break;
case MSG_UNREGISTER_CLIENT:
synchronized (mClients) {
mClients.remove(msg.replyTo);
}
break;
case MSG_SEND:
try {
Bundle bundle = (Bundle) msg.obj;
onHandleRequest(bundle);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
break;
default:
super.handleMessage(msg);
}
}
};
final Messenger mMessenger = new Messenger(mHandler);
public IBinder getBinder() {
return mMessenger.getBinder();
}
public void sendProgress(Bundle bundle) {
for (int i = mClients.size() - 1; i >= 0; i--) {
try {
Message msg = Message.obtain(null, ClientConnector.MSG_WORK_PROGRESS, bundle);
mClients.get(i).send(msg);
} catch (RemoteException e) {
// The client is dead. Remove it from the list;
// we are going through the list from back to front
// so this is safe to do inside the loop.
mClients.remove(i);
}
}
}
protected abstract void onHandleRequest(Bundle bundle);
}
|
Revision |
Author |
Commited |
Message |
4
|
FMMortaroli
|
Fri 19 Apr, 2013 16:54:38 +0000 |
|