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.os.ParcelFileDescriptor; 19 import android.os.ParcelFileDescriptor.AutoCloseInputStream; 20 21 import androidx.annotation.Nullable; 22 23 import com.bumptech.glide.Priority; 24 import com.bumptech.glide.load.DataSource; 25 import com.bumptech.glide.load.Options; 26 import com.bumptech.glide.load.data.DataFetcher; 27 import com.bumptech.glide.load.model.ModelLoader; 28 import com.bumptech.glide.load.model.ModelLoaderFactory; 29 import com.bumptech.glide.load.model.MultiModelLoaderFactory; 30 31 import java.io.InputStream; 32 33 /** 34 * Glide custom model loader for {@link CurrentWallpaperAsset}. 35 */ 36 public class CurrentWallpaperAssetLoader implements 37 ModelLoader<CurrentWallpaperAsset, InputStream> { 38 39 @Override handles(CurrentWallpaperAsset currentWallpaperAsset)40 public boolean handles(CurrentWallpaperAsset currentWallpaperAsset) { 41 return true; 42 } 43 44 @Nullable 45 @Override buildLoadData(CurrentWallpaperAsset currentWallpaperAsset, int width, int height, Options options)46 public LoadData<InputStream> buildLoadData(CurrentWallpaperAsset currentWallpaperAsset, 47 int width, int height, Options options) { 48 return new LoadData<>(currentWallpaperAsset.getKey(), 49 new CurrentWallpaperAssetDataFetcher(currentWallpaperAsset)); 50 } 51 52 /** 53 * Factory that constructs {@link ResourceAssetLoader} instances. 54 */ 55 public static class CurrentWallpaperAssetLoaderFactory 56 implements ModelLoaderFactory<CurrentWallpaperAsset, InputStream> { CurrentWallpaperAssetLoaderFactory()57 public CurrentWallpaperAssetLoaderFactory() { 58 } 59 60 @Override build( MultiModelLoaderFactory multiFactory)61 public ModelLoader<CurrentWallpaperAsset, InputStream> build( 62 MultiModelLoaderFactory multiFactory) { 63 return new CurrentWallpaperAssetLoader(); 64 } 65 66 @Override teardown()67 public void teardown() { 68 // no-op 69 } 70 } 71 72 private static class CurrentWallpaperAssetDataFetcher implements DataFetcher<InputStream> { 73 74 private CurrentWallpaperAsset mAsset; 75 CurrentWallpaperAssetDataFetcher(CurrentWallpaperAsset asset)76 CurrentWallpaperAssetDataFetcher(CurrentWallpaperAsset asset) { 77 mAsset = asset; 78 } 79 80 @Override loadData(Priority priority, final DataCallback<? super InputStream> callback)81 public void loadData(Priority priority, final DataCallback<? super InputStream> callback) { 82 ParcelFileDescriptor pfd = mAsset.getWallpaperPfd(); 83 84 if (pfd == null) { 85 callback.onLoadFailed(new Exception("ParcelFileDescriptor for wallpaper is null, " 86 + "unable to open InputStream.")); 87 return; 88 } 89 90 callback.onDataReady(new AutoCloseInputStream(pfd)); 91 } 92 93 @Override getDataSource()94 public DataSource getDataSource() { 95 return DataSource.LOCAL; 96 } 97 98 @Override cancel()99 public void cancel() { 100 // no op 101 } 102 103 @Override cleanup()104 public void cleanup() { 105 // no op 106 } 107 108 @Override getDataClass()109 public Class<InputStream> getDataClass() { 110 return InputStream.class; 111 } 112 } 113 } 114