1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.wallpaper.asset; 17 18 import android.graphics.drawable.Drawable; 19 20 import com.bumptech.glide.Priority; 21 import com.bumptech.glide.load.DataSource; 22 import com.bumptech.glide.load.Options; 23 import com.bumptech.glide.load.data.DataFetcher; 24 import com.bumptech.glide.load.model.ModelLoader; 25 import com.bumptech.glide.load.model.ModelLoaderFactory; 26 import com.bumptech.glide.load.model.MultiModelLoaderFactory; 27 28 import androidx.annotation.Nullable; 29 30 /** 31 * Custom Glide {@link ModelLoader} which can load {@link Drawable} objects from 32 * {@link WallpaperModel} objects. 33 */ 34 public class WallpaperModelLoader implements ModelLoader<WallpaperModel, Drawable> { 35 36 @Override handles(WallpaperModel wallpaperModel)37 public boolean handles(WallpaperModel wallpaperModel) { 38 return true; 39 } 40 41 @Nullable 42 @Override buildLoadData(WallpaperModel wallpaperModel, int width, int height, Options options)43 public LoadData<Drawable> buildLoadData(WallpaperModel wallpaperModel, int width, int height, 44 Options options) { 45 return new LoadData<>(wallpaperModel.getKey(), 46 new WallpaperFetcher(wallpaperModel, width, height)); 47 } 48 49 /** 50 * Factory that constructs {@link WallpaperModelLoader} instances. 51 */ 52 public static class WallpaperModelLoaderFactory 53 implements ModelLoaderFactory<WallpaperModel, Drawable> { WallpaperModelLoaderFactory()54 public WallpaperModelLoaderFactory() { 55 } 56 57 @Override build(MultiModelLoaderFactory multiFactory)58 public ModelLoader<WallpaperModel, Drawable> build(MultiModelLoaderFactory multiFactory) { 59 return new WallpaperModelLoader(); 60 } 61 62 @Override teardown()63 public void teardown() { 64 // no-op 65 } 66 } 67 68 /** 69 * Fetcher class for fetching wallpaper image data from a {@link WallpaperModel}. 70 */ 71 private static class WallpaperFetcher implements DataFetcher<Drawable> { 72 73 private WallpaperModel mWallpaperModel; 74 private int mWidth; 75 private int mHeight; 76 WallpaperFetcher(WallpaperModel wallpaperModel, int width, int height)77 public WallpaperFetcher(WallpaperModel wallpaperModel, int width, int height) { 78 mWallpaperModel = wallpaperModel; 79 mWidth = width; 80 mHeight = height; 81 } 82 83 @Override loadData(Priority priority, DataCallback<? super Drawable> callback)84 public void loadData(Priority priority, DataCallback<? super Drawable> callback) { 85 callback.onDataReady(mWallpaperModel.getDrawable(mWidth, mHeight)); 86 } 87 88 @Override getDataSource()89 public DataSource getDataSource() { 90 return DataSource.LOCAL; 91 } 92 93 @Override cancel()94 public void cancel() { 95 // no-op 96 } 97 98 @Override cleanup()99 public void cleanup() { 100 // no-op 101 } 102 103 @Override getDataClass()104 public Class<Drawable> getDataClass() { 105 return Drawable.class; 106 } 107 } 108 } 109