Created
March 16, 2015 19:01
-
-
Save funkyidol/79d188a51b89b2f79721 to your computer and use it in GitHub Desktop.
Facebook Login Activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.accounts.Account; | |
import android.accounts.AccountManager; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.TextView; | |
import com.facebook.AppEventsLogger; | |
import com.facebook.Session; | |
import com.facebook.SessionState; | |
import com.facebook.UiLifecycleHelper; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class FacebookLoginActivityNew extends Activity { | |
private static final int REAUTH_ACTIVITY_CODE = 100; | |
private static final List<String> PUBLISH_PERMISSIONS = Arrays | |
.asList("publish_actions"); | |
private static final List<String> READ_PERMISSIONS = Arrays | |
.asList("email"); | |
private static final String TAG = "FacebookLoginActivity"; | |
private UiLifecycleHelper lifecycleHelper; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
lifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() { | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
onSessionStateChanged(session, state, exception); | |
} | |
}); | |
lifecycleHelper.onCreate(savedInstanceState); | |
ensureOpenSession(); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
// Update the display every time we are started. | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
// Call the 'activateApp' method to log an app event for use in analytics and advertising reporting. Do so in | |
// the onResume methods of the primary Activities that an app may be launched into. | |
AppEventsLogger.activateApp(this); | |
} | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); | |
switch (requestCode) { | |
case REAUTH_ACTIVITY_CODE: | |
Session session = Session.getActiveSession(); | |
if (session != null) { | |
onSessionStateChanged(session, session.getState(), null); | |
} | |
break; | |
} | |
} | |
private boolean ensureOpenSession() { | |
if (Session.getActiveSession() == null || | |
!Session.getActiveSession().isOpened()) { | |
Session.openActiveSession(this, true, READ_PERMISSIONS, new Session.StatusCallback() { | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
onSessionStateChanged(session, state, exception); | |
} | |
}); | |
return false; | |
} | |
return true; | |
} | |
private boolean sessionHasNecessaryPerms(Session session) { | |
if (session != null && session.getPermissions() != null) { | |
for (String requestedPerm : PUBLISH_PERMISSIONS) { | |
if (!session.getPermissions().contains(requestedPerm)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
return false; | |
} | |
private List<String> getMissingPermissions(Session session) { | |
List<String> missingPerms = new ArrayList<String>(PUBLISH_PERMISSIONS); | |
if (session != null && session.getPermissions() != null) { | |
for (String requestedPerm : PUBLISH_PERMISSIONS) { | |
if (session.getPermissions().contains(requestedPerm)) { | |
missingPerms.remove(requestedPerm); | |
} | |
} | |
} | |
return missingPerms; | |
} | |
private void onSessionStateChanged(final Session session, SessionState state, | |
Exception exception) { | |
if (state.isOpened() && !sessionHasNecessaryPerms(session)) { | |
requestPublishPermissions(session); | |
} else if (state.isOpened()) { | |
finishFacebookLogin(session); | |
} | |
} | |
private void requestPublishPermissions(Session session) { | |
if (session != null) { | |
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest( | |
this,getMissingPermissions(session)); | |
newPermissionsRequest.setRequestCode(REAUTH_ACTIVITY_CODE); | |
Session mSession = Session.openActiveSessionFromCache(this); | |
mSession.requestNewPublishPermissions(newPermissionsRequest); | |
} | |
} | |
private void finishFacebookLogin(Session session) { | |
if (ensureOpenSession()) { | |
Log.d(TAG, "Facebook permissions complete"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment