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 com.bumptech.glide.Priority;
19 import com.bumptech.glide.load.DataSource;
20 import com.bumptech.glide.load.Options;
21 import com.bumptech.glide.load.data.DataFetcher;
22 import com.bumptech.glide.load.model.ModelLoader;
23 import com.bumptech.glide.load.model.ModelLoaderFactory;
24 import com.bumptech.glide.load.model.MultiModelLoaderFactory;
25 
26 import java.io.InputStream;
27 
28 import androidx.annotation.Nullable;
29 
30 /**
31  * Glide ModelLoader which loads InputStreams from ResourceAssets.
32  */
33 public class ResourceAssetLoader implements ModelLoader<ResourceAsset, InputStream> {
34 
35     @Override
handles(ResourceAsset resourceAsset)36     public boolean handles(ResourceAsset resourceAsset) {
37         return true;
38     }
39 
40     @Nullable
41     @Override
buildLoadData(ResourceAsset resourceAsset, int unusedWidth, int unusedHeight, Options options)42     public LoadData<InputStream> buildLoadData(ResourceAsset resourceAsset, int unusedWidth,
43                                                int unusedHeight, Options options) {
44         return new LoadData<>(resourceAsset.getKey(), new ResourceAssetFetcher(resourceAsset));
45     }
46 
47     /**
48      * Factory that constructs {@link ResourceAssetLoader} instances.
49      */
50     public static class ResourceAssetLoaderFactory
51             implements ModelLoaderFactory<ResourceAsset, InputStream> {
ResourceAssetLoaderFactory()52         public ResourceAssetLoaderFactory() {
53         }
54 
55         @Override
build(MultiModelLoaderFactory multiFactory)56         public ModelLoader<ResourceAsset, InputStream> build(MultiModelLoaderFactory multiFactory) {
57             return new ResourceAssetLoader();
58         }
59 
60         @Override
teardown()61         public void teardown() {
62             // no-op
63         }
64     }
65 
66     /**
67      * Glide DataFetcher for ResourceAsset.
68      */
69     protected static class ResourceAssetFetcher implements DataFetcher<InputStream> {
70 
71         private ResourceAsset mResourceAsset;
72 
ResourceAssetFetcher(ResourceAsset resourceAsset)73         public ResourceAssetFetcher(ResourceAsset resourceAsset) {
74             mResourceAsset = resourceAsset;
75         }
76 
77         @Override
loadData(Priority priority, final DataCallback<? super InputStream> callback)78         public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
79             callback.onDataReady(
80                     mResourceAsset.getResources().openRawResource(mResourceAsset.getResId()));
81         }
82 
83         @Override
getDataSource()84         public DataSource getDataSource() {
85             return DataSource.LOCAL;
86         }
87 
88         @Override
cancel()89         public void cancel() {
90             // no op
91         }
92 
93         @Override
cleanup()94         public void cleanup() {
95             // no op
96         }
97 
98         @Override
getDataClass()99         public Class<InputStream> getDataClass() {
100             return InputStream.class;
101         }
102     }
103 }
104