Skip to content

Commit

Permalink
Fix so that uid is now available on Android and iOS by making a /me g…
Browse files Browse the repository at this point in the history
…raph call
  • Loading branch information
Dave Johnson committed Aug 22, 2011
1 parent 1b5c17e commit 71d106e
Showing 1 changed file with 115 additions and 101 deletions.
216 changes: 115 additions & 101 deletions native/android/src/com/facebook/phonegap/ConnectPlugin.java
Expand Up @@ -22,90 +22,104 @@
public class ConnectPlugin extends Plugin {

Facebook facebook;
String userId;

@Override
public PluginResult execute(String action, JSONArray args, final String callbackId) {
PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
pr.setKeepCallback(true);
if (action.equals("init")) {
try {
facebook = new Facebook(args.getString(0));
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
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()) {
Log.d("FB", "Session already valid");
pr = new PluginResult(PluginResult.Status.OK, getResponse());
} else {
final ConnectPlugin me = this;
String[] permissions = new String[args.length()];
try {
for (int i=0; i<args.length(); i++) {
permissions[i] = args.getString(i);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return new PluginResult(PluginResult.Status.ERROR, "Invalid JSON args used. Expected a string array of permissions.");
}

this.ctx.setActivityResultCallback(this);
this.facebook.authorize(this.ctx, permissions, 1234567890, new DialogListener() {
@Override
public void onComplete(Bundle values) {
Log.d("FB", "authorized");
me.success(getResponse(), callbackId);
}

@Override
public void onFacebookError(FacebookError error) {
Log.d("FB", "facebook error");
me.error("Facebook error: " + error.getMessage(), callbackId);
}

@Override
public void onError(DialogError e) {
Log.d("FB", "other error");
me.error("Dialog error: " + e.getMessage(), callbackId);
}

@Override
public void onCancel() {
Log.d("FB", "cancel");
me.error("Cancelled", callbackId);
}
});
}
} else {
pr = new PluginResult(PluginResult.Status.ERROR, "Must call FB.init before FB.login");
}
} else if (action.equals("logout")) {
if (facebook != null) {
try {
facebook.logout(this.ctx);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pr = new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "Error logging out.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pr = new PluginResult(PluginResult.Status.IO_EXCEPTION, "Error logging out.");
}
pr = new PluginResult(PluginResult.Status.OK, getResponse());
}
} else if (action.equals("getLoginStatus")) {
if (facebook != null) {
pr = new PluginResult(PluginResult.Status.OK, getResponse());
}
}
return pr;
}
@Override
public PluginResult execute(String action, JSONArray args, final String callbackId) {
PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
pr.setKeepCallback(true);
if (action.equals("init")) {
try {
facebook = new Facebook(args.getString(0));
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
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()) {
Log.d("FB", "Session already valid");
pr = new PluginResult(PluginResult.Status.OK, getResponse());
} else {
final ConnectPlugin me = this;
String[] permissions = new String[args.length()];
try {
for (int i=0; i<args.length(); i++) {
permissions[i] = args.getString(i);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return new PluginResult(PluginResult.Status.ERROR, "Invalid JSON args used. Expected a string array of permissions.");
}

this.ctx.setActivityResultCallback(this);
this.facebook.authorize(this.ctx, permissions, 1234567890, new DialogListener() {
@Override
public void onComplete(Bundle values) {
Log.d("FB", "authorized");
try {
JSONObject o = new JSONObject(me.facebook.request("/me"));
me.userId = o.getString("id");
me.success(getResponse(), callbackId);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onFacebookError(FacebookError error) {
Log.d("FB", "facebook error");
me.error("Facebook error: " + error.getMessage(), callbackId);
}

@Override
public void onError(DialogError e) {
Log.d("FB", "other error");
me.error("Dialog error: " + e.getMessage(), callbackId);
}

@Override
public void onCancel() {
Log.d("FB", "cancel");
me.error("Cancelled", callbackId);
}
});
}
} else {
pr = new PluginResult(PluginResult.Status.ERROR, "Must call FB.init before FB.login");
}
} else if (action.equals("logout")) {
if (facebook != null) {
try {
facebook.logout(this.ctx);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pr = new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "Error logging out.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pr = new PluginResult(PluginResult.Status.IO_EXCEPTION, "Error logging out.");
}
pr = new PluginResult(PluginResult.Status.OK, getResponse());
}
} else if (action.equals("getLoginStatus")) {
if (facebook != null) {
pr = new PluginResult(PluginResult.Status.OK, getResponse());
}
}
return pr;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand All @@ -115,23 +129,23 @@ 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()+"\","+
" \"secret\": \"b082c4620cdac27e0371f2c674026662\","+
" \"session_key\": true,"+
" \"sig\": \"...\","+
" \"uid\": \"...\""+
" }"+
"}";
try {
return new JSONObject(response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new JSONObject();
String response = "{"+
" \"status\": \""+(facebook.isSessionValid() ? "connected" : "unknown")+"\","+
" \"session\": {"+
" \"access_token\": \""+facebook.getAccessToken()+"\","+
" \"expires\": \""+facebook.getAccessExpires()+"\","+
" \"secret\": \"b082c4620cdac27e0371f2c674026662\","+
" \"session_key\": true,"+
" \"sig\": \"...\","+
" \"uid\": \""+this.userId+"\""+
" }"+
"}";
try {
return new JSONObject(response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new JSONObject();
}
}

0 comments on commit 71d106e

Please sign in to comment.