Index: chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
index 9f0590a64eaaa24ec6345cea8b28e4da7fa34e56..9a2ed117c5bb0d94e0ed962bd04d3b6ed3c2d8aa 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
@@ -40,6 +40,7 @@ import org.chromium.base.VisibleForTesting; |
import org.chromium.base.annotations.SuppressFBWarnings; |
import org.chromium.base.metrics.RecordHistogram; |
import org.chromium.chrome.R; |
+import org.chromium.chrome.browser.BlockingFileProvider; |
import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
import org.chromium.ui.UiUtils; |
@@ -179,14 +180,14 @@ public class ShareHelper { |
* @param url URL of the page to be shared. |
* @param screenshot Screenshot of the page to be shared. |
*/ |
- public static void share(boolean shareDirectly, Activity activity, String title, String url, |
- Bitmap screenshot) { |
+ public static void share( |
+ boolean shareDirectly, Activity activity, String title, String url, Uri uri) { |
if (shareDirectly) { |
- shareWithLastUsed(activity, title, url, screenshot); |
+ shareWithLastUsed(activity, title, url, uri); |
} else if (TargetChosenReceiver.isSupported()) { |
- makeIntentAndShare(activity, title, url, screenshot, null); |
+ makeIntentAndShare(activity, title, url, uri, null); |
} else { |
- showShareDialog(activity, title, url, screenshot); |
+ showShareDialog(activity, title, url, uri); |
} |
} |
@@ -250,6 +251,63 @@ public class ShareHelper { |
} |
/** |
+ * Writes the screenshot file and notifies the file provider that the file is ready to be |
+ * accessed by the client. |
+ * |
+ * The bitmap is compressed to jpeg before being written to the file. |
nyquist
2016/07/14 17:36:32
Nit: JPEG
ssid
2016/07/19 18:32:57
Done.
|
+ * |
+ * @param fileName The unique id given to the file provider for the screenshot. |
+ * @param screenshot The screenshot bitmap to be written to file. |
+ * @param activity Activity that is used to access package manager. |
+ */ |
+ public static void onScreenshotReady( |
+ final String fileName, final Bitmap screenshot, final Activity activity) { |
+ if (screenshot == null) { |
+ BlockingFileProvider.notifyFileReady(fileName, null); |
nyquist
2016/07/14 17:36:32
Should this be posted as a task on the main thread
ssid
2016/07/19 18:32:57
Below it is on a task since it needs to compress a
|
+ return; |
+ } |
+ |
+ new AsyncTask<Void, Void, File>() { |
+ @Override |
+ protected File doInBackground(Void... params) { |
+ FileOutputStream fOut = null; |
+ try { |
+ File path = new File(UiUtils.getDirectoryForImageCapture(activity) + "/" |
+ + SHARE_IMAGES_DIRECTORY_NAME); |
+ if (path.exists() || path.mkdir()) { |
+ File saveFile = File.createTempFile(fileName, JPEG_EXTENSION, path); |
+ fOut = new FileOutputStream(saveFile); |
+ screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut); |
+ fOut.flush(); |
+ fOut.close(); |
+ return saveFile; |
+ } |
+ } catch (IOException ie) { |
+ if (fOut != null) { |
+ try { |
+ fOut.close(); |
+ } catch (IOException e) { |
+ // Ignore exception. |
+ } |
+ } |
+ } |
+ |
+ return null; |
+ } |
+ |
+ @Override |
+ protected void onPostExecute(File saveFile) { |
+ if (ApplicationStatus.getStateForApplication() |
nyquist
2016/07/14 17:36:32
If the state is not correct, should notifyFileRead
ssid
2016/07/19 18:32:57
Done.
|
+ != ApplicationState.HAS_DESTROYED_ACTIVITIES) { |
+ Uri fileUri = saveFile == null ? null : UiUtils.getUriForImageCaptureFile( |
+ activity, saveFile); |
+ BlockingFileProvider.notifyFileReady(fileName, fileUri); |
+ } |
+ } |
+ }.execute(); |
+ } |
+ |
+ /** |
* Creates and shows a share intent picker dialog. |
* |
* @param activity Activity that is used to access package manager. |
@@ -257,8 +315,8 @@ public class ShareHelper { |
* @param url URL of the page to be shared. |
* @param screenshot Screenshot of the page to be shared. |
*/ |
- private static void showShareDialog(final Activity activity, final String title, |
- final String url, final Bitmap screenshot) { |
+ private static void showShareDialog( |
+ final Activity activity, final String title, final String url, final Uri uri) { |
Intent intent = getShareIntent(title, url, null); |
PackageManager manager = activity.getPackageManager(); |
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0); |
@@ -282,7 +340,7 @@ public class ShareHelper { |
ComponentName component = |
new ComponentName(ai.applicationInfo.packageName, ai.name); |
setLastShareComponentName(activity, component); |
- makeIntentAndShare(activity, title, url, screenshot, component); |
+ makeIntentAndShare(activity, title, url, uri, component); |
dialog.dismiss(); |
} |
}); |
@@ -296,11 +354,10 @@ public class ShareHelper { |
* @param url URL of the page to be shared. |
* @param screenshot Screenshot of the page to be shared. |
*/ |
- private static void shareWithLastUsed( |
- Activity activity, String title, String url, Bitmap screenshot) { |
+ private static void shareWithLastUsed(Activity activity, String title, String url, Uri uri) { |
ComponentName component = getLastShareComponentName(activity); |
if (component == null) return; |
- makeIntentAndShare(activity, title, url, screenshot, component); |
+ makeIntentAndShare(activity, title, url, uri, component); |
} |
private static void shareIntent(Activity activity, Intent sharingIntent) { |
@@ -313,52 +370,11 @@ public class ShareHelper { |
} |
private static void makeIntentAndShare(final Activity activity, final String title, |
- final String url, final Bitmap screenshot, final ComponentName component) { |
- if (screenshot == null) { |
+ final String url, final Uri uri, final ComponentName component) { |
+ if (uri == null) { |
shareIntent(activity, getDirectShareIntentForComponent(title, url, null, component)); |
} else { |
- new AsyncTask<Void, Void, File>() { |
- @Override |
- protected File doInBackground(Void... params) { |
- FileOutputStream fOut = null; |
- try { |
- File path = new File(UiUtils.getDirectoryForImageCapture(activity), |
- SHARE_IMAGES_DIRECTORY_NAME); |
- if (path.exists() || path.mkdir()) { |
- File saveFile = File.createTempFile( |
- String.valueOf(System.currentTimeMillis()), |
- JPEG_EXTENSION, path); |
- fOut = new FileOutputStream(saveFile); |
- screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut); |
- fOut.flush(); |
- fOut.close(); |
- |
- return saveFile; |
- } |
- } catch (IOException ie) { |
- if (fOut != null) { |
- try { |
- fOut.close(); |
- } catch (IOException e) { |
- // Ignore exception. |
- } |
- } |
- } |
- |
- return null; |
- } |
- |
- @Override |
- protected void onPostExecute(File saveFile) { |
- if (ApplicationStatus.getStateForApplication() |
- != ApplicationState.HAS_DESTROYED_ACTIVITIES) { |
- Uri screenshotUri = saveFile == null |
- ? null : UiUtils.getUriForImageCaptureFile(activity, saveFile); |
- shareIntent(activity, getDirectShareIntentForComponent( |
- title, url, screenshotUri, component)); |
- } |
- } |
- }.execute(); |
+ shareIntent(activity, getDirectShareIntentForComponent(title, url, uri, component)); |
} |
} |