Commit fb9b01d2 authored by jaekyun's avatar jaekyun Committed by Commit bot

Refactor codes to run only the bitmap converting logic in background

This is to fix regression of https://codereview.chromium.org/913033002.

BUG=455996

Review URL: https://codereview.chromium.org/972293003

Cr-Commit-Position: refs/heads/master@{#319010}
parent 96024224
...@@ -17,6 +17,7 @@ import android.content.pm.PackageManager.NameNotFoundException; ...@@ -17,6 +17,7 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
...@@ -119,7 +120,7 @@ public class ShareHelper { ...@@ -119,7 +120,7 @@ public class ShareHelper {
*/ */
private static void showShareDialog(final Activity activity, final String title, private static void showShareDialog(final Activity activity, final String title,
final String url, final Bitmap screenshot) { final String url, final Bitmap screenshot) {
Intent intent = getShareIntent(activity, title, url, null); Intent intent = getShareIntent(title, url, null);
PackageManager manager = activity.getPackageManager(); PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0); List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
assert resolveInfoList.size() > 0; assert resolveInfoList.size() > 0;
...@@ -166,21 +167,46 @@ public class ShareHelper { ...@@ -166,21 +167,46 @@ public class ShareHelper {
private static void makeIntentAndShare(final Activity activity, final String title, private static void makeIntentAndShare(final Activity activity, final String title,
final String url, final Bitmap screenshot, final ComponentName component) { final String url, final Bitmap screenshot, final ComponentName component) {
if (screenshot == null) { if (screenshot == null) {
activity.startActivity( activity.startActivity(getDirectShareIntentForComponent(title, url, null, component));
getDirectShareIntentForComponent(activity, title, url, null, component));
} else { } else {
new AsyncTask<Void, Void, Intent>() { new AsyncTask<Void, Void, File>() {
@Override @Override
protected Intent doInBackground(Void... params) { protected File doInBackground(Void... params) {
return getDirectShareIntentForComponent( FileOutputStream fOut = null;
activity, title, url, screenshot, component); try {
File path = new File(UiUtils.getDirectoryForImageCapture(activity),
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();
return saveFile;
}
} catch (IOException ie) {
if (fOut != null) {
try {
fOut.close();
} catch (IOException e) {
// Ignore exception.
}
}
}
return null;
} }
@Override @Override
protected void onPostExecute(Intent intent) { protected void onPostExecute(File saveFile) {
if (ApplicationStatus.getStateForApplication() if (ApplicationStatus.getStateForApplication()
!= ApplicationState.HAS_DESTROYED_ACTIVITIES) { != ApplicationState.HAS_DESTROYED_ACTIVITIES) {
activity.startActivity(intent); Uri screenshotUri = saveFile == null
? null : UiUtils.getUriForImageCaptureFile(activity, saveFile);
activity.startActivity(getDirectShareIntentForComponent(
title, url, screenshotUri, component));
} }
} }
}.execute(); }.execute();
...@@ -217,47 +243,20 @@ public class ShareHelper { ...@@ -217,47 +243,20 @@ public class ShareHelper {
} }
@VisibleForTesting @VisibleForTesting
public static Intent getShareIntent( public static Intent getShareIntent(String title, String url, Uri screenshotUri) {
Context context, String title, String url, Bitmap screenshot) {
url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url); url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, title); intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, url); intent.putExtra(Intent.EXTRA_TEXT, url);
if (screenshot != null) { if (screenshotUri != null) intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
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; return intent;
} }
private static Intent getDirectShareIntentForComponent( private static Intent getDirectShareIntentForComponent(
Context context, String title, String url, Bitmap screenshot, ComponentName component) { String title, String url, Uri screenshotUri, ComponentName component) {
Intent intent = getShareIntent(context, title, url, screenshot); Intent intent = getShareIntent(title, url, screenshotUri);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
| Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setComponent(component); intent.setComponent(component);
......
...@@ -26,8 +26,7 @@ public class ShareUrlTest extends ChromeShellTestBase { ...@@ -26,8 +26,7 @@ public class ShareUrlTest extends ChromeShellTestBase {
} }
private void assertCorrectUrl(String originalUrl, String sharedUrl) { private void assertCorrectUrl(String originalUrl, String sharedUrl) {
Intent intent = ShareHelper.getShareIntent( Intent intent = ShareHelper.getShareIntent("", sharedUrl, null);
getInstrumentation().getTargetContext(), "", sharedUrl, null);
assert (intent.hasExtra(Intent.EXTRA_TEXT)); assert (intent.hasExtra(Intent.EXTRA_TEXT));
String url = intent.getStringExtra(Intent.EXTRA_TEXT); String url = intent.getStringExtra(Intent.EXTRA_TEXT);
assertEquals(originalUrl, url); assertEquals(originalUrl, url);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment