import java.util.Arrays;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.facebook.model.GraphUser;
public class Activity extends Activity {
private Session.StatusCallback statusCallback = new SessionStatusCallback();
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private ProgressDialog mProgressDialog = null;
private SharedPreferences preferences;
private TextView txtFBName;
private Button txtFBLogin;
private boolean isFacebookLogin;
private String userName,Email,bdate,contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
preferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
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);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
txtFBLogin = (Button) findViewById(R.id.activity_login_btn_fb);
txtFBName = (TextView) findViewById(R.id.textView1);
updateView();
txtFBLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isFacebookLogin) {
postImage();
//openShareContentDialog(FBpostActivity.this, 0, userName);
} else {
onClickLogin();
}
}
});
}
public void postImage() {
if (checkPermissions()) {
Bitmap img = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Request uploadRequest = Request.newUploadPhotoRequest(
Session.getActiveSession(), img, new Request.Callback() {
@Override
public void onCompleted(Response response) {
Toast.makeText(FBpostActivity.this,
"Photo uploaded successfully",
Toast.LENGTH_LONG).show();
}
});
uploadRequest.executeAsync();
} else {
requestPermissions();
}
}
public boolean checkPermissions() {
Session s = Session.getActiveSession();
if (s != null) {
return s.getPermissions().contains("publish_actions");
} else
return false;
}
public void requestPermissions() {
Session s = Session.getActiveSession();
if (s != null)
s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
this, PERMISSIONS));
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForPublish(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(PERMISSIONS));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private void onClickLogout() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
txtFBName.setText("");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
getFBUserData(session);
updateView();
}
}
public void openShareContentDialog(Context activity, final int option, String userName) {
final Dialog dialogView = new Dialog(activity);
dialogView.setContentView(R.layout.facebook_dialog);
dialogView.setCancelable(false);
dialogView.setCanceledOnTouchOutside(false);
dialogView.setTitle("Share Message");
final Button btnLogout = (Button) dialogView.findViewById(R.id.facebook_dialog_btnLogout);
final Button btnCancel = (Button) dialogView.findViewById(R.id.facebook_dialog_btnCancel);
final Button btnPost = (Button) dialogView.findViewById(R.id.facebook_dialog_btnPost);
final EditText edDescription = (EditText) dialogView.findViewById(R.id.facebook_dialog_edttext_post);
final TextView txtUserName = (TextView) dialogView.findViewById(R.id.facebook_dialog_txt_username);
if (option == 0) {
txtUserName.setText(userName);
} else if (option == 1) {
txtUserName.setText(userName);
}
/**
* Get the data from dialog's control and share the data on faceBook or
* Twitter according to selectedOption.
*/
btnLogout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialogView.dismiss();
if (option == 0) {
onClickLogout();
} else if (option == 1) {
//\\ shared.shareOnTwitter(edDescription.getText().toString());
}
}
});
btnPost.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialogView.dismiss();
if (option == 0) {
postStatusUpdate(edDescription.getText().toString().trim());
} else if (option == 1) {
// shared.shareOnTwitter(edDescription.getText().toString());
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialogView.dismiss();
}
});
dialogView.show();
}
private void updateView() {
preferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
Session session = Session.getActiveSession();
if (session.isOpened()) {
txtFBLogin.setText("Already Login");
isFacebookLogin = true;
final String name = preferences.getString("Name", "");
//final String email = preferences.getString("Email", "");
if (name.equalsIgnoreCase("")) {
} else {
txtFBName.setVisibility(View.VISIBLE);
txtFBName.setText("Hello" + " " + name);
}
} else {
txtFBLogin.setText("Login");
isFacebookLogin = false;
txtFBName.setText("");
txtFBName.setVisibility(View.GONE);
}
}
private void postStatusUpdate(String user) {
if (txtFBName.getText() != null && hasPublishPermission()) {
Log.v("KETAN", "hasPublishPermission" + hasPublishPermission());
final String message = getString(R.string.status_update, user, (new Date().toString()));
Request request = Request.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
@Override
public void onCompleted(Response response) {
Toast.makeText(FBpostActivity.this, message, Toast.LENGTH_LONG).show();
}
});
request.executeAsync();
}
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
public void getFBUserData(Session session) {
if (session.isOpened()) {
if (FBpostActivity.this != null) {
mProgressDialog = ProgressDialog.show(FBpostActivity.this, "", "Please Wait...");
}
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
String link = user.getLink();
userName = user.getUsername();
Email = user.getFirstName();
bdate = user.getBirthday();
isFacebookLogin = true;
if (link.length() != 0) {
SharedPreferences preferences = FBpostActivity.this.getSharedPreferences(
FBpostActivity.this.getString(R.string.app_name), Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString("Link", link);
editor.putString("Name", userName);
editor.commit();
}
if (userName != null && userName.length() > 0) {
txtFBName.setText(userName);
Toast.makeText(getApplicationContext(), ""+userName+Email+bdate, 10).show();
//openShareContentDialog(FBpostActivity.this, 0, userName);
}
}
}
});
}
}
}
<activity android:name="com.facebook.LoginActivity"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id"/>