Skip to content

Instantly share code, notes, and snippets.

@vishalpawale
Last active July 10, 2022 11:04
Show Gist options
  • Save vishalpawale/5556996 to your computer and use it in GitHub Desktop.
Save vishalpawale/5556996 to your computer and use it in GitHub Desktop.
Sample Gist for FbShare
package com.sudosaints.android;
import java.util.ArrayList;
import java.util.List;
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 android.widget.Toast;
import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
public class FbShareActivity extends Activity {
ImageView shareButton;
List<String> permissions;
ProgressDialog dialog;
Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
shareButton = (ImageView) findViewById(R.id.shareButton);
/**
* Facebook Permissions
*/
permissions = new ArrayList<String>();
permissions.add("publish_actions");
/** End */
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publishStory();
}
});
/**
* Facebook Session Initialization
*/
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.addCallback(statusCallback);
Session.setActiveSession(session);
}
Log.d("FbShare", "Session State - " + session.getState());
/** End **/
}
/**
* Facebook Methods
*/
private void publishStory() {
Session session = Session.getActiveSession();
if(session != null && session.getState().isOpened()) {
checkSessionAndPost();
} else {
Log.d("FbShare", "Session is null");
session = new Session(FbShareActivity.this);
Session.setActiveSession(session);
session.addCallback(statusCallback);
Log.d("FbShare", "Session info - " + session);
try {
Log.d("FbShare", "Opening session for read");
session.openForRead(new Session.OpenRequest(FbShareActivity.this));
} catch(UnsupportedOperationException exception) {
exception.printStackTrace();
Log.d("FbShare", "Exception Caught");
Toast.makeText(FbShareActivity.this, "Unable to post your score on facebook", Toast.LENGTH_LONG).show();
}
}
}
private void checkSessionAndPost (){
Session session = Session.getActiveSession();
session.addCallback(statusCallback);
Log.d("FbShare", "Session Permissions Are - " + session.getPermissions());
if(session.getPermissions().contains("publish_actions")) {
publishAction(session);
} else {
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(FbShareActivity.this, permissions));
}
}
private void publishAction(Session session) {
Log.d("FbShare", "Inside publishAction()");
dialog = new ProgressDialog(FbShareActivity.this);
dialog.setMessage("Please wait...Posting the status");
dialog.show();
Bundle postParams = new Bundle();
postParams.putString("name", "App Name");
postParams.putString("caption", "Caption");
postParams.putString("description", "description");
postParams.putString("link", "http://www.sudosaints.com");
postParams.putString("message", "message");
Request.Callback callback = new Request.Callback() {
@Override
public void onCompleted(Response response) {
dialog.dismiss();
FacebookRequestError error = response.getError();
if(error != null) {
Log.d("FbShare", "Facebook error - " + error.getErrorMessage());
Log.d("FbShare", "Error code - " + error.getErrorCode());
Log.d("FbShare", "JSON Response - " + error.getRequestResult());
Log.d("FbShare", "Error Category - " + error.getCategory());
Toast.makeText(FbShareActivity.this, "Failed to share the post.Please try again", Toast.LENGTH_SHORT).show();
shareButton.setEnabled(true);
} else {
Toast.makeText(FbShareActivity.this, "Successfully shared the post", Toast.LENGTH_SHORT).show();
shareButton.setEnabled(false);
}
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask asyncTask = new RequestAsyncTask(request);
asyncTask.execute();
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
//Check if Session is Opened or not, if open & clicked on share button publish the story
if(session != null && state.isOpened()) {
Log.d("FbShare", "Session is opened");
if(session.getPermissions().contains("publish_actions")) {
Log.d("FbShare", "Starting share");
publishAction(session);
} else {
Log.d("FbShare", "Session dont have permissions");
publishStory();
}
} else {
Log.d("FbShare", "Invalid fb Session");
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FbShare", "Result Code is - " + resultCode +"");
Session.getActiveSession().addCallback(statusCallback);
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment