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 62d2926c61cb6ded5d51ef57685679a66d9aaebc..5fcad3e7556c5490d9826ae85f0434bf5c183333 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 |
@@ -17,17 +17,25 @@ import android.content.pm.PackageManager.NameNotFoundException; |
import android.content.pm.ResolveInfo; |
import android.graphics.Bitmap; |
import android.graphics.drawable.Drawable; |
+import android.os.AsyncTask; |
import android.preference.PreferenceManager; |
+import android.util.Log; |
import android.view.MenuItem; |
import android.view.View; |
import android.widget.AdapterView; |
import android.widget.AdapterView.OnItemClickListener; |
import org.chromium.base.ApiCompatibilityUtils; |
+import org.chromium.base.ApplicationState; |
+import org.chromium.base.ApplicationStatus; |
import org.chromium.base.VisibleForTesting; |
import org.chromium.chrome.R; |
import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
+import org.chromium.ui.UiUtils; |
+import java.io.File; |
+import java.io.FileOutputStream; |
+import java.io.IOException; |
import java.util.Collections; |
import java.util.List; |
@@ -36,22 +44,55 @@ import java.util.List; |
*/ |
public class ShareHelper { |
+ private static final String TAG = "ShareHelper"; |
+ |
private static final String PACKAGE_NAME_KEY = "last_shared_package_name"; |
private static final String CLASS_NAME_KEY = "last_shared_class_name"; |
/** |
- * Intent extra for sharing screenshots via the Share intent. |
- * |
- * Copied from {@link android.provider.Browser} as it is marked as {@literal @hide}. |
+ * Directory name for screenshots. |
*/ |
- private static final String EXTRA_SHARE_SCREENSHOT = "share_screenshot"; |
+ private static final String SCREENSHOT_DIRECTORY_NAME = "screenshot"; |
private ShareHelper() {} |
+ private static void deleteScreenshotFiles(File file) { |
+ if (!file.exists()) return; |
+ if (file.isDirectory()) { |
+ for (File f : file.listFiles()) deleteScreenshotFiles(f); |
+ } |
+ if (!file.delete()) { |
+ Log.w(TAG, "Failed to delete screenshot file: " + file.getAbsolutePath()); |
+ } |
+ } |
+ |
+ /** |
+ * Clears all shared screenshot files. |
+ */ |
+ public static void clearSharedScreenshots(final Context context) { |
+ new AsyncTask<Void, Void, Void>() { |
+ @Override |
+ protected Void doInBackground(Void... params) { |
+ try { |
+ File imagePath = UiUtils.getDirectoryForImageCapture(context); |
+ deleteScreenshotFiles(new File(imagePath, SCREENSHOT_DIRECTORY_NAME)); |
+ } catch (IOException ie) { |
+ // Ignore exception. |
+ } |
+ return null; |
+ } |
+ }.execute(); |
+ } |
+ |
/** |
* Creates and shows a share intent picker dialog or starts a share intent directly with the |
* activity that was most recently used to share based on shareDirectly value. |
* |
+ * This function will save |screenshot| under {app's root}/files/images/screenshot (or |
+ * /sdcard/DCIM/browser-images/screenshot if ADK is lower than JB MR2). |
+ * Cleaning up doesn't happen automatically, and so an app should call clearSharedScreenshots() |
+ * explicitly when needed. |
+ * |
* @param shareDirectly Whether it should share directly with the activity that was most |
* recently used to share. |
* @param activity Activity that is used to access package manager. |
@@ -78,7 +119,7 @@ public class ShareHelper { |
*/ |
private static void showShareDialog(final Activity activity, final String title, |
final String url, final Bitmap screenshot) { |
- Intent intent = getShareIntent(title, url, screenshot); |
+ Intent intent = getShareIntent(activity, title, url, null); |
PackageManager manager = activity.getPackageManager(); |
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0); |
assert resolveInfoList.size() > 0; |
@@ -101,8 +142,7 @@ public class ShareHelper { |
ComponentName component = |
new ComponentName(ai.applicationInfo.packageName, ai.name); |
setLastShareComponentName(activity, component); |
- Intent intent = getDirectShareIntentForComponent(title, url, screenshot, component); |
- activity.startActivity(intent); |
+ makeIntentAndShare(activity, title, url, screenshot, component); |
dialog.dismiss(); |
} |
}); |
@@ -120,8 +160,31 @@ public class ShareHelper { |
Activity activity, String title, String url, Bitmap screenshot) { |
ComponentName component = getLastShareComponentName(activity); |
if (component == null) return; |
- Intent intent = getDirectShareIntentForComponent(title, url, screenshot, component); |
- activity.startActivity(intent); |
+ makeIntentAndShare(activity, title, url, screenshot, component); |
+ } |
+ |
+ private static void makeIntentAndShare(final Activity activity, final String title, |
+ final String url, final Bitmap screenshot, final ComponentName component) { |
+ if (screenshot == null) { |
+ activity.startActivity( |
+ getDirectShareIntentForComponent(activity, title, url, null, component)); |
+ } else { |
+ new AsyncTask<Void, Void, Intent>() { |
+ @Override |
+ protected Intent doInBackground(Void... params) { |
+ return getDirectShareIntentForComponent( |
+ activity, title, url, screenshot, component); |
+ } |
+ |
+ @Override |
+ protected void onPostExecute(Intent intent) { |
+ if (ApplicationStatus.getStateForApplication() |
+ != ApplicationState.HAS_DESTROYED_ACTIVITIES) { |
+ activity.startActivity(intent); |
+ } |
+ } |
+ }.execute(); |
+ } |
} |
/** |
@@ -154,20 +217,47 @@ public class ShareHelper { |
} |
@VisibleForTesting |
- public static Intent getShareIntent(String title, String url, Bitmap screenshot) { |
+ public static Intent getShareIntent( |
+ Context context, String title, String url, Bitmap screenshot) { |
url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url); |
Intent intent = new Intent(Intent.ACTION_SEND); |
intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); |
intent.setType("text/plain"); |
intent.putExtra(Intent.EXTRA_SUBJECT, title); |
intent.putExtra(Intent.EXTRA_TEXT, url); |
- if (screenshot != null) intent.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot); |
+ if (screenshot != null) { |
+ FileOutputStream fOut = null; |
+ try { |
+ File path = new File( |
+ UiUtils.getDirectoryForImageCapture(context), SCREENSHOT_DIRECTORY_NAME); |
+ if (path.exists() || path.mkdir()) { |
+ File saveFile = File.createTempFile( |
+ String.valueOf(System.currentTimeMillis()), ".jpg", path); |
+ fOut = new FileOutputStream(saveFile); |
+ screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut); |
+ fOut.flush(); |
+ fOut.close(); |
+ |
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
+ intent.putExtra(Intent.EXTRA_STREAM, |
+ UiUtils.getUriForImageCaptureFile(context, saveFile)); |
+ } |
+ } catch (IOException ie) { |
+ if (fOut != null) { |
+ try { |
+ fOut.close(); |
+ } catch (IOException e) { |
+ // Ignore exception. |
+ } |
+ } |
+ } |
+ } |
return intent; |
} |
- private static Intent getDirectShareIntentForComponent(String title, String url, |
- Bitmap screenshot, ComponentName component) { |
- Intent intent = getShareIntent(title, url, screenshot); |
+ private static Intent getDirectShareIntentForComponent( |
+ Context context, String title, String url, Bitmap screenshot, ComponentName component) { |
+ Intent intent = getShareIntent(context, title, url, screenshot); |
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
intent.setComponent(component); |