Last active
July 5, 2016 10:55
-
-
Save harsu-ag/1edf3bb653193f273a396c4263a1e36f to your computer and use it in GitHub Desktop.
A class to implement Facebook Login just in few functions.
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
/** | |
* Created by harsu on 22-06-2016. | |
* <p> | |
* This class reduces the BoilerPlate code for Implementing Facebook Login by providing an easy to access class and functions. | |
* <p> | |
* 1. Create an instance of the class in your Sign in activity. | |
* 2. Set a View whose onClick would Invoke Facebook Sign In Procedure along with the request params using {@link #setLoginView(View, Activity, Collection)} | |
* or {@link #setLoginView(View, Fragment, Collection)} | |
* 3. call the {@link #onActivityResult(int, int, Intent)} in your sign In activity's onActivityResult(). | |
* 4. store the data by implementing your logic in {@link #storeData(Map)} | |
* | |
*/ | |
public class FacebookLoginManager { | |
Context context; | |
CallbackManager callbackManager; | |
AccessTokenTracker accessTokenTracker; | |
ProfileTracker profileTracker; | |
Callback callback; | |
/** | |
* Default constructor | |
* | |
* @param context | |
* @param callback | |
*/ | |
public FacebookManager(Context context, final Callback callback) { | |
this.context = context; | |
this.callback = callback; | |
FacebookSdk.sdkInitialize(context.getApplicationContext()); | |
callbackManager = CallbackManager.Factory.create(); | |
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { | |
@Override | |
public void onSuccess(LoginResult loginResult) { | |
callback.onSuccess(); | |
facebookSignIn(loginResult); | |
} | |
@Override | |
public void onCancel() { | |
callback.onFailure(Error.REQUEST_CANCELLED); | |
} | |
@Override | |
public void onError(FacebookException e) { | |
callback.onFailure(Error.NO_INTERNET_CONNECTION); | |
} | |
}); | |
} | |
/** | |
* Call this function from the Activity's onActivityResult(...) | |
* | |
* @param requestCode | |
* @param resultCode | |
* @param data | |
*/ | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
callbackManager.onActivityResult(requestCode, resultCode, data); | |
} | |
/** | |
* Add a view as Login Starter. | |
* | |
* @param view | |
* @param activity | |
* @param readPermissions the Array list of permission to be requested. | |
*/ | |
public void setLoginView(View view, final Activity activity, final Collection<String> readPermissions) { | |
view.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
LoginManager.getInstance().logInWithReadPermissions(activity, readPermissions); | |
} | |
}); | |
} | |
/** | |
* Add a view as Login Starter. | |
* | |
* @param view | |
* @param fragment | |
* @param readPermissions the Array list of permission to be requested. | |
*/ | |
public void setLoginView(View view, final Fragment fragment, final Collection<String> readPermissions) { | |
view.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
LoginManager.getInstance().logInWithReadPermissions(fragment, readPermissions); | |
} | |
}); | |
} | |
private void facebookSignIn(LoginResult loginResult) { | |
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { | |
@Override | |
public void onCompleted(JSONObject object, GraphResponse response) { | |
Log.i("LoginActivity", response.toString()); | |
// Get facebook data from login | |
Map<String, String> params = new HashMap<>(); | |
try { | |
params.put("name", object.getString("name")); | |
params.put("email", object.getString("email")); | |
params.put("gender", object.getString("gender")); | |
params.put("user_name", object.getString("email")); | |
params.put("profilepic", object.getJSONObject("picture").getJSONObject("data").getString("url")); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
storeData(params); | |
} | |
}); | |
Bundle parameters = new Bundle(); | |
//all the fields Required to extract from Graph API | |
parameters.putString("fields", "name, email,gender,picture.type(large) "); | |
request.setParameters(parameters); | |
request.executeAsync(); | |
} | |
/** | |
* implement send data to server logic or store to a local database. On success or failure of the implemented logic | |
* call {@link Callback#onSuccess()} , call {@link Callback#onFailure(int)} using the error code from {@link Error}, | |
* To receive proper callBacks to the Caller Activity and Enabling it's Flow. | |
* | |
* @param params | |
*/ | |
private void storeData(Map<String, String> params) { | |
} | |
/** | |
* Call this function to logout of Facebook. | |
* It gives a {@link Callback#onSuccess()} if successfully Executed. | |
* <p> | |
* You can further branch out to remove data from server or local Database and finally call | |
* {@link Callback#onSuccess()}. | |
*/ | |
public void logout() { | |
LoginManager.getInstance().logOut(); | |
//branch out from here if needed | |
callback.onSuccess(); | |
} | |
/** | |
* | |
*/ | |
public void startTracking() { | |
accessTokenTracker = new AccessTokenTracker() { | |
@Override | |
protected void onCurrentAccessTokenChanged( | |
AccessToken oldAccessToken, | |
AccessToken currentAccessToken) { | |
AccessToken.setCurrentAccessToken(currentAccessToken); | |
} | |
}; | |
profileTracker = new ProfileTracker() { | |
@Override | |
protected void onCurrentProfileChanged( | |
Profile oldProfile, | |
Profile currentProfile) { | |
Profile.setCurrentProfile(currentProfile); | |
} | |
}; | |
accessTokenTracker.startTracking(); | |
} | |
public void stopTracking() { | |
if (accessTokenTracker != null && profileTracker != null) { | |
accessTokenTracker.stopTracking(); | |
profileTracker.stopTracking(); | |
} | |
} | |
/** | |
* CallBack to receive success or failure of login, logout. | |
*/ | |
public interface Callback { | |
public void onSuccess(); | |
public void onFailure(int error); | |
} | |
/** | |
* store int values of error that this class can throw | |
*/ | |
public interface Error { | |
int NO_INTERNET_CONNECTION = 0; | |
int REQUEST_CANCELLED = 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment