-
-
Save vishalpawale/5560725 to your computer and use it in GitHub Desktop.
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); | |
} | |
} |
Is this also usable on a fragment? or do I have to place the events on the fragmentActivity?
Hi, Sorry for so late.
Yes you have to place events on Fragment Activity.You can pass callback to your fragment for initiating facebook login.
How to add the application Id
How do i add the application id....I kindly need an urgent assistance. Tnx
You can add app id in manifest.
Thank you!
Hi,
I have implmented this code.
When I hit the facebook login button it is showing the login screen of facebook.
But when i close without login and try to hit the login button again it crashes saying "session: an attempt was made to open an already opened session."
hi you have to use FbShareActivity.this not should be FbLoginActivity ???
if(!session.isOpened()) {
session.openForRead(new Session.OpenRequest(FbShareActivity.this).setCallback(statusCallback).setPermissions(permissions));
} else {
Session.openActiveSession(FbShareActivity.this, true, statusCallback);
}
thanks a lot !!!
Hi , what is the FbShareActivity in this code.??
hey how to import the library there are alot of error in the code !!!!
which library i import????
please give me
Glad it helped :)