Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.android;
18
 
19
import android.content.Context;
20
import android.os.Bundle;
21
 
22
import java.io.FileNotFoundException;
23
import java.io.IOException;
24
import java.net.MalformedURLException;
25
 
26
/**
27
 * A sample implementation of asynchronous API requests. This class provides
28
 * the ability to execute API methods and have the call return immediately,
29
 * without blocking the calling thread. This is necessary when accessing the
30
 * API in the UI thread, for instance. The request response is returned to 
31
 * the caller via a callback interface, which the developer must implement.
32
 *
33
 * This sample implementation simply spawns a new thread for each request,
34
 * and makes the API call immediately.  This may work in many applications,
35
 * but more sophisticated users may re-implement this behavior using a thread
36
 * pool, a network thread, a request queue, or other mechanism.  Advanced
37
 * functionality could be built, such as rate-limiting of requests, as per
38
 * a specific application's needs.
39
 *
40
 * @deprecated
41
 *
42
 * @see RequestListener
43
 *        The callback interface.
44
 *
45
 * @author  Jim Brusstar (jimbru@fb.com),
46
 *          Yariv Sadan (yariv@fb.com),
47
 *          Luke Shepard (lshepard@fb.com)
48
 */
49
@Deprecated
50
public class AsyncFacebookRunner {
51
 
52
    Facebook fb;
53
 
54
    public AsyncFacebookRunner(Facebook fb) {
55
        this.fb = fb;
56
    }
57
 
58
    /**
59
     * Invalidate the current user session by removing the access token in
60
     * memory, clearing the browser cookies, and calling auth.expireSession
61
     * through the API. The application will be notified when logout is
62
     * complete via the callback interface.
63
     * <p/>
64
     * Note that this method is asynchronous and the callback will be invoked
65
     * in a background thread; operations that affect the UI will need to be
66
     * posted to the UI thread or an appropriate handler.
67
     * <p/>
68
     * This method is deprecated.  See {@link Facebook} and {@link com.facebook.Session} for more info.
69
     *
70
     * @param context
71
     *            The Android context in which the logout should be called: it
72
     *            should be the same context in which the login occurred in
73
     *            order to clear any stored cookies
74
     * @param listener
75
     *            Callback interface to notify the application when the request
76
     *            has completed.
77
     * @param state
78
     *            An arbitrary object used to identify the request when it
79
     *            returns to the callback. This has no effect on the request
80
     *            itself.
81
     */
82
    @Deprecated
83
    public void logout(final Context context,
84
                       final RequestListener listener,
85
                       final Object state) {
86
        new Thread() {
87
            @Override public void run() {
88
                try {
89
                    String response = fb.logoutImpl(context);
90
                    if (response.length() == 0 || response.equals("false")){
91
                        listener.onFacebookError(new FacebookError(
92
                                "auth.expireSession failed"), state);
93
                        return;
94
                    }
95
                    listener.onComplete(response, state);
96
                } catch (FileNotFoundException e) {
97
                    listener.onFileNotFoundException(e, state);
98
                } catch (MalformedURLException e) {
99
                    listener.onMalformedURLException(e, state);
100
                } catch (IOException e) {
101
                    listener.onIOException(e, state);
102
                }
103
            }
104
        }.start();
105
    }
106
 
107
    @Deprecated
108
    public void logout(final Context context, final RequestListener listener) {
109
        logout(context, listener, /* state */ null);
110
    }
111
 
112
    /**
113
     * Make a request to Facebook's old (pre-graph) API with the given
114
     * parameters. One of the parameter keys must be "method" and its value
115
     * should be a valid REST server API method.
116
     * <p/>
117
     * See http://developers.facebook.com/docs/reference/rest/
118
     * <p/>
119
     * Note that this method is asynchronous and the callback will be invoked
120
     * in a background thread; operations that affect the UI will need to be
121
     * posted to the UI thread or an appropriate handler.
122
     * <p/>
123
     * Example:
124
     * <code>
125
     *  Bundle parameters = new Bundle();
126
     *  parameters.putString("method", "auth.expireSession", new Listener());
127
     *  String response = request(parameters);
128
     * </code>
129
     * <p/>
130
     * This method is deprecated.  See {@link Facebook} and {@link com.facebook.Request} for more info.
131
     *
132
     * @param parameters
133
     *            Key-value pairs of parameters to the request. Refer to the
134
     *            documentation: one of the parameters must be "method".
135
     * @param listener
136
     *            Callback interface to notify the application when the request
137
     *            has completed.
138
     * @param state
139
     *            An arbitrary object used to identify the request when it
140
     *            returns to the callback. This has no effect on the request
141
     *            itself.
142
     */
143
    @Deprecated
144
    public void request(Bundle parameters,
145
                        RequestListener listener,
146
                        final Object state) {
147
        request(null, parameters, "GET", listener, state);
148
    }
149
 
150
    @Deprecated
151
    public void request(Bundle parameters, RequestListener listener) {
152
        request(null, parameters, "GET", listener, /* state */ null);
153
    }
154
 
155
    /**
156
     * Make a request to the Facebook Graph API without any parameters.
157
     * <p/>
158
     * See http://developers.facebook.com/docs/api
159
     * <p/>
160
     * Note that this method is asynchronous and the callback will be invoked
161
     * in a background thread; operations that affect the UI will need to be
162
     * posted to the UI thread or an appropriate handler.
163
     * <p/>
164
     * This method is deprecated.  See {@link Facebook} and {@link com.facebook.Request} for more info.
165
     *
166
     * @param graphPath
167
     *            Path to resource in the Facebook graph, e.g., to fetch data
168
     *            about the currently logged authenticated user, provide "me",
169
     *            which will fetch http://graph.facebook.com/me
170
     * @param listener
171
     *            Callback interface to notify the application when the request
172
     *            has completed.
173
     * @param state
174
     *            An arbitrary object used to identify the request when it
175
     *            returns to the callback. This has no effect on the request
176
     *            itself.
177
     */
178
    @Deprecated
179
    public void request(String graphPath,
180
                        RequestListener listener,
181
                        final Object state) {
182
        request(graphPath, new Bundle(), "GET", listener, state);
183
    }
184
 
185
    @Deprecated
186
    public void request(String graphPath, RequestListener listener) {
187
        request(graphPath, new Bundle(), "GET", listener, /* state */ null);
188
    }
189
 
190
    /**
191
     * Make a request to the Facebook Graph API with the given string parameters
192
     * using an HTTP GET (default method).
193
     * <p/>
194
     * See http://developers.facebook.com/docs/api
195
     * <p/>
196
     * Note that this method is asynchronous and the callback will be invoked
197
     * in a background thread; operations that affect the UI will need to be
198
     * posted to the UI thread or an appropriate handler.
199
     * <p/>
200
     * This method is deprecated.  See {@link Facebook} and {@link com.facebook.Request} for more info.
201
     *
202
     * @param graphPath
203
     *            Path to resource in the Facebook graph, e.g., to fetch data
204
     *            about the currently logged authenticated user, provide "me",
205
     *            which will fetch http://graph.facebook.com/me
206
     * @param parameters
207
     *            key-value string parameters, e.g. the path "search" with
208
     *            parameters "q" : "facebook" would produce a query for the
209
     *            following graph resource:
210
     *            https://graph.facebook.com/search?q=facebook
211
     * @param listener
212
     *            Callback interface to notify the application when the request
213
     *            has completed.
214
     * @param state
215
     *            An arbitrary object used to identify the request when it
216
     *            returns to the callback. This has no effect on the request
217
     *            itself.
218
     */
219
    @Deprecated
220
    public void request(String graphPath,
221
                        Bundle parameters,
222
                        RequestListener listener,
223
                        final Object state) {
224
        request(graphPath, parameters, "GET", listener, state);
225
    }
226
 
227
    @Deprecated
228
    public void request(String graphPath,
229
                        Bundle parameters,
230
                        RequestListener listener) {
231
        request(graphPath, parameters, "GET", listener, /* state */ null);
232
    }
233
 
234
    /**
235
     * Make a request to the Facebook Graph API with the given HTTP method and
236
     * string parameters. Note that binary data parameters (e.g. pictures) are
237
     * not yet supported by this helper function.
238
     * <p/>
239
     * See http://developers.facebook.com/docs/api
240
     * <p/>
241
     * Note that this method is asynchronous and the callback will be invoked
242
     * in a background thread; operations that affect the UI will need to be
243
     * posted to the UI thread or an appropriate handler.
244
     * <p/>
245
     * This method is deprecated.  See {@link Facebook} and {@link com.facebook.Request} for more info.
246
     *
247
     * @param graphPath
248
     *            Path to resource in the Facebook graph, e.g., to fetch data
249
     *            about the currently logged authenticated user, provide "me",
250
     *            which will fetch http://graph.facebook.com/me
251
     * @param parameters
252
     *            key-value string parameters, e.g. the path "search" with
253
     *            parameters {"q" : "facebook"} would produce a query for the
254
     *            following graph resource:
255
     *            https://graph.facebook.com/search?q=facebook
256
     * @param httpMethod
257
     *            http verb, e.g. "POST", "DELETE"
258
     * @param listener
259
     *            Callback interface to notify the application when the request
260
     *            has completed.
261
     * @param state
262
     *            An arbitrary object used to identify the request when it
263
     *            returns to the callback. This has no effect on the request
264
     *            itself.
265
     */
266
    @Deprecated
267
    public void request(final String graphPath,
268
                        final Bundle parameters,
269
                        final String httpMethod,
270
                        final RequestListener listener,
271
                        final Object state) {
272
        new Thread() {
273
            @Override public void run() {
274
                try {
275
                    String resp = fb.requestImpl(graphPath, parameters, httpMethod);
276
                    listener.onComplete(resp, state);
277
                } catch (FileNotFoundException e) {
278
                    listener.onFileNotFoundException(e, state);
279
                } catch (MalformedURLException e) {
280
                    listener.onMalformedURLException(e, state);
281
                } catch (IOException e) {
282
                    listener.onIOException(e, state);
283
                }
284
            }
285
        }.start();
286
    }
287
 
288
    /**
289
     * Callback interface for API requests.
290
     * <p/>
291
     * Each method includes a 'state' parameter that identifies the calling
292
     * request. It will be set to the value passed when originally calling the
293
     * request method, or null if none was passed.
294
     * <p/>
295
     * This interface is deprecated.  See {@link Facebook} and {@link com.facebook.Request} for more info.
296
     */
297
    @Deprecated
298
    public static interface RequestListener {
299
 
300
        /**
301
         * Called when a request completes with the given response.
302
         *
303
         * Executed by a background thread: do not update the UI in this method.
304
         */
305
        public void onComplete(String response, Object state);
306
 
307
        /**
308
         * Called when a request has a network or request error.
309
         *
310
         * Executed by a background thread: do not update the UI in this method.
311
         */
312
        public void onIOException(IOException e, Object state);
313
 
314
        /**
315
         * Called when a request fails because the requested resource is
316
         * invalid or does not exist.
317
         *
318
         * Executed by a background thread: do not update the UI in this method.
319
         */
320
        public void onFileNotFoundException(FileNotFoundException e,
321
                                            Object state);
322
 
323
        /**
324
         * Called if an invalid graph path is provided (which may result in a
325
         * malformed URL).
326
         *
327
         * Executed by a background thread: do not update the UI in this method.
328
         */
329
        public void onMalformedURLException(MalformedURLException e,
330
                                            Object state);
331
 
332
        /**
333
         * Called when the server-side Facebook method fails.
334
         *
335
         * Executed by a background thread: do not update the UI in this method.
336
         */
337
        public void onFacebookError(FacebookError e, Object state);
338
 
339
    }
340
 
341
}