前回facebook sdkとかを使う時にリファレンスが充実している訳ではないので、主な作業facebookのsdkを改変して使いやすいようにしたいと思います。
もし、facebookのsdkのファイルがお手元になかったら、下記よりダウンロードができます。
https://github.com/facebook/facebook-android-sdk/
で今回はsimpleを解析して中身を理解してみたいとおもいます。
BaseDialogListener.java
基本的にはDialogListenerをimplementsしたクラスが必要だったので作成された形
BaseRequestListener.java
基本的にはRequestListenerをimplementsしたクラスが必要だっただけ。
上記の2つは、イベントの取得をする為に用意をしておいて、Example.javaでさらにリスナーを継承して利用をする為のクラスです。
SessionEvents.java
複雑そうに見えるが、実際には関数をまとめてstaticで保持しているutilクラスのような存在で
SessionStore.java
一度取得したtokenを保持してくれるクラスで
ここまでは結構、解析をする必要はなくて自分でも同様に使えばよいクラスなんですが、
LoginButton.java
割りと複雑そうに書いてありますが、基本的には、 メインのクラスから送られてきたFacebookクラスの継承したものを、 現在のセッション情報を基に画像を切り替えたり、クリック時の時の対応をする
のですが、個人的には、Example.javaの中で行なってしまってもいいのかと思っています。
Example.javaに他でも使いやすいような手順がしめしてあります。
APP_IDを最初に保持しておく必要がある。
if (APP_ID == null) { Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + "specified before running this example: see Example.java"); }
各レイアウトのボタンなどに名前を設定。特にfacebook限定の動作ではないですね。
setContentView(R.layout.main); mLoginButton = (LoginButton) findViewById(R.id.login); mText = (TextView) Example.this.findViewById(R.id.txt); mRequestButton = (Button) findViewById(R.id.requestButton); mPostButton = (Button) findViewById(R.id.postButton); mDeleteButton = (Button) findViewById(R.id.deletePostButton); mUploadButton = (Button) findViewById(R.id.uploadButton);
facebook.java用:セッションを確認するなどをしています。
mFacebook = new Facebook(APP_ID); mAsyncRunner = new AsyncFacebookRunner(mFacebook); SessionStore.restore(mFacebook, this);
Listenerでauthの認証とログアウト時のイベントを取得
SessionEvents.addAuthListener(new SampleAuthListener()); SessionEvents.addLogoutListener(new SampleLogoutListener());
authは成功と失敗が取得できるようです。
public class SampleAuthListener implements AuthListener { public void onAuthSucceed() { mText.setText("You have logged in! "); mRequestButton.setVisibility(View.VISIBLE); mUploadButton.setVisibility(View.VISIBLE); mPostButton.setVisibility(View.VISIBLE); } public void onAuthFail(String error) { mText.setText("Login Failed: " + error); } }
ログアウトは基本的にはリクエストを出して失敗ではなくスタートと終了だけあるみたい
public class SampleLogoutListener implements LogoutListener { public void onLogoutBegin() { mText.setText("Logging out..."); } public void onLogoutFinish() { mText.setText("You have logged out! "); mRequestButton.setVisibility(View.INVISIBLE); mUploadButton.setVisibility(View.INVISIBLE); mPostButton.setVisibility(View.INVISIBLE); } }
情報を取得する部分で今回はmeと書いてあるので、自分の情報が取得できます。
mRequestButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mAsyncRunner.request("me", new SampleRequestListener()); } });
リクエストリスナーを元にユーザーの”me”の情報が取得できているので、名前の情報を取得する
public class SampleRequestListener extends BaseRequestListener { public void onComplete(final String response, final Object state) { try { // process the response here: executed in background thread Log.d("Facebook-Example", "Response: " + response.toString()); JSONObject json = Util.parseJson(response); final String name = json.getString("name"); // then post the processed result back to the UI thread // if we do not do this, an runtime exception will be generated // e.g. "CalledFromWrongThreadException: Only the original // thread that created a view hierarchy can touch its views." Example.this.runOnUiThread(new Runnable() { public void run() { mText.setText("Hello there, " + name + "!"); } }); } catch (JSONException e) { Log.w("Facebook-Example", "JSON Error in response"); } catch (FacebookError e) { Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); } } }
現在のセッションを持ったのかどうか。と言うのを確認してそれによってボタンを出し訳しています
mUploadButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);
ダイアログを出して、そこでpost
mPostButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mFacebook.dialog(Example.this, "feed", new SampleDialogListener()); } });
ダイアログを経由している部分
public class SampleDialogListener extends BaseDialogListener { public void onComplete(Bundle values) { final String postId = values.getString("post_id"); if (postId != null) { Log.d("Facebook-Example", "Dialog Success! post_id=" + postId); mAsyncRunner.request(postId, new WallPostRequestListener()); mDeleteButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mAsyncRunner.request(postId, new Bundle(), "DELETE", new WallPostDeleteListener(), null); } }); mDeleteButton.setVisibility(View.VISIBLE); } else { Log.d("Facebook-Example", "No wall post made"); } } }
ポストのイベントリスナーが返してくれる
public class WallPostRequestListener extends BaseRequestListener { public void onComplete(final String response, final Object state) { Log.d("Facebook-Example", "Got response: " + response); String message = "<empty>"; try { JSONObject json = Util.parseJson(response); message = json.getString("message"); } catch (JSONException e) { Log.w("Facebook-Example", "JSON Error in response"); } catch (FacebookError e) { Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); } final String text = "Your Wall Post: " + message; Example.this.runOnUiThread(new Runnable() { public void run() { mText.setText(text); } }); } }
ポストしたのを消してくれる時のイベント
public class WallPostDeleteListener extends BaseRequestListener { public void onComplete(final String response, final Object state) { if (response.equals("true")) { Log.d("Facebook-Example", "Successfully deleted wall post"); Example.this.runOnUiThread(new Runnable() { public void run() { mDeleteButton.setVisibility(View.INVISIBLE); mText.setText("Deleted Wall Post"); } }); } else { Log.d("Facebook-Example", "Could not delete wall post"); } } }
のように長くなってしまいましたが、一個一個いろんな役割があって割りと単純な作業をして作ってくれてます。全体はsdkの方から直接見てみてください!