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.os.ParcelFileDescriptor;
19 import android.os.ParcelFileDescriptor.AutoCloseInputStream;
20 
21 import com.bumptech.glide.Priority;
22 import com.bumptech.glide.load.DataSource;
23 import com.bumptech.glide.load.Options;
24 import com.bumptech.glide.load.data.DataFetcher;
25 import com.bumptech.glide.load.model.ModelLoader;
26 import com.bumptech.glide.load.model.ModelLoaderFactory;
27 import com.bumptech.glide.load.model.MultiModelLoaderFactory;
28 
29 import java.io.InputStream;
30 
31 import androidx.annotation.Nullable;
32 
33 /**
34  * Glide custom model loader for {@link CurrentWallpaperAssetVN}.
35  */
36 public class CurrentWallpaperAssetVNLoader implements
37         ModelLoader<CurrentWallpaperAssetVN, InputStream> {
38 
39     @Override
handles(CurrentWallpaperAssetVN currentWallpaperAssetVN)40     public boolean handles(CurrentWallpaperAssetVN currentWallpaperAssetVN) {
41         return true;
42     }
43 
44     @Nullable
45     @Override
buildLoadData(CurrentWallpaperAssetVN currentWallpaperAssetVN, int width, int height, Options options)46     public LoadData<InputStream> buildLoadData(CurrentWallpaperAssetVN currentWallpaperAssetVN,
47                                                int width, int height, Options options) {
48         return new LoadData<>(currentWallpaperAssetVN.getKey(),
49                 new CurrentWallpaperAssetVNDataFetcher(currentWallpaperAssetVN));
50     }
51 
52     /**
53      * Factory that constructs {@link ResourceAssetLoader} instances.
54      */
55     public static class CurrentWallpaperAssetVNLoaderFactory
56             implements ModelLoaderFactory<CurrentWallpaperAssetVN, InputStream> {
CurrentWallpaperAssetVNLoaderFactory()57         public CurrentWallpaperAssetVNLoaderFactory() {
58         }
59 
60         @Override
build( MultiModelLoaderFactory multiFactory)61         public ModelLoader<CurrentWallpaperAssetVN, InputStream> build(
62                 MultiModelLoaderFactory multiFactory) {
63             return new CurrentWallpaperAssetVNLoader();
64         }
65 
66         @Override
teardown()67         public void teardown() {
68             // no-op
69         }
70     }
71 
72     private static class CurrentWallpaperAssetVNDataFetcher implements DataFetcher<InputStream> {
73 
74         private CurrentWallpaperAssetVN mAsset;
75 
CurrentWallpaperAssetVNDataFetcher(CurrentWallpaperAssetVN asset)76         public CurrentWallpaperAssetVNDataFetcher(CurrentWallpaperAssetVN asset) {
77             mAsset = asset;
78         }
79 
80         @Override
loadData(Priority priority, final DataCallback<? super InputStream> callback)81         public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
82             ParcelFileDescriptor pfd = mAsset.getWallpaperPfd();
83 
84             if (pfd == null) {
85                 callback.onLoadFailed(new Exception("ParcelFileDescriptor for wallpaper is null, unable "
86                         + "to open InputStream."));
87                 return;
88             }
89 
90             callback.onDataReady(new AutoCloseInputStream(pfd));
91         }
92 
93         @Override
getDataSource()94         public DataSource getDataSource() {
95             return DataSource.LOCAL;
96         }
97 
98         @Override
cancel()99         public void cancel() {
100             // no op
101         }
102 
103         @Override
cleanup()104         public void cleanup() {
105             // no op
106         }
107 
108         @Override
getDataClass()109         public Class<InputStream> getDataClass() {
110             return InputStream.class;
111         }
112     }
113 }
114