Skip to content

Commit

Permalink
version 0.1.8.27
Browse files Browse the repository at this point in the history
prerelease
  • Loading branch information
gonojuarez committed Aug 23, 2021
1 parent 23c6117 commit b5b6c06
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
* */
Expand All @@ -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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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;
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -281,6 +245,15 @@ public RequestBuilder<Drawable> useDecodeFormat(RequestBuilder<Drawable> 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;
}





}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b5b6c06

Please sign in to comment.