Search in sources :

Example 6 with ShareApi

use of com.facebook.share.ShareApi in project facebook-android-sdk by facebook.

the class RequestTests method testExecuteUploadPhotoToAlbum.

@LargeTest
public void testExecuteUploadPhotoToAlbum() throws InterruptedException, JSONException {
    // first create an album
    Bundle params = new Bundle();
    params.putString("name", "Foo");
    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/albums", params, HttpMethod.POST);
    GraphResponse response = request.executeAndWait();
    JSONObject jsonResponse = response.getJSONObject();
    assertNotNull(jsonResponse);
    String albumId = jsonResponse.optString("id");
    assertNotNull(albumId);
    // upload an image to the album
    Bitmap image = createTestBitmap(128);
    SharePhoto photo = new SharePhoto.Builder().setBitmap(image).setUserGenerated(true).build();
    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
    final ShareApi shareApi = new ShareApi(content);
    shareApi.setGraphNode(albumId);
    final AtomicReference<String> imageId = new AtomicReference<>(null);
    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            shareApi.share(new FacebookCallback<Sharer.Result>() {

                @Override
                public void onSuccess(Sharer.Result result) {
                    imageId.set(result.getPostId());
                    notifyShareFinished();
                }

                @Override
                public void onCancel() {
                    notifyShareFinished();
                }

                @Override
                public void onError(FacebookException error) {
                    notifyShareFinished();
                }

                private void notifyShareFinished() {
                    synchronized (shareApi) {
                        shareApi.notifyAll();
                    }
                }
            });
        }
    });
    synchronized (shareApi) {
        shareApi.wait(REQUEST_TIMEOUT_MILLIS);
    }
    assertNotNull(imageId.get());
    // now check to see if the image is in the album
    GraphRequest listRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), albumId + "/photos");
    GraphResponse listResponse = listRequest.executeAndWait();
    JSONObject listObject = listResponse.getJSONObject();
    assertNotNull(listObject);
    JSONArray jsonList = listObject.optJSONArray("data");
    assertNotNull(jsonList);
    boolean found = false;
    for (int i = 0; i < jsonList.length(); i++) {
        JSONObject imageObject = jsonList.getJSONObject(i);
        if (imageId.get().equals(imageObject.optString("id"))) {
            found = true;
        }
    }
    assertTrue(found);
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) AtomicReference(java.util.concurrent.atomic.AtomicReference) Bitmap(android.graphics.Bitmap) JSONObject(org.json.JSONObject) SharePhoto(com.facebook.share.model.SharePhoto) Sharer(com.facebook.share.Sharer) ShareApi(com.facebook.share.ShareApi) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 7 with ShareApi

use of com.facebook.share.ShareApi in project facebook-android-sdk by facebook.

the class RequestTests method testUploadVideoFileToUserId.

@LargeTest
public void testUploadVideoFileToUserId() throws IOException, URISyntaxException {
    File tempFile = null;
    try {
        GraphRequest meRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), null);
        GraphResponse meResponse = meRequest.executeAndWait();
        JSONObject meJson = meResponse.getJSONObject();
        assertNotNull(meJson);
        String userId = meJson.optString("id");
        assertNotNull(userId);
        tempFile = createTempFileFromAsset("DarkScreen.mov");
        ShareVideo video = new ShareVideo.Builder().setLocalUrl(Uri.fromFile(tempFile)).build();
        ShareVideoContent content = new ShareVideoContent.Builder().setVideo(video).build();
        final ShareApi shareApi = new ShareApi(content);
        shareApi.setGraphNode(userId);
        final AtomicReference<String> videoId = new AtomicReference<>(null);
        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                shareApi.share(new FacebookCallback<Sharer.Result>() {

                    @Override
                    public void onSuccess(Sharer.Result result) {
                        videoId.set(result.getPostId());
                        notifyShareFinished();
                    }

                    @Override
                    public void onCancel() {
                        notifyShareFinished();
                    }

                    @Override
                    public void onError(FacebookException error) {
                        notifyShareFinished();
                    }

                    private void notifyShareFinished() {
                        synchronized (shareApi) {
                            shareApi.notifyAll();
                        }
                    }
                });
            }
        });
        synchronized (shareApi) {
            shareApi.wait(REQUEST_TIMEOUT_MILLIS);
        }
        assertNotNull(videoId.get());
    } catch (Exception ex) {
        fail();
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}
Also used : ShareVideo(com.facebook.share.model.ShareVideo) AtomicReference(java.util.concurrent.atomic.AtomicReference) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) ShareVideoContent(com.facebook.share.model.ShareVideoContent) Sharer(com.facebook.share.Sharer) File(java.io.File) ShareApi(com.facebook.share.ShareApi) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

ShareApi (com.facebook.share.ShareApi)7 LargeTest (android.test.suitebuilder.annotation.LargeTest)5 Sharer (com.facebook.share.Sharer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ReactMethod (com.facebook.react.bridge.ReactMethod)2 ShareContent (com.facebook.share.model.ShareContent)2 ShareOpenGraphAction (com.facebook.share.model.ShareOpenGraphAction)2 ShareOpenGraphContent (com.facebook.share.model.ShareOpenGraphContent)2 ShareOpenGraphObject (com.facebook.share.model.ShareOpenGraphObject)2 ShareVideo (com.facebook.share.model.ShareVideo)2 ShareVideoContent (com.facebook.share.model.ShareVideoContent)2 File (java.io.File)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Bitmap (android.graphics.Bitmap)1 Bundle (android.os.Bundle)1 SharePhoto (com.facebook.share.model.SharePhoto)1 SharePhotoContent (com.facebook.share.model.SharePhotoContent)1