Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public final class FingerPaintActivity extends Activity implements View.OnClickListener {
private CallbackManager callbackManager;
private ProgressDialog progressDialog;
private Bitmap viewBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
uploadPhoto(loginResult.getAccessToken());
}
@Override
public void onCancel() {
// No-op
}
@Override
public void onError(FacebookException e) {
progressDialog.dismiss();
new AlertDialog.Builder(FingerPaintActivity.this)
.setTitle(R.string.facebook_dialog_title)
.setMessage(R.string.facebook_auth_failure)
.setPositiveButton("Ok", null)
.show();
}
});
progressDialog = new ProgressDialog(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onClick(final View button) {
switch (button.getId()) {
case R.id.facebook_share:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null || accessToken.isExpired() ||
!accessToken.getPermissions().contains("publish_actions")) {
progressDialog.setMessage(getString(R.string.facebook_login_progress));
progressDialog.show();
LoginManager.getInstance().logInWithPublishPermissions(this,
Collections.singletonList("publish_actions"));
} else {
uploadPhoto(accessToken);
}
break;
}
}
private void uploadPhoto(AccessToken accessToken) {
progressDialog.setMessage(getString(R.string.facebook_share_progress));
if (!progressDialog.isShowing()) {
progressDialog.show();
}
if (viewBitmap != null) {
synchronized (viewBitmap) {
Bundle parameters = new Bundle(1);
parameters.putParcelable("picture", viewBitmap);
parameters.putString("message", getString(R.string.facebook_share_text,
getString(R.string.url_play_store, getPackageName())));
GraphRequest request = new GraphRequest(accessToken, "me/photos", parameters,
HttpMethod.POST, new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
progressDialog.dismiss();
FacebookRequestError error = graphResponse.getError();
if (error == null) {
Toast.makeText(FingerPaintActivity.this, R.string.facebook_share_success,
Toast.LENGTH_SHORT).show();
} else {
Log.e("facebook error", error.getErrorMessage());
Toast.makeText(FingerPaintActivity.this, R.string.facebook_share_failure,
Toast.LENGTH_SHORT).show();
}
}
});
request.executeAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment