From b5b6c068d95454dba8326cce99a8913c52f99a7f Mon Sep 17 00:00:00 2001 From: gonojuarez Date: Mon, 23 Aug 2021 09:26:02 -0300 Subject: [PATCH] version 0.1.8.27 prerelease --- .../utils/GlideAttatcher.java | 109 +++++++----------- .../utils/ValidateContext.java | 32 +++++ 2 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/ValidateContext.java diff --git a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java index 87c2631..0c20b84 100644 --- a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java +++ b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java @@ -1,6 +1,7 @@ package com.stonefacesoft.pictogramslibrary.utils; import android.app.Activity; +import android.app.ActivityManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; @@ -26,6 +27,8 @@ import java.io.File; import java.net.URL; +import static com.stonefacesoft.pictogramslibrary.utils.ValidateContext.isValidContextFromGlide; + public class GlideAttatcher implements GlideModelTypes { private final Glide glide; private final Context mContext; @@ -43,13 +46,6 @@ public GlideAttatcher(@NonNull Context mContext) { glide = Glide.get(this.mContext); } - /** - * Attached a picture in an Imageview - */ - public void attachedOnImaView(@NonNull Object drawable, @NonNull ImageView imageView) { - if (isValidContextFromGlide(mContext)) - getGlide().getRequestManagerRetriever().get(mContext).load(drawable).override(150, 150).transform(new RoundedCorners(16)).into(imageView); - } /* * Use this flag in order to set an scaleoption * */ @@ -68,43 +64,15 @@ public GlideAttatcher useDecodeFormatRGB565(){ return this; } - - public void attachedOnImaView(@NonNull ImageView imageView, @NonNull Object drawable) { - if (isValidContextFromGlide(mContext)) - getGlide().getRequestManagerRetriever().get(mContext).load(drawable).override(150, 150).transform(new RoundedCorners(20)).into(imageView); - } - - public void attachedOnImaView(@NonNull ImageView imageView, @NonNull Object drawable, String pathuri) { - - if (isValidContextFromGlide(mContext)) - if (pathuri.isEmpty()) { - getGlide().getRequestManagerRetriever().get(mContext).load(drawable).override(150, 150).transform(new RoundedCorners(16)).into(imageView); - } else { - File file = new File(pathuri); - getGlide().getRequestManagerRetriever().get(mContext).load(file).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - return false; - } - }).override(width, height).transform(new RoundedCorners(20)).into(imageView); - } - } - /** * clean the glide memory if you need */ public void clearMemory() { - glide.clearMemory(); + if(getAvailableMemory().lowMemory) + glide.clearMemory(); } - - private Glide getGlide() { return glide; } @@ -114,29 +82,7 @@ private Glide getGlide() { * * @return true if the activity exist or false when the activity not exist */ - private boolean isValidContextFromGlide(Context context) { - Activity activity = getmActivityFromContext(context); - if (activity == null){ - return false;} - return !isActivityDestroyed(activity); - } - /** - * @return call the activity by context and return that - */ - private Activity getmActivityFromContext(Context context) { - if (context instanceof Activity) { - return (Activity) context; - } - return null; - } - - /** - * @return show if the activity exist or not. - */ - private boolean isActivityDestroyed(Activity activity) { - return activity.isDestroyed() || activity.isFinishing() || activity.isChangingConfigurations(); - } public GlideAttatcher setHeight(int height) { this.height = height; @@ -163,71 +109,89 @@ public GlideAttatcher UseCornerRadius(boolean useCornerRadious) { @NonNull @Override public Object loadDrawable(@Nullable Bitmap bitmap, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(bitmap).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable Drawable drawable, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(drawable).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable String string, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(string).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable Uri uri, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uri).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable File file, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(file).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable Integer resourceId, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(resourceId).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @Override public Object loadDrawable(@Nullable URL url, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)){ glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(url).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable byte[] model, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(model).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @NonNull @Override public Object loadDrawable(@Nullable Object model, ImageView imageView) { - if (isValidContextFromGlide(mContext)) + if (isValidContextFromGlide(mContext)) { glideScaleItem(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(model).fallback(R.drawable.ic_baseline_cloud_download_24).error(R.drawable.ic_baseline_cloud_download_24).override(width, height))))).into(imageView); + clearMemory(); + } return this; } @@ -281,6 +245,15 @@ public RequestBuilder useDecodeFormat(RequestBuilder drawabl return drawableRequestBuilder; } + private ActivityManager.MemoryInfo getAvailableMemory(){ + ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); + activityManager.getMemoryInfo(memoryInfo); + return memoryInfo; + } + + + } diff --git a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/ValidateContext.java b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/ValidateContext.java new file mode 100644 index 0000000..93c1ab4 --- /dev/null +++ b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/ValidateContext.java @@ -0,0 +1,32 @@ +package com.stonefacesoft.pictogramslibrary.utils; + +import android.app.Activity; +import android.content.Context; + +public class ValidateContext { + + + public static boolean isValidContextFromGlide(Context context) { + Activity activity = getmActivityFromContext(context); + if (activity == null){ + return false;} + return !isActivityDestroyed(activity); + } + + /** + * @return call the activity by context and return that + */ + private static Activity getmActivityFromContext(Context context) { + if (context instanceof Activity) { + return (Activity) context; + } + return null; + } + + /** + * @return show if the activity exist or not. + */ + private static boolean isActivityDestroyed(Activity activity) { + return activity.isDestroyed() || activity.isFinishing() || activity.isChangingConfigurations(); + } +}