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.graphics.drawable.Drawable;
19 
20 import com.bumptech.glide.load.Options;
21 import com.bumptech.glide.load.ResourceDecoder;
22 import com.bumptech.glide.load.engine.Resource;
23 import com.bumptech.glide.load.resource.drawable.DrawableResource;
24 
25 import java.io.IOException;
26 
27 import androidx.annotation.Nullable;
28 
29 /**
30  * Identity {@link ResourceDecoder} implementation that simply passes through a Drawable.
31  * <p>
32  * Register this class in a {@link com.bumptech.glide.module.GlideModule} on Glide's
33  * {@link com.bumptech.glide.Registry} in order to support loading {@link Drawable} objects for
34  * {@link com.bumptech.glide.load.model.ModelLoader} implementations that directly load
35  * {@link Drawable} data as an output type.
36  */
37 public class DrawableResourceDecoder implements ResourceDecoder<Drawable, Drawable> {
38 
39     @Override
handles(Drawable source, Options options)40     public boolean handles(Drawable source, Options options) throws IOException {
41         return true;
42     }
43 
44     @Nullable
45     @Override
decode(Drawable source, int width, int height, Options options)46     public Resource<Drawable> decode(Drawable source, int width, int height, Options options)
47             throws IOException {
48         return new DrawableResource<Drawable>(source) {
49             @Override
50             public Class<Drawable> getResourceClass() {
51                 return Drawable.class;
52             }
53 
54             @Override
55             public int getSize() {
56                 // Return size assuming that each pixel takes 4 bytes of memory (Bitmap.Config.ARGB_8888).
57                 return get().getIntrinsicWidth() * get().getIntrinsicHeight() * 4;
58             }
59 
60             @Override
61             public void recycle() {
62                 // no-op
63             }
64         };
65     }
66 }
67