| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook.internal;
|
|
|
18 |
|
|
|
19 |
import android.content.ComponentName;
|
|
|
20 |
import android.content.Context;
|
|
|
21 |
import android.content.Intent;
|
|
|
22 |
import android.content.ServiceConnection;
|
|
|
23 |
import android.os.*;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* com.facebook.internal is solely for the use of other packages within the Facebook SDK for Android. Use of
|
|
|
27 |
* any of the classes in this package is unsupported, and they may be modified or removed without warning at
|
|
|
28 |
* any time.
|
|
|
29 |
*/
|
|
|
30 |
abstract public class PlatformServiceClient implements ServiceConnection {
|
|
|
31 |
private final Context context;
|
|
|
32 |
private final Handler handler;
|
|
|
33 |
private CompletedListener listener;
|
|
|
34 |
private boolean running;
|
|
|
35 |
private Messenger sender;
|
|
|
36 |
private int requestMessage;
|
|
|
37 |
private int replyMessage;
|
|
|
38 |
private final String applicationId;
|
|
|
39 |
private final int protocolVersion;
|
|
|
40 |
|
|
|
41 |
public PlatformServiceClient(Context context, int requestMessage, int replyMessage, int protocolVersion,
|
|
|
42 |
String applicationId) {
|
|
|
43 |
Context applicationContext = context.getApplicationContext();
|
|
|
44 |
|
|
|
45 |
this.context = (applicationContext != null) ? applicationContext : context;
|
|
|
46 |
this.requestMessage = requestMessage;
|
|
|
47 |
this.replyMessage = replyMessage;
|
|
|
48 |
this.applicationId = applicationId;
|
|
|
49 |
this.protocolVersion = protocolVersion;
|
|
|
50 |
|
|
|
51 |
handler = new Handler() {
|
|
|
52 |
@Override
|
|
|
53 |
public void handleMessage(Message message) {
|
|
|
54 |
PlatformServiceClient.this.handleMessage(message);
|
|
|
55 |
}
|
|
|
56 |
};
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public void setCompletedListener(CompletedListener listener) {
|
|
|
60 |
this.listener = listener;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
protected Context getContext() {
|
|
|
64 |
return context;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public boolean start() {
|
|
|
68 |
if (running) {
|
|
|
69 |
return false;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Make sure that the service can handle the requested protocol version
|
|
|
73 |
int availableVersion = NativeProtocol.getLatestAvailableProtocolVersionForService(context, protocolVersion);
|
|
|
74 |
if (availableVersion == NativeProtocol.NO_PROTOCOL_AVAILABLE) {
|
|
|
75 |
return false;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
Intent intent = NativeProtocol.createPlatformServiceIntent(context);
|
|
|
79 |
if (intent == null) {
|
|
|
80 |
return false;
|
|
|
81 |
} else {
|
|
|
82 |
running = true;
|
|
|
83 |
context.bindService(intent, this, Context.BIND_AUTO_CREATE);
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public void cancel() {
|
|
|
89 |
running = false;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
93 |
sender = new Messenger(service);
|
|
|
94 |
sendMessage();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public void onServiceDisconnected(ComponentName name) {
|
|
|
98 |
sender = null;
|
|
|
99 |
try {
|
|
|
100 |
context.unbindService(this);
|
|
|
101 |
} catch (IllegalArgumentException ex) {
|
|
|
102 |
// Do nothing, the connection was already unbound
|
|
|
103 |
}
|
|
|
104 |
callback(null);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private void sendMessage() {
|
|
|
108 |
Bundle data = new Bundle();
|
|
|
109 |
data.putString(NativeProtocol.EXTRA_APPLICATION_ID, applicationId);
|
|
|
110 |
|
|
|
111 |
populateRequestBundle(data);
|
|
|
112 |
|
|
|
113 |
Message request = Message.obtain(null, requestMessage);
|
|
|
114 |
request.arg1 = protocolVersion;
|
|
|
115 |
request.setData(data);
|
|
|
116 |
request.replyTo = new Messenger(handler);
|
|
|
117 |
|
|
|
118 |
try {
|
|
|
119 |
sender.send(request);
|
|
|
120 |
} catch (RemoteException e) {
|
|
|
121 |
callback(null);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
protected abstract void populateRequestBundle(Bundle data);
|
|
|
126 |
|
|
|
127 |
protected void handleMessage(Message message) {
|
|
|
128 |
if (message.what == replyMessage) {
|
|
|
129 |
Bundle extras = message.getData();
|
|
|
130 |
String errorType = extras.getString(NativeProtocol.STATUS_ERROR_TYPE);
|
|
|
131 |
if (errorType != null) {
|
|
|
132 |
callback(null);
|
|
|
133 |
} else {
|
|
|
134 |
callback(extras);
|
|
|
135 |
}
|
|
|
136 |
context.unbindService(this);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
private void callback(Bundle result) {
|
|
|
141 |
if (!running) {
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
running = false;
|
|
|
145 |
|
|
|
146 |
CompletedListener callback = listener;
|
|
|
147 |
if (callback != null) {
|
|
|
148 |
callback.completed(result);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public interface CompletedListener {
|
|
|
153 |
void completed(Bundle result);
|
|
|
154 |
}
|
|
|
155 |
}
|