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.graphics.Bitmap; 19 import android.graphics.Rect; 20 21 import com.android.wallpaper.asset.Asset; 22 import com.android.wallpaper.asset.Asset.BitmapReceiver; 23 import com.android.wallpaper.module.BitmapCropper; 24 25 /** 26 * Test double for BitmapCropper. 27 */ 28 public class TestBitmapCropper implements BitmapCropper { 29 30 private boolean mFailNextCall; 31 TestBitmapCropper()32 public TestBitmapCropper() { 33 mFailNextCall = false; 34 } 35 36 @Override cropAndScaleBitmap(Asset asset, float scale, Rect cropRect, Callback callback)37 public void cropAndScaleBitmap(Asset asset, float scale, Rect cropRect, 38 Callback callback) { 39 if (mFailNextCall) { 40 callback.onError(null /* throwable */); 41 return; 42 } 43 // Crop rect in pixels of source image. 44 Rect scaledCropRect = new Rect( 45 Math.round((float) cropRect.left / scale), 46 Math.round((float) cropRect.top / scale), 47 Math.round((float) cropRect.right / scale), 48 Math.round((float) cropRect.bottom / scale)); 49 50 asset.decodeBitmapRegion(scaledCropRect, cropRect.width(), cropRect.height(), 51 new BitmapReceiver() { 52 @Override 53 public void onBitmapDecoded(Bitmap bitmap) { 54 callback.onBitmapCropped(bitmap); 55 } 56 }); 57 } 58 } 59