384 lines
12 KiB
Java
384 lines
12 KiB
Java
package com.ttstd.dialer.utils;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.widget.ImageView;
|
||
|
||
import androidx.annotation.DrawableRes;
|
||
import androidx.annotation.NonNull;
|
||
import androidx.annotation.Nullable;
|
||
import androidx.fragment.app.Fragment;
|
||
import androidx.fragment.app.FragmentActivity;
|
||
|
||
import com.bumptech.glide.RequestBuilder;
|
||
import com.bumptech.glide.request.RequestListener;
|
||
import com.ttstd.dialer.glide.GlideApp;
|
||
|
||
/**
|
||
* Glide 图片加载工具类
|
||
* 包含 Activity/Fragment 生命周期检查和常用加载方法
|
||
*/
|
||
public class GlideUtils {
|
||
|
||
private static final String TAG = "GlideUtils";
|
||
|
||
/**
|
||
* 检查 Activity 是否有效(未销毁且未 finishing)
|
||
*
|
||
* @param activity Activity
|
||
* @return true: 有效, false: 已销毁或正在销毁
|
||
*/
|
||
public static boolean isActivityValid(@Nullable Activity activity) {
|
||
if (activity == null) {
|
||
Logger.w(TAG, "Activity is null");
|
||
return false;
|
||
}
|
||
if (activity.isFinishing()) {
|
||
Logger.w(TAG, "Activity is finishing: " + activity.getClass().getSimpleName());
|
||
return false;
|
||
}
|
||
if (activity.isDestroyed()) {
|
||
Logger.w(TAG, "Activity is destroyed: " + activity.getClass().getSimpleName());
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 检查 FragmentActivity 是否有效
|
||
*
|
||
* @param activity FragmentActivity
|
||
* @return true: 有效, false: 已销毁或正在销毁
|
||
*/
|
||
public static boolean isFragmentActivityValid(@Nullable FragmentActivity activity) {
|
||
if (activity == null) {
|
||
Logger.w(TAG, "FragmentActivity is null");
|
||
return false;
|
||
}
|
||
return isActivityValid(activity);
|
||
}
|
||
|
||
/**
|
||
* 检查 Fragment 是否有效
|
||
*
|
||
* @param fragment Fragment
|
||
* @return true: 有效, false: 未附加或 Activity 已销毁
|
||
*/
|
||
public static boolean isFragmentValid(@Nullable Fragment fragment) {
|
||
if (fragment == null) {
|
||
Logger.w(TAG, "Fragment is null");
|
||
return false;
|
||
}
|
||
if (!fragment.isAdded()) {
|
||
Logger.w(TAG, "Fragment is not added");
|
||
return false;
|
||
}
|
||
Activity activity = fragment.getActivity();
|
||
return isActivityValid(activity);
|
||
}
|
||
|
||
/**
|
||
* 安全地获取 Glide RequestManager
|
||
* 自动检查 Context 类型和生命周期状态
|
||
*
|
||
* @param context Context
|
||
* @return RequestManager,如果 Context 无效则返回 null
|
||
*/
|
||
@Nullable
|
||
public static RequestBuilder<Drawable> getSafeRequestManager(@Nullable Context context) {
|
||
if (context == null) {
|
||
Logger.w(TAG, "Context is null");
|
||
return null;
|
||
}
|
||
|
||
if (context instanceof FragmentActivity) {
|
||
FragmentActivity activity = (FragmentActivity) context;
|
||
if (!isFragmentActivityValid(activity)) {
|
||
return null;
|
||
}
|
||
return GlideApp.with(activity).asDrawable();
|
||
} else if (context instanceof Activity) {
|
||
Activity activity = (Activity) context;
|
||
if (!isActivityValid(activity)) {
|
||
return null;
|
||
}
|
||
return GlideApp.with(activity).asDrawable();
|
||
} else {
|
||
// Application Context,无需检查生命周期
|
||
return GlideApp.with(context).asDrawable();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安全地加载图片到 ImageView
|
||
* 自动检查 Activity 生命周期
|
||
*
|
||
* @param context Context
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
*/
|
||
public static void loadImageSafe(@Nullable Context context, @Nullable String url, @NonNull ImageView imageView) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.into(imageView);
|
||
}
|
||
|
||
/**
|
||
* 安全地加载图片到 ImageView(带占位图和错误图)
|
||
*
|
||
* @param context Context
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
* @param errorId 错误图资源ID
|
||
*/
|
||
public static void loadImageSafe(@Nullable Context context, @Nullable String url,
|
||
@NonNull ImageView imageView,
|
||
@DrawableRes int errorId) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.error(errorId)
|
||
.into(imageView);
|
||
}
|
||
|
||
public static void loadImageSafe(@Nullable Context context, int id,
|
||
@NonNull ImageView imageView,
|
||
@DrawableRes int errorId) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(id)
|
||
.error(errorId)
|
||
.into(imageView);
|
||
}
|
||
|
||
public static void loadImageSafe(@Nullable Context context, @Nullable String url,
|
||
@NonNull ImageView imageView,
|
||
Drawable drawable) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.error(drawable)
|
||
.into(imageView);
|
||
}
|
||
|
||
public static void loadImageSafe(@Nullable Context context, @Nullable String url,
|
||
@NonNull ImageView imageView,
|
||
Bitmap bitmap) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.error(bitmap)
|
||
.into(imageView);
|
||
}
|
||
|
||
|
||
/**
|
||
* 安全地加载图片到 ImageView(带占位图和错误图)
|
||
*
|
||
* @param context Context
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
* @param placeholderId 占位图资源ID
|
||
* @param errorId 错误图资源ID
|
||
*/
|
||
public static void loadImageSafe(@Nullable Context context, @Nullable String url,
|
||
@NonNull ImageView imageView,
|
||
@DrawableRes int placeholderId,
|
||
@DrawableRes int errorId) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.placeholder(placeholderId)
|
||
.error(errorId)
|
||
.into(imageView);
|
||
}
|
||
|
||
/**
|
||
* 安全地加载 Bitmap 图片
|
||
*
|
||
* @param context Context
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
*/
|
||
public static void loadBitmapSafe(@Nullable Context context, @Nullable String url, @NonNull ImageView imageView) {
|
||
if (context == null || url == null) {
|
||
Logger.w(TAG, "Context or URL is null");
|
||
return;
|
||
}
|
||
|
||
RequestBuilder<Bitmap> requestManager = null;
|
||
|
||
if (context instanceof FragmentActivity) {
|
||
FragmentActivity activity = (FragmentActivity) context;
|
||
if (!isFragmentActivityValid(activity)) {
|
||
return;
|
||
}
|
||
requestManager = GlideApp.with(activity).asBitmap();
|
||
} else if (context instanceof Activity) {
|
||
Activity activity = (Activity) context;
|
||
if (!isActivityValid(activity)) {
|
||
return;
|
||
}
|
||
requestManager = GlideApp.with(activity).asBitmap();
|
||
}
|
||
|
||
if (requestManager != null) {
|
||
requestManager
|
||
.load(url)
|
||
.into(imageView);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安全地加载 Drawable 图片(带回调)
|
||
*
|
||
* @param context Context
|
||
* @param url 图片URL
|
||
* @param listener 加载回调监听器
|
||
*/
|
||
public static void loadWithListener(@Nullable Context context, @Nullable String url,
|
||
@NonNull RequestListener<Drawable> listener) {
|
||
RequestBuilder<Drawable> requestManager = getSafeRequestManager(context);
|
||
if (requestManager == null) {
|
||
Logger.w(TAG, "Skip loading image due to invalid context");
|
||
return;
|
||
}
|
||
|
||
requestManager
|
||
.load(url)
|
||
.listener(listener)
|
||
.submit();
|
||
}
|
||
|
||
/**
|
||
* 从 Fragment 安全地加载图片
|
||
*
|
||
* @param fragment Fragment
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
*/
|
||
public static void loadImageFromFragment(@Nullable Fragment fragment, @Nullable String url,
|
||
@NonNull ImageView imageView) {
|
||
if (!isFragmentValid(fragment)) {
|
||
Logger.w(TAG, "Skip loading from invalid fragment");
|
||
return;
|
||
}
|
||
|
||
GlideApp.with(fragment)
|
||
.load(url)
|
||
.into(imageView);
|
||
}
|
||
|
||
/**
|
||
* 从 Fragment 安全地加载图片(带占位图和错误图)
|
||
*
|
||
* @param fragment Fragment
|
||
* @param url 图片URL
|
||
* @param imageView 目标ImageView
|
||
* @param placeholderId 占位图资源ID
|
||
* @param errorId 错误图资源ID
|
||
*/
|
||
public static void loadImageFromFragment(@Nullable Fragment fragment, @Nullable String url,
|
||
@NonNull ImageView imageView,
|
||
@DrawableRes int placeholderId,
|
||
@DrawableRes int errorId) {
|
||
if (!isFragmentValid(fragment)) {
|
||
Logger.w(TAG, "Skip loading from invalid fragment");
|
||
return;
|
||
}
|
||
|
||
GlideApp.with(fragment)
|
||
.load(url)
|
||
.placeholder(placeholderId)
|
||
.error(errorId)
|
||
.into(imageView);
|
||
}
|
||
|
||
/**
|
||
* 清除 ImageView 的图片加载请求
|
||
* 通常在 RecyclerView 复用时使用
|
||
*
|
||
* @param imageView ImageView
|
||
*/
|
||
public static void clearImage(@NonNull ImageView imageView) {
|
||
GlideApp.with(imageView.getContext()).clear(imageView);
|
||
}
|
||
|
||
/**
|
||
* 暂停所有图片加载请求
|
||
*
|
||
* @param context Context
|
||
*/
|
||
public static void pauseRequests(@Nullable Context context) {
|
||
if (context != null) {
|
||
GlideApp.with(context).pauseRequests();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 恢复所有图片加载请求
|
||
*
|
||
* @param context Context
|
||
*/
|
||
public static void resumeRequests(@Nullable Context context) {
|
||
if (context != null) {
|
||
GlideApp.with(context).resumeRequests();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清除内存缓存
|
||
*
|
||
* @param context Context
|
||
*/
|
||
public static void clearMemoryCache(@Nullable Context context) {
|
||
if (context != null) {
|
||
GlideApp.get(context).clearMemory();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清除磁盘缓存(需在后台线程执行)
|
||
*
|
||
* @param context Context
|
||
*/
|
||
public static void clearDiskCache(@Nullable Context context) {
|
||
if (context != null) {
|
||
GlideApp.get(context).clearDiskCache();
|
||
}
|
||
}
|
||
}
|