blob: 6de89ba0aebe925eba8b846c668ddcfdef703795 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.asset;
17
Jon Miranda16ea1b12017-12-12 14:52:48 -080018import com.bumptech.glide.Priority;
19import com.bumptech.glide.load.DataSource;
20import com.bumptech.glide.load.Options;
21import com.bumptech.glide.load.data.DataFetcher;
22import com.bumptech.glide.load.model.ModelLoader;
23import com.bumptech.glide.load.model.ModelLoaderFactory;
24import com.bumptech.glide.load.model.MultiModelLoaderFactory;
25
26import java.io.InputStream;
27
Sunny Goyal8600a3f2018-08-15 12:48:01 -070028import androidx.annotation.Nullable;
29
Jon Miranda16ea1b12017-12-12 14:52:48 -080030/**
31 * Glide ModelLoader which loads InputStreams from ResourceAssets.
32 */
33public class ResourceAssetLoader implements ModelLoader<ResourceAsset, InputStream> {
34
35 @Override
36 public boolean handles(ResourceAsset resourceAsset) {
37 return true;
38 }
39
40 @Nullable
41 @Override
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> {
52 public ResourceAssetLoaderFactory() {
53 }
54
55 @Override
56 public ModelLoader<ResourceAsset, InputStream> build(MultiModelLoaderFactory multiFactory) {
57 return new ResourceAssetLoader();
58 }
59
60 @Override
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
73 public ResourceAssetFetcher(ResourceAsset resourceAsset) {
74 mResourceAsset = resourceAsset;
75 }
76
77 @Override
78 public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
79 callback.onDataReady(
80 mResourceAsset.getResources().openRawResource(mResourceAsset.getResId()));
81 }
82
83 @Override
84 public DataSource getDataSource() {
85 return DataSource.LOCAL;
86 }
87
88 @Override
89 public void cancel() {
90 // no op
91 }
92
93 @Override
94 public void cleanup() {
95 // no op
96 }
97
98 @Override
99 public Class<InputStream> getDataClass() {
100 return InputStream.class;
101 }
102 }
103}