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

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

Issue 2775373002: Add a Share Icon to Tabular Context Menu (Closed)
Patch Set: git rebase Created 3 years, 8 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 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } else { 307 } else {
308 showShareDialog( 308 showShareDialog(
309 saveLastUsed, activity, title, text, url, offlineUri, screen shotUri, callback); 309 saveLastUsed, activity, title, text, url, offlineUri, screen shotUri, callback);
310 } 310 }
311 } 311 }
312 312
313 /** 313 /**
314 * Trigger the share action for the given image data. 314 * Trigger the share action for the given image data.
315 * @param activity The activity used to trigger the share action. 315 * @param activity The activity used to trigger the share action.
316 * @param jpegImageData The image data to be shared in jpeg format. 316 * @param jpegImageData The image data to be shared in jpeg format.
317 * @param name When this is not null, it will share the image directly with the
318 * {@link ComponentName}
317 */ 319 */
318 public static void shareImage(final Activity activity, final byte[] jpegImag eData) { 320 public static void shareImage(
321 final Activity activity, final byte[] jpegImageData, final Component Name name) {
319 if (jpegImageData.length == 0) { 322 if (jpegImageData.length == 0) {
320 Log.w(TAG, "Share failed -- Received image contains no data."); 323 Log.w(TAG, "Share failed -- Received image contains no data.");
321 return; 324 return;
322 } 325 }
323 326
324 new AsyncTask<Void, Void, File>() { 327 new AsyncTask<Void, Void, File>() {
325 @Override 328 @Override
326 protected File doInBackground(Void... params) { 329 protected File doInBackground(Void... params) {
327 FileOutputStream fOut = null; 330 FileOutputStream fOut = null;
328 try { 331 try {
(...skipping 25 matching lines...) Expand all
354 } 357 }
355 358
356 @Override 359 @Override
357 protected void onPostExecute(File saveFile) { 360 protected void onPostExecute(File saveFile) {
358 if (saveFile == null) return; 361 if (saveFile == null) return;
359 362
360 if (ApplicationStatus.getStateForApplication() 363 if (ApplicationStatus.getStateForApplication()
361 != ApplicationState.HAS_DESTROYED_ACTIVITIES) { 364 != ApplicationState.HAS_DESTROYED_ACTIVITIES) {
362 Uri imageUri = ApiCompatibilityUtils.getUriForImageCaptureFi le(saveFile); 365 Uri imageUri = ApiCompatibilityUtils.getUriForImageCaptureFi le(saveFile);
363 366
364 Intent chooserIntent = Intent.createChooser(getShareImageInt ent(imageUri), 367 if (name == null) {
365 activity.getString(R.string.share_link_chooser_title )); 368 Intent chooserIntent = Intent.createChooser(getShareImag eIntent(imageUri),
366 fireIntent(activity, chooserIntent); 369 activity.getString(R.string.share_link_chooser_t itle));
370 fireIntent(activity, chooserIntent);
371 } else {
372 Intent imageIntent = getShareImageIntent(imageUri);
373 imageIntent.setComponent(name);
374 fireIntent(activity, imageIntent);
375 }
367 } 376 }
368 } 377 }
369 }.execute(); 378 }.execute();
370 } 379 }
371 380
372 /** 381 /**
373 * Persists the screenshot file and notifies the file provider that the file is ready to be 382 * Persists the screenshot file and notifies the file provider that the file is ready to be
374 * accessed by the client. 383 * accessed by the client.
375 * 384 *
376 * The bitmap is compressed to JPEG before being written to the file. 385 * The bitmap is compressed to JPEG before being written to the file.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 shareIntent(saveLastUsed, activity, intent, callback); 541 shareIntent(saveLastUsed, activity, intent, callback);
533 } 542 }
534 543
535 /** 544 /**
536 * Set the icon and the title for the menu item used for direct share. 545 * Set the icon and the title for the menu item used for direct share.
537 * 546 *
538 * @param activity Activity that is used to access the package manager. 547 * @param activity Activity that is used to access the package manager.
539 * @param item The menu item that is used for direct share 548 * @param item The menu item that is used for direct share
540 */ 549 */
541 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) { 550 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) {
551 Pair<Drawable, CharSequence> directShare = getShareableIconAndName(activ ity);
552 Drawable directShareIcon = directShare.first;
553 CharSequence directShareTitle = directShare.second;
554
555 item.setIcon(directShareIcon);
556 if (directShareTitle != null) {
557 item.setTitle(
558 activity.getString(R.string.accessibility_menu_share_via, di rectShareTitle));
559 }
560 }
561
562 /**
563 * Get the icon and name of the most recently shared app within chrome.
564 * @param activity Activity that is used to access the package manager.
565 * @return The Image and the String of the recently shared Icon.
566 */
567 public static Pair<Drawable, CharSequence> getShareableIconAndName(Activity activity) {
542 Drawable directShareIcon = null; 568 Drawable directShareIcon = null;
543 CharSequence directShareTitle = null; 569 CharSequence directShareTitle = null;
544 570
545 final ComponentName component = getLastShareComponentName(); 571 final ComponentName component = getLastShareComponentName();
546 boolean isComponentValid = false; 572 boolean isComponentValid = false;
547 if (component != null) { 573 if (component != null) {
548 Intent intent = getShareIntent(activity, "", "", "", null, null); 574 Intent intent = getShareIntent(activity, "", "", "", null, null);
549 intent.setPackage(component.getPackageName()); 575 intent.setPackage(component.getPackageName());
550 PackageManager manager = activity.getPackageManager(); 576 PackageManager manager = activity.getPackageManager();
551 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0); 577 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 // Use the default null values. 615 // Use the default null values.
590 } catch (ExecutionException ee) { 616 } catch (ExecutionException ee) {
591 // Use the default null values. 617 // Use the default null values.
592 } catch (TimeoutException te) { 618 } catch (TimeoutException te) {
593 // Use the default null values. 619 // Use the default null values.
594 } 620 }
595 RecordHistogram.recordBooleanHistogram( 621 RecordHistogram.recordBooleanHistogram(
596 "Android.IsLastSharedAppInfoRetrieved", retrieved); 622 "Android.IsLastSharedAppInfoRetrieved", retrieved);
597 } 623 }
598 624
599 item.setIcon(directShareIcon); 625 return new Pair<>(directShareIcon, directShareTitle);
600 if (directShareTitle != null) {
601 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia,
602 directShareTitle));
603 }
604 } 626 }
605 627
606 /* 628 /*
607 * Stores the component selected for sharing last time share was called. 629 * Stores the component selected for sharing last time share was called.
608 * 630 *
609 * This method is public since it is used in tests to avoid creating share d ialog. 631 * This method is public since it is used in tests to avoid creating share d ialog.
610 */ 632 */
611 @VisibleForTesting 633 @VisibleForTesting
612 public static void setLastShareComponentName(ComponentName component) { 634 public static void setLastShareComponentName(ComponentName component) {
613 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 635 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 688
667 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title, 689 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title,
668 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) { 690 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) {
669 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri); 691 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri);
670 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 692 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
671 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 693 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
672 intent.setComponent(component); 694 intent.setComponent(component);
673 return intent; 695 return intent;
674 } 696 }
675 697
676 private static ComponentName getLastShareComponentName() { 698 public static ComponentName getLastShareComponentName() {
David Trainor- moved to gerrit 2017/03/30 23:50:22 Javadoc?
JJ 2017/03/31 20:58:54 Done.
677 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 699 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
678 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); 700 String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
679 String className = preferences.getString(CLASS_NAME_KEY, null); 701 String className = preferences.getString(CLASS_NAME_KEY, null);
680 if (packageName == null || className == null) return null; 702 if (packageName == null || className == null) return null;
681 return new ComponentName(packageName, className); 703 return new ComponentName(packageName, className);
682 } 704 }
683 } 705 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698