Ejemplo n.º 1
0
  private static void finalizeLogin(
      Session session,
      SessionState state,
      Exception exception,
      final UnityMessage unityMessage,
      final Activity activityToClose) {
    if (activityToClose != null) {
      activityToClose.finish();
    }

    if (!session.isOpened() && state != SessionState.CLOSED_LOGIN_FAILED) {
      unityMessage.sendError("Unknown error while opening session. Check logcat for details.");
      return;
    }

    if (session.isOpened()) {
      unityMessage.put("opened", true);
    } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
      unityMessage.putCancelled();
    }

    if (session.getAccessToken() == null || session.getAccessToken().equals("")) {
      unityMessage.send();
      return;
    }

    // there's a chance a subset of the permissions were allowed even if the login was cancelled
    // if the access token is there, try to get it anyways

    // add a callback to update the access token when it changes
    session.addCallback(
        new StatusCallback() {
          @Override
          public void call(Session session, SessionState state, Exception exception) {
            if (session == null || session.getAccessToken() == null) {
              return;
            }
            final UnityMessage unityMessage = new UnityMessage("OnAccessTokenUpdate");
            unityMessage.put("access_token", session.getAccessToken());
            unityMessage.put(
                "expiration_timestamp", "" + session.getExpirationDate().getTime() / 1000);
            unityMessage.send();
          }
        });
    unityMessage.put("access_token", session.getAccessToken());
    unityMessage.put("expiration_timestamp", "" + session.getExpirationDate().getTime() / 1000);
    Request.newMeRequest(
            session,
            new Request.GraphUserCallback() {
              @Override
              public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
                  unityMessage.put("user_id", user.getId());
                }
                unityMessage.send();
              }
            })
        .executeAsync();
  }
Ejemplo n.º 2
0
 private static Session.NewPermissionsRequest getNewPermissionsRequest(
     Session session, StatusCallback callback, List<String> permissions, Activity activity) {
   Session.NewPermissionsRequest req = new Session.NewPermissionsRequest(activity, permissions);
   req.setCallback(callback);
   // This should really be "req.setCallback(callback);"
   // Unfortunately the current underlying SDK won't add the callback when you do it that way
   // TODO: when upgrading to the latest see if this can be "req.setCallback(callback);"
   // if it still doesn't have it, file a bug!
   session.addCallback(callback);
   req.setDefaultAudience(SessionDefaultAudience.FRIENDS);
   return req;
 }
Ejemplo n.º 3
0
  /** 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);
  }
Ejemplo n.º 4
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);
  }