aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorAndrea Colombo <acolombo25@users.noreply.github.com>2023-09-14 14:42:42 +0000
committerGitHub <noreply@github.com>2023-09-14 14:42:42 +0000
commit77d177e7f92f25849cc000d0a20b00534aa3c82f (patch)
treed0c1e8501ef51c9528bc28c7fa17041bde3b94b3 /app/src/main
parent6576205f90b21350571b8601125890b1aa5e6664 (diff)
downloadinfinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar.gz
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar.bz2
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar.lz
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar.xz
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.tar.zst
infinity-for-reddit-77d177e7f92f25849cc000d0a20b00534aa3c82f.zip
Add option to Add subreddit to Home Screen as shortcut (#1485)
* Add shortcut to Add Subreddit to Home Screen * Add null check for subredditIcon
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java27
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/shortcut/ShortcutManager.java37
-rw-r--r--app/src/main/res/menu/view_subreddit_detail_activity.xml16
-rw-r--r--app/src/main/res/values/strings.xml1
4 files changed, 74 insertions, 7 deletions
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<Bitmap>() {
+ @Override
+ public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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);
+ }
+}
+
diff --git a/app/src/main/res/menu/view_subreddit_detail_activity.xml b/app/src/main/res/menu/view_subreddit_detail_activity.xml
index e23d5191..877c6719 100644
--- a/app/src/main/res/menu/view_subreddit_detail_activity.xml
+++ b/app/src/main/res/menu/view_subreddit_detail_activity.xml
@@ -36,32 +36,38 @@
app:showAsAction="never" />
<item
- android:id="@+id/action_add_to_multireddit_view_subreddit_detail_activity"
+ android:id="@+id/action_add_to_home_screen_view_subreddit_detail_activity"
android:orderInCategory="6"
+ android:title="@string/action_add_to_home_screen"
+ app:showAsAction="never" />
+
+ <item
+ android:id="@+id/action_add_to_multireddit_view_subreddit_detail_activity"
+ android:orderInCategory="7"
android:title="@string/action_add_to_multireddit"
app:showAsAction="never" />
<item
android:id="@+id/action_add_to_post_filter_view_subreddit_detail_activity"
- android:orderInCategory="7"
+ android:orderInCategory="8"
android:title="@string/action_add_to_post_filter"
app:showAsAction="never" />
<item
android:id="@+id/action_share_view_subreddit_detail_activity"
- android:orderInCategory="8"
+ android:orderInCategory="9"
android:title="@string/action_share"
app:showAsAction="never" />
<item
android:id="@+id/action_go_to_wiki_view_subreddit_detail_activity"
- android:orderInCategory="9"
+ android:orderInCategory="10"
android:title="@string/action_go_to_wiki"
app:showAsAction="never" />
<item
android:id="@+id/action_contact_mods_view_subreddit_detail_activity"
- android:orderInCategory="10"
+ android:orderInCategory="11"
android:title="@string/action_contact_mods"
app:showAsAction="never" />
</menu>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1cf7ff8a..939b75c1 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -96,6 +96,7 @@
<string name="action_playback_speed">Playback Speed</string>
<string name="action_contact_mods">Contact Mods</string>
<string name="action_more_options">More Options</string>
+ <string name="action_add_to_home_screen">Add to Home screen</string>
<string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
<string name="retrieve_token_error">Error Retrieving the token</string>