Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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. | |
|
Theresa
2017/03/30 15:12:45
nit: ...will share the image directly with the app
JJ
2017/03/30 16:20:17
Whoops. You just got a glimpse into what my javado
| |
| 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 Loading... | |
| 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 Loading... | |
| 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 Drawable directShareIcon; | |
| 552 CharSequence directShareTitle; | |
|
Theresa
2017/03/30 15:12:45
nit: these can both defined when they are set belo
JJ
2017/03/30 16:20:17
Done.
| |
| 553 | |
| 554 Pair<Drawable, CharSequence> directShare = getShareableIconAndName(activ ity); | |
| 555 directShareIcon = directShare.first; | |
| 556 directShareTitle = directShare.second; | |
| 557 | |
| 558 item.setIcon(directShareIcon); | |
| 559 if (directShareTitle != null) { | |
| 560 item.setTitle( | |
| 561 activity.getString(R.string.accessibility_menu_share_via, di rectShareTitle)); | |
| 562 } | |
| 563 } | |
| 564 | |
| 565 /** | |
| 566 * Get the icon and name of the most recently shared app within chrome. | |
| 567 * @param activity Activity that is used to access the package manager. | |
| 568 * @return The Image and the String of the recently shared Icon. | |
| 569 */ | |
| 570 public static Pair<Drawable, CharSequence> getShareableIconAndName(Activity activity) { | |
| 542 Drawable directShareIcon = null; | 571 Drawable directShareIcon = null; |
| 543 CharSequence directShareTitle = null; | 572 CharSequence directShareTitle = null; |
| 544 | 573 |
| 545 final ComponentName component = getLastShareComponentName(); | 574 final ComponentName component = getLastShareComponentName(); |
| 546 boolean isComponentValid = false; | 575 boolean isComponentValid = false; |
| 547 if (component != null) { | 576 if (component != null) { |
| 548 Intent intent = getShareIntent(activity, "", "", "", null, null); | 577 Intent intent = getShareIntent(activity, "", "", "", null, null); |
| 549 intent.setPackage(component.getPackageName()); | 578 intent.setPackage(component.getPackageName()); |
| 550 PackageManager manager = activity.getPackageManager(); | 579 PackageManager manager = activity.getPackageManager(); |
| 551 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0); | 580 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(in tent, 0); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 589 // Use the default null values. | 618 // Use the default null values. |
| 590 } catch (ExecutionException ee) { | 619 } catch (ExecutionException ee) { |
| 591 // Use the default null values. | 620 // Use the default null values. |
| 592 } catch (TimeoutException te) { | 621 } catch (TimeoutException te) { |
| 593 // Use the default null values. | 622 // Use the default null values. |
| 594 } | 623 } |
| 595 RecordHistogram.recordBooleanHistogram( | 624 RecordHistogram.recordBooleanHistogram( |
| 596 "Android.IsLastSharedAppInfoRetrieved", retrieved); | 625 "Android.IsLastSharedAppInfoRetrieved", retrieved); |
| 597 } | 626 } |
| 598 | 627 |
| 599 item.setIcon(directShareIcon); | 628 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 } | 629 } |
| 605 | 630 |
| 606 /* | 631 /* |
| 607 * Stores the component selected for sharing last time share was called. | 632 * Stores the component selected for sharing last time share was called. |
| 608 * | 633 * |
| 609 * This method is public since it is used in tests to avoid creating share d ialog. | 634 * This method is public since it is used in tests to avoid creating share d ialog. |
| 610 */ | 635 */ |
| 611 @VisibleForTesting | 636 @VisibleForTesting |
| 612 public static void setLastShareComponentName(ComponentName component) { | 637 public static void setLastShareComponentName(ComponentName component) { |
| 613 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 638 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 666 | 691 |
| 667 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title, | 692 private static Intent getDirectShareIntentForComponent(Activity activity, St ring title, |
| 668 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) { | 693 String text, String url, Uri offlineUri, Uri screenshotUri, Componen tName component) { |
| 669 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri); | 694 Intent intent = getShareIntent(activity, title, text, url, offlineUri, s creenshotUri); |
| 670 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | 695 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 671 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); | 696 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
| 672 intent.setComponent(component); | 697 intent.setComponent(component); |
| 673 return intent; | 698 return intent; |
| 674 } | 699 } |
| 675 | 700 |
| 676 private static ComponentName getLastShareComponentName() { | 701 public static ComponentName getLastShareComponentName() { |
| 677 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 702 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 678 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); | 703 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); |
| 679 String className = preferences.getString(CLASS_NAME_KEY, null); | 704 String className = preferences.getString(CLASS_NAME_KEY, null); |
| 680 if (packageName == null || className == null) return null; | 705 if (packageName == null || className == null) return null; |
| 681 return new ComponentName(packageName, className); | 706 return new ComponentName(packageName, className); |
| 682 } | 707 } |
| 683 } | 708 } |
| OLD | NEW |