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