Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3646)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java

Issue 2241303002: Revert of navigator.share: Resolve promise only after user chooses a target. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 10e37003b6d30427035f16a308ed4efb7e27fa4a..6ee99ab36e0a232950239e430a812b401389f438 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
@@ -24,9 +24,7 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
-import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
-import android.text.TextUtils;
import android.util.Pair;
import android.view.MenuItem;
import android.view.View;
@@ -91,18 +89,6 @@
}
/**
- * Callback interface for when a target is chosen.
- */
- public static interface TargetChosenCallback {
- /**
- * Called when the user chooses a target in the share dialog.
- *
- * Note that if the user cancels the share dialog, this callback is never called.
- */
- public void onTargetChosen(ComponentName chosenComponent);
- }
-
- /**
* Receiver to record the chosen component when sharing an Intent.
*/
static class TargetChosenReceiver extends BroadcastReceiver {
@@ -112,23 +98,12 @@
private static String sTargetChosenReceiveAction;
private static TargetChosenReceiver sLastRegisteredReceiver;
- private final boolean mSaveLastUsed;
- @Nullable private final TargetChosenCallback mCallback;
-
- private TargetChosenReceiver(boolean saveLastUsed,
- @Nullable TargetChosenCallback callback) {
- mSaveLastUsed = saveLastUsed;
- mCallback = callback;
- }
-
static boolean isSupported() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
- static void sendChooserIntent(boolean saveLastUsed, Activity activity,
- Intent sharingIntent,
- @Nullable TargetChosenCallback callback) {
+ static void sendChooserIntent(Activity activity, Intent sharingIntent) {
synchronized (LOCK) {
if (sTargetChosenReceiveAction == null) {
sTargetChosenReceiveAction = activity.getPackageName() + "/"
@@ -138,7 +113,7 @@
if (sLastRegisteredReceiver != null) {
context.unregisterReceiver(sLastRegisteredReceiver);
}
- sLastRegisteredReceiver = new TargetChosenReceiver(saveLastUsed, callback);
+ sLastRegisteredReceiver = new TargetChosenReceiver();
context.registerReceiver(
sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
}
@@ -146,11 +121,11 @@
Intent intent = new Intent(sTargetChosenReceiveAction);
intent.setPackage(activity.getPackageName());
intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
- final PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,
+ final PendingIntent callback = PendingIntent.getBroadcast(activity, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Intent chooserIntent = Intent.createChooser(sharingIntent,
activity.getString(R.string.share_link_chooser_title),
- pendingIntent.getIntentSender());
+ callback.getIntentSender());
activity.startActivity(chooserIntent);
}
@@ -167,10 +142,7 @@
}
ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);
- if (mCallback != null) {
- mCallback.onTargetChosen(target);
- }
- if (mSaveLastUsed && target != null) {
+ if (target != null) {
setLastShareComponentName(target);
}
}
@@ -205,27 +177,19 @@
*
* @param shareDirectly Whether it should share directly with the activity that was most
* recently used to share.
- * @param saveLastUsed Whether to save the chosen activity for future direct sharing.
* @param activity Activity that is used to access package manager.
* @param title Title of the page to be shared.
- * @param text Text to be shared. If both |text| and |url| are supplied, they are concatenated
- * with a space.
* @param url URL of the page to be shared.
* @param screenshot Screenshot of the page to be shared.
- * @param callback Optional callback to be called when user makes a choice. Will not be called
- * if receiving a response when the user makes a choice is not supported (on
- * older Android versions).
- */
- public static void share(boolean shareDirectly, boolean saveLastUsed, Activity activity,
- String title, String text, String url, Bitmap screenshot,
- @Nullable TargetChosenCallback callback) {
+ */
+ public static void share(boolean shareDirectly, Activity activity, String title, String url,
+ Bitmap screenshot) {
if (shareDirectly) {
- shareWithLastUsed(activity, title, text, url, screenshot);
+ shareWithLastUsed(activity, title, url, screenshot);
} else if (TargetChosenReceiver.isSupported()) {
- makeIntentAndShare(saveLastUsed, activity, title, text, url, screenshot, null,
- callback);
+ makeIntentAndShare(activity, title, url, screenshot, null);
} else {
- showShareDialog(saveLastUsed, activity, title, text, url, screenshot, callback);
+ showShareDialog(activity, title, url, screenshot);
}
}
@@ -291,21 +255,14 @@
/**
* Creates and shows a share intent picker dialog.
*
- * @param saveLastUsed Whether to save the chosen activity for future direct sharing.
* @param activity Activity that is used to access package manager.
* @param title Title of the page to be shared.
- * @param text Text to be shared. If both |text| and |url| are supplied, they are concatenated
- * with a space.
* @param url URL of the page to be shared.
* @param screenshot Screenshot of the page to be shared.
- * @param callback Optional callback to be called when user makes a choice. Will not be called
- * if receiving a response when the user makes a choice is not supported (on
- * older Android versions).
- */
- private static void showShareDialog(final boolean saveLastUsed, final Activity activity,
- final String title, final String text, final String url, final Bitmap screenshot,
- @Nullable final TargetChosenCallback callback) {
- Intent intent = getShareIntent(activity, title, text, url, null);
+ */
+ private static void showShareDialog(final Activity activity, final String title,
+ final String url, final Bitmap screenshot) {
+ Intent intent = getShareIntent(activity, title, url, null);
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
assert resolveInfoList.size() > 0;
@@ -327,10 +284,8 @@
ActivityInfo ai = info.activityInfo;
ComponentName component =
new ComponentName(ai.applicationInfo.packageName, ai.name);
- callback.onTargetChosen(component);
- if (saveLastUsed) setLastShareComponentName(component);
- makeIntentAndShare(false, activity, title, text, url, screenshot, component,
- null);
+ setLastShareComponentName(component);
+ makeIntentAndShare(activity, title, url, screenshot, component);
dialog.dismiss();
}
});
@@ -341,38 +296,30 @@
* If there is no most recently used activity, it does nothing.
* @param activity Activity that is used to start the share intent.
* @param title Title of the page to be shared.
- * @param text Text to be shared. If both |text| and |url| are supplied, they are concatenated
- * with a space.
* @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 text, String url, Bitmap screenshot) {
+ Activity activity, String title, String url, Bitmap screenshot) {
ComponentName component = getLastShareComponentName();
if (component == null) return;
- makeIntentAndShare(false, activity, title, text, url, screenshot, component, null);
- }
-
- private static void shareIntent(boolean saveLastUsed, Activity activity, Intent sharingIntent,
- @Nullable TargetChosenCallback callback) {
+ makeIntentAndShare(activity, title, url, screenshot, component);
+ }
+
+ private static void shareIntent(Activity activity, Intent sharingIntent) {
if (sharingIntent.getComponent() != null) {
- // If a component was specified, there should not also be a callback.
- assert callback == null;
activity.startActivity(sharingIntent);
} else {
assert TargetChosenReceiver.isSupported();
- TargetChosenReceiver.sendChooserIntent(saveLastUsed, activity, sharingIntent, callback);
- }
- }
-
- private static void makeIntentAndShare(final boolean saveLastUsed, final Activity activity,
- final String title, final String text, final String url, final Bitmap screenshot,
- final ComponentName component, @Nullable final TargetChosenCallback callback) {
+ TargetChosenReceiver.sendChooserIntent(activity, sharingIntent);
+ }
+ }
+
+ private static void makeIntentAndShare(final Activity activity, final String title,
+ final String url, final Bitmap screenshot, final ComponentName component) {
if (screenshot == null) {
- shareIntent(
- saveLastUsed, activity,
- getDirectShareIntentForComponent(activity, title, text, url, null, component),
- callback);
+ shareIntent(activity,
+ getDirectShareIntentForComponent(activity, title, url, null, component));
} else {
new AsyncTask<Void, Void, File>() {
@Override
@@ -411,11 +358,8 @@
!= ApplicationState.HAS_DESTROYED_ACTIVITIES) {
Uri screenshotUri = saveFile == null
? null : UiUtils.getUriForImageCaptureFile(activity, saveFile);
- shareIntent(
- saveLastUsed, activity,
- getDirectShareIntentForComponent(activity, title, text, url,
- screenshotUri, component),
- callback);
+ shareIntent(activity, getDirectShareIntentForComponent(
+ activity, title, url, screenshotUri, component));
}
}
}.execute();
@@ -435,7 +379,7 @@
final ComponentName component = getLastShareComponentName();
boolean isComponentValid = false;
if (component != null) {
- Intent intent = getShareIntent(activity, "", "", "", null);
+ Intent intent = getShareIntent(activity, "", "", null);
intent.setPackage(component.getPackageName());
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
@@ -495,22 +439,13 @@
@VisibleForTesting
protected static Intent getShareIntent(
- Activity activity, String title, String text, String url, Uri screenshotUri) {
- if (!TextUtils.isEmpty(url)) {
- url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
- if (!TextUtils.isEmpty(text)) {
- // Concatenate text and URL with a space.
- text = text + " " + url;
- } else {
- text = url;
- }
- }
-
+ Activity activity, String title, String url, Uri screenshotUri) {
+ 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, text);
+ intent.putExtra(Intent.EXTRA_TEXT, url);
intent.putExtra(EXTRA_TASK_ID, activity.getTaskId());
if (screenshotUri != null) {
@@ -534,9 +469,9 @@
}
private static Intent getDirectShareIntentForComponent(
- Activity activity, String title, String text, String url,
+ Activity activity, String title, String url,
Uri screenshotUri, ComponentName component) {
- Intent intent = getShareIntent(activity, title, text, url, screenshotUri);
+ Intent intent = getShareIntent(activity, title, url, screenshotUri);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
| Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setComponent(component);

Powered by Google App Engine
This is Rietveld 408576698