1 /* 2 * Copyright (C) 2010 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 17 package android.graphics; 18 19 import com.android.ide.common.rendering.api.LayoutLog; 20 import com.android.layoutlib.bridge.Bridge; 21 import com.android.layoutlib.bridge.impl.DelegateManager; 22 import com.android.ninepatch.NinePatchChunk; 23 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 24 25 import android.graphics.drawable.NinePatchDrawable; 26 27 import java.io.ByteArrayInputStream; 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 import java.lang.ref.SoftReference; 33 import java.util.HashMap; 34 import java.util.Map; 35 36 /** 37 * Delegate implementing the native methods of android.graphics.NinePatch 38 * 39 * Through the layoutlib_create tool, the original native methods of NinePatch have been replaced 40 * by calls to methods of the same name in this delegate class. 41 * 42 * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager} 43 * around to map int to instance of the delegate. 44 * 45 */ 46 public final class NinePatch_Delegate { 47 48 // ---- delegate manager ---- 49 private static final DelegateManager<NinePatch_Delegate> sManager = 50 new DelegateManager<>(NinePatch_Delegate.class); 51 52 // ---- delegate helper data ---- 53 /** 54 * Cache map for {@link NinePatchChunk}. 55 * When the chunks are created they are serialized into a byte[], and both are put 56 * in the cache, using a {@link SoftReference} for the chunk. The default Java classes 57 * for {@link NinePatch} and {@link NinePatchDrawable} only reference to the byte[] data, and 58 * provide this for drawing. 59 * Using the cache map allows us to not have to deserialize the byte[] back into a 60 * {@link NinePatchChunk} every time a rendering is done. 61 */ 62 private final static Map<byte[], SoftReference<NinePatchChunk>> sChunkCache = new HashMap<>(); 63 64 // ---- delegate data ---- 65 private byte[] chunk; 66 67 68 // ---- Public Helper methods ---- 69 70 /** 71 * Serializes the given chunk. 72 * 73 * @return the serialized data for the chunk. 74 */ serialize(NinePatchChunk chunk)75 public static byte[] serialize(NinePatchChunk chunk) { 76 // serialize the chunk to get a byte[] 77 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 78 ObjectOutputStream oos = null; 79 try { 80 oos = new ObjectOutputStream(baos); 81 oos.writeObject(chunk); 82 } catch (IOException e) { 83 Bridge.getLog().error(null, "Failed to serialize NinePatchChunk.", e, null /*data*/); 84 return null; 85 } finally { 86 if (oos != null) { 87 try { 88 oos.close(); 89 } catch (IOException ignored) { 90 } 91 } 92 } 93 94 // get the array and add it to the cache 95 byte[] array = baos.toByteArray(); 96 sChunkCache.put(array, new SoftReference<>(chunk)); 97 return array; 98 } 99 100 /** 101 * Returns a {@link NinePatchChunk} object for the given serialized representation. 102 * 103 * If the chunk is present in the cache then the object from the cache is returned, otherwise 104 * the array is deserialized into a {@link NinePatchChunk} object. 105 * 106 * @param array the serialized representation of the chunk. 107 * @return the NinePatchChunk or null if deserialization failed. 108 */ getChunk(byte[] array)109 public static NinePatchChunk getChunk(byte[] array) { 110 SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array); 111 NinePatchChunk chunk = chunkRef == null ? null : chunkRef.get(); 112 if (chunk == null) { 113 ByteArrayInputStream bais = new ByteArrayInputStream(array); 114 try (ObjectInputStream ois = new ObjectInputStream(bais)) { 115 chunk = (NinePatchChunk) ois.readObject(); 116 117 // put back the chunk in the cache 118 if (chunk != null) { 119 sChunkCache.put(array, new SoftReference<>(chunk)); 120 } 121 } catch (IOException e) { 122 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 123 "Failed to deserialize NinePatchChunk content.", e, null /*data*/); 124 return null; 125 } catch (ClassNotFoundException e) { 126 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 127 "Failed to deserialize NinePatchChunk class.", e, null /*data*/); 128 return null; 129 } 130 } 131 132 return chunk; 133 } 134 135 // ---- native methods ---- 136 137 @LayoutlibDelegate isNinePatchChunk(byte[] chunk)138 /*package*/ static boolean isNinePatchChunk(byte[] chunk) { 139 NinePatchChunk chunkObject = getChunk(chunk); 140 return chunkObject != null; 141 } 142 143 @LayoutlibDelegate validateNinePatchChunk(byte[] chunk)144 /*package*/ static long validateNinePatchChunk(byte[] chunk) { 145 // the default JNI implementation only checks that the byte[] has the same 146 // size as the C struct it represent. Since we cannot do the same check (serialization 147 // will return different size depending on content), we do nothing. 148 NinePatch_Delegate newDelegate = new NinePatch_Delegate(); 149 newDelegate.chunk = chunk; 150 return sManager.addNewDelegate(newDelegate); 151 } 152 153 @LayoutlibDelegate nativeFinalize(long nativeNinePatch)154 /*package*/ static void nativeFinalize(long nativeNinePatch) { 155 NinePatch_Delegate delegate = sManager.getDelegate(nativeNinePatch); 156 if (delegate != null && delegate.chunk != null) { 157 sChunkCache.remove(delegate.chunk); 158 } 159 sManager.removeJavaReferenceFor(nativeNinePatch); 160 } 161 162 163 @LayoutlibDelegate nativeGetTransparentRegion(long bitmapHandle, long chunk, Rect location)164 /*package*/ static long nativeGetTransparentRegion(long bitmapHandle, long chunk, 165 Rect location) { 166 return 0; 167 } 168 getChunk(long nativeNinePatch)169 static byte[] getChunk(long nativeNinePatch) { 170 NinePatch_Delegate delegate = sManager.getDelegate(nativeNinePatch); 171 if (delegate != null) { 172 return delegate.chunk; 173 } 174 return null; 175 } 176 clearCache()177 public static void clearCache() { 178 sChunkCache.clear(); 179 } 180 } 181