|
| 1 | +/* |
| 2 | + * Copyright 2010 Facebook, Inc. |
| 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 java.io.FileNotFoundException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.MalformedURLException; |
| 22 | + |
| 23 | +import android.content.Context; |
| 24 | +import android.os.Bundle; |
| 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 | + * @see RequestListener |
| 41 | + * The callback interface. |
| 42 | + * |
| 43 | + |
| 44 | + * |
| 45 | + */ |
| 46 | +public class AsyncFacebookRunner { |
| 47 | + |
| 48 | + Facebook fb; |
| 49 | + |
| 50 | + public AsyncFacebookRunner(Facebook fb) { |
| 51 | + this.fb = fb; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Invalidate the current user session by removing the access token in |
| 56 | + * memory, clearing the browser cookies, and calling auth.expireSession |
| 57 | + * through the API. The application will be notified when logout is |
| 58 | + * complete via the callback interface. |
| 59 | + * |
| 60 | + * Note that this method is asynchronous and the callback will be invoked |
| 61 | + * in a background thread; operations that affect the UI will need to be |
| 62 | + * posted to the UI thread or an appropriate handler. |
| 63 | + * |
| 64 | + * @param context |
| 65 | + * The Android context in which the logout should be called: it |
| 66 | + * should be the same context in which the login occurred in |
| 67 | + * order to clear any stored cookies |
| 68 | + * @param listener |
| 69 | + * Callback interface to notify the application when the request |
| 70 | + * has completed. |
| 71 | + */ |
| 72 | + public void logout(final Context context, final RequestListener listener) { |
| 73 | + new Thread() { |
| 74 | + @Override public void run() { |
| 75 | + try { |
| 76 | + String response = fb.logout(context); |
| 77 | + if (response.length() == 0 || response.equals("false")){ |
| 78 | + listener.onFacebookError(new FacebookError( |
| 79 | + "auth.expireSession failed")); |
| 80 | + return; |
| 81 | + } |
| 82 | + listener.onComplete(response); |
| 83 | + } catch (FileNotFoundException e) { |
| 84 | + listener.onFileNotFoundException(e); |
| 85 | + } catch (MalformedURLException e) { |
| 86 | + listener.onMalformedURLException(e); |
| 87 | + } catch (IOException e) { |
| 88 | + listener.onIOException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + }.start(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Make a request to Facebook's old (pre-graph) API with the given |
| 96 | + * parameters. One of the parameter keys must be "method" and its value |
| 97 | + * should be a valid REST server API method. |
| 98 | + * |
| 99 | + * |
| 100 | + * See http://developers.facebook.com/docs/reference/rest/ |
| 101 | + * |
| 102 | + * Note that this method is asynchronous and the callback will be invoked |
| 103 | + * in a background thread; operations that affect the UI will need to be |
| 104 | + * posted to the UI thread or an appropriate handler. |
| 105 | + * |
| 106 | + * Example: |
| 107 | + * <code> |
| 108 | + * Bundle parameters = new Bundle(); |
| 109 | + * parameters.putString("method", "auth.expireSession", new Listener()); |
| 110 | + * String response = request(parameters); |
| 111 | + * </code> |
| 112 | + * |
| 113 | + * @param parameters |
| 114 | + * Key-value pairs of parameters to the request. Refer to the |
| 115 | + * documentation: one of the parameters must be "method". |
| 116 | + * @param listener |
| 117 | + * Callback interface to notify the application when the request |
| 118 | + * has completed. |
| 119 | + */ |
| 120 | + public void request(Bundle parameters, |
| 121 | + RequestListener listener) { |
| 122 | + request(null, parameters, "GET", listener); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Make a request to the Facebook Graph API without any parameters. |
| 127 | + * |
| 128 | + * See http://developers.facebook.com/docs/api |
| 129 | + * |
| 130 | + * Note that this method is asynchronous and the callback will be invoked |
| 131 | + * in a background thread; operations that affect the UI will need to be |
| 132 | + * posted to the UI thread or an appropriate handler. |
| 133 | + * |
| 134 | + * @param graphPath |
| 135 | + * Path to resource in the Facebook graph, e.g., to fetch data |
| 136 | + * about the currently logged authenticated user, provide "me", |
| 137 | + * which will fetch http://graph.facebook.com/me |
| 138 | + * @param listener |
| 139 | + * Callback interface to notify the application when the request |
| 140 | + * has completed. |
| 141 | + */ |
| 142 | + public void request(String graphPath, |
| 143 | + RequestListener listener) { |
| 144 | + request(graphPath, new Bundle(), "GET", listener); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Make a request to the Facebook Graph API with the given string parameters |
| 149 | + * using an HTTP GET (default method). |
| 150 | + * |
| 151 | + * See http://developers.facebook.com/docs/api |
| 152 | + * |
| 153 | + * Note that this method is asynchronous and the callback will be invoked |
| 154 | + * in a background thread; operations that affect the UI will need to be |
| 155 | + * posted to the UI thread or an appropriate handler. |
| 156 | + * |
| 157 | + * @param graphPath |
| 158 | + * Path to resource in the Facebook graph, e.g., to fetch data |
| 159 | + * about the currently logged authenticated user, provide "me", |
| 160 | + * which will fetch http://graph.facebook.com/me |
| 161 | + * @param parameters |
| 162 | + * key-value string parameters, e.g. the path "search" with |
| 163 | + * parameters "q" : "facebook" would produce a query for the |
| 164 | + * following graph resource: |
| 165 | + * https://graph.facebook.com/search?q=facebook |
| 166 | + * @param listener |
| 167 | + * Callback interface to notify the application when the request |
| 168 | + * has completed. |
| 169 | + */ |
| 170 | + public void request(String graphPath, |
| 171 | + Bundle parameters, |
| 172 | + RequestListener listener) { |
| 173 | + request(graphPath, parameters, "GET", listener); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Make a request to the Facebook Graph API with the given HTTP method and |
| 178 | + * string parameters. Note that binary data parameters (e.g. pictures) are |
| 179 | + * not yet supported by this helper function. |
| 180 | + * |
| 181 | + * See http://developers.facebook.com/docs/api |
| 182 | + * |
| 183 | + * Note that this method is asynchronous and the callback will be invoked |
| 184 | + * in a background thread; operations that affect the UI will need to be |
| 185 | + * posted to the UI thread or an appropriate handler. |
| 186 | + * |
| 187 | + * @param graphPath |
| 188 | + * Path to resource in the Facebook graph, e.g., to fetch data |
| 189 | + * about the currently logged authenticated user, provide "me", |
| 190 | + * which will fetch http://graph.facebook.com/me |
| 191 | + * @param parameters |
| 192 | + * key-value string parameters, e.g. the path "search" with |
| 193 | + * parameters {"q" : "facebook"} would produce a query for the |
| 194 | + * following graph resource: |
| 195 | + * https://graph.facebook.com/search?q=facebook |
| 196 | + * @param httpMethod |
| 197 | + * http verb, e.g. "POST", "DELETE" |
| 198 | + * @param listener |
| 199 | + * Callback interface to notify the application when the request |
| 200 | + * has completed. |
| 201 | + */ |
| 202 | + public void request(final String graphPath, |
| 203 | + final Bundle parameters, |
| 204 | + final String httpMethod, |
| 205 | + final RequestListener listener) { |
| 206 | + new Thread() { |
| 207 | + @Override public void run() { |
| 208 | + try { |
| 209 | + String resp = fb.request(graphPath, parameters, httpMethod); |
| 210 | + listener.onComplete(resp); |
| 211 | + } catch (FileNotFoundException e) { |
| 212 | + listener.onFileNotFoundException(e); |
| 213 | + } catch (MalformedURLException e) { |
| 214 | + listener.onMalformedURLException(e); |
| 215 | + } catch (IOException e) { |
| 216 | + listener.onIOException(e); |
| 217 | + } |
| 218 | + } |
| 219 | + }.start(); |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + /** |
| 224 | + * Callback interface for API requests. |
| 225 | + * |
| 226 | + */ |
| 227 | + public static interface RequestListener { |
| 228 | + |
| 229 | + /** |
| 230 | + * Called when a request completes with the given response. |
| 231 | + * |
| 232 | + * Executed by a background thread: do not update the UI in this method. |
| 233 | + */ |
| 234 | + public void onComplete(String response); |
| 235 | + |
| 236 | + /** |
| 237 | + * Called when a request has a network or request error. |
| 238 | + * |
| 239 | + * Executed by a background thread: do not update the UI in this method. |
| 240 | + */ |
| 241 | + public void onIOException(IOException e); |
| 242 | + |
| 243 | + /** |
| 244 | + * Called when a request fails because the requested resource is |
| 245 | + * invalid or does not exist. |
| 246 | + * |
| 247 | + * Executed by a background thread: do not update the UI in this method. |
| 248 | + */ |
| 249 | + public void onFileNotFoundException(FileNotFoundException e); |
| 250 | + |
| 251 | + /** |
| 252 | + * Called if an invalid graph path is provided (which may result in a |
| 253 | + * malformed URL). |
| 254 | + * |
| 255 | + * Executed by a background thread: do not update the UI in this method. |
| 256 | + */ |
| 257 | + public void onMalformedURLException(MalformedURLException e); |
| 258 | + |
| 259 | + /** |
| 260 | + * Called when the server-side Facebook method fails. |
| 261 | + * |
| 262 | + * Executed by a background thread: do not update the UI in this method. |
| 263 | + */ |
| 264 | + public void onFacebookError(FacebookError e); |
| 265 | + |
| 266 | + } |
| 267 | + |
| 268 | +} |
0 commit comments