Java Code Examples for org.chromium.chrome.browser.share.ShareHelper#share()

The following examples show how to use org.chromium.chrome.browser.share.ShareHelper#share() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChromeActivity.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Triggered when the share menu item is selected.
 * This creates and shows a share intent picker dialog or starts a share intent directly.
 * @param shareDirectly Whether it should share directly with the activity that was most
 *                      recently used to share.
 * @param isIncognito Whether currentTab is incognito.
 */
public void onShareMenuItemSelected(final boolean shareDirectly, boolean isIncognito) {
    final Tab currentTab = getActivityTab();
    if (currentTab == null) return;

    final Activity mainActivity = this;
    ContentBitmapCallback callback = new ContentBitmapCallback() {
                @Override
                public void onFinishGetBitmap(Bitmap bitmap, int response) {
                    // Check whether this page is an offline page, and use its online URL if so.
                    String url = currentTab.getOfflinePageOriginalUrl();
                    RecordHistogram.recordBooleanHistogram(
                            "OfflinePages.SharedPageWasOffline", url != null);

                    // If there is no entry in the offline pages DB for this tab, use the tab's
                    // URL directly.
                    if (url == null) url = currentTab.getUrl();

                    ShareHelper.share(
                            shareDirectly, mainActivity, currentTab.getTitle(), url, bitmap);
                    if (shareDirectly) {
                        RecordUserAction.record("MobileMenuDirectShare");
                    } else {
                        RecordUserAction.record("MobileMenuShare");
                    }
                }
            };
    if (isIncognito || currentTab.getWebContents() == null) {
        callback.onFinishGetBitmap(null, ReadbackResponse.SURFACE_UNAVAILABLE);
    } else {
        currentTab.getWebContents().getContentBitmapAsync(
                Bitmap.Config.ARGB_8888, 1.f, EMPTY_RECT, callback);
    }
}
 
Example 2
Source File: ShareServiceImpl.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void share(String title, String text, Url url, final ShareResponse callback) {
    RecordHistogram.recordEnumeratedHistogram("WebShare.ApiCount", WEBSHARE_METHOD_SHARE,
            WEBSHARE_METHOD_COUNT);

    if (mActivity == null) {
        RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                WEBSHARE_OUTCOME_UNKNOWN_FAILURE, WEBSHARE_OUTCOME_COUNT);
        callback.call("Share failed");
        return;
    }

    ShareHelper.TargetChosenCallback innerCallback = new ShareHelper.TargetChosenCallback() {
        public void onTargetChosen(ComponentName chosenComponent) {
            RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                    WEBSHARE_OUTCOME_SUCCESS, WEBSHARE_OUTCOME_COUNT);
            callback.call(null);
        }

        public void onCancel() {
            RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                    WEBSHARE_OUTCOME_CANCELED, WEBSHARE_OUTCOME_COUNT);
            callback.call("Share canceled");
        }
    };

    ShareHelper.share(false, false, mActivity, title, text, url.url, null, null, innerCallback);
}
 
Example 3
Source File: ShareServiceImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void share(String title, String text, Url url, final ShareResponse callback) {
    RecordHistogram.recordEnumeratedHistogram("WebShare.ApiCount", WEBSHARE_METHOD_SHARE,
            WEBSHARE_METHOD_COUNT);

    if (mActivity == null) {
        RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                WEBSHARE_OUTCOME_UNKNOWN_FAILURE, WEBSHARE_OUTCOME_COUNT);
        callback.call(ShareError.INTERNAL_ERROR);
        return;
    }

    ShareHelper.TargetChosenCallback innerCallback = new ShareHelper.TargetChosenCallback() {
        @Override
        public void onTargetChosen(ComponentName chosenComponent) {
            RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                    WEBSHARE_OUTCOME_SUCCESS, WEBSHARE_OUTCOME_COUNT);
            callback.call(ShareError.OK);
        }

        @Override
        public void onCancel() {
            RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
                    WEBSHARE_OUTCOME_CANCELED, WEBSHARE_OUTCOME_COUNT);
            callback.call(ShareError.CANCELED);
        }
    };

    ShareHelper.share(false, false, mActivity, title, text, url.url, null, null, innerCallback);
}
 
Example 4
Source File: ChromeActivity.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
private void triggerShare(
        final Tab currentTab, final boolean shareDirectly, boolean isIncognito) {
    final Activity mainActivity = this;
    WebContents webContents = currentTab.getWebContents();

    RecordHistogram.recordBooleanHistogram(
            "OfflinePages.SharedPageWasOffline", currentTab.isOfflinePage());
    boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled();

    // Share an empty blockingUri in place of screenshot file. The file ready notification is
    // sent by onScreenshotReady call below when the file is written.
    final Uri blockingUri = (isIncognito || webContents == null)
            ? null
            : ChromeFileProvider.generateUriAndBlockAccess(mainActivity);
    if (canShareOfflinePage) {
        OfflinePageUtils.shareOfflinePage(shareDirectly, true, mainActivity, null,
                blockingUri, null, currentTab);
    } else {
        ShareHelper.share(shareDirectly, true, mainActivity, currentTab.getTitle(), null,
                currentTab.getUrl(), null, blockingUri, null);
        if (shareDirectly) {
            RecordUserAction.record("MobileMenuDirectShare");
        } else {
            RecordUserAction.record("MobileMenuShare");
        }
    }

    if (blockingUri == null) return;

    // Start screenshot capture and notify the provider when it is ready.
    ContentBitmapCallback callback = new ContentBitmapCallback() {
        @Override
        public void onFinishGetBitmap(Bitmap bitmap, int response) {
            ShareHelper.saveScreenshotToDisk(bitmap, mainActivity,
                    new Callback<Uri>() {
                        @Override
                        public void onResult(Uri result) {
                            // Unblock the file once it is saved to disk.
                            ChromeFileProvider.notifyFileReady(blockingUri, result);
                        }
                    });
        }
    };
    if (!mScreenshotCaptureSkippedForTesting) {
        webContents.getContentBitmapAsync(Bitmap.Config.ARGB_8888, 1.f, EMPTY_RECT, callback);
    } else {
        callback.onFinishGetBitmap(null, ReadbackResponse.SURFACE_UNAVAILABLE);
    }
}
 
Example 5
Source File: ChromeActivity.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void triggerShare(
        final Tab currentTab, final boolean shareDirectly, boolean isIncognito) {
    final Activity mainActivity = this;
    WebContents webContents = currentTab.getWebContents();

    RecordHistogram.recordBooleanHistogram(
            "OfflinePages.SharedPageWasOffline", OfflinePageUtils.isOfflinePage(currentTab));
    boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled();

    // Share an empty blockingUri in place of screenshot file. The file ready notification is
    // sent by onScreenshotReady call below when the file is written.
    final Uri blockingUri = (isIncognito || webContents == null)
            ? null
            : ChromeFileProvider.generateUriAndBlockAccess(mainActivity);
    if (canShareOfflinePage) {
        OfflinePageUtils.shareOfflinePage(shareDirectly, true, mainActivity, null,
                blockingUri, null, currentTab);
    } else {
        ShareHelper.share(shareDirectly, true, mainActivity, currentTab.getTitle(), null,
                currentTab.getUrl(), null, blockingUri, null);
        if (shareDirectly) {
            RecordUserAction.record("MobileMenuDirectShare");
        } else {
            RecordUserAction.record("MobileMenuShare");
        }
    }

    if (blockingUri == null) return;

    // Start screenshot capture and notify the provider when it is ready.
    ContentBitmapCallback callback = new ContentBitmapCallback() {
        @Override
        public void onFinishGetBitmap(Bitmap bitmap, int response) {
            ShareHelper.saveScreenshotToDisk(bitmap, mainActivity,
                    new Callback<Uri>() {
                        @Override
                        public void onResult(Uri result) {
                            // Unblock the file once it is saved to disk.
                            ChromeFileProvider.notifyFileReady(blockingUri, result);
                        }
                    });
        }
    };
    if (mScreenshotCaptureSkippedForTesting) {
        callback.onFinishGetBitmap(null, ReadbackResponse.SURFACE_UNAVAILABLE);
    } else {
        webContents.getContentBitmapAsync(0, 0, callback);
    }
}