/** Get the current user profile info */
  private void makeMeRequest() {
    final Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
      Request request =
          Request.newMeRequest(
              session,
              new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                  isMakeMeRequestStarted = false;
                  dismissProgressDialog();

                  if (session == Session.getActiveSession() && user != null) {
                    mUser = user;
                    updateUI();
                  }
                  if (response != null && response.getError() != null) {
                    Utils.handleError(
                        getActivity(), response.getError(), Utils.getProfileReadPermission());
                  }
                }
              });
      request.executeAsync();
    }
  }
  /**
   * Using the specifiedSession member variable (which may be nil), find the real session to log to
   * (with an access token). Precedence: 1) specified session, 2) activeSession, 3) app
   * authenticated session via Client Token.
   */
  private Session sessionToLogTo() {

    synchronized (this) {
      Session session = specifiedSession;

      // Require an open session.

      if (session == null || !session.isOpened()) {
        session = Session.getActiveSession();
      }

      if (session == null || !session.isOpened() || session.getAccessToken() == null) {

        if (appAuthSession == null) {

          // Build and stash a client-token based session.

          // Form the clientToken based access token from appID and client token.
          String tokenString = String.format("%s|%s", applicationId, clientToken);
          AccessToken token =
              AccessToken.createFromString(tokenString, null, AccessTokenSource.CLIENT_TOKEN);

          appAuthSession =
              new Session(null, applicationId, new NonCachingTokenCachingStrategy(), false);
          appAuthSession.open(token, null);
        }

        session = appAuthSession;
      }

      return session;
    }
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.com_facebook_usersettingsfragment, container, false);
    loginButton =
        (LoginButton) view.findViewById(R.id.com_facebook_usersettingsfragment_login_button);
    loginButton.setProperties(loginButtonProperties);
    loginButton.setFragment(this);
    loginButton.setLoginLogoutEventName(AnalyticsEvents.EVENT_USER_SETTINGS_USAGE);

    Session session = getSession();
    if (session != null && !session.equals(Session.getActiveSession())) {
      loginButton.setSession(session);
    }
    connectedStateLabel =
        (TextView) view.findViewById(R.id.com_facebook_usersettingsfragment_profile_name);

    // if no background is set for some reason, then default to Facebook blue
    if (view.getBackground() == null) {
      view.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
    } else {
      view.getBackground().setDither(true);
    }
    return view;
  }
 private Session createSession() {
   Session activeSession = Session.getActiveSession();
   if (activeSession == null || activeSession.getState().isClosed()) {
     activeSession = new Session.Builder(this).setApplicationId(appId).build();
     Session.setActiveSession(activeSession);
   }
   return activeSession;
 }
Beispiel #5
0
  @Override
  protected void onResumeFragments() {
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) showFragment(SELECTION, false);
    else showFragment(SPLASH, false);
  }
 private void performPublish(PendingAction action) {
   Session session = Session.getActiveSession();
   if (session != null) {
     pendingAction = action;
     if (hasPublishPermission()) {
       // We can do the action right away.
       handlePendingAction();
     } else {
       // We need to get new permissions, then complete the action when we get called back.
       session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
     }
   }
 }
  private void updateUI() {
    Session session = Session.getActiveSession();
    boolean enableButtons = (session != null && session.isOpened());

    postStatusUpdateButton.setEnabled(enableButtons);
    postPhotoButton.setEnabled(enableButtons);
    pickFriendsButton.setEnabled(enableButtons);
    pickPlaceButton.setEnabled(enableButtons);

    if (enableButtons && user != null) {
      profilePictureView.setProfileId(user.getId());
      greeting.setText("Greetings " + user.getFirstName());
    } else {
      profilePictureView.setProfileId(null);
      greeting.setText(null);
    }
  }
  private boolean initializeActiveSessionWithCachedToken(Context context) {
    if (context == null) {
      return false;
    }

    Session session = Session.getActiveSession();
    if (session != null) {
      return session.isOpened();
    }

    String applicationId = Utility.getMetadataApplicationId(context);
    if (applicationId == null) {
      return false;
    }

    return Session.openActiveSessionFromCache(context) != null;
  }
  @Override
  public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);

    final Session session = Session.getActiveSession();
    if (mUser == null && !hidden && session != null && session.isOpened()) {
      if (!isMakeMeRequestStarted) {
        makeMeRequest();
        isMakeMeRequestStarted = true;
      }
      if (!isStatusesRequestStarted) {
        startStatusesRequest();
        isStatusesRequestStarted = true;
      }
      showProgressDialog();
    }
  }
  /** Get the user's status */
  private void startStatusesRequest() {
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
      new Request(
              session,
              "/me/statuses",
              null,
              HttpMethod.GET,
              new Request.Callback() {
                public void onCompleted(Response response) {
                  isStatusesRequestStarted = false;
                  dismissProgressDialog();

                  handleStatusResponse(response);
                }
              })
          .executeAsync();
    }
  }
 private boolean hasPublishPermission() {
   Session session = Session.getActiveSession();
   return session != null && session.getPermissions().contains("publish_actions");
 }
Beispiel #12
0
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
 }
Beispiel #13
0
  public void loginWithFacebook() {
    // start Facebook Login
    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
      Session session = new Session.Builder(this).build();
      Session.setActiveSession(session);
      currentSession = session;
    }

    // Ask for username and password
    Session.OpenRequest op = new Session.OpenRequest(this);

    op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
    op.setCallback(null);

    List<String> permissions = new ArrayList<String>();
    permissions.add("email");
    permissions.add("user_status");
    permissions.add("friends_status");
    permissions.add("read_stream");
    op.setPermissions(permissions);

    Session session = new Session.Builder(LoginActivity.this).build();
    Session.setActiveSession(session);
    session.addCallback(
        new Session.StatusCallback() {

          // callback when session changes state
          @Override
          public void call(Session session, SessionState state, Exception exception) {
            final String accessToken = session.getAccessToken();

            if (session.isOpened()) {
              Request.executeMeRequestAsync(
                  session,
                  new Request.GraphUserCallback() {

                    public void onCompleted(GraphUser user, Response response) {
                      if (user == null) {
                        DialogHelper.showRetryDialog(
                            LoginActivity.this,
                            getString(R.string.could_not_connect_fb_account),
                            new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialogInterface, int i) {
                                loginWithFacebook();
                              }
                            },
                            new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialogInterface, int i) {
                                finish();
                              }
                            });

                      } else {
                        final String name = user.getName();
                        final String email = (String) user.getProperty("email");

                        Toast.makeText(LoginActivity.this, name + " " + email, Toast.LENGTH_LONG)
                            .show();
                        UserHelper.setCurrentUser(user, accessToken);

                        Intent i = new Intent(LoginActivity.this, RevibeActivity.class);
                        startActivity(i);
                        finish();
                      }
                    }
                  });
            }
          }
        });

    session.openForRead(op);
  }