| 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;
|
|
|
18 |
|
|
|
19 |
import android.os.Handler;
|
|
|
20 |
|
|
|
21 |
import java.util.*;
|
|
|
22 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* RequestBatch contains a list of Request objects that can be sent to Facebook in a single round-trip.
|
|
|
26 |
*/
|
|
|
27 |
public class RequestBatch extends AbstractList<Request> {
|
|
|
28 |
private static AtomicInteger idGenerator = new AtomicInteger();
|
|
|
29 |
|
|
|
30 |
private Handler callbackHandler;
|
|
|
31 |
private List<Request> requests = new ArrayList<Request>();
|
|
|
32 |
private int timeoutInMilliseconds = 0;
|
|
|
33 |
private final String id = Integer.valueOf(idGenerator.incrementAndGet()).toString();
|
|
|
34 |
private List<Callback> callbacks = new ArrayList<Callback>();
|
|
|
35 |
private String batchApplicationId;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Constructor. Creates an empty batch.
|
|
|
39 |
*/
|
|
|
40 |
public RequestBatch() {
|
|
|
41 |
this.requests = new ArrayList<Request>();
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Constructor.
|
|
|
46 |
* @param requests the requests to add to the batch
|
|
|
47 |
*/
|
|
|
48 |
public RequestBatch(Collection<Request> requests) {
|
|
|
49 |
this.requests = new ArrayList<Request>(requests);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Constructor.
|
|
|
54 |
* @param requests the requests to add to the batch
|
|
|
55 |
*/
|
|
|
56 |
public RequestBatch(Request... requests) {
|
|
|
57 |
this.requests = Arrays.asList(requests);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Constructor.
|
|
|
62 |
* @param requests the requests to add to the batch
|
|
|
63 |
*/
|
|
|
64 |
public RequestBatch(RequestBatch requests) {
|
|
|
65 |
this.requests = new ArrayList<Request>(requests);
|
|
|
66 |
this.callbackHandler = requests.callbackHandler;
|
|
|
67 |
this.timeoutInMilliseconds = requests.timeoutInMilliseconds;
|
|
|
68 |
this.callbacks = new ArrayList<Callback>(requests.callbacks);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Gets the timeout to wait for responses from the server before a timeout error occurs.
|
|
|
73 |
* @return the timeout, in milliseconds; 0 (the default) means do not timeout
|
|
|
74 |
*/
|
|
|
75 |
public int getTimeout() {
|
|
|
76 |
return timeoutInMilliseconds;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Sets the timeout to wait for responses from the server before a timeout error occurs.
|
|
|
81 |
* @param timeoutInMilliseconds the timeout, in milliseconds; 0 means do not timeout
|
|
|
82 |
*/
|
|
|
83 |
public void setTimeout(int timeoutInMilliseconds) {
|
|
|
84 |
if (timeoutInMilliseconds < 0) {
|
|
|
85 |
throw new IllegalArgumentException("Argument timeoutInMilliseconds must be >= 0.");
|
|
|
86 |
}
|
|
|
87 |
this.timeoutInMilliseconds = timeoutInMilliseconds;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Adds a batch-level callback which will be called when the entire batch has finished executing.
|
|
|
92 |
*
|
|
|
93 |
* @param callback the callback
|
|
|
94 |
*/
|
|
|
95 |
public void addCallback(Callback callback) {
|
|
|
96 |
if (!callbacks.contains(callback)) {
|
|
|
97 |
callbacks.add(callback);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Removes a batch-level callback.
|
|
|
103 |
*
|
|
|
104 |
* @param callback the callback
|
|
|
105 |
*/
|
|
|
106 |
public void removeCallback(Callback callback) {
|
|
|
107 |
callbacks.remove(callback);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@Override
|
|
|
111 |
public final boolean add(Request request) {
|
|
|
112 |
return requests.add(request);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
@Override
|
|
|
116 |
public final void add(int location, Request request) {
|
|
|
117 |
requests.add(location, request);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
@Override
|
|
|
121 |
public final void clear() {
|
|
|
122 |
requests.clear();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
@Override
|
|
|
126 |
public final Request get(int i) {
|
|
|
127 |
return requests.get(i);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
@Override
|
|
|
131 |
public final Request remove(int location) {
|
|
|
132 |
return requests.remove(location);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
@Override
|
|
|
136 |
public final Request set(int location, Request request) {
|
|
|
137 |
return requests.set(location, request);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
@Override
|
|
|
141 |
public final int size() {
|
|
|
142 |
return requests.size();
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
final String getId() {
|
|
|
146 |
return id;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
final Handler getCallbackHandler() {
|
|
|
150 |
return callbackHandler;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
final void setCallbackHandler(Handler callbackHandler) {
|
|
|
154 |
this.callbackHandler = callbackHandler;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
final List<Request> getRequests() {
|
|
|
158 |
return requests;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
final List<Callback> getCallbacks() {
|
|
|
162 |
return callbacks;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
final String getBatchApplicationId() {
|
|
|
166 |
return batchApplicationId;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
final void setBatchApplicationId(String batchApplicationId) {
|
|
|
170 |
this.batchApplicationId = batchApplicationId;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Executes this batch on the current thread and returns the responses.
|
|
|
175 |
* <p/>
|
|
|
176 |
* This should only be used if you have transitioned off the UI thread.
|
|
|
177 |
*
|
|
|
178 |
* @return a list of Response objects representing the results of the requests; responses are returned in the same
|
|
|
179 |
* order as the requests were specified.
|
|
|
180 |
*
|
|
|
181 |
* @throws FacebookException
|
|
|
182 |
* If there was an error in the protocol used to communicate with the service
|
|
|
183 |
* @throws IllegalArgumentException if the passed in RequestBatch is empty
|
|
|
184 |
* @throws NullPointerException if the passed in RequestBatch or any of its contents are null
|
|
|
185 |
*/
|
|
|
186 |
public final List<Response> executeAndWait() {
|
|
|
187 |
return executeAndWaitImpl();
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Executes this batch asynchronously. This function will return immediately, and the batch will
|
|
|
192 |
* be processed on a separate thread. In order to process results of a request, or determine
|
|
|
193 |
* whether a request succeeded or failed, a callback must be specified (see
|
|
|
194 |
* {@link Request#setCallback(com.facebook.Request.Callback)})
|
|
|
195 |
* <p/>
|
|
|
196 |
* This should only be called from the UI thread.
|
|
|
197 |
*
|
|
|
198 |
* @return a RequestAsyncTask that is executing the request
|
|
|
199 |
*
|
|
|
200 |
* @throws IllegalArgumentException if this batch is empty
|
|
|
201 |
* @throws NullPointerException if any of the contents of this batch are null
|
|
|
202 |
*/
|
|
|
203 |
public final RequestAsyncTask executeAsync() {
|
|
|
204 |
return executeAsyncImpl();
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Specifies the interface that consumers of the RequestBatch class can implement in order to be notified when the
|
|
|
209 |
* entire batch completes execution. It will be called after all per-Request callbacks are called.
|
|
|
210 |
*/
|
|
|
211 |
public interface Callback {
|
|
|
212 |
/**
|
|
|
213 |
* The method that will be called when a batch completes.
|
|
|
214 |
*
|
|
|
215 |
* @param batch the RequestBatch containing the Requests which were executed
|
|
|
216 |
*/
|
|
|
217 |
void onBatchCompleted(RequestBatch batch);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Specifies the interface that consumers of the RequestBatch class can implement in order to be notified when the
|
|
|
222 |
* batch makes progress. The frequency of the callbacks can be controlled using
|
|
|
223 |
* {@link com.facebook.Settings#setOnProgressThreshold(long)}.
|
|
|
224 |
*/
|
|
|
225 |
public interface OnProgressCallback extends Callback {
|
|
|
226 |
/**
|
|
|
227 |
* The method that will be called when a batch makes progress.
|
|
|
228 |
*
|
|
|
229 |
* @param batch the RequestBatch containing the Requests which were executed
|
|
|
230 |
* @param current the current value of the progress
|
|
|
231 |
* @param max the max (target) value of the progress
|
|
|
232 |
*/
|
|
|
233 |
void onBatchProgress(RequestBatch batch, long current, long max);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
List<Response> executeAndWaitImpl() {
|
|
|
237 |
return Request.executeBatchAndWait(this);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
RequestAsyncTask executeAsyncImpl() {
|
|
|
241 |
return Request.executeBatchAsync(this);
|
|
|
242 |
}
|
|
|
243 |
}
|