Last active
February 22, 2019 19:26
-
-
Save vishalpawale/5560725 to your computer and use it in GitHub Desktop.
Skeleton for login into Facebook
This file contains 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
package com.sudosaints.android; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ImageView; | |
import com.facebook.Request; | |
import com.facebook.Response; | |
import com.facebook.Session; | |
import com.facebook.SessionState; | |
import com.facebook.model.GraphObject; | |
import com.facebook.model.GraphUser; | |
public class FbLoginActivity extends Activity { | |
private static List<String> permissions; | |
Session.StatusCallback statusCallback = new SessionStatusCallback(); | |
ProgressDialog dialog; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.opening_screen_layout); | |
ImageView fbButton = (ImageView) findViewById(R.id.fbButton); | |
/***** FB Permissions *****/ | |
permissions = new ArrayList<String>(); | |
permissions.add("email"); | |
/***** End FB Permissions *****/ | |
fbButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// TODO Check if there is any Active Session, otherwise Open New Session | |
Session session = Session.getActiveSession(); | |
if(!session.isOpened()) { | |
session.openForRead(new Session.OpenRequest(FbShareActivity.this).setCallback(statusCallback).setPermissions(permissions)); | |
} else { | |
Session.openActiveSession(FbShareActivity.this, true, statusCallback); | |
} | |
} | |
}); | |
Session session = Session.getActiveSession(); | |
if(session == null) { | |
if(savedInstanceState != null) { | |
session = Session.restoreSession(this, null, statusCallback, savedInstanceState); | |
} | |
if(session == null) { | |
session = new Session(this); | |
} | |
Session.setActiveSession(session); | |
session.addCallback(statusCallback); | |
if(session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { | |
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(permissions)); | |
} | |
} | |
} | |
private class SessionStatusCallback implements Session.StatusCallback { | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
//Check if Session is Opened or not | |
processSessionStatus(session, state, exception); | |
} | |
} | |
public void processSessionStatus(Session session, SessionState state, Exception exception) { | |
if(session != null && session.isOpened()) { | |
if(session.getPermissions().contains("email")) { | |
//Show Progress Dialog | |
dialog = new ProgressDialog(FbShareActivity.this); | |
dialog.setMessage("Loggin in.."); | |
dialog.show(); | |
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { | |
@Override | |
public void onCompleted(GraphUser user, Response response) { | |
if (dialog!=null && dialog.isShowing()) { | |
dialog.dismiss(); | |
} | |
if(user != null) { | |
Map<String, Object> responseMap = new HashMap<String, Object>(); | |
GraphObject graphObject = response.getGraphObject(); | |
responseMap = graphObject.asMap(); | |
Log.i("FbLogin", "Response Map KeySet - " + responseMap.keySet()); | |
// TODO : Get Email responseMap.get("email"); | |
String fb_id = user.getId(); | |
String email = null; | |
String name = (String) responseMap.get("name"); | |
if (responseMap.get("email")!=null) { | |
email = responseMap.get("email").toString(); | |
//TODO Login successfull Start your next activity | |
startActivity(new Intent(FbLoginActivity.this, HomeActivity.class)); | |
} | |
else { | |
//Clear all session info & ask user to login again | |
Session session = Session.getActiveSession(); | |
if(session != null) { | |
session.closeAndClearTokenInformation(); | |
} | |
} | |
} | |
} | |
}); | |
} else { | |
session.requestNewReadPermissions(new Session.NewPermissionsRequest(FbShareActivity.this, permissions)); | |
} | |
} | |
} | |
/********** Activity Methods **********/ | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
Log.d("FbLogin", "Result Code is - " + resultCode +""); | |
Session.getActiveSession().onActivityResult(FbShareActivity.this, requestCode, resultCode, data); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
// TODO Save current session | |
super.onSaveInstanceState(outState); | |
Session session = Session.getActiveSession(); | |
Session.saveSession(session, outState); | |
} | |
@Override | |
protected void onStart() { | |
// TODO Add status callback | |
super.onStart(); | |
Session.getActiveSession().addCallback(statusCallback); | |
} | |
@Override | |
protected void onStop() { | |
// TODO Remove callback | |
super.onStop(); | |
Session.getActiveSession().removeCallback(statusCallback); | |
} | |
} |
hey how to import the library there are alot of error in the code !!!!
which library i import????
please give me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi , what is the FbShareActivity in this code.??