Skip to content

Commit b63ba97

Browse files
Jos ShepherdJesse MacFadyen
authored andcommitted
Facebook Android SDK plugin start
1 parent f3fdae6 commit b63ba97

File tree

9 files changed

+1796
-0
lines changed

9 files changed

+1796
-0
lines changed

Android/Facebook/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Facebook for PhoneGap on Android #
2+
by Jos Shepherd
3+
4+
This is an attempt to make a PhoneGap plugin from the Facebook Android SDK:
5+
https://github.com/facebook/facebook-android-sdk
6+
7+
It is currently not functional (the login dialog is silently failing to appear)
8+
9+
10+
## Licence ##
11+
12+
The MIT License
13+
14+
Copyright (c) 2010 Jos Shepherd
15+
16+
Permission is hereby granted, free of charge, to any person obtaining a copy
17+
of this software and associated documentation files (the "Software"), to deal
18+
in the Software without restriction, including without limitation the rights
19+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
copies of the Software, and to permit persons to whom the Software is
21+
furnished to do so, subject to the following conditions:
22+
23+
The above copyright notice and this permission notice shall be included in
24+
all copies or substantial portions of the Software.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32+
THE SOFTWARE.
33+
34+
35+
36+
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
20+
* Encapsulation of Dialog Error.
21+
*
22+
23+
*/
24+
public class DialogError extends Throwable {
25+
26+
private static final long serialVersionUID = 1L;
27+
28+
/**
29+
* The ErrorCode received by the WebView: see
30+
* http://developer.android.com/reference/android/webkit/WebViewClient.html
31+
*/
32+
private int mErrorCode;
33+
34+
/** The URL that the dialog was trying to load */
35+
private String mFailingUrl;
36+
37+
public DialogError(String message, int errorCode, String failingUrl) {
38+
super(message);
39+
mErrorCode = errorCode;
40+
mFailingUrl = failingUrl;
41+
}
42+
43+
int getErrorCode() {
44+
return mErrorCode;
45+
}
46+
47+
String getFailingUrl() {
48+
return mFailingUrl;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)