Android アプリケーションに Facebook のログイン オプションがあります。
デバイスに Facebook アプリケーションがインストールされていない場合、このログインは正常に機能します。しかし、Facebook アプリケーションをインストールすると、いくつかの問題が発生します。
では、アプリケーションの Facebook ログインに、Facebook アプリがインストールされているかどうかを無視して前の方法で続行するように指示するにはどうすればよいですか (Facebook アプリケーションがインストールされていないと仮定します)。
これは、fbLoginButton をクリックしたときに呼び出されるアクティビティです。
public class Example extends Activity {
public static final String APP_ID = myAppIdHere;
private LoginButton mLoginButton;
private TextView mText;
private Button mRequestButton;
private Button mPostButton;
private Button mDeleteButton;
private Button mUploadButton;
public static Activity ExampleActivity;
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExampleActivity = this;
if (APP_ID == null) {
Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " +
"specified before running this example: see Example.java");
}
setContentView(R.layout.facebook);
mLoginButton = (LoginButton) findViewById(R.id.login);
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
mLoginButton.init(this, mFacebook);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public class SampleAuthListener implements AuthListener {
public void onAuthSucceed() {
}
public void onAuthFail(String error) {
}
}
public class SampleLogoutListener implements LogoutListener {
public void onLogoutBegin() {
}
public void onLogoutFinish() {
}
}
これは LoginButton クラスです:
public class LoginButton extends ImageButton {
public static Facebook mFb;
public static String facebookID;
public static String firstName;
public static String lastName;
public static String email = "";
public static String sex;
private Handler mHandler;
private SessionListener mSessionListener = new SessionListener();
private String[] mPermissions = new String[] { "read_stream", "email" };
private Activity mActivity;
public LoginButton(Context context) {
super(context);
}
public LoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoginButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(final Activity activity, final Facebook fb) {
init(activity, fb, new String[] {});
}
public void init(final Activity activity, final Facebook fb,
final String[] permissions) {
mActivity = activity;
mFb = fb;
mPermissions = new String[] { "read_stream", "email" };
;
mHandler = new Handler();
setBackgroundColor(Color.TRANSPARENT);
setAdjustViewBounds(true);
if(mFb.isSessionValid()){
getUser();
SessionEvents.onLogoutBegin();
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFb);
asyncRunner.logout(getContext(), new LogoutRequestListener());
}else{
setImageResource(R.drawable.login_button);
drawableStateChanged();
}
SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener(mSessionListener);
setOnClickListener(new ButtonOnClickListener());
}
private final class ButtonOnClickListener implements OnClickListener {
public void onClick(View arg0) {
if (mFb.isSessionValid()) {
getUser();
SessionEvents.onLogoutBegin();
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFb);
asyncRunner.logout(getContext(), new LogoutRequestListener());
} else {
mFb.authorize(mActivity, mPermissions,
new LoginDialogListener());
}
}
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
}
private class LogoutRequestListener extends BaseRequestListener {
public void onComplete(String response, final Object state) {
mHandler.post(new Runnable() {
public void run() {
SessionEvents.onLogoutFinish();
}
});
}
}
private class SessionListener implements AuthListener, LogoutListener {
public void onAuthSucceed() {
setImageResource(R.drawable.logout_button);
SessionStore.save(mFb, getContext());
}
public void onAuthFail(String error) {
}
public void onLogoutBegin() {
}
public void onLogoutFinish() {
SessionStore.clear(getContext());
setImageResource(R.drawable.login_button);
}
}
public static void getUser() {
try {
JSONObject json = Util.parseJson(mFb.request("me"));
facebookID = json.getString("id");
firstName = json.getString("first_name");
lastName = json.getString("last_name");
email = json.getString("email");
sex = json.getString("gender");
String mFirstName = firstName;
String mLastName = lastName;
String mEmail = email;
String mSex = sex;
Log.d("User Details", "You are logged in : " + facebookID
+ mFirstName + "." + mLastName + " Email is: " + mEmail
+ "-" + mSex);
SharedPreferences.Editor editor = SignUpActivity.settings.edit();
// Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
editor.putString("fbEmail", email);
editor.putString("fbId", facebookID);
editor.putString("fbSex", sex);
editor.putString("fbFirstname", firstName);
editor.putString("fbLastname", lastName);
// Commit the edits!
editor.commit();
} catch (Exception e) {
// TODO: handle exception
Log.d("Exception in getUser", "+++++" + e.toString());
}
}
}
これは SessionEvents クラスです:
public class SessionEvents {
private static LinkedList<AuthListener> mAuthListeners = new LinkedList<AuthListener>();
private static LinkedList<LogoutListener> mLogoutListeners = new LinkedList<LogoutListener>();
public static void addAuthListener(AuthListener listener) {
mAuthListeners.add(listener);
}
public static void removeAuthListener(AuthListener listener) {
mAuthListeners.remove(listener);
}
public static void addLogoutListener(LogoutListener listener) {
mLogoutListeners.add(listener);
}
public static void removeLogoutListener(LogoutListener listener) {
mLogoutListeners.remove(listener);
}
public static void onLoginSuccess() {
LoginButton.getUser();
for (AuthListener listener : mAuthListeners) {
listener.onAuthSucceed();
}
}
public static void onLoginError(String error) {
for (AuthListener listener : mAuthListeners) {
listener.onAuthFail(error);
}
}
public static void onLogoutBegin() {
for (LogoutListener l : mLogoutListeners) {
l.onLogoutBegin();
}
}
public static void onLogoutFinish() {
for (LogoutListener l : mLogoutListeners) {
l.onLogoutFinish();
}
}
public static interface AuthListener {
public void onAuthSucceed();
public void onAuthFail(String error);
}
public static interface LogoutListener {
public void onLogoutBegin();
public void onLogoutFinish();
}
}
これは SessionStore クラスです:
public class SessionStore extends Activity{
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-session";
public static boolean save(Facebook session, Context context) {
Context contextObj = Example.ExampleActivity;
Intent i=new Intent(contextObj , SignUpActivity.class);
contextObj.startActivity(i);
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, session.getAccessToken());
editor.putLong(EXPIRES, session.getAccessExpires());
return editor.commit();
}
public static boolean restore(Facebook session, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
session.setAccessToken(savedSession.getString(TOKEN, null));
session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
return session.isSessionValid();
}
public static void clear(Context context) {
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
}
}
これは BaseRequestListener クラスです:
public abstract class BaseRequestListener implements RequestListener {
public void onFacebookError(FacebookError e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onFileNotFoundException(FileNotFoundException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onIOException(IOException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onMalformedURLException(MalformedURLException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
}