Example #1
0
  public static void callFacebookLogout(Context context) {
    Session session = Session.getActiveSession();
    if (session != null) {

      if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
        // clear your preferences if saved

        Intent i = new Intent();
        i.setClass(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
      }
    } else {

      session = new Session(context);
      Session.setActiveSession(session);

      session.closeAndClearTokenInformation();
      // clear your preferences if saved

      Intent i = new Intent();
      i.setClass(context, MainActivity.class);
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(i);
    }
  }
 public static void logout(int cbIndex) {
   mStatusCallback.mCallByMode = SessionStatusCallback.CallByLogout;
   mStatusCallback.mCallbackIndex = cbIndex;
   Session session = Session.getActiveSession();
   if (!session.isClosed()) session.closeAndClearTokenInformation();
   else Log.e(TAG, "FB.logout() called without an access token.");
 }
Example #3
0
 private void onClickLogin() {
   Session session = Session.getActiveSession();
   if (!session.isOpened() && !session.isClosed()) {
     session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
   } else {
     Session.openActiveSession(this, true, statusCallback);
   }
 }
Example #4
0
 public static void closeSession() {
   Session session = Session.getActiveSession();
   if (!session.isClosed()) {
     session.closeAndClearTokenInformation();
     SharedPreferenceUtil.removeValue(SharedPreferenceUtil.PREF_FACEBOOK_TOKEN);
     SharedPreferenceUtil.removeValue(SharedPreferenceUtil.PREF_FACEBOOK_EXPIRE_DATE);
   }
 }
  private void login(JSONArray args, CallbackContext callbackContext) throws JSONException {
    Session session = Session.getActiveSession();

    if (session == null || session.isClosed()) {
      Log.d(TAG, "Building new session");
      session = new Session.Builder(cordova.getActivity()).setApplicationId(this.appId).build();
      Session.setActiveSession(session);
    } else {
      Log.d(TAG, "Existing session " + session.getState());
    }

    if (session.isOpened()) {
      Log.d(TAG, "Session already open");
      callbackContext.success(buildAuthorizationResponse(session));
      return;
    }

    final CallbackContext callback = callbackContext;

    List<String> permissions = new ArrayList<String>();

    permissions.add("basic_info");
    JSONArray argumentPermissions = args.getJSONArray(0);

    for (int i = 0; i < argumentPermissions.length(); i++) {
      permissions.add(argumentPermissions.getString(i));
    }

    OpenRequest openRequest =
        new OpenRequest(cordova.getActivity())
            .setPermissions(permissions)
            .setCallback(
                new StatusCallback() {
                  @Override
                  public void call(Session session, SessionState state, Exception exception) {
                    Log.d(TAG, "In status callback open for read " + state);

                    if (state == SessionState.OPENING) {
                      return;
                    }

                    session.removeCallback(this);

                    try {
                      JSONObject response = buildAuthorizationResponse(session);

                      callback.success(response);
                    } catch (JSONException e) {
                      Log.e(TAG, "JSONException", e);
                      callback.error(e.toString());
                    }
                  }
                });

    cordova.setActivityResultCallback(this);

    session.openForRead(openRequest);
  }
  public static void login(String params, final Activity activity) {
    Session session = Session.getActiveSession();
    if (session == null) {
      Log.w(FB.TAG, "Session not found. Call init() before calling login()");
      return;
    }
    // if the old session is closed (or login was cancelled), create new one
    if (session.isClosed()) {
      session =
          new Builder(FB.getUnityActivity()).setApplicationId(session.getApplicationId()).build();
      Session.setActiveSession(session);
    }
    final UnityMessage unityMessage = new UnityMessage("OnLoginComplete");

    unityMessage.put("key_hash", FB.getKeyHash());

    // parse and separate the permissions into read and publish permissions
    List<String> permissions = new ArrayList<String>();
    UnityParams unity_params = UnityParams.parse(params, "couldn't parse login params: " + params);
    if (unity_params.hasString("scope")) {
      permissions =
          new ArrayList<String>(Arrays.asList(unity_params.getString("scope").split(",")));
    }
    List<String> publishPermissions = new ArrayList<String>();
    List<String> readPermissions = new ArrayList<String>();
    if (permissions.size() > 0) {
      for (String s : permissions) {
        if (s.length() == 0) {
          continue;
        }
        if (Session.isPublishPermission(s)) {
          publishPermissions.add(s);
        } else {
          readPermissions.add((s));
        }
      }
    }
    boolean hasPublishPermissions = !publishPermissions.isEmpty();

    // check to see if the readPermissions have been TOSed already
    // we don't need to show the readPermissions dialog if they have all been TOSed even though it's
    // a mix
    // of permissions
    boolean showMixedPermissionsFlow =
        hasPublishPermissions && !session.getPermissions().containsAll(readPermissions);

    // if we're logging in and showing a mix of publish and read permission, we need to split up the
    // dialogs
    // first just show the read permissions, after they are accepted show publish permissions
    if (showMixedPermissionsFlow) {
      Session.StatusCallback afterReadPermissionCallback =
          getAfterReadPermissionLoginCallback(unityMessage, publishPermissions, activity);
      sessionOpenRequest(session, afterReadPermissionCallback, activity, readPermissions, false);
    } else {
      Session.StatusCallback finalCallback = getFinalCallback(unityMessage, activity);
      sessionOpenRequest(session, finalCallback, activity, permissions, hasPublishPermissions);
    }
  }
Example #7
0
 public void loginWithFacebook() {
   //  TipsManager.setIsJustLogined(true);
   Session session = Session.getActiveSession();
   if (!session.isOpened() && !session.isClosed()) {
     session.openForRead(new Session.OpenRequest(activity).setCallback(facebookStatusCallback));
   } else {
     Session.openActiveSession(activity, true, facebookStatusCallback);
   }
 }
 public void onClickLogin() {
   Session session = Session.getActiveSession();
   if (!session.isOpened() && !session.isClosed()) {
     session.openForRead(
         new Session.OpenRequest(mContext).setPermissions(this.mPermissions).setCallback(this));
   } else {
     Session.openActiveSession(mContext, true, this);
   }
 }
  protected void logout(CallbackContext callbackContext) {
    Session session = Session.getActiveSession();

    if (session == null || session.isClosed()) {
      callbackContext.error("User is not logged in");
      return;
    }

    session.closeAndClearTokenInformation();
    callbackContext.success();
  }
Example #10
0
 @Override
 protected void onResume() {
   super.onResume();
   // For scenarios where the main activity is launched and user
   // session is not null, the session state change notification
   // may not be triggered. Trigger it if it's open/closed.
   Session session = Session.getActiveSession();
   if (session != null && (session.isOpened() || session.isClosed())) {
     onSessionStateChange(session, session.getState(), null);
   }
   uiHelper.onResume();
 }
 public void onResume() {
   // For scenarios where the main activity is launched and user
   // session is not null, the session state change notification
   // may not be triggered. Trigger it if it's open/closed.
   Session session = Session.getActiveSession();
   if (session != null && (session.isOpened() || session.isClosed())) {
     if (session.getState().isOpened()) {
       Log.i(TAG, "Logged in...");
     } else if (session.getState().isClosed()) {
       Log.i(TAG, "Logged out...");
     }
   }
   uiHelper.onResume();
 }
    @Override
    public void call(Session session, SessionState state, Exception exception) {
      Log.v("SessionStatusCallback", "call cbIndex:" + mCallbackIndex);
      Log.v("SessionStatusCallback", "state:" + state.toString());
      if (exception != null) Log.v("SessionStatusCallback", "exception:" + exception.toString());
      switch (mCallByMode) {
        case CallByLogin:
          if (session.isOpened()) {
            mCallByMode = CallByNull;
            // Date curDate = new Date();
            // Log.v(Tag,"getExpirationDate:"+(session.getExpirationDate().getTime()-curDate.getTime()));

            if (mCallbackIndex != -1)
              nativeCallback(
                  mCallbackIndex,
                  "{\"authResponse\":{\"accessToken\":\""
                      + session.getAccessToken()
                      + "\"},\"status\":\"connected\"}");
          } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
            mCallByMode = CallByNull;
            if (mCallbackIndex != -1)
              nativeCallback(
                  mCallbackIndex, "{\"authResponse\":null,\"status\":\"not_authorized\"}");
          }
          break;
        case CallByLogout:
          if (session.isClosed()) {
            mCallByMode = CallByNull;
            if (mCallbackIndex != -1)
              nativeCallback(mCallbackIndex, "{\"authResponse\":{},\"status\":\"unknown\"}");
          }
          break;
        case CallByFBUI:
          {
            if (session.isOpened()) {
              mCallByMode = CallByNull;
              Message message = myHandler.obtainMessage(MyHandler.EXECUTE_WEBDIALOG, params);
              message.arg1 = mCallbackIndex;
              message.sendToTarget();
            } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
              mCallByMode = CallByNull;
              if (mCallbackIndex != -1) nativeCallback(mCallbackIndex, "null");
            }
            break;
          }
        default:
          break;
      }
    }
Example #13
0
 @Override
 protected void onResume() {
   // TODO Auto-generated method stub
   super.onResume();
   mHelper.onResume();
   if (!hasTryLogin) {
     if (FacebookUtil.isFacebookVlidate(this)) mAction = PendingAction.NONE;
     else mAction = PendingAction.GET_INFO;
     Session session = Session.getActiveSession();
     if (session != null && (!session.isOpened() && !session.isClosed())) {
       session.openForRead(
           new Session.OpenRequest(this).setPermissions(null).setCallback(mCallback));
     } else {
       Session.openActiveSession(this, true, mCallback);
     }
   }
 }
Example #14
0
 private void profile() {
   /*Bundle extras = getIntent().getExtras();
   if (extras != null) {
    Intent i = new Intent(MainActivity.this, Profile.class);
    i.putExtras(extras);
       startActivity(i);
      }else{
    	  Intent i = new Intent(MainActivity.this, Profile.class);
       startActivity(i);
      }*/
   // session.checkLogin();
   if (session.isLoggedIn() || sessionFacebook.isOpened()) {
     Intent i = new Intent(MainActivity.this, Profile.class);
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(i);
   }
   if (sessionFacebook.isClosed()) {
     session.checkLogin();
   }
 }
  public static void ui(String params, int cbIndex) {
    Session session = Session.getActiveSession();
    if (session == null) {
      session = new Session(mContext);
      Session.setActiveSession(session);
    }

    if (session.isOpened()) {
      Message message = myHandler.obtainMessage(MyHandler.EXECUTE_WEBDIALOG, params);
      message.arg1 = cbIndex;
      message.sendToTarget();
    } else {
      mStatusCallback.mCallByMode = SessionStatusCallback.CallByFBUI;
      mStatusCallback.mCallbackIndex = cbIndex;
      mStatusCallback.params = params;

      if (!session.isClosed()) {
        session.openForRead(new Session.OpenRequest(mActivity).setCallback(mStatusCallback));
      } else {
        Session.openActiveSession(mActivity, true, mStatusCallback);
      }
    }
  }
  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); }
     */
  }
 // call this in onResume of activity
 public void onResume() {
   Session session = Session.getActiveSession();
   if (session != null && (session.isOpened() || session.isClosed())) {
     onSessionStateChange(session, session.getState(), null);
   }
 }
Example #18
0
 private void onClickLogout() {
   Session session = Session.getActiveSession();
   if (!session.isClosed()) {
     session.closeAndClearTokenInformation();
   }
 }