Search in sources :

Example 1 with ShareApi

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

the class RequestTests method testShareOpenGraphContentWithBadType.

@LargeTest
public void testShareOpenGraphContentWithBadType() throws Exception {
    ShareOpenGraphObject ogObject = new ShareOpenGraphObject.Builder().putString("og:title", "a title").putString("og:type", TEST_OG_OBJECT_TYPE).putString("og:description", "a description").build();
    ShareOpenGraphAction ogAction = new ShareOpenGraphAction.Builder().setActionType(TEST_OG_ACTION_TYPE + "bad").putObject("test", ogObject).build();
    ShareOpenGraphContent content = new ShareOpenGraphContent.Builder().setAction(ogAction).setPreviewPropertyName("test").build();
    final ShareApi shareApi = new ShareApi(content);
    final AtomicReference<String> actionId = new AtomicReference<>(null);
    final AtomicBoolean errorOccurred = new AtomicBoolean(false);
    getActivity().runOnUiThread(new Runnable() {

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

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

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

                @Override
                public void onError(FacebookException error) {
                    errorOccurred.set(true);
                    notifyShareFinished();
                }

                private void notifyShareFinished() {
                    synchronized (shareApi) {
                        shareApi.notifyAll();
                    }
                }
            });
        }
    });
    synchronized (shareApi) {
        shareApi.wait(REQUEST_TIMEOUT_MILLIS);
    }
    assertNull(actionId.get());
    assertTrue(errorOccurred.get());
}
Also used : ShareOpenGraphAction(com.facebook.share.model.ShareOpenGraphAction) ShareOpenGraphObject(com.facebook.share.model.ShareOpenGraphObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShareOpenGraphContent(com.facebook.share.model.ShareOpenGraphContent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Sharer(com.facebook.share.Sharer) ShareApi(com.facebook.share.ShareApi) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 2 with ShareApi

use of com.facebook.share.ShareApi in project react-native-fbsdk by facebook.

the class FBShareAPIModule method canShare.

/**
     * Check if the content can be shared.
     * @param shareContentMap must be a valid {@link ShareContent}
     * @param promise Use promise to pass the result to JS.
     */
@ReactMethod
public void canShare(ReadableMap shareContentMap, Promise promise) {
    ShareContent shareContent = Utility.buildShareContent(shareContentMap);
    if (shareContent != null) {
        ShareApi shareApi = new ShareApi(shareContent);
        promise.resolve(shareApi.canShare());
    } else {
        promise.reject("ShareContent cannot be null");
    }
}
Also used : ShareApi(com.facebook.share.ShareApi) ShareContent(com.facebook.share.model.ShareContent) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 3 with ShareApi

use of com.facebook.share.ShareApi in project react-native-fbsdk by facebook.

the class FBShareAPIModule method share.

/**
     * Share the content.
     * @param shareContentMap must be a valid {@link ShareContent}.
     * @param graphNode The graph node to share to (this can be a user id, event id, page id, group id, album
     * id, etc).
     * @param message The custom message that will accompany the share content.
     * @param promise Use promise to pass share api result to JS.
     */
@ReactMethod
public void share(ReadableMap shareContentMap, String graphNode, String message, Promise promise) {
    ShareContent shareContent = Utility.buildShareContent(shareContentMap);
    if (shareContent != null) {
        ShareApi shareApi = new ShareApi(shareContent);
        shareApi.setGraphNode(graphNode);
        shareApi.setMessage(message);
        shareApi.share(new ShareAPICallback(promise));
    } else {
        promise.reject("ShareContent cannot be null");
    }
}
Also used : ShareApi(com.facebook.share.ShareApi) ShareContent(com.facebook.share.model.ShareContent) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 4 with ShareApi

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

the class RequestTests method testShareOpenGraphContent.

@LargeTest
public void testShareOpenGraphContent() throws Exception {
    ShareOpenGraphObject ogObject = new ShareOpenGraphObject.Builder().putString("og:title", "a title").putString("og:type", TEST_OG_OBJECT_TYPE).putString("og:description", "a description").build();
    ShareOpenGraphAction ogAction = new ShareOpenGraphAction.Builder().setActionType(TEST_OG_ACTION_TYPE).putObject("test", ogObject).build();
    ShareOpenGraphContent content = new ShareOpenGraphContent.Builder().setAction(ogAction).setPreviewPropertyName("test").build();
    final ShareApi shareApi = new ShareApi(content);
    final AtomicReference<String> actionId = new AtomicReference<>(null);
    getActivity().runOnUiThread(new Runnable() {

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

                @Override
                public void onSuccess(Sharer.Result result) {
                    actionId.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(actionId.get());
}
Also used : ShareOpenGraphAction(com.facebook.share.model.ShareOpenGraphAction) ShareOpenGraphObject(com.facebook.share.model.ShareOpenGraphObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShareOpenGraphContent(com.facebook.share.model.ShareOpenGraphContent) Sharer(com.facebook.share.Sharer) ShareApi(com.facebook.share.ShareApi) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 5 with ShareApi

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

the class RequestTests method testUploadVideoFile.

@LargeTest
public void testUploadVideoFile() throws IOException, URISyntaxException {
    File tempFile = null;
    try {
        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);
        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) 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