OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.webshare; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.Intent; |
| 9 import android.support.annotation.Nullable; |
| 10 |
| 11 import org.chromium.chrome.R; |
| 12 import org.chromium.content.browser.ContentViewCore; |
| 13 import org.chromium.content_public.browser.WebContents; |
| 14 import org.chromium.mojo.system.MojoException; |
| 15 import org.chromium.mojom.webshare.ShareService; |
| 16 import org.chromium.ui.base.WindowAndroid; |
| 17 |
| 18 /** |
| 19 * Android implementation of the ShareService service defined in |
| 20 * third_party/WebKit/public/platform/modules/webshare/webshare.mojom. |
| 21 */ |
| 22 public class ShareServiceImpl implements ShareService { |
| 23 private final WindowAndroid mWindow; |
| 24 private final Context mContext; |
| 25 |
| 26 public ShareServiceImpl(@Nullable WebContents webContents) { |
| 27 mWindow = windowFromWebContents(webContents); |
| 28 mContext = contextFromWindow(mWindow); |
| 29 } |
| 30 |
| 31 @Override |
| 32 public void close() {} |
| 33 |
| 34 @Override |
| 35 public void onConnectionError(MojoException e) {} |
| 36 |
| 37 @Override |
| 38 public void share(String title, String text, ShareResponse callback) { |
| 39 if (mContext == null) { |
| 40 callback.call("Share failed"); |
| 41 return; |
| 42 } |
| 43 |
| 44 String chooserTitle = mContext.getString(R.string.share_link_chooser_tit
le); |
| 45 Intent send = new Intent(Intent.ACTION_SEND); |
| 46 send.setType("text/plain"); |
| 47 send.putExtra(Intent.EXTRA_SUBJECT, title); |
| 48 send.putExtra(Intent.EXTRA_TEXT, text); |
| 49 |
| 50 Intent chooser = Intent.createChooser(send, chooserTitle); |
| 51 if (!mWindow.showIntent(chooser, null, null)) { |
| 52 callback.call("Share failed"); |
| 53 return; |
| 54 } |
| 55 |
| 56 // Success. |
| 57 // TODO(mgiuca): Wait until the user has made a choice, and report failu
re if they cancel |
| 58 // the picker or something else goes wrong. |
| 59 callback.call(null); |
| 60 } |
| 61 |
| 62 @Nullable |
| 63 private static WindowAndroid windowFromWebContents(@Nullable WebContents web
Contents) { |
| 64 if (webContents == null) return null; |
| 65 |
| 66 ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webCon
tents); |
| 67 if (contentViewCore == null) return null; |
| 68 |
| 69 return contentViewCore.getWindowAndroid(); |
| 70 } |
| 71 |
| 72 @Nullable |
| 73 private static Context contextFromWindow(@Nullable WindowAndroid window) { |
| 74 if (window == null) return null; |
| 75 |
| 76 return window.getActivity().get(); |
| 77 } |
| 78 } |
OLD | NEW |