package com.phonegap.facebook;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.facebook.android.DialogError;
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;
public class ConnectPlugin extends Plugin {
Facebook facebook;
String userId;
//used for dialog auth
public String[] permissions = new String[] {};
public String callbackId;
@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));
Log.d("PhoneGapLog", "reauthorize");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
String access_token = prefs.getString("access_token", null);
Long expires = prefs.getLong("access_expires", -1);
if (access_token != null && expires != -1) {
this.facebook.setAccessToken(access_token);
this.facebook.setAccessExpires(expires);
try {
JSONObject o = new JSONObject(this.facebook.request("/me"));
this.userId = o.getString("id");
} 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();
}
}
if(facebook.isSessionValid() && this.userId != null) {
return new PluginResult(PluginResult.Status.OK, this.getResponse());
}
else {
return new PluginResult(PluginResult.Status.NO_RESULT);
}
} 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.permissions = permissions;
this.callbackId = callbackId;
Runnable runnable = new Runnable() {
public void run() {
me.facebook.authorize((Activity)me.ctx, me.permissions, new AuthorizeListener(me));
};
};
this.ctx.runOnUiThread(runnable);
}
} 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);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
prefs.edit().putLong("access_expires", -1).commit();
prefs.edit().putString("access_token", null).commit();
} 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;
}
public static final String SINGLE_SIGN_ON_DISABLED = "service_disabled";
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public JSONObject getResponse() {
String response = "{"+
" \"status\": \""+(facebook.isSessionValid() ? "connected" : "unknown")+"\","+
" \"session\": {"+
" \"access_token\": \""+facebook.getAccessToken()+"\","+
" \"expires\": \""+facebook.getAccessExpires()+"\","+
" \"secret\": \"9e6c510c087d9367cf0f52034acf5954\","+
" \"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();
}
class AuthorizeListener implements DialogListener {
final ConnectPlugin fba;
public AuthorizeListener(ConnectPlugin fba){
super();
this.fba = fba;
}
public void onComplete(Bundle values) {
// Handle a successful login
String token = this.fba.facebook.getAccessToken();
long token_expires = this.fba.facebook.getAccessExpires();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.fba.ctx);
prefs.edit().putLong("access_expires", token_expires).commit();
prefs.edit().putString("access_token", token).commit();
Log.d("FB", "authorized");
Log.d("PhoneGapLog", values.toString());
try {
JSONObject o = new JSONObject(this.fba.facebook.request("/me"));
this.fba.userId = o.getString("id");
this.fba.success(getResponse(), this.fba.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();
}
}
public void onFacebookError(FacebookError e) {
Log.d("FB", "facebook error");
this.fba.error("Facebook error: " + e.getMessage(), callbackId);
}
public void onError(DialogError e) {
Log.d("FB", "other error");
this.fba.error("Dialog error: " + e.getMessage(), this.fba.callbackId);
}
public void onCancel() {
Log.d("FB", "cancel");
this.fba.error("Cancelled", this.fba.callbackId);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
PhoneGAP采用移动Javascript+html5框架开发移动程序入门源码
共415个文件
png:106个
xml:78个
js:42个
5星 · 超过95%的资源 需积分: 9 62 下载量 8 浏览量
2012-04-03
07:02:32
上传
评论 5
收藏 10.08MB ZIP 举报
温馨提示
Beginning PhoneGap Mobile Web Framework for JavaScript and HTML5。 源代码。 包含Sencha,html5,等源代码。
资源推荐
资源详情
资源评论
收起资源包目录
PhoneGAP采用移动Javascript+html5框架开发移动程序入门源码 (415个子文件)
.tmp_index.html.19293~ 2KB
.tmp_index.html.41208~ 4KB
.tmp_index.html.41208~ 4KB
.tmp_index.html.4397~ 4KB
.tmp_index.html.46786~ 2KB
.tmp_index.html.57797~ 2KB
.tmp_index.html.57797~ 2KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
.classpath 482B
.classpath 447B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
.classpath 341B
apple.css 142KB
sencha-touch.css 140KB
android.css 137KB
bb6.css 119KB
jquery.mobile-1.0a4.1.css 55KB
jquery.mobile-1.0a4.1.min.css 41KB
jquery.mobile-1.0a4.1.min.css 41KB
jquery.mobile-1.0a4.1.min.css 41KB
clean_rtl.css 29KB
clean_rtl.css 29KB
clean.css 29KB
clean.css 29KB
PhoneGap_GWT_Helloworld.css 568B
PhoneGap_GWT_Helloworld.css 568B
mapapp.css 477B
app.css 133B
clear.cache.gif 43B
clear.cache.gif 43B
4DB4F4D1CDF1F047D845E60FF15735C1.cache.html 37KB
4DB4F4D1CDF1F047D845E60FF15735C1.cache.html 37KB
A994A169B98864CB0285B503721C7855.cache.html 36KB
A994A169B98864CB0285B503721C7855.cache.html 36KB
hosted.html 11KB
hosted.html 11KB
index.html 8KB
index.html 4KB
index.html 4KB
index.html 4KB
index.complete.html 3KB
index.html 3KB
index.html 3KB
index.html 2KB
index.html 2KB
PhoneGap_GWT_Helloworld.html 2KB
index.html 2KB
PhoneGap_GWT_Helloworld.html 2KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 916B
index.html 716B
gwt-servlet.jar 4.77MB
phonegap-1.1.0.jar 154KB
phonegap-1.1.0.jar 154KB
phonegap-1.1.0.jar 154KB
phonegap-1.1.0.jar 154KB
phonegap-1.1.0.jar 154KB
共 415 条
- 1
- 2
- 3
- 4
- 5
鹧鸪菜
- 粉丝: 323
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页