Navigation Menu

Skip to content

Commit

Permalink
PhoneGap 1.5 / Cordova compabitibility changes. Fixes for Android to …
Browse files Browse the repository at this point in the history
…conform to latest Facebook SDKs, with the help of pull requests - 99, 110, and 190.
  • Loading branch information
Christine Abernathy committed Mar 26, 2012
1 parent 1b4ddd6 commit 13b7464
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -71,7 +71,7 @@ and get into the nitty-gritty for the platform of your choice:

1. [Create a basic Cordova Android application](http://www.phonegap.com/start/#android).

2. In the Cordova Android application you will need to put the following in your `res/xml/plugins.xml` file: <pre>&lt;plugin name="org.apache.cordova.facebook.Connect" value="org.apache.cordova.facebook.Connect" /&gt;</pre>
2. In the Cordova Android application you will need to put the following in your `res/xml/plugins.xml` file: <pre>&lt;plugin name="org.apache.cordova.facebook.Connect" value="org.apache.cordova.facebook.ConnectPlugin" /&gt;</pre>

3. You'll need to build + include the Facebook Android SDK and build + patch the
Facebook JavaScript SDK:
Expand All @@ -91,6 +91,8 @@ and get into the nitty-gritty for the platform of your choice:

5. From the Cordova Facebook Connect Plugin folder copy the `www/cdv-plugin-fb-connect.js` and `lib/facebook_js_sdk.js` files into your application's `assets/www` folder.

6. Temporary change to use a common plugin file. Modify cdv-plugin-fb-connect.js in your project, and replace **Cordova.exec** with **cordova.exec**. There is a current case sensitive incompatibility between the cordova-[RELEASE].js files for iOS and Android that should be resolved in a future release. When that happens, this step will be unnecessary.

Now you are ready to create your application! Check out the `example` folder for what the HTML, JS etc looks like. Note that you will need to replace your appId if you use the example index.html file.

You can run the application from either the command line (`ant clean && ant debug install`) or from Eclipse.
Expand Down
8 changes: 4 additions & 4 deletions example/Simple/www/index.html
Expand Up @@ -13,7 +13,8 @@

<div id="data">loading ...</div>

<script src="http://localhost:8080/target/target-script-min.js#anonymous"></script>
<!--<script src="http://localhost:8080/target/target-script-min.js#anonymous"></script>-->
<div id="fb-root"></div>
<!-- cordova -->
<script src="cordova-1.5.0.js"></script>
<!-- cordova facebook plugin -->
Expand All @@ -23,8 +24,8 @@

<script>

if (typeof Cordova == 'undefined') alert('Cordova variable does not exist. Check that you have included phonegap.js correctly');
if (typeof CDV == 'undefined') alert('CDV variable does not exist. Check that you have included pg-plugin-fb-connect.js correctly');
if ((typeof cordova == 'undefined') && (typeof Cordova == 'undefined')) alert('Cordova variable does not exist. Check that you have included cordova.js correctly');
if (typeof CDV == 'undefined') alert('CDV variable does not exist. Check that you have included cdv-plugin-fb-connect.js correctly');
if (typeof FB == 'undefined') alert('FB variable does not exist. Check that you have included the Facebook JS SDK file.');

FB.Event.subscribe('auth.login', function(response) {
Expand Down Expand Up @@ -110,7 +111,6 @@
var params = {
method: 'feed',
name: 'Facebook Dialogs',
from: '116204184',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
Expand Down
@@ -1,7 +1,8 @@
package com.phonegap.facebook;
package org.apache.cordova.facebook;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -20,8 +21,8 @@
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;

public class ConnectPlugin extends Plugin {

Expand All @@ -33,6 +34,8 @@ public class ConnectPlugin extends Plugin {
//used for dialog auth
private String[] permissions = new String[] {};
private String callbackId;
private Bundle paramBundle;
private String method;

@Override
public PluginResult execute(String action, JSONArray args, final String callbackId) {
Expand All @@ -47,7 +50,7 @@ public PluginResult execute(String action, JSONArray args, final String callback

Log.d(TAG, "init: Initializing plugin.");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx.getContext());
String access_token = prefs.getString("access_token", null);
Long expires = prefs.getLong("access_expires", -1);

Expand All @@ -70,7 +73,7 @@ public PluginResult execute(String action, JSONArray args, final String callback
}

if(facebook.isSessionValid() && this.userId != null) {
return new PluginResult(PluginResult.Status.OK, this.getResponse());
return new PluginResult(PluginResult.Status.OK, this.getResponse());
}
else {
return new PluginResult(PluginResult.Status.NO_RESULT);
Expand All @@ -81,7 +84,7 @@ public PluginResult execute(String action, JSONArray args, final String callback
return new PluginResult(PluginResult.Status.ERROR, "Invalid JSON args used. expected a string as the first arg.");
}
}

else if (action.equals("login")) {
if (facebook != null) {
if (facebook.isSessionValid()) {
Expand Down Expand Up @@ -114,13 +117,13 @@ public void run() {
pr = new PluginResult(PluginResult.Status.ERROR, "Must call init before login.");
}
}

else if (action.equals("logout")) {
if (facebook != null) {
try {
facebook.logout(this.ctx);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
facebook.logout(this.ctx.getContext());

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx.getContext());
prefs.edit().putLong("access_expires", -1).commit();
prefs.edit().putString("access_token", null).commit();
} catch (MalformedURLException e) {
Expand All @@ -145,6 +148,48 @@ else if (action.equals("getLoginStatus")) {
pr = new PluginResult(PluginResult.Status.ERROR, "Must call init before getLoginStatus.");
}
}

else if (action.equals("showDialog")) {
if (facebook != null) {
Bundle collect = new Bundle();
JSONObject params = null;
try {
params = args.getJSONObject(0);
} catch (JSONException e) {
params = new JSONObject();
}

final ConnectPlugin me = this;
Iterator<String> iter = params.keys();
while (iter.hasNext()) {
String key = iter.next();
if (key.equals("method")) {
try {
this.method = params.getString(key);
} catch (JSONException e) {
Log.w(TAG, "Nonstring method parameter provided to dialog");
}
} else {
try {
collect.putString(key, params.getString(key));
} catch (JSONException e) {
// Need to handle JSON parameters
Log.w(TAG, "Nonstring parameter provided to dialog discarded");
}
}
}
this.paramBundle = new Bundle(collect);
Runnable runnable = new Runnable() {
public void run() {
me.facebook.dialog (me.ctx.getContext(), me.method , me.paramBundle , new UIDialogListener(me));
};
};
this.ctx.runOnUiThread(runnable);
} else {
pr = new PluginResult(PluginResult.Status.ERROR, "Must call init before showDialog.");
}

}

return pr;
}
Expand All @@ -159,12 +204,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
public JSONObject getResponse() {
String response = "{"+
"\"status\": \""+(facebook.isSessionValid() ? "connected" : "unknown")+"\","+
"\"session\": {"+
"\"access_token\": \""+facebook.getAccessToken()+"\","+
"\"expires\": \""+facebook.getAccessExpires()+"\","+
"\"authResponse\": {"+
"\"accessToken\": \""+facebook.getAccessToken()+"\","+
"\"expiresIn\": \""+facebook.getAccessExpires()+"\","+
"\"session_key\": true,"+
"\"sig\": \"...\","+
"\"uid\": \""+this.userId+"\""+
"\"userId\": \""+this.userId+"\""+
"}"+
"}";

Expand All @@ -176,6 +221,36 @@ public JSONObject getResponse() {
}
return new JSONObject();
}

class UIDialogListener implements DialogListener {
final ConnectPlugin fba;

public UIDialogListener(ConnectPlugin fba){
super();
this.fba = fba;
}

public void onComplete(Bundle values) {
// Handle a successful dialog
Log.d(TAG,values.toString());
this.fba.success(new PluginResult(PluginResult.Status.OK), this.fba.callbackId);
}

public void onFacebookError(FacebookError e) {
Log.d(TAG, "facebook error");
this.fba.error("Facebook error: " + e.getMessage(), callbackId);
}

public void onError(DialogError e) {
Log.d(TAG, "other error");
this.fba.error("Dialog error: " + e.getMessage(), this.fba.callbackId);
}

public void onCancel() {
Log.d(TAG, "cancel");
this.fba.error("Cancelled", this.fba.callbackId);
}
}

class AuthorizeListener implements DialogListener {
final ConnectPlugin fba;
Expand All @@ -190,7 +265,7 @@ public void onComplete(Bundle values) {

String token = this.fba.facebook.getAccessToken();
long token_expires = this.fba.facebook.getAccessExpires();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.fba.ctx);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.fba.ctx.getContext());
prefs.edit().putLong("access_expires", token_expires).commit();
prefs.edit().putString("access_token", token).commit();

Expand Down

0 comments on commit 13b7464

Please sign in to comment.