From b626c5fb737d7dfdd6f88fabeb95ab3afd4800e4 Mon Sep 17 00:00:00 2001 From: Angelo Suzuki <1063155+tinsukE@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:42:38 +0200 Subject: [PATCH] Use an OkHttpClient with User-Agent when initializing BigImageViewer. If not passed, BigImageViewer overrides the Glide OkHttp downloader upon initialization with an empty one: https://github.com/Piasy/BigImageViewer/blob/067f8eb/GlideImageLoader/src/main/java/com/github/piasy/biv/loader/glide/GlideProgressSupport.java#L76 In that case, Glide creates a default User-Agent: https://github.com/bumptech/glide/blob/cbdc1e78b4ee0f335054bb2781f46cf8db82f914/library/src/main/java/com/bumptech/glide/load/model/LazyHeaders.java#L103-L106 Example User-Agent: Dalvik/2.1.0 (Linux; U; Android 11; sdk_gphone_x86 Build/RSR1.201013.001) https://files.catbox.moe is filtering out that User-Agent. Using the App one fixes it. --- .../eu/toldi/infinityforlemmy/NetworkModule.java | 15 ++++++++++++++- .../activities/ViewImageOrGifActivity.java | 6 +++++- .../activities/ViewPostDetailActivity.java | 6 +++++- .../ViewRedditGalleryImageOrGifFragment.java | 7 ++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java b/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java index c197f718..b4641ee8 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java @@ -10,9 +10,9 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; import eu.toldi.infinityforlemmy.apis.StreamableAPI; -import eu.toldi.infinityforlemmy.privatemessage.LemmyPrivateMessageAPI; import eu.toldi.infinityforlemmy.comment.LemmyCommentAPI; import eu.toldi.infinityforlemmy.post.LemmyPostAPI; +import eu.toldi.infinityforlemmy.privatemessage.LemmyPrivateMessageAPI; import eu.toldi.infinityforlemmy.utils.APIUtils; import okhttp3.ConnectionPool; import okhttp3.Interceptor; @@ -34,6 +34,19 @@ abstract class NetworkModule { .build(); } + @Provides + @Named("glide") + @Singleton + static OkHttpClient provideGlideOkHttp(@Named("base") OkHttpClient baseOkHttp) { + return baseOkHttp.newBuilder() + .addInterceptor(chain -> chain.proceed( + chain.request() + .newBuilder() + .header("User-Agent", APIUtils.USER_AGENT) + .build() + )) + .build(); + } @Provides @Named("base") diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewImageOrGifActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewImageOrGifActivity.java index 4185e4e5..c63a7d48 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewImageOrGifActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewImageOrGifActivity.java @@ -78,6 +78,7 @@ import eu.toldi.infinityforlemmy.font.TitleFontStyle; import eu.toldi.infinityforlemmy.services.DownloadMediaService; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.Utils; +import okhttp3.OkHttpClient; public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback, CustomFontReceiver { @@ -108,6 +109,9 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("glide") + OkHttpClient okHttpClient; + @Inject Executor mExecutor; private boolean isActionBarHidden = false; private boolean isDownloading = false; @@ -146,7 +150,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa getTheme().applyStyle(ContentFontFamily.valueOf(mSharedPreferences .getString(SharedPreferencesUtils.CONTENT_FONT_FAMILY_KEY, ContentFontFamily.Default.name())).getResId(), true); - BigImageViewer.initialize(GlideImageLoader.with(this.getApplicationContext())); + BigImageViewer.initialize(GlideImageLoader.with(this.getApplicationContext(), okHttpClient)); setContentView(R.layout.activity_view_image_or_gif); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java index 2a85143e..f46a5e6a 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java @@ -85,6 +85,7 @@ import eu.toldi.infinityforlemmy.postfilter.PostFilter; import eu.toldi.infinityforlemmy.readpost.ReadPost; import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; +import okhttp3.OkHttpClient; import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; @@ -147,6 +148,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + @Inject + @Named("glide") + OkHttpClient okHttpClient; @State ArrayList posts; @State @@ -195,7 +199,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele super.onCreate(savedInstanceState); - BigImageViewer.initialize(GlideImageLoader.with(this.getApplicationContext())); + BigImageViewer.initialize(GlideImageLoader.with(this.getApplicationContext(), okHttpClient)); setContentView(R.layout.activity_view_post_detail); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewRedditGalleryImageOrGifFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewRedditGalleryImageOrGifFragment.java index 7407566d..e2b372a4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewRedditGalleryImageOrGifFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewRedditGalleryImageOrGifFragment.java @@ -52,6 +52,7 @@ import java.io.File; import java.util.concurrent.Executor; import javax.inject.Inject; +import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; @@ -69,6 +70,7 @@ import eu.toldi.infinityforlemmy.bottomsheetfragments.UrlMenuBottomSheetFragment import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.services.DownloadMediaService; import eu.toldi.infinityforlemmy.utils.Utils; +import okhttp3.OkHttpClient; public class ViewRedditGalleryImageOrGifFragment extends Fragment { @@ -105,6 +107,9 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { ImageView wallpaperImageView; @Inject Executor mExecutor; + @Inject + @Named("glide") + OkHttpClient okHttpClient; private ViewRedditGalleryActivity activity; private RequestManager glide; @@ -122,7 +127,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - BigImageViewer.initialize(GlideImageLoader.with(activity.getApplicationContext())); + BigImageViewer.initialize(GlideImageLoader.with(activity.getApplicationContext(), okHttpClient)); View rootView = inflater.inflate(R.layout.fragment_view_reddit_gallery_image_or_gif, container, false);