Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created November 10, 2017 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxnjdlys/d6d11175a757d8c5496e7703e80b9a85 to your computer and use it in GitHub Desktop.
Save xxnjdlys/d6d11175a757d8c5496e7703e80b9a85 to your computer and use it in GitHub Desktop.
package com.dailyupfitness.common.configuration;
import android.app.ActivityManager;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.Headers;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.BaseGlideUrlLoader;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.bumptech.glide.module.GlideModule;
import java.io.InputStream;
/**
* Glide configurations
* Created by zhangge on 16/10/14.
*/
public class GlideConfigurationModule implements GlideModule {
static final String USER_AGENT = "Mozilla/5.0 (Windows NT 5.2; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
static final String APP_AGENT = "DailyUp";
@Override
public void applyOptions(Context context, GlideBuilder builder) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (null != activityManager) {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
builder.setDecodeFormat(memoryInfo.lowMemory ?
DecodeFormat.PREFER_RGB_565 : DecodeFormat.PREFER_ARGB_8888);
}
}
@Override
public void registerComponents(Context context, Glide glide) {
glide.register(String.class, InputStream.class, new HeaderLoader.Factory());
}
private static class HeaderLoader extends BaseGlideUrlLoader<String> {
static final Headers HEADERS = new LazyHeaders.Builder()
.addHeader("User-Agent", USER_AGENT)
.addHeader("App-Agent", APP_AGENT)
.build();
HeaderLoader(Context context) {
super(context);
}
@Override protected String getUrl(String model, int width, int height) {
return model;
}
@Override protected Headers getHeaders(String model, int width, int height) {
return HEADERS;
}
public static class Factory implements ModelLoaderFactory<String, InputStream> {
@Override public StreamModelLoader<String> build(Context context, GenericLoaderFactory factories) {
return new HeaderLoader(context);
}
@Override public void teardown() { /* nothing to free */ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment