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.widget.ImageView; 26 27 import androidx.annotation.Nullable; 28 29 import com.android.wallpaper.asset.Asset; 30 31 32 /** 33 * Test implementation of Asset which blocks on Bitmap decoding operations. 34 */ 35 public final class TestAsset extends Asset { 36 37 private Bitmap mBitmap; 38 private final boolean mIsCorrupt; 39 40 /** 41 * Constructs an asset underpinned by a 1x1 bitmap uniquely identifiable by the given pixel 42 * color. 43 * 44 * @param pixelColor Color of the asset's single pixel. 45 * @param isCorrupt Whether or not the asset is corrupt and fails to validly decode bitmaps and 46 * dimensions. 47 */ TestAsset(int pixelColor, boolean isCorrupt)48 public TestAsset(int pixelColor, boolean isCorrupt) { 49 mIsCorrupt = isCorrupt; 50 51 if (!mIsCorrupt) { 52 mBitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888); 53 mBitmap.setPixel(0, 0, pixelColor); 54 } else { 55 mBitmap = null; 56 } 57 } 58 59 @Override decodeBitmap(BitmapReceiver receiver)60 public void decodeBitmap(BitmapReceiver receiver) { 61 Handler.getMain().post(() -> 62 receiver.onBitmapDecoded(mBitmap)); 63 } 64 65 @Override decodeBitmap(int targetWidth, int targetHeight, boolean useHardwareBitmapIfPossible, BitmapReceiver receiver)66 public void decodeBitmap(int targetWidth, int targetHeight, boolean useHardwareBitmapIfPossible, 67 BitmapReceiver receiver) { 68 Handler.getMain().post(() -> 69 receiver.onBitmapDecoded(mBitmap)); 70 } 71 72 @Override decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight, boolean shouldAdjustForRtl, BitmapReceiver receiver)73 public void decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight, 74 boolean shouldAdjustForRtl, BitmapReceiver receiver) { 75 Handler.getMain().post(() -> 76 receiver.onBitmapDecoded(mBitmap)); 77 } 78 79 @Override decodeRawDimensions(Activity unused, DimensionsReceiver receiver)80 public void decodeRawDimensions(Activity unused, DimensionsReceiver receiver) { 81 Handler.getMain().post(() -> 82 receiver.onDimensionsDecoded(mIsCorrupt ? null : new Point(1, 1))); 83 } 84 85 @Override supportsTiling()86 public boolean supportsTiling() { 87 return false; 88 } 89 90 @Override loadDrawableWithTransition( Context context, ImageView imageView, int transitionDurationMillis, @Nullable DrawableLoadedListener drawableLoadedListener, int placeholderColor)91 public void loadDrawableWithTransition( 92 Context context, 93 ImageView imageView, 94 int transitionDurationMillis, 95 @Nullable DrawableLoadedListener drawableLoadedListener, 96 int placeholderColor) { 97 if (drawableLoadedListener != null) { 98 drawableLoadedListener.onDrawableLoaded(); 99 } 100 } 101 102 /** Returns the bitmap synchronously. Convenience method for tests. */ getBitmap()103 public Bitmap getBitmap() { 104 return mBitmap; 105 } 106 107 @Override getLowResBitmap(Context context)108 public Bitmap getLowResBitmap(Context context) { 109 return mBitmap; 110 } 111 setBitmap(Bitmap bitmap)112 public void setBitmap(Bitmap bitmap) { 113 mBitmap = bitmap; 114 } 115 } 116