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

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

Issue 2917703004: [Android] Wrap all share parameters into the ShareParams class (Closed)
Patch Set: Created 3 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.share; 5 package org.chromium.chrome.browser.share;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.app.Activity; 8 import android.app.Activity;
9 import android.app.PendingIntent; 9 import android.app.PendingIntent;
10 import android.content.BroadcastReceiver; 10 import android.content.BroadcastReceiver;
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 278
279 /** 279 /**
280 * Creates and shows a share intent picker dialog or starts a share intent d irectly with the 280 * Creates and shows a share intent picker dialog or starts a share intent d irectly with the
281 * activity that was most recently used to share based on shareDirectly valu e. 281 * activity that was most recently used to share based on shareDirectly valu e.
282 * 282 *
283 * This function will save |screenshot| under {app's root}/files/images/scre enshot (or 283 * This function will save |screenshot| under {app's root}/files/images/scre enshot (or
284 * /sdcard/DCIM/browser-images/screenshot if ADK is lower than JB MR2). 284 * /sdcard/DCIM/browser-images/screenshot if ADK is lower than JB MR2).
285 * Cleaning up doesn't happen automatically, and so an app should call clear SharedScreenshots() 285 * Cleaning up doesn't happen automatically, and so an app should call clear SharedScreenshots()
286 * explicitly when needed. 286 * explicitly when needed.
287 * 287 *
288 * @param shareDirectly Whether it should share directly with the activity t hat was most 288 * @param params The container holding the share parameters.
289 * recently used to share.
290 * @param saveLastUsed Whether to save the chosen activity for future direct sharing.
291 * @param activity Activity that is used to access package manager.
292 * @param title Title of the page to be shared.
293 * @param text Text to be shared. If both |text| and |url| are supplied, the y are concatenated
294 * with a space.
295 * @param url URL of the page to be shared.
296 * @param offlineUri URI to the offline MHTML file to be shared.
297 * @param screenshotUri Uri of the screenshot of the page to be shared.
298 * @param callback Optional callback to be called when user makes a choice. Will not be called
299 * if receiving a response when the user makes a choice is n ot supported (on
300 * older Android versions).
301 */ 289 */
302 public static void share(boolean shareDirectly, boolean saveLastUsed, Activi ty activity, 290 public static void share(ShareParams params) {
303 String title, String text, String url, @Nullable Uri offlineUri, Uri screenshotUri, 291 if (params.isShareDirectly()) {
304 @Nullable TargetChosenCallback callback) { 292 shareWithLastUsed(params);
305 if (shareDirectly) {
306 shareWithLastUsed(activity, title, text, url, offlineUri, screenshot Uri);
307 } else if (TargetChosenReceiver.isSupported()) { 293 } else if (TargetChosenReceiver.isSupported()) {
308 makeIntentAndShare(saveLastUsed, activity, title, text, url, offline Uri, screenshotUri, 294 makeIntentAndShare(params, null);
309 null, callback);
310 } else { 295 } else {
311 showShareDialog( 296 showShareDialog(params);
312 saveLastUsed, activity, title, text, url, offlineUri, screen shotUri, callback);
313 } 297 }
314 } 298 }
315 299
316 /** 300 /**
317 * Trigger the share action for the given image data. 301 * Trigger the share action for the given image data.
318 * @param activity The activity used to trigger the share action. 302 * @param activity The activity used to trigger the share action.
319 * @param jpegImageData The image data to be shared in jpeg format. 303 * @param jpegImageData The image data to be shared in jpeg format.
320 * @param name When this is not null, it will share the image directly with the 304 * @param name When this is not null, it will share the image directly with the
321 * {@link ComponentName} 305 * {@link ComponentName}
322 */ 306 */
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 fileUri = ApiCompatibilityUtils.getUriForImageCaptureFile(sa vedFile); 417 fileUri = ApiCompatibilityUtils.getUriForImageCaptureFile(sa vedFile);
434 } 418 }
435 callback.onResult(fileUri); 419 callback.onResult(fileUri);
436 } 420 }
437 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 421 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
438 } 422 }
439 423
440 /** 424 /**
441 * Creates and shows a share intent picker dialog. 425 * Creates and shows a share intent picker dialog.
442 * 426 *
443 * @param saveLastUsed Whether to save the chosen activity for future direct sharing. 427 * @param params The container holding the share parameters.
444 * @param activity Activity that is used to access package manager.
445 * @param title Title of the page to be shared.
446 * @param text Text to be shared. If both |text| and |url| are supplied, the y are concatenated
447 * with a space.
448 * @param url URL of the page to be shared.
449 * @oaram offlineUri URI of the offline page to be shared.
450 * @param screenshotUri Uri of the screenshot of the page to be shared.
451 * @param callback Optional callback to be called when user makes a choice. Will not be called
452 * if receiving a response when the user makes a choice is n ot supported (on
453 * older Android versions).
454 */ 428 */
455 private static void showShareDialog(final boolean saveLastUsed, final Activi ty activity, 429 private static void showShareDialog(final ShareParams params) {
456 final String title, final String text, final String url, final Uri o fflineUri, 430 Activity activity = params.getActivity();
457 final Uri screenshotUri, @Nullable final TargetChosenCallback callba ck) { 431 final TargetChosenCallback callback = params.getCallback();
Yusuf 2017/06/01 20:41:45 is it the callback that is creating the problem he
ltian 2017/06/01 23:27:11 No, I think the callback problem has been solved s
458 Intent intent = getShareIntent(activity, title, text, url, null, null); 432 ShareParams simpleShareParams =
Ted C 2017/06/01 20:39:03 does this need to be any different from the defaul
ltian 2017/06/01 23:27:07 Yes, after digging on these I find both of them on
433 new ShareParams.Builder(params.getActivity(), params.getTitle(), params.getUrl())
434 .setText(params.getText())
435 .build();
436 Intent intent = getShareIntent(simpleShareParams);
459 PackageManager manager = activity.getPackageManager(); 437 PackageManager manager = activity.getPackageManager();
460 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0); 438 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0);
461 assert resolveInfoList.size() > 0; 439 assert resolveInfoList.size() > 0;
462 if (resolveInfoList.size() == 0) return; 440 if (resolveInfoList.size() == 0) return;
463 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager)); 441 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager));
464 442
465 final ShareDialogAdapter adapter = 443 final ShareDialogAdapter adapter =
466 new ShareDialogAdapter(activity, manager, resolveInfoList); 444 new ShareDialogAdapter(activity, manager, resolveInfoList);
467 AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style. AlertDialogTheme); 445 AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style. AlertDialogTheme);
468 builder.setTitle(activity.getString(R.string.share_link_chooser_title)); 446 builder.setTitle(activity.getString(R.string.share_link_chooser_title));
469 builder.setAdapter(adapter, null); 447 builder.setAdapter(adapter, null);
470 448
471 // Need a mutable object to record whether the callback has been fired. 449 // Need a mutable object to record whether the callback has been fired.
472 final boolean[] callbackCalled = new boolean[1]; 450 final boolean[] callbackCalled = new boolean[1];
473 451
474 final AlertDialog dialog = builder.create(); 452 final AlertDialog dialog = builder.create();
475 dialog.show(); 453 dialog.show();
476 dialog.getListView().setOnItemClickListener(new OnItemClickListener() { 454 dialog.getListView().setOnItemClickListener(new OnItemClickListener() {
477 @Override 455 @Override
478 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { 456 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) {
479 ResolveInfo info = adapter.getItem(position); 457 ResolveInfo info = adapter.getItem(position);
480 ActivityInfo ai = info.activityInfo; 458 ActivityInfo ai = info.activityInfo;
481 ComponentName component = 459 ComponentName component =
482 new ComponentName(ai.applicationInfo.packageName, ai.nam e); 460 new ComponentName(ai.applicationInfo.packageName, ai.nam e);
461
483 if (callback != null && !callbackCalled[0]) { 462 if (callback != null && !callbackCalled[0]) {
484 callback.onTargetChosen(component); 463 callback.onTargetChosen(component);
485 callbackCalled[0] = true; 464 callbackCalled[0] = true;
486 } 465 }
487 if (saveLastUsed) setLastShareComponentName(component); 466 if (params.isSaveLastUsed()) setLastShareComponentName(component );
488 makeIntentAndShare(false, activity, title, text, url, offlineUri , screenshotUri, 467 makeIntentAndShare(params, component);
489 component, null);
490 dialog.dismiss(); 468 dialog.dismiss();
491 } 469 }
492 }); 470 });
493 471
494 if (callback != null) { 472 if (callback != null) {
495 dialog.setOnDismissListener(new OnDismissListener() { 473 dialog.setOnDismissListener(new OnDismissListener() {
496 @Override 474 @Override
497 public void onDismiss(DialogInterface dialog) { 475 public void onDismiss(DialogInterface dialog) {
498 if (!callbackCalled[0]) { 476 if (!callbackCalled[0]) {
499 callback.onCancel(); 477 callback.onCancel();
500 callbackCalled[0] = true; 478 callbackCalled[0] = true;
501 } 479 }
502 } 480 }
503 }); 481 });
504 } 482 }
505 483
506 if (sFakeIntentReceiverForTesting != null) { 484 if (sFakeIntentReceiverForTesting != null) {
507 sFakeIntentReceiverForTesting.onCustomChooserShown(dialog); 485 sFakeIntentReceiverForTesting.onCustomChooserShown(dialog);
508 } 486 }
509 } 487 }
510 488
511 /** 489 /**
512 * Starts a share intent with the activity that was most recently used to sh are. 490 * Starts a share intent with the activity that was most recently used to sh are.
513 * If there is no most recently used activity, it does nothing. 491 * If there is no most recently used activity, it does nothing.
514 * @param activity Activity that is used to start the share intent. 492 * @param params The container holding the share parameters.
515 * @param title Title of the page to be shared.
516 * @param text Text to be shared. If both |text| and |url| are supplied, the y are concatenated
517 * with a space.
518 * @param url URL of the page to be shared.
519 * @oaram offlineUri URI of the offline page to be shared.
520 * @param screenshotUri Uri of the screenshot of the page to be shared.
521 */ 493 */
522 private static void shareWithLastUsed(Activity activity, String title, Strin g text, String url, 494 private static void shareWithLastUsed(ShareParams params) {
Ted C 2017/06/01 20:39:03 I think this is only used in share, let's just rem
ltian 2017/06/01 23:27:09 Done.
523 Uri offlineUri, Uri screenshotUri) {
524 ComponentName component = getLastShareComponentName(); 495 ComponentName component = getLastShareComponentName();
525 if (component == null) return; 496 if (component == null) return;
526 makeIntentAndShare( 497 makeIntentAndShare(params, component);
527 false, activity, title, text, url, offlineUri, screenshotUri, co mponent, null);
528 } 498 }
529 499
530 private static void shareIntent(boolean saveLastUsed, Activity activity, Int ent sharingIntent, 500 private static void shareIntent(ShareParams params, Intent sharingIntent) {
Ted C 2017/06/01 20:39:03 Is this only used from within makeIntentAndShare?
ltian 2017/06/01 23:27:08 Done.
531 @Nullable TargetChosenCallback callback) {
532 if (sharingIntent.getComponent() != null) { 501 if (sharingIntent.getComponent() != null) {
533 // If a component was specified, there should not also be a callback . 502 // If a component was specified, there should not also be a callback .
534 assert callback == null; 503 assert params.getCallback() == null;
535 fireIntent(activity, sharingIntent); 504 fireIntent(params.getActivity(), sharingIntent);
536 } else { 505 } else {
537 assert TargetChosenReceiver.isSupported(); 506 assert TargetChosenReceiver.isSupported();
538 TargetChosenReceiver.sendChooserIntent(saveLastUsed, activity, shari ngIntent, callback); 507 TargetChosenReceiver.sendChooserIntent(params.isSaveLastUsed(), para ms.getActivity(),
508 sharingIntent, params.getCallback());
Yusuf 2017/06/01 20:41:45 why not also pass params here?
ltian 2017/06/01 23:27:07 The only reason is because in ShareImage the onPos
539 } 509 }
540 } 510 }
541 511
542 private static void makeIntentAndShare(final boolean saveLastUsed, final Act ivity activity, 512 private static void makeIntentAndShare(ShareParams params, ComponentName com ponent) {
Ted C 2017/06/01 20:39:03 let's mark ComponentName with @Nullable
Yusuf 2017/06/01 20:41:45 why is componentName not a part of the shareParam?
ltian 2017/06/01 23:27:07 Done.
ltian 2017/06/01 23:27:11 One reason is in showShareDialog's onItemClick, it
Yusuf 2017/06/02 20:07:46 Acknowledged.
543 final String title, final String text, final String url, final Uri o fflineUri, 513 Intent intent = getDirectShareIntentForComponent(params, component);
544 final Uri screenshotUri, final ComponentName component, 514 shareIntent(params, intent);
545 @Nullable final TargetChosenCallback callback) {
546 Intent intent = getDirectShareIntentForComponent(
547 activity, title, text, url, offlineUri, screenshotUri, component );
548 shareIntent(saveLastUsed, activity, intent, callback);
549 } 515 }
550 516
551 /** 517 /**
552 * Set the icon and the title for the menu item used for direct share. 518 * Set the icon and the title for the menu item used for direct share.
553 * 519 *
554 * @param activity Activity that is used to access the package manager. 520 * @param activity Activity that is used to access the package manager.
555 * @param item The menu item that is used for direct share 521 * @param item The menu item that is used for direct share
556 */ 522 */
557 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) { 523 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) {
558 Intent shareIntent = getShareIntent(activity, "", "", "", null, null); 524 ShareParams params = new ShareParams.Builder(activity, "", "").setText(" ").build();
525 Intent shareIntent = getShareIntent(params);
559 Pair<Drawable, CharSequence> directShare = getShareableIconAndName(activ ity, shareIntent); 526 Pair<Drawable, CharSequence> directShare = getShareableIconAndName(activ ity, shareIntent);
560 Drawable directShareIcon = directShare.first; 527 Drawable directShareIcon = directShare.first;
561 CharSequence directShareTitle = directShare.second; 528 CharSequence directShareTitle = directShare.second;
562 529
563 item.setIcon(directShareIcon); 530 item.setIcon(directShareIcon);
564 if (directShareTitle != null) { 531 if (directShareTitle != null) {
565 item.setTitle( 532 item.setTitle(
566 activity.getString(R.string.accessibility_menu_share_via, di rectShareTitle)); 533 activity.getString(R.string.accessibility_menu_share_via, di rectShareTitle));
567 } 534 }
568 } 535 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 @VisibleForTesting 609 @VisibleForTesting
643 public static void setLastShareComponentName(ComponentName component) { 610 public static void setLastShareComponentName(ComponentName component) {
644 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 611 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
645 SharedPreferences.Editor editor = preferences.edit(); 612 SharedPreferences.Editor editor = preferences.edit();
646 editor.putString(PACKAGE_NAME_KEY, component.getPackageName()); 613 editor.putString(PACKAGE_NAME_KEY, component.getPackageName());
647 editor.putString(CLASS_NAME_KEY, component.getClassName()); 614 editor.putString(CLASS_NAME_KEY, component.getClassName());
648 editor.apply(); 615 editor.apply();
649 } 616 }
650 617
651 @VisibleForTesting 618 @VisibleForTesting
652 public static Intent getShareIntent(Activity activity, String title, String text, String url, 619 public static Intent getShareIntent(ShareParams params) {
Ted C 2017/06/01 20:39:03 let's rename this getShareLinkIntent to differenti
ltian 2017/06/01 23:27:10 The problem I meet to combine these two is the Int
653 Uri offlineUri, Uri screenshotUri) { 620 String url = params.getUrl();
621 String text = params.getText();
654 if (!TextUtils.isEmpty(url)) { 622 if (!TextUtils.isEmpty(url)) {
655 url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url); 623 url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
656 if (!TextUtils.isEmpty(text)) { 624 if (!TextUtils.isEmpty(text)) {
657 // Concatenate text and URL with a space. 625 // Concatenate text and URL with a space.
658 text = text + " " + url; 626 text = text + " " + url;
659 } else { 627 } else {
660 text = url; 628 text = url;
661 } 629 }
662 } 630 }
663 631
664 Intent intent = new Intent(Intent.ACTION_SEND); 632 Intent intent = new Intent(Intent.ACTION_SEND);
665 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); 633 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
666 intent.putExtra(Intent.EXTRA_SUBJECT, title); 634 intent.putExtra(Intent.EXTRA_SUBJECT, params.getTitle());
667 intent.putExtra(Intent.EXTRA_TEXT, text); 635 intent.putExtra(Intent.EXTRA_TEXT, text);
668 intent.putExtra(EXTRA_TASK_ID, activity.getTaskId()); 636 intent.putExtra(EXTRA_TASK_ID, params.getActivity().getTaskId());
669 637
638 Uri screenshotUri = params.getScreenshotUri();
670 if (screenshotUri != null) { 639 if (screenshotUri != null) {
Ted C 2017/06/01 20:39:03 tangential cleanup, but it seems like we should be
ltian 2017/06/01 23:27:07 Done.
671 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 640 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
672 } 641 }
673 if (screenshotUri != null) { 642 if (screenshotUri != null) {
674 // To give read access to an Intent target, we need to put |screensh otUri| in clipData 643 // To give read access to an Intent target, we need to put |screensh otUri| in clipData
675 // because adding Intent.FLAG_GRANT_READ_URI_PERMISSION doesn't work for 644 // because adding Intent.FLAG_GRANT_READ_URI_PERMISSION doesn't work for
676 // EXTRA_SHARE_SCREENSHOT_AS_STREAM. 645 // EXTRA_SHARE_SCREENSHOT_AS_STREAM.
677 intent.setClipData(ClipData.newRawUri("", screenshotUri)); 646 intent.setClipData(ClipData.newRawUri("", screenshotUri));
678 intent.putExtra(EXTRA_SHARE_SCREENSHOT_AS_STREAM, screenshotUri); 647 intent.putExtra(EXTRA_SHARE_SCREENSHOT_AS_STREAM, screenshotUri);
679 } 648 }
680 if (offlineUri == null) { 649 if (params.getOfflineUri() == null) {
681 intent.setType("text/plain"); 650 intent.setType("text/plain");
682 } else { 651 } else {
683 intent.setType("multipart/related"); 652 intent.setType("multipart/related");
684 intent.putExtra(Intent.EXTRA_STREAM, offlineUri); 653 intent.putExtra(Intent.EXTRA_STREAM, params.getOfflineUri());
685 } 654 }
686 return intent; 655 return intent;
687 } 656 }
688 657
689 /** 658 /**
690 * Creates an Intent to share an image. 659 * Creates an Intent to share an image.
691 * @param imageUri The Uri of the image. 660 * @param imageUri The Uri of the image.
692 * @return The Intent used to share the image. 661 * @return The Intent used to share the image.
693 */ 662 */
694 public static Intent getShareImageIntent(Uri imageUri) { 663 public static Intent getShareImageIntent(Uri imageUri) {
695 Intent intent = new Intent(Intent.ACTION_SEND); 664 Intent intent = new Intent(Intent.ACTION_SEND);
696 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); 665 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
697 intent.setType("image/jpeg"); 666 intent.setType("image/jpeg");
698 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 667 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
699 intent.putExtra(Intent.EXTRA_STREAM, imageUri); 668 intent.putExtra(Intent.EXTRA_STREAM, imageUri);
700 return intent; 669 return intent;
701 } 670 }
702 671
703 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title, 672 private static Intent getDirectShareIntentForComponent(
Ted C 2017/06/01 20:39:03 tangential cleanup, this method is used for all sh
ltian 2017/06/01 23:27:13 Done.
704 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) { 673 ShareParams params, ComponentName component) {
705 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri); 674 Intent intent = getShareIntent(params);
706 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 675 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
707 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 676 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
708 intent.setComponent(component); 677 intent.setComponent(component);
709 return intent; 678 return intent;
710 } 679 }
711 680
712 /** 681 /**
713 * Gets the {@link ComponentName} of the app that was used to last share. 682 * Gets the {@link ComponentName} of the app that was used to last share.
714 */ 683 */
715 @Nullable 684 @Nullable
716 public static ComponentName getLastShareComponentName() { 685 public static ComponentName getLastShareComponentName() {
717 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 686 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
718 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); 687 String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
719 String className = preferences.getString(CLASS_NAME_KEY, null); 688 String className = preferences.getString(CLASS_NAME_KEY, null);
720 if (packageName == null || className == null) return null; 689 if (packageName == null || className == null) return null;
721 return new ComponentName(packageName, className); 690 return new ComponentName(packageName, className);
722 } 691 }
723 } 692 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698