1 package com.android.wallpaper.asset; 2 3 import android.content.Context; 4 import android.graphics.drawable.Drawable; 5 6 import com.android.wallpaper.asset.CurrentWallpaperAssetVNLoader.CurrentWallpaperAssetVNLoaderFactory; 7 import com.android.wallpaper.asset.LiveWallpaperThumbAssetLoader.LiveWallpaperThumbAssetLoaderFactory; 8 import com.android.wallpaper.asset.ResourceAssetLoader.ResourceAssetLoaderFactory; 9 import com.android.wallpaper.asset.SystemStaticAssetLoader.SystemStaticAssetLoaderFactory; 10 import com.android.wallpaper.asset.WallpaperModelLoader.WallpaperModelLoaderFactory; 11 12 import com.bumptech.glide.Glide; 13 import com.bumptech.glide.GlideBuilder; 14 import com.bumptech.glide.Registry; 15 import com.bumptech.glide.load.DecodeFormat; 16 import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory; 17 import com.bumptech.glide.load.engine.cache.MemorySizeCalculator; 18 import com.bumptech.glide.module.GlideModule; 19 import com.bumptech.glide.request.RequestOptions; 20 21 import java.io.InputStream; 22 23 /** 24 * Provides configuration for Glide, such as specifying an internal disk cache size. 25 */ 26 public class WallpaperGlideModule implements GlideModule { 27 /** 28 * Disk cache size for wallpaper: 100MB. 29 */ 30 private static final int WALLPAPER_DISK_CACHE_SIZE_BYTES = 100 * 1024 * 1024; 31 32 @Override applyOptions(Context context, GlideBuilder builder)33 public void applyOptions(Context context, GlideBuilder builder) { 34 // Default Glide cache size is 250MB so make the wallpaper cache much smaller at 100MB. 35 builder.setDiskCache(new InternalCacheDiskCacheFactory( 36 context, WALLPAPER_DISK_CACHE_SIZE_BYTES)); 37 38 // Default # of bitmap pool screens is 4, so reduce to 2 to make room for the additional memory 39 // consumed by tiling large images in preview and also the large bitmap consumed by the live 40 // wallpaper for daily rotation. 41 MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context) 42 .setBitmapPoolScreens(2f) 43 .setMemoryCacheScreens(1.2f) 44 .build(); 45 builder.setMemorySizeCalculator(calculator); 46 builder.setDefaultRequestOptions( 47 new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888)); 48 } 49 50 @Override registerComponents(Context context, Glide glide, Registry registry)51 public void registerComponents(Context context, Glide glide, Registry registry) { 52 registry.append(WallpaperModel.class, Drawable.class, new WallpaperModelLoaderFactory()); 53 registry.append(ResourceAsset.class, InputStream.class, new ResourceAssetLoaderFactory()); 54 registry.append(SystemStaticAsset.class, InputStream.class, 55 new SystemStaticAssetLoaderFactory()); 56 registry.append(LiveWallpaperThumbAsset.class, Drawable.class, 57 new LiveWallpaperThumbAssetLoaderFactory()); 58 registry.append(CurrentWallpaperAssetVN.class, InputStream.class, 59 new CurrentWallpaperAssetVNLoaderFactory()); 60 registry.append(Drawable.class, Drawable.class, new DrawableResourceDecoder()); 61 } 62 } 63