From 77d177e7f92f25849cc000d0a20b00534aa3c82f Mon Sep 17 00:00:00 2001 From: Andrea Colombo Date: Thu, 14 Sep 2023 16:42:42 +0200 Subject: Add option to Add subreddit to Home Screen as shortcut (#1485) * Add shortcut to Add Subreddit to Home Screen * Add null check for subredditIcon --- .../activities/ViewSubredditDetailActivity.java | 27 ++++++++++++++-- .../subreddit/shortcut/ShortcutManager.java | 37 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/shortcut/ShortcutManager.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 5203c567..367edb37 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -1,8 +1,12 @@ package ml.docilealligator.infinityforreddit.activities; +import static android.graphics.BitmapFactory.decodeResource; + import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -37,6 +41,8 @@ import androidx.viewpager2.widget.ViewPager2; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.MaterialToolbar; @@ -121,6 +127,7 @@ import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditSubscription; import ml.docilealligator.infinityforreddit.subreddit.SubredditViewModel; +import ml.docilealligator.infinityforreddit.subreddit.shortcut.ShortcutManager; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -237,6 +244,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private int subscribedColor; private int fabOption; private MaterialAlertDialogBuilder nsfwWarningBuilder; + private Bitmap subredditIconBitmap; @Override protected void onCreate(Bundle savedInstanceState) { @@ -437,11 +445,23 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .into(iconGifImageView); iconGifImageView.setOnClickListener(null); } else { - glide.load(subredditData.getIconUrl()) + glide.asBitmap() + .load(subredditData.getIconUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(216, 0))) .error(glide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(216, 0)))) - .into(iconGifImageView); + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + subredditIconBitmap = resource; + iconGifImageView.setImageBitmap(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + subredditIconBitmap = null; + } + }); iconGifImageView.setOnClickListener(view -> { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getIconUrl()); @@ -1175,6 +1195,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, "r/" + subredditName); startActivity(intent); return true; + } else if (itemId == R.id.action_add_to_home_screen_view_subreddit_detail_activity) { + Bitmap icon = subredditIconBitmap == null ? decodeResource(getResources(), R.drawable.subreddit_default_icon) : subredditIconBitmap; + return ShortcutManager.requestPinShortcut(this, subredditName, icon); } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/shortcut/ShortcutManager.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/shortcut/ShortcutManager.java new file mode 100644 index 00000000..dd92253b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/shortcut/ShortcutManager.java @@ -0,0 +1,37 @@ +package ml.docilealligator.infinityforreddit.subreddit.shortcut; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; + +import androidx.annotation.NonNull; +import androidx.core.content.pm.ShortcutInfoCompat; +import androidx.core.content.pm.ShortcutManagerCompat; +import androidx.core.graphics.drawable.IconCompat; + +import ml.docilealligator.infinityforreddit.BuildConfig; +import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; + +public class ShortcutManager { + private static ShortcutInfoCompat getInfo(Context context, @NonNull String subreddit, @NonNull Bitmap icon) { + final Intent shortcut = new Intent(context, ViewSubredditDetailActivity.class); + shortcut.setPackage(context.getPackageName()); + shortcut.setAction(Intent.ACTION_MAIN); + shortcut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + shortcut.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subreddit); + + String shortcutId = BuildConfig.APPLICATION_ID + ".shortcut." + subreddit; + String subredditName = "r/" + subreddit; + return new ShortcutInfoCompat.Builder(context, shortcutId) + .setIntent(shortcut) + .setShortLabel(subredditName) + .setAlwaysBadged() + .setIcon(IconCompat.createWithBitmap(icon)) + .build(); + } + + public static boolean requestPinShortcut(Context context, @NonNull String subreddit, @NonNull Bitmap icon) { + return ShortcutManagerCompat.requestPinShortcut(context, getInfo(context, subreddit, icon), null); + } +} + -- cgit v1.2.3