Skip to content

Commit bd5eda9

Browse files
committed
Improvements to sharing content with thumbnail
1 parent 761c0ff commit bd5eda9

2 files changed

Lines changed: 30 additions & 32 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.graphics.Bitmap;
88
import android.graphics.drawable.Drawable;
99

10+
import androidx.annotation.Nullable;
11+
1012
import com.squareup.picasso.Cache;
1113
import com.squareup.picasso.LruCache;
1214
import com.squareup.picasso.OkHttp3Downloader;
@@ -24,9 +26,6 @@
2426

2527
import okhttp3.OkHttpClient;
2628

27-
import androidx.annotation.NonNull;
28-
import androidx.annotation.Nullable;
29-
3029
public final class PicassoHelper {
3130
public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG";
3231
private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY
@@ -162,8 +161,9 @@ public String key() {
162161
}
163162

164163
@Nullable
165-
public static Bitmap getImageFromCacheIfPresent(@NonNull final String imageUrl) {
166-
return picassoCache.get(imageUrl);
164+
public static Bitmap getImageFromCacheIfPresent(final String imageUrl) {
165+
// URLs in the internal cache finish with \n so we need to add \n to image URLs
166+
return picassoCache.get(imageUrl + "\n");
167167
}
168168

169169
public static void loadNotificationIcon(final String url,

app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,11 @@ private static String getDefaultAppPackageName(@NonNull final Context context,
244244
/**
245245
* Open the android share sheet to share a content.
246246
*
247+
* <p>
247248
* For Android 10+ users, a content preview is shown, which includes the title of the shared
248-
* content.
249-
* Support sharing the image of the content needs to done, if possible.
249+
* content and an image preview the content, if its URL is not null or empty and its
250+
* corresponding image is in the image cache.
251+
* </p>
250252
*
251253
* @param context the context to use
252254
* @param title the title of the content
@@ -272,8 +274,12 @@ public static void shareText(@NonNull final Context context,
272274
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
273275
&& !TextUtils.isEmpty(imagePreviewUrl)
274276
&& PicassoHelper.getShouldLoadImages()) {
275-
shareIntent.setClipData(generateClipDataForImagePreview(context, imagePreviewUrl));
276-
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
277+
278+
final ClipData clipData = generateClipDataForImagePreview(context, imagePreviewUrl);
279+
if (clipData != null) {
280+
shareIntent.setClipData(clipData);
281+
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
282+
}
277283
}
278284

279285
openAppChooser(context, shareIntent, false);
@@ -283,14 +289,9 @@ public static void shareText(@NonNull final Context context,
283289
* Open the android share sheet to share a content.
284290
*
285291
* <p>
286-
* For Android 10+ users, a content preview is shown, which includes the title of the shared
287-
* content and an image preview the content, if its URL is not null or empty and its
288-
* corresponding image is in the image cache.
289-
* </p>
290-
*
291-
* <p>
292292
* This calls {@link #shareText(Context, String, String, String)} with an empty string for the
293-
* {@code imagePreviewUrl} parameter.
293+
* {@code imagePreviewUrl} parameter. This method should be used when the shared content has no
294+
* preview thumbnail.
294295
* </p>
295296
*
296297
* @param context the context to use
@@ -327,19 +328,19 @@ public static void copyToClipboard(@NonNull final Context context, final String
327328
* Generate a {@link ClipData} with the image of the content shared, if it's in the app cache.
328329
*
329330
* <p>
330-
* In order to not manage network issues (timeouts, DNS issues, low connection speed, ...) when
331-
* sharing a content, only images in the {@link com.squareup.picasso.LruCache LruCache} used by
332-
* the Picasso library inside {@link PicassoHelper} are used as preview images. If the
333-
* thumbnail image is not yet loaded, no {@link ClipData} will be generated and {@code null}
334-
* will be returned in this case.
331+
* In order not to worry about network issues (timeouts, DNS issues, low connection speed, ...)
332+
* when sharing a content, only images in the {@link com.squareup.picasso.LruCache LruCache}
333+
* used by the Picasso library inside {@link PicassoHelper} are used as preview images. If the
334+
* thumbnail image is not in the cache, no {@link ClipData} will be generated and {@code null}
335+
* will be returned.
335336
* </p>
336337
*
337338
* <p>
338339
* In order to display the image in the content preview of the Android share sheet, an URI of
339340
* the content, accessible and readable by other apps has to be generated, so a new file inside
340341
* the application cache will be generated, named {@code android_share_sheet_image_preview.jpg}
341342
* (if a file under this name already exists, it will be overwritten). The thumbnail will be
342-
* compressed in JPEG format, with a {@code 100} compression level.
343+
* compressed in JPEG format, with a {@code 90} compression level.
343344
* </p>
344345
*
345346
* <p>
@@ -354,8 +355,8 @@ public static void copyToClipboard(@NonNull final Context context, final String
354355
* </p>
355356
*
356357
* <p>
357-
* This method has only an effect on the system share sheet (if OEMs didn't change Android
358-
* system standard behavior) on Android API 29 and higher.
358+
* Using the result of this method when sharing has only an effect on the system share sheet (if
359+
* OEMs didn't change Android system standard behavior) on Android API 29 and higher.
359360
* </p>
360361
*
361362
* @param context the context to use
@@ -367,9 +368,7 @@ private static ClipData generateClipDataForImagePreview(
367368
@NonNull final Context context,
368369
@NonNull final String thumbnailUrl) {
369370
try {
370-
// URLs in the internal cache finish with \n so we need to add \n to image URLs
371-
final Bitmap bitmap = PicassoHelper.getImageFromCacheIfPresent(thumbnailUrl + "\n");
372-
371+
final Bitmap bitmap = PicassoHelper.getImageFromCacheIfPresent(thumbnailUrl);
373372
if (bitmap == null) {
374373
return null;
375374
}
@@ -386,20 +385,19 @@ private static ClipData generateClipDataForImagePreview(
386385
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
387386
fileOutputStream.close();
388387

389-
final ClipData clipData = ClipData.newUri(applicationContext.getContentResolver(),
390-
"",
388+
final ClipData clipData = ClipData.newUri(applicationContext.getContentResolver(), "",
391389
FileProvider.getUriForFile(applicationContext,
392390
BuildConfig.APPLICATION_ID + ".provider",
393391
thumbnailPreviewFile));
392+
394393
if (DEBUG) {
395394
Log.d(TAG, "ClipData successfully generated for Android share sheet: " + clipData);
396395
}
397-
398396
return clipData;
397+
399398
} catch (final Exception e) {
400399
Log.w(TAG, "Error when setting preview image for share sheet", e);
400+
return null;
401401
}
402-
403-
return null;
404402
}
405403
}

0 commit comments

Comments
 (0)