@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(this.getApplicationContext());
    requestWindowFeature(Window.FEATURE_NO_TITLE); // hide title
    getWindow()
        .setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); // hide top bar
    setContentView(R.layout.highscorescreen);

    btn_back = (Button) findViewById(R.id.btn_back);
    btn_back.setOnClickListener(this);

    theScore = getSharedPreferences("thePref", Context.MODE_PRIVATE);
    theText = (TextView) findViewById(R.id.textView);
    theText.setText("" + theScore.getInt("HighScore", 0));

    callbackManager = CallbackManager.Factory.create();

    List<String> PERMISSIONS = Arrays.asList("publish_actions");

    loginBtn = (LoginButton) findViewById(R.id.fb_login_button);
    loginManager = LoginManager.getInstance();
    loginManager.logInWithPublishPermissions(this, PERMISSIONS);

    loginBtn.registerCallback(
        callbackManager,
        new FacebookCallback<LoginResult>() {

          @Override
          public void onSuccess(LoginResult loginResult) {

            sharePhotoToFacebook();
          }

          @Override
          public void onCancel() {
            userName.setText("Login attempt canceled.");
          }

          @Override
          public void onError(FacebookException e) {
            userName.setText("Login attempt failed.");
          }
        });
  }
Beispiel #2
0
    @Override
    public void onClick(View v) {
      callExternalOnClickListener(v);

      Context context = getContext();

      AccessToken accessToken = AccessToken.getCurrentAccessToken();

      if (accessToken != null) {
        // 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;
          Profile profile = Profile.getCurrentProfile();
          if (profile != null && profile.getName() != null) {
            message =
                String.format(
                    getResources().getString(R.string.com_facebook_loginview_logged_in_as),
                    profile.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) {
                      getLoginManager().logOut();
                    }
                  })
              .setNegativeButton(cancel, null);
          builder.create().show();
        } else {
          getLoginManager().logOut();
        }
      } else {
        LoginManager loginManager = getLoginManager();
        loginManager.setDefaultAudience(getDefaultAudience());
        loginManager.setLoginBehavior(getLoginBehavior());

        if (LoginAuthorizationType.PUBLISH.equals(properties.authorizationType)) {
          if (LoginButton.this.getFragment() != null) {
            loginManager.logInWithPublishPermissions(
                LoginButton.this.getFragment(), properties.permissions);
          } else {
            loginManager.logInWithPublishPermissions(
                LoginButton.this.getActivity(), properties.permissions);
          }
        } else {
          if (LoginButton.this.getFragment() != null) {
            loginManager.logInWithReadPermissions(
                LoginButton.this.getFragment(), properties.permissions);
          } else {
            loginManager.logInWithReadPermissions(
                LoginButton.this.getActivity(), properties.permissions);
          }
        }
      }

      AppEventsLogger logger = AppEventsLogger.newLogger(getContext());

      Bundle parameters = new Bundle();
      parameters.putInt("logging_in", (accessToken != null) ? 0 : 1);

      logger.logSdkEvent(loginLogoutEventName, null, parameters);
    }
  private void executeGraph(JSONArray args, CallbackContext callbackContext) throws JSONException {
    graphContext = callbackContext;
    PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
    pr.setKeepCallback(true);
    graphContext.sendPluginResult(pr);

    graphPath = args.getString(0);
    JSONArray arr = args.getJSONArray(1);

    final Set<String> permissions = new HashSet<String>(arr.length());
    for (int i = 0; i < arr.length(); i++) {
      permissions.add(arr.getString(i));
    }

    if (permissions.size() == 0) {
      makeGraphCall();
      return;
    }

    boolean publishPermissions = false;
    boolean readPermissions = false;
    String declinedPermission = null;

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken.getPermissions().containsAll(permissions)) {
      makeGraphCall();
      return;
    }

    Set<String> declined = accessToken.getDeclinedPermissions();

    // Figure out if we have all permissions
    for (String permission : permissions) {
      if (declined.contains(permission)) {
        declinedPermission = permission;
        break;
      }

      if (isPublishPermission(permission)) {
        publishPermissions = true;
      } else {
        readPermissions = true;
      }

      // Break if we have a mixed bag, as this is an error
      if (publishPermissions && readPermissions) {
        break;
      }
    }

    if (declinedPermission != null) {
      graphContext.error("This request needs declined permission: " + declinedPermission);
    }

    if (publishPermissions && readPermissions) {
      graphContext.error("Cannot ask for both read and publish permissions.");
      return;
    }

    cordova.setActivityResultCallback(this);
    LoginManager loginManager = LoginManager.getInstance();
    // Check for write permissions, the default is read (empty)
    if (publishPermissions) {
      // Request new publish permissions
      loginManager.logInWithPublishPermissions(cordova.getActivity(), permissions);
    } else {
      // Request new read permissions
      loginManager.logInWithReadPermissions(cordova.getActivity(), permissions);
    }
  }