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); } } } }
public void setReadPermissions(List<String> permissions, Session session) { if (SessionAuthorizationType.PUBLISH.equals(authorizationType)) { throw new UnsupportedOperationException( "Cannot call setReadPermissions after setPublishPermissions has been called."); } if (validatePermissions(permissions, SessionAuthorizationType.READ, session)) { this.permissions = permissions; authorizationType = SessionAuthorizationType.READ; } }
private boolean validatePermissions( List<String> permissions, SessionAuthorizationType authType, Session currentSession) { if (SessionAuthorizationType.PUBLISH.equals(authType)) { if (Utility.isNullOrEmpty(permissions)) { throw new IllegalArgumentException( "Permissions for publish actions cannot be null or empty."); } } if (currentSession != null && currentSession.isOpened()) { if (!Utility.isSubset(permissions, currentSession.getPermissions())) { Log.e(TAG, "Cannot set additional permissions when session is already open."); return false; } } return true; }
@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); } } } } }