blob: 1c6b2c674a0f882a1621f1ffab9edb870e2f8667 [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
18import android.graphics.drawable.Drawable;
Jon Miranda16ea1b12017-12-12 14:52:48 -080019
20import com.bumptech.glide.Priority;
21import com.bumptech.glide.load.DataSource;
22import com.bumptech.glide.load.Options;
23import com.bumptech.glide.load.data.DataFetcher;
24import com.bumptech.glide.load.model.ModelLoader;
25import com.bumptech.glide.load.model.ModelLoaderFactory;
26import com.bumptech.glide.load.model.MultiModelLoaderFactory;
27
Sunny Goyal8600a3f2018-08-15 12:48:01 -070028import androidx.annotation.Nullable;
29
Jon Miranda16ea1b12017-12-12 14:52:48 -080030/**
31 * Glide model loader for live wallpaper thumbnails.
32 */
33public class LiveWallpaperThumbAssetLoader implements
34 ModelLoader<LiveWallpaperThumbAsset, Drawable> {
35
36 @Override
37 public boolean handles(LiveWallpaperThumbAsset liveWallpaperThumbAsset) {
38 return true;
39 }
40
41 @Nullable
42 @Override
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> {
54 public LiveWallpaperThumbAssetLoaderFactory() {
55 }
56
57 @Override
58 public ModelLoader<LiveWallpaperThumbAsset, Drawable> build(
59 MultiModelLoaderFactory multiFactory) {
60 return new LiveWallpaperThumbAssetLoader();
61 }
62
63 @Override
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
76 public LiveWallpaperThumbFetcher(LiveWallpaperThumbAsset liveWallpaperThumbAsset) {
77 mLiveWallpaperThumbAsset = liveWallpaperThumbAsset;
78 }
79
80 @Override
81 public void loadData(Priority priority, DataCallback<? super Drawable> callback) {
82 callback.onDataReady(mLiveWallpaperThumbAsset.getThumbnailDrawable());
83 }
84
85 @Override
86 public DataSource getDataSource() {
87 return DataSource.LOCAL;
88 }
89
90 @Override
91 public void cancel() {
92 // no-op
93 }
94
95 @Override
96 public void cleanup() {
97 // no-op
98 }
99
100 @Override
101 public Class<Drawable> getDataClass() {
102 return Drawable.class;
103 }
104 }
105}