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