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

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: Created 3 years, 9 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 whatever the
318 * component name is.
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 26 matching lines...) Expand all
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(activity, 365 Uri imageUri = ApiCompatibilityUtils.getUriForImageCaptureFi le(activity,
363 saveFile); 366 saveFile);
364 367
365 Intent chooserIntent = Intent.createChooser(getShareImageInt ent(imageUri), 368 if (name == null) {
366 activity.getString(R.string.share_link_chooser_title )); 369 Intent chooserIntent = Intent.createChooser(getShareImag eIntent(imageUri),
367 fireIntent(activity, chooserIntent); 370 activity.getString(R.string.share_link_chooser_t itle));
371 fireIntent(activity, chooserIntent);
372 } else {
373 Intent imageIntent = getShareImageIntent(imageUri);
374 imageIntent.setComponent(name);
375 fireIntent(activity, imageIntent);
376 }
368 } 377 }
369 } 378 }
370 }.execute(); 379 }.execute();
371 } 380 }
372 381
373 /** 382 /**
374 * Persists the screenshot file and notifies the file provider that the file is ready to be 383 * Persists the screenshot file and notifies the file provider that the file is ready to be
375 * accessed by the client. 384 * accessed by the client.
376 * 385 *
377 * The bitmap is compressed to JPEG before being written to the file. 386 * The bitmap is compressed to JPEG before being written to the file.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 shareIntent(saveLastUsed, activity, intent, callback); 542 shareIntent(saveLastUsed, activity, intent, callback);
534 } 543 }
535 544
536 /** 545 /**
537 * Set the icon and the title for the menu item used for direct share. 546 * Set the icon and the title for the menu item used for direct share.
538 * 547 *
539 * @param activity Activity that is used to access the package manager. 548 * @param activity Activity that is used to access the package manager.
540 * @param item The menu item that is used for direct share 549 * @param item The menu item that is used for direct share
541 */ 550 */
542 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) { 551 public static void configureDirectShareMenuItem(Activity activity, MenuItem item) {
552 Drawable directShareIcon;
553 CharSequence directShareTitle;
554
555 Pair<Drawable, CharSequence> directShare = getShareableIconAndName(activ ity);
556 directShareIcon = directShare.first;
557 directShareTitle = directShare.second;
558
559 item.setIcon(directShareIcon);
560 if (directShareTitle != null) {
561 item.setTitle(
562 activity.getString(R.string.accessibility_menu_share_via, di rectShareTitle));
563 }
564 }
565
566 /**
567 * Get the icon and name of the most recently shared app within chrome.
568 * @param activity Activity that is used to access the package manager.
569 * @return The Image and the String of the recently shared Icon.
570 */
571 public static Pair<Drawable, CharSequence> getShareableIconAndName(Activity activity) {
543 Drawable directShareIcon = null; 572 Drawable directShareIcon = null;
544 CharSequence directShareTitle = null; 573 CharSequence directShareTitle = null;
545 574
546 final ComponentName component = getLastShareComponentName(); 575 final ComponentName component = getLastShareComponentName();
547 boolean isComponentValid = false; 576 boolean isComponentValid = false;
548 if (component != null) { 577 if (component != null) {
549 Intent intent = getShareIntent(activity, "", "", "", null, null); 578 Intent intent = getShareIntent(activity, "", "", "", null, null);
550 intent.setPackage(component.getPackageName()); 579 intent.setPackage(component.getPackageName());
551 PackageManager manager = activity.getPackageManager(); 580 PackageManager manager = activity.getPackageManager();
552 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0); 581 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 // Use the default null values. 619 // Use the default null values.
591 } catch (ExecutionException ee) { 620 } catch (ExecutionException ee) {
592 // Use the default null values. 621 // Use the default null values.
593 } catch (TimeoutException te) { 622 } catch (TimeoutException te) {
594 // Use the default null values. 623 // Use the default null values.
595 } 624 }
596 RecordHistogram.recordBooleanHistogram( 625 RecordHistogram.recordBooleanHistogram(
597 "Android.IsLastSharedAppInfoRetrieved", retrieved); 626 "Android.IsLastSharedAppInfoRetrieved", retrieved);
598 } 627 }
599 628
600 item.setIcon(directShareIcon); 629 return new Pair<>(directShareIcon, directShareTitle);
601 if (directShareTitle != null) {
602 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia,
603 directShareTitle));
604 }
605 } 630 }
606 631
607 /* 632 /*
608 * Stores the component selected for sharing last time share was called. 633 * Stores the component selected for sharing last time share was called.
609 * 634 *
610 * This method is public since it is used in tests to avoid creating share d ialog. 635 * This method is public since it is used in tests to avoid creating share d ialog.
611 */ 636 */
612 @VisibleForTesting 637 @VisibleForTesting
613 public static void setLastShareComponentName(ComponentName component) { 638 public static void setLastShareComponentName(ComponentName component) {
614 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 639 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 692
668 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title, 693 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title,
669 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) { 694 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) {
670 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri); 695 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri);
671 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 696 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
672 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 697 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
673 intent.setComponent(component); 698 intent.setComponent(component);
674 return intent; 699 return intent;
675 } 700 }
676 701
677 private static ComponentName getLastShareComponentName() { 702 public static ComponentName getLastShareComponentName() {
678 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); 703 SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
679 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); 704 String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
680 String className = preferences.getString(CLASS_NAME_KEY, null); 705 String className = preferences.getString(CLASS_NAME_KEY, null);
681 if (packageName == null || className == null) return null; 706 if (packageName == null || className == null) return null;
682 return new ComponentName(packageName, className); 707 return new ComponentName(packageName, className);
683 } 708 }
684 } 709 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698