private void openSession( String applicationId, List<String> permissions, SessionLoginBehavior behavior, int activityCode, SessionAuthorizationType authType) { if (sessionTracker != null) { Session currentSession = sessionTracker.getSession(); if (currentSession == null || currentSession.getState().isClosed()) { Session session = new Session.Builder(getActivity()).setApplicationId(applicationId).build(); Session.setActiveSession(session); currentSession = session; } if (!currentSession.isOpened()) { Session.OpenRequest openRequest = new Session.OpenRequest(this) .setPermissions(permissions) .setLoginBehavior(behavior) .setRequestCode(activityCode); if (SessionAuthorizationType.PUBLISH.equals(authType)) { currentSession.openForPublish(openRequest); } else { currentSession.openForRead(openRequest); } } } }
/** Connects the user to facebook */ public void connectToFB() { List<String> permissions = new ArrayList<String>(); permissions.add("publish_actions"); permissions.add("email"); currentSession = new Session.Builder(context).build(); currentSession.addCallback(sessionStatusCallback); Session.OpenRequest openRequest = new Session.OpenRequest(context); openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO); openRequest.setRequestCode(Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE); openRequest.setPermissions(permissions); currentSession.openForPublish(openRequest); }
static void sessionOpenRequest( Session session, Session.StatusCallback callback, Activity activity, List<String> permissions, boolean publish) { if (session.isOpened()) { Session.NewPermissionsRequest req = getNewPermissionsRequest(session, callback, permissions, activity); if (publish) { session.requestNewPublishPermissions(req); } else { session.requestNewReadPermissions(req); } } else { OpenRequest req = getOpenRequest(callback, permissions, activity); if (publish) { session.openForPublish(req); } else { session.openForRead(req); } } }
@Override public void onClick(View v) { Context context = getContext(); final Session openSession = sessionTracker.getOpenSession(); if (openSession != null) { // If the Session is currently open, it must mean we need to log out if (confirmLogout) { // Create a confirmation dialog String logout = getResources().getString(R.string.com_facebook_loginview_log_out_action); String cancel = getResources().getString(R.string.com_facebook_loginview_cancel_action); String message; if (user != null && user.getName() != null) { message = String.format( getResources().getString(R.string.com_facebook_loginview_logged_in_as), user.getName()); } else { message = getResources().getString(R.string.com_facebook_loginview_logged_in_using_facebook); } AlertDialog.Builder builder = new AlertDialog.Builder(context); builder .setMessage(message) .setCancelable(true) .setPositiveButton( logout, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { openSession.closeAndClearTokenInformation(); } }) .setNegativeButton(cancel, null); builder.create().show(); } else { openSession.closeAndClearTokenInformation(); } } else { Session currentSession = sessionTracker.getSession(); if (currentSession == null || currentSession.getState().isClosed()) { sessionTracker.setSession(null); Session session = new Session.Builder(context).setApplicationId(applicationId).build(); Session.setActiveSession(session); currentSession = session; } if (!currentSession.isOpened()) { Session.OpenRequest openRequest = null; if (parentFragment != null) { openRequest = new Session.OpenRequest(parentFragment); } else if (context instanceof Activity) { openRequest = new Session.OpenRequest((Activity) context); } if (openRequest != null) { openRequest.setDefaultAudience(properties.defaultAudience); openRequest.setPermissions(properties.permissions); openRequest.setLoginBehavior(properties.loginBehavior); if (SessionAuthorizationType.PUBLISH.equals(properties.authorizationType)) { currentSession.openForPublish(openRequest); } else { currentSession.openForRead(openRequest); } } } } }
public static void login(int cbIndex, String scope) { Log.i(TAG, "dsafsdfas"); Session session = Session.getActiveSession(); if (session == null) { session = new Session(mContext); Session.setActiveSession(session); } String[] permissions = null; mNeedPublishPermissions = false; if (scope != null) { permissions = scope.split(","); for (int i = 0; i < permissions.length; i++) { if (allPublishPermissions.contains(permissions[i])) { mNeedPublishPermissions = true; break; } } } if (session.isOpened()) { Log.i(TAG, "openActiveSession2"); if (scope == null || session.getPermissions().containsAll(Arrays.asList(permissions))) { Log.e(TAG, "FB.login() called when user is already connected."); } else { mStatusCallback.mCallByMode = SessionStatusCallback.CallByLogin; mStatusCallback.mCallbackIndex = cbIndex; if (mNeedPublishPermissions) { session.requestNewPublishPermissions( new Session.NewPermissionsRequest(mActivity, Arrays.asList(permissions))); } else { session.requestNewReadPermissions( new Session.NewPermissionsRequest(mActivity, Arrays.asList(permissions))); } } } else { mStatusCallback.mCallByMode = SessionStatusCallback.CallByLogin; mStatusCallback.mCallbackIndex = cbIndex; Log.i(TAG, "openActiveSession1"); if (!session.isClosed()) { mOpenRequest = new Session.OpenRequest(mActivity); if (permissions != null) mOpenRequest.setPermissions(Arrays.asList(permissions)); mOpenRequest.setCallback(mStatusCallback); if (mNeedPublishPermissions) { Log.i(TAG, "openActiveSession4"); session.openForPublish(mOpenRequest); } else { Log.i(TAG, "openActiveSession5"); session.openForRead(mOpenRequest); } } else { Log.i(TAG, "openActiveSession"); Session.openActiveSession(mActivity, true, mStatusCallback); } } /* * Session session = Session.getActiveSession(); if (!session.isOpened() * && !session.isClosed()) { session.openForRead(new * Session.OpenRequest(mActivity) * .setPermissions(Arrays.asList("basic_info")).setCallback( * mStatusCallback)); } else { Session.openActiveSession(mActivity, * true, mStatusCallback); } */ }