1 /* 2 * Copyright (C) 2019 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.testing; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Bitmap.Config; 22 import android.graphics.Point; 23 import android.graphics.Rect; 24 import android.os.Handler; 25 import android.util.Log; 26 import android.widget.ImageView; 27 28 import androidx.annotation.Nullable; 29 30 import com.android.wallpaper.asset.Asset; 31 32 import java.io.File; 33 import java.io.FileOutputStream; 34 import java.io.IOException; 35 36 37 /** 38 * Test implementation of Asset which blocks on Bitmap decoding operations. 39 */ 40 public final class TestAsset extends Asset { 41 private static final String TAG = "TestAsset"; 42 43 private Bitmap mBitmap; 44 private final boolean mIsCorrupt; 45 46 /** 47 * Constructs an asset underpinned by a 1x1 bitmap uniquely identifiable by the given pixel 48 * color. 49 * 50 * @param pixelColor Color of the asset's single pixel. 51 * @param isCorrupt Whether or not the asset is corrupt and fails to validly decode bitmaps and 52 * dimensions. 53 */ TestAsset(int pixelColor, boolean isCorrupt)54 public TestAsset(int pixelColor, boolean isCorrupt) { 55 this(pixelColor, isCorrupt, 1, 1); 56 } 57 58 /** 59 * Constructs an asset underpinned by a width x height bitmap uniquely identifiable by the given 60 * pixel color. 61 * 62 * @param pixelColor Color of the asset's single pixel. 63 * @param isCorrupt Whether or not the asset is corrupt and fails to validly decode bitmaps and 64 * dimensions. 65 * @param width Width of asset image 66 * @param height Height of asset image 67 */ TestAsset(int pixelColor, boolean isCorrupt, int width, int height)68 public TestAsset(int pixelColor, boolean isCorrupt, int width, int height) { 69 mIsCorrupt = isCorrupt; 70 71 if (!mIsCorrupt) { 72 mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 73 mBitmap.setPixel(0, 0, pixelColor); 74 } else { 75 mBitmap = null; 76 } 77 } 78 79 @Override decodeBitmap(BitmapReceiver receiver)80 public void decodeBitmap(BitmapReceiver receiver) { 81 Handler.getMain().post(() -> 82 receiver.onBitmapDecoded(mBitmap)); 83 } 84 85 @Override decodeBitmap(int targetWidth, int targetHeight, boolean useHardwareBitmapIfPossible, BitmapReceiver receiver)86 public void decodeBitmap(int targetWidth, int targetHeight, boolean useHardwareBitmapIfPossible, 87 BitmapReceiver receiver) { 88 Handler.getMain().post(() -> 89 receiver.onBitmapDecoded(mBitmap)); 90 } 91 92 @Override decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight, boolean shouldAdjustForRtl, BitmapReceiver receiver)93 public void decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight, 94 boolean shouldAdjustForRtl, BitmapReceiver receiver) { 95 Handler.getMain().post(() -> 96 receiver.onBitmapDecoded(mBitmap)); 97 } 98 99 @Override decodeRawDimensions(Activity unused, DimensionsReceiver receiver)100 public void decodeRawDimensions(Activity unused, DimensionsReceiver receiver) { 101 Handler.getMain().post(() -> 102 receiver.onDimensionsDecoded( 103 mIsCorrupt ? null : new Point(mBitmap.getWidth(), mBitmap.getHeight()))); 104 } 105 106 @Override supportsTiling()107 public boolean supportsTiling() { 108 return false; 109 } 110 111 @Override loadDrawableWithTransition( Context context, ImageView imageView, int transitionDurationMillis, @Nullable DrawableLoadedListener drawableLoadedListener, int placeholderColor)112 public void loadDrawableWithTransition( 113 Context context, 114 ImageView imageView, 115 int transitionDurationMillis, 116 @Nullable DrawableLoadedListener drawableLoadedListener, 117 int placeholderColor) { 118 if (drawableLoadedListener != null) { 119 drawableLoadedListener.onDrawableLoaded(); 120 } 121 } 122 123 /** 124 * Returns the bitmap synchronously. Convenience method for tests. 125 */ getBitmap()126 public Bitmap getBitmap() { 127 return mBitmap; 128 } 129 130 @Override getLowResBitmap(Context context)131 public Bitmap getLowResBitmap(Context context) { 132 return mBitmap; 133 } 134 setBitmap(Bitmap bitmap)135 public void setBitmap(Bitmap bitmap) { 136 mBitmap = bitmap; 137 } 138 139 @Override copy(File dest)140 public void copy(File dest) { 141 try (FileOutputStream ofs = new FileOutputStream(dest)) { 142 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, ofs); 143 } catch (IOException e) { 144 Log.e(TAG, "Error writing test asset", e); 145 throw new RuntimeException(e); 146 } 147 } 148 } 149