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  * Glide model loader for live wallpaper thumbnails.
32  */
33 public class LiveWallpaperThumbAssetLoader implements
34         ModelLoader<LiveWallpaperThumbAsset, Drawable> {
35 
36     @Override
handles(LiveWallpaperThumbAsset liveWallpaperThumbAsset)37     public boolean handles(LiveWallpaperThumbAsset liveWallpaperThumbAsset) {
38         return true;
39     }
40 
41     @Nullable
42     @Override
buildLoadData(LiveWallpaperThumbAsset liveWallpaperThumbAsset, int unusedWidth, int unusedHeight, Options options)43     public LoadData<Drawable> buildLoadData(LiveWallpaperThumbAsset liveWallpaperThumbAsset,
44                                             int unusedWidth, int unusedHeight, Options options) {
45         return new LoadData<>(liveWallpaperThumbAsset.getKey(),
46                 new LiveWallpaperThumbFetcher(liveWallpaperThumbAsset));
47     }
48 
49     /**
50      * Factory that constructs {@link LiveWallpaperThumbAssetLoader} instances.
51      */
52     public static class LiveWallpaperThumbAssetLoaderFactory
53             implements ModelLoaderFactory<LiveWallpaperThumbAsset, Drawable> {
LiveWallpaperThumbAssetLoaderFactory()54         public LiveWallpaperThumbAssetLoaderFactory() {
55         }
56 
57         @Override
build( MultiModelLoaderFactory multiFactory)58         public ModelLoader<LiveWallpaperThumbAsset, Drawable> build(
59                 MultiModelLoaderFactory multiFactory) {
60             return new LiveWallpaperThumbAssetLoader();
61         }
62 
63         @Override
teardown()64         public void teardown() {
65             // no-op
66         }
67     }
68 
69     /**
70      * Fetcher class for fetching wallpaper image data from a {@link LiveWallpaperThumbAsset}.
71      */
72     private static class LiveWallpaperThumbFetcher implements DataFetcher<Drawable> {
73 
74         private LiveWallpaperThumbAsset mLiveWallpaperThumbAsset;
75 
LiveWallpaperThumbFetcher(LiveWallpaperThumbAsset liveWallpaperThumbAsset)76         public LiveWallpaperThumbFetcher(LiveWallpaperThumbAsset liveWallpaperThumbAsset) {
77             mLiveWallpaperThumbAsset = liveWallpaperThumbAsset;
78         }
79 
80         @Override
loadData(Priority priority, DataCallback<? super Drawable> callback)81         public void loadData(Priority priority, DataCallback<? super Drawable> callback) {
82             callback.onDataReady(mLiveWallpaperThumbAsset.getThumbnailDrawable());
83         }
84 
85         @Override
getDataSource()86         public DataSource getDataSource() {
87             return DataSource.LOCAL;
88         }
89 
90         @Override
cancel()91         public void cancel() {
92             // no-op
93         }
94 
95         @Override
cleanup()96         public void cleanup() {
97             // no-op
98         }
99 
100         @Override
getDataClass()101         public Class<Drawable> getDataClass() {
102             return Drawable.class;
103         }
104     }
105 }
106