Search in sources :

Example 1 with FacebookCallback

use of com.facebook.FacebookCallback in project facebook-android-sdk by facebook.

the class ShareApi method sharePhotoContent.

private void sharePhotoContent(final SharePhotoContent photoContent, final FacebookCallback<Sharer.Result> callback) {
    final Mutable<Integer> requestCount = new Mutable<Integer>(0);
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    final ArrayList<GraphRequest> requests = new ArrayList<GraphRequest>();
    final ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    final ArrayList<GraphResponse> errorResponses = new ArrayList<GraphResponse>();
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject result = response.getJSONObject();
            if (result != null) {
                results.add(result);
            }
            if (response.getError() != null) {
                errorResponses.add(response);
            }
            requestCount.value -= 1;
            if (requestCount.value == 0) {
                if (!errorResponses.isEmpty()) {
                    ShareInternalUtility.invokeCallbackWithResults(callback, null, errorResponses.get(0));
                } else if (!results.isEmpty()) {
                    final String postId = results.get(0).optString("id");
                    ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
                }
            }
        }
    };
    try {
        for (SharePhoto photo : photoContent.getPhotos()) {
            Bundle params;
            try {
                params = getSharePhotoCommonParameters(photo, photoContent);
            } catch (JSONException e) {
                ShareInternalUtility.invokeCallbackWithException(callback, e);
                return;
            }
            final Bitmap bitmap = photo.getBitmap();
            final Uri photoUri = photo.getImageUrl();
            String caption = photo.getCaption();
            if (caption == null) {
                caption = this.getMessage();
            }
            if (bitmap != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), bitmap, caption, params, requestCallback));
            } else if (photoUri != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), photoUri, caption, params, requestCallback));
            }
        }
        requestCount.value += requests.size();
        for (GraphRequest request : requests) {
            request.executeAsync();
        }
    } catch (final FileNotFoundException ex) {
        ShareInternalUtility.invokeCallbackWithException(callback, ex);
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) Uri(android.net.Uri) Mutable(com.facebook.internal.Mutable) Bitmap(android.graphics.Bitmap) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) AccessToken(com.facebook.AccessToken)

Example 2 with FacebookCallback

use of com.facebook.FacebookCallback in project facebook-android-sdk by facebook.

the class CreateAppGroupDialog method registerCallbackImpl.

@Override
protected void registerCallbackImpl(final CallbackManagerImpl callbackManager, final FacebookCallback<Result> callback) {
    final ResultProcessor resultProcessor = (callback == null) ? null : new ResultProcessor(callback) {

        @Override
        public void onSuccess(AppCall appCall, Bundle results) {
            callback.onSuccess(new Result(results.getString("id")));
        }
    };
    CallbackManagerImpl.Callback callbackManagerCallback = new CallbackManagerImpl.Callback() {

        @Override
        public boolean onActivityResult(int resultCode, Intent data) {
            return ShareInternalUtility.handleActivityResult(getRequestCode(), resultCode, data, resultProcessor);
        }
    };
    callbackManager.registerCallback(getRequestCode(), callbackManagerCallback);
}
Also used : AppCall(com.facebook.internal.AppCall) FacebookCallback(com.facebook.FacebookCallback) Bundle(android.os.Bundle) CallbackManagerImpl(com.facebook.internal.CallbackManagerImpl) Intent(android.content.Intent) ResultProcessor(com.facebook.share.internal.ResultProcessor)

Example 3 with FacebookCallback

use of com.facebook.FacebookCallback in project facebook-android-sdk by facebook.

the class ShareApi method shareLinkContent.

private void shareLinkContent(final ShareLinkContent linkContent, final FacebookCallback<Sharer.Result> callback) {
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final Bundle parameters = new Bundle();
    this.addCommonParameters(parameters, linkContent);
    parameters.putString("message", this.getMessage());
    parameters.putString("link", Utility.getUriString(linkContent.getContentUrl()));
    parameters.putString("picture", Utility.getUriString(linkContent.getImageUrl()));
    parameters.putString("name", linkContent.getContentTitle());
    parameters.putString("description", linkContent.getContentDescription());
    parameters.putString("ref", linkContent.getRef());
    new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("feed"), parameters, HttpMethod.POST, requestCallback).executeAsync();
}
Also used : FacebookCallback(com.facebook.FacebookCallback) GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle)

Example 4 with FacebookCallback

use of com.facebook.FacebookCallback in project facebook-android-sdk by facebook.

the class ShareApi method shareOpenGraphContent.

private void shareOpenGraphContent(final ShareOpenGraphContent openGraphContent, final FacebookCallback<Sharer.Result> callback) {
    // In order to create a new Open Graph action using a custom object that does not already
    // exist (objectID or URL), you must first send a request to post the object and then
    // another to post the action.  If a local image is supplied with the object or action, that
    // must be staged first and then referenced by the staging URL that is returned by that
    // request.
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final ShareOpenGraphAction action = openGraphContent.getAction();
    final Bundle parameters = action.getBundle();
    this.addCommonParameters(parameters, openGraphContent);
    if (!Utility.isNullOrEmpty(this.getMessage())) {
        parameters.putString("message", this.getMessage());
    }
    final CollectionMapper.OnMapperCompleteListener stageCallback = new CollectionMapper.OnMapperCompleteListener() {

        @Override
        public void onComplete() {
            try {
                handleImagesOnAction(parameters);
                new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath(URLEncoder.encode(action.getActionType(), DEFAULT_CHARSET)), parameters, HttpMethod.POST, requestCallback).executeAsync();
            } catch (final UnsupportedEncodingException ex) {
                ShareInternalUtility.invokeCallbackWithException(callback, ex);
            }
        }

        @Override
        public void onError(FacebookException exception) {
            ShareInternalUtility.invokeCallbackWithException(callback, exception);
        }
    };
    this.stageOpenGraphAction(parameters, stageCallback);
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) CollectionMapper(com.facebook.internal.CollectionMapper)

Example 5 with FacebookCallback

use of com.facebook.FacebookCallback in project facebook-android-sdk by facebook.

the class FacebookLoginActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook_login);
    callbackManager = CallbackManager.Factory.create();
    fbLoginButton = (LoginButton) findViewById(R.id._fb_login);
    profilePictureView = (ProfilePictureView) findViewById(R.id.user_pic);
    profilePictureView.setCropped(true);
    userNameView = (TextView) findViewById(R.id.user_name);
    final Button deAuthButton = (Button) findViewById(R.id.deauth);
    deAuthButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (!isLoggedIn()) {
                Toast.makeText(FacebookLoginActivity.this, R.string.app_not_logged_in, Toast.LENGTH_LONG).show();
                return;
            }
            GraphRequest.Callback callback = new GraphRequest.Callback() {

                @Override
                public void onCompleted(GraphResponse response) {
                    try {
                        if (response.getError() != null) {
                            Toast.makeText(FacebookLoginActivity.this, getResources().getString(R.string.failed_to_deauth, response.toString()), Toast.LENGTH_LONG).show();
                        } else if (response.getJSONObject().getBoolean(SUCCESS)) {
                            LoginManager.getInstance().logOut();
                        // updateUI();?
                        }
                    } catch (JSONException ex) {
                    /* no op */
                    }
                }
            };
            GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), GRAPH_PATH, new Bundle(), HttpMethod.DELETE, callback);
            request.executeAsync();
        }
    });
    final Button permsButton = (Button) findViewById(R.id.perms);
    permsButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(final View v) {
            Intent selectPermsIntent = new Intent(FacebookLoginActivity.this, PermissionSelectActivity.class);
            startActivityForResult(selectPermsIntent, PICK_PERMS_REQUEST);
        }
    });
    // Callback registration
    fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(final LoginResult loginResult) {
            // App code
            Toast.makeText(FacebookLoginActivity.this, R.string.success, Toast.LENGTH_LONG).show();
            updateUI();
        }

        @Override
        public void onCancel() {
            // App code
            Toast.makeText(FacebookLoginActivity.this, R.string.cancel, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(final FacebookException exception) {
            // App code
            Toast.makeText(FacebookLoginActivity.this, R.string.error, Toast.LENGTH_LONG).show();
        }
    });
    new ProfileTracker() {

        @Override
        protected void onCurrentProfileChanged(final Profile oldProfile, final Profile currentProfile) {
            updateUI();
        }
    };
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) LoginResult(com.facebook.login.LoginResult) JSONException(org.json.JSONException) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) ProfilePictureView(com.facebook.login.widget.ProfilePictureView) Profile(com.facebook.Profile) FacebookCallback(com.facebook.FacebookCallback) ProfileTracker(com.facebook.ProfileTracker) Button(android.widget.Button) LoginButton(com.facebook.login.widget.LoginButton) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException)

Aggregations

Bundle (android.os.Bundle)6 FacebookCallback (com.facebook.FacebookCallback)6 GraphRequest (com.facebook.GraphRequest)4 GraphResponse (com.facebook.GraphResponse)4 Intent (android.content.Intent)3 JSONObject (org.json.JSONObject)3 FacebookException (com.facebook.FacebookException)2 AppCall (com.facebook.internal.AppCall)2 CallbackManagerImpl (com.facebook.internal.CallbackManagerImpl)2 ResultProcessor (com.facebook.share.internal.ResultProcessor)2 JSONException (org.json.JSONException)2 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 AccessToken (com.facebook.AccessToken)1 Profile (com.facebook.Profile)1 ProfileTracker (com.facebook.ProfileTracker)1 CollectionMapper (com.facebook.internal.CollectionMapper)1