1/* 2 * Copyright (C) 2011 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 17package android.graphics; 18 19import com.android.layoutlib.bridge.Bridge; 20import com.android.layoutlib.bridge.impl.DelegateManager; 21import com.android.ninepatch.NinePatchChunk; 22import com.android.resources.Density; 23import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 24 25import android.content.res.BridgeResources.NinePatchInputStream; 26import android.graphics.BitmapFactory.Options; 27import android.graphics.Bitmap_Delegate.BitmapCreateFlags; 28 29import java.io.FileDescriptor; 30import java.io.IOException; 31import java.io.InputStream; 32import java.util.EnumSet; 33import java.util.Set; 34 35/** 36 * Delegate implementing the native methods of android.graphics.BitmapFactory 37 * 38 * Through the layoutlib_create tool, the original native methods of BitmapFactory have been 39 * replaced by calls to methods of the same name in this delegate class. 40 * 41 * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager} 42 * around to map int to instance of the delegate. 43 * 44 */ 45/*package*/ class BitmapFactory_Delegate { 46 47 // ------ Native Delegates ------ 48 49 @LayoutlibDelegate 50 /*package*/ static Bitmap nativeDecodeStream(InputStream is, byte[] storage, 51 Rect padding, Options opts) { 52 Bitmap bm = null; 53 54 Density density = Density.MEDIUM; 55 Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE); 56 if (opts != null) { 57 density = Density.getEnum(opts.inDensity); 58 if (opts.inPremultiplied) { 59 bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED); 60 } 61 } 62 63 try { 64 if (is instanceof NinePatchInputStream) { 65 NinePatchInputStream npis = (NinePatchInputStream) is; 66 npis.disableFakeMarkSupport(); 67 68 // load the bitmap as a nine patch 69 com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load( 70 npis, true /*is9Patch*/, false /*convert*/); 71 72 // get the bitmap and chunk objects. 73 bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags, 74 density); 75 NinePatchChunk chunk = ninePatch.getChunk(); 76 77 // put the chunk in the bitmap 78 bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk)); 79 80 // read the padding 81 int[] paddingarray = chunk.getPadding(); 82 padding.left = paddingarray[0]; 83 padding.top = paddingarray[1]; 84 padding.right = paddingarray[2]; 85 padding.bottom = paddingarray[3]; 86 } else { 87 // load the bitmap directly. 88 bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density); 89 } 90 } catch (IOException e) { 91 Bridge.getLog().error(null,"Failed to load image" , e, null); 92 } 93 94 return bm; 95 } 96 97 @LayoutlibDelegate 98 /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd, 99 Rect padding, Options opts) { 100 opts.inBitmap = null; 101 return null; 102 } 103 104 @LayoutlibDelegate 105 /*package*/ static Bitmap nativeDecodeAsset(int asset, Rect padding, Options opts) { 106 opts.inBitmap = null; 107 return null; 108 } 109 110 @LayoutlibDelegate 111 /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset, 112 int length, Options opts) { 113 opts.inBitmap = null; 114 return null; 115 } 116 117 @LayoutlibDelegate 118 /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) { 119 return true; 120 } 121} 122