mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-31 15:23:00 +00:00 
			
		
		
		
	Improvements to sharing content with thumbnail
This commit is contained in:
		| @@ -7,6 +7,8 @@ import android.content.Context; | |||||||
| import android.graphics.Bitmap; | import android.graphics.Bitmap; | ||||||
| import android.graphics.drawable.Drawable; | import android.graphics.drawable.Drawable; | ||||||
|  |  | ||||||
|  | import androidx.annotation.Nullable; | ||||||
|  |  | ||||||
| import com.squareup.picasso.Cache; | import com.squareup.picasso.Cache; | ||||||
| import com.squareup.picasso.LruCache; | import com.squareup.picasso.LruCache; | ||||||
| import com.squareup.picasso.OkHttp3Downloader; | import com.squareup.picasso.OkHttp3Downloader; | ||||||
| @@ -24,9 +26,6 @@ import java.util.function.Consumer; | |||||||
|  |  | ||||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||||
|  |  | ||||||
| import androidx.annotation.NonNull; |  | ||||||
| import androidx.annotation.Nullable; |  | ||||||
|  |  | ||||||
| public final class PicassoHelper { | public final class PicassoHelper { | ||||||
|     public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG"; |     public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG"; | ||||||
|     private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY |     private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY | ||||||
| @@ -162,8 +161,9 @@ public final class PicassoHelper { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Nullable |     @Nullable | ||||||
|     public static Bitmap getImageFromCacheIfPresent(@NonNull final String imageUrl) { |     public static Bitmap getImageFromCacheIfPresent(final String imageUrl) { | ||||||
|         return picassoCache.get(imageUrl); |         // URLs in the internal cache finish with \n so we need to add \n to image URLs | ||||||
|  |         return picassoCache.get(imageUrl + "\n"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void loadNotificationIcon(final String url, |     public static void loadNotificationIcon(final String url, | ||||||
|   | |||||||
| @@ -244,9 +244,11 @@ public final class ShareUtils { | |||||||
|     /** |     /** | ||||||
|      * Open the android share sheet to share a content. |      * Open the android share sheet to share a content. | ||||||
|      * |      * | ||||||
|  |      * <p> | ||||||
|      * For Android 10+ users, a content preview is shown, which includes the title of the shared |      * For Android 10+ users, a content preview is shown, which includes the title of the shared | ||||||
|      * content. |      * content and an image preview the content, if its URL is not null or empty and its | ||||||
|      * Support sharing the image of the content needs to done, if possible. |      * corresponding image is in the image cache. | ||||||
|  |      * </p> | ||||||
|      * |      * | ||||||
|      * @param context         the context to use |      * @param context         the context to use | ||||||
|      * @param title           the title of the content |      * @param title           the title of the content | ||||||
| @@ -272,9 +274,13 @@ public final class ShareUtils { | |||||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q |         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q | ||||||
|                 && !TextUtils.isEmpty(imagePreviewUrl) |                 && !TextUtils.isEmpty(imagePreviewUrl) | ||||||
|                 && PicassoHelper.getShouldLoadImages()) { |                 && PicassoHelper.getShouldLoadImages()) { | ||||||
|             shareIntent.setClipData(generateClipDataForImagePreview(context, imagePreviewUrl)); |  | ||||||
|  |             final ClipData clipData = generateClipDataForImagePreview(context, imagePreviewUrl); | ||||||
|  |             if (clipData != null) { | ||||||
|  |                 shareIntent.setClipData(clipData); | ||||||
|                 shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |                 shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         openAppChooser(context, shareIntent, false); |         openAppChooser(context, shareIntent, false); | ||||||
|     } |     } | ||||||
| @@ -283,14 +289,9 @@ public final class ShareUtils { | |||||||
|      * Open the android share sheet to share a content. |      * Open the android share sheet to share a content. | ||||||
|      * |      * | ||||||
|      * <p> |      * <p> | ||||||
|      * For Android 10+ users, a content preview is shown, which includes the title of the shared |  | ||||||
|      * content and an image preview the content, if its URL is not null or empty and its |  | ||||||
|      * corresponding image is in the image cache. |  | ||||||
|      * </p> |  | ||||||
|      * |  | ||||||
|      * <p> |  | ||||||
|      * This calls {@link #shareText(Context, String, String, String)} with an empty string for the |      * This calls {@link #shareText(Context, String, String, String)} with an empty string for the | ||||||
|      * {@code imagePreviewUrl} parameter. |      * {@code imagePreviewUrl} parameter. This method should be used when the shared content has no | ||||||
|  |      * preview thumbnail. | ||||||
|      * </p> |      * </p> | ||||||
|      * |      * | ||||||
|      * @param context the context to use |      * @param context the context to use | ||||||
| @@ -327,11 +328,11 @@ public final class ShareUtils { | |||||||
|      * Generate a {@link ClipData} with the image of the content shared, if it's in the app cache. |      * Generate a {@link ClipData} with the image of the content shared, if it's in the app cache. | ||||||
|      * |      * | ||||||
|      * <p> |      * <p> | ||||||
|      * In order to not manage network issues (timeouts, DNS issues, low connection speed, ...) when |      * In order not to worry about network issues (timeouts, DNS issues, low connection speed, ...) | ||||||
|      * sharing a content, only images in the {@link com.squareup.picasso.LruCache LruCache} used by |      * when sharing a content, only images in the {@link com.squareup.picasso.LruCache LruCache} | ||||||
|      * the Picasso library inside {@link PicassoHelper} are used as preview images. If the |      * used by the Picasso library inside {@link PicassoHelper} are used as preview images. If the | ||||||
|      * thumbnail image is not yet loaded, no {@link ClipData} will be generated and {@code null} |      * thumbnail image is not in the cache, no {@link ClipData} will be generated and {@code null} | ||||||
|      * will be returned in this case. |      * will be returned. | ||||||
|      * </p> |      * </p> | ||||||
|      * |      * | ||||||
|      * <p> |      * <p> | ||||||
| @@ -339,7 +340,7 @@ public final class ShareUtils { | |||||||
|      * the content, accessible and readable by other apps has to be generated, so a new file inside |      * the content, accessible and readable by other apps has to be generated, so a new file inside | ||||||
|      * the application cache will be generated, named {@code android_share_sheet_image_preview.jpg} |      * the application cache will be generated, named {@code android_share_sheet_image_preview.jpg} | ||||||
|      * (if a file under this name already exists, it will be overwritten). The thumbnail will be |      * (if a file under this name already exists, it will be overwritten). The thumbnail will be | ||||||
|      * compressed in JPEG format, with a {@code 100} compression level. |      * compressed in JPEG format, with a {@code 90} compression level. | ||||||
|      * </p> |      * </p> | ||||||
|      * |      * | ||||||
|      * <p> |      * <p> | ||||||
| @@ -354,8 +355,8 @@ public final class ShareUtils { | |||||||
|      * </p> |      * </p> | ||||||
|      * |      * | ||||||
|      * <p> |      * <p> | ||||||
|      * This method has only an effect on the system share sheet (if OEMs didn't change Android |      * Using the result of this method when sharing has only an effect on the system share sheet (if | ||||||
|      * system standard behavior) on Android API 29 and higher. |      * OEMs didn't change Android system standard behavior) on Android API 29 and higher. | ||||||
|      * </p> |      * </p> | ||||||
|      * |      * | ||||||
|      * @param context      the context to use |      * @param context      the context to use | ||||||
| @@ -367,9 +368,7 @@ public final class ShareUtils { | |||||||
|             @NonNull final Context context, |             @NonNull final Context context, | ||||||
|             @NonNull final String thumbnailUrl) { |             @NonNull final String thumbnailUrl) { | ||||||
|         try { |         try { | ||||||
|             // URLs in the internal cache finish with \n so we need to add \n to image URLs |             final Bitmap bitmap = PicassoHelper.getImageFromCacheIfPresent(thumbnailUrl); | ||||||
|             final Bitmap bitmap = PicassoHelper.getImageFromCacheIfPresent(thumbnailUrl + "\n"); |  | ||||||
|  |  | ||||||
|             if (bitmap == null) { |             if (bitmap == null) { | ||||||
|                 return null; |                 return null; | ||||||
|             } |             } | ||||||
| @@ -386,20 +385,19 @@ public final class ShareUtils { | |||||||
|             bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream); |             bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream); | ||||||
|             fileOutputStream.close(); |             fileOutputStream.close(); | ||||||
|  |  | ||||||
|             final ClipData clipData = ClipData.newUri(applicationContext.getContentResolver(), |             final ClipData clipData = ClipData.newUri(applicationContext.getContentResolver(), "", | ||||||
|                         "", |  | ||||||
|                         FileProvider.getUriForFile(applicationContext, |                         FileProvider.getUriForFile(applicationContext, | ||||||
|                                 BuildConfig.APPLICATION_ID + ".provider", |                                 BuildConfig.APPLICATION_ID + ".provider", | ||||||
|                                 thumbnailPreviewFile)); |                                 thumbnailPreviewFile)); | ||||||
|  |  | ||||||
|             if (DEBUG) { |             if (DEBUG) { | ||||||
|                 Log.d(TAG, "ClipData successfully generated for Android share sheet: " + clipData); |                 Log.d(TAG, "ClipData successfully generated for Android share sheet: " + clipData); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return clipData; |             return clipData; | ||||||
|  |  | ||||||
|         } catch (final Exception e) { |         } catch (final Exception e) { | ||||||
|             Log.w(TAG, "Error when setting preview image for share sheet", e); |             Log.w(TAG, "Error when setting preview image for share sheet", e); | ||||||
|         } |  | ||||||
|  |  | ||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stypox
					Stypox