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,
84                     null /*data*/);
85             return null;
86         } finally {
87             if (oos != null) {
88                 try {
89                     oos.close();
90                 } catch (IOException ignored) {
91                 }
92             }
93         }
94 
95         // get the array and add it to the cache
96         byte[] array = baos.toByteArray();
97         sChunkCache.put(array, new SoftReference<>(chunk));
98         return array;
99     }
100 
101     /**
102      * Returns a {@link NinePatchChunk} object for the given serialized representation.
103      *
104      * If the chunk is present in the cache then the object from the cache is returned, otherwise
105      * the array is deserialized into a {@link NinePatchChunk} object.
106      *
107      * @param array the serialized representation of the chunk.
108      * @return the NinePatchChunk or null if deserialization failed.
109      */
getChunk(byte[] array)110     public static NinePatchChunk getChunk(byte[] array) {
111         SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array);
112         NinePatchChunk chunk = chunkRef == null ? null : chunkRef.get();
113         if (chunk == null) {
114             ByteArrayInputStream bais = new ByteArrayInputStream(array);
115             try (ObjectInputStream ois = new ObjectInputStream(bais)) {
116                 chunk = (NinePatchChunk) ois.readObject();
117 
118                 // put back the chunk in the cache
119                 if (chunk != null) {
120                     sChunkCache.put(array, new SoftReference<>(chunk));
121                 }
122             } catch (IOException e) {
123                 Bridge.getLog().error(LayoutLog.TAG_BROKEN,
124                         "Failed to deserialize NinePatchChunk content.", e, null, null /*data*/);
125                 return null;
126             } catch (ClassNotFoundException e) {
127                 Bridge.getLog().error(LayoutLog.TAG_BROKEN,
128                         "Failed to deserialize NinePatchChunk class.", e, null, null /*data*/);
129                 return null;
130             }
131         }
132 
133         return chunk;
134     }
135 
136     // ---- native methods ----
137 
138     @LayoutlibDelegate
isNinePatchChunk(byte[] chunk)139     /*package*/ static boolean isNinePatchChunk(byte[] chunk) {
140         NinePatchChunk chunkObject = getChunk(chunk);
141         return chunkObject != null;
142     }
143 
144     @LayoutlibDelegate
validateNinePatchChunk(byte[] chunk)145     /*package*/ static long validateNinePatchChunk(byte[] chunk) {
146         // the default JNI implementation only checks that the byte[] has the same
147         // size as the C struct it represent. Since we cannot do the same check (serialization
148         // will return different size depending on content), we do nothing.
149         NinePatch_Delegate newDelegate = new NinePatch_Delegate();
150         newDelegate.chunk = chunk;
151         return sManager.addNewDelegate(newDelegate);
152     }
153 
154     @LayoutlibDelegate
nativeFinalize(long nativeNinePatch)155     /*package*/ static void nativeFinalize(long nativeNinePatch) {
156         NinePatch_Delegate delegate = sManager.getDelegate(nativeNinePatch);
157         if (delegate != null && delegate.chunk != null) {
158             sChunkCache.remove(delegate.chunk);
159         }
160         sManager.removeJavaReferenceFor(nativeNinePatch);
161     }
162 
163 
164     @LayoutlibDelegate
nativeGetTransparentRegion(long bitmapHandle, long chunk, Rect location)165     /*package*/ static long nativeGetTransparentRegion(long bitmapHandle, long chunk,
166             Rect location) {
167         return 0;
168     }
169 
getChunk(long nativeNinePatch)170     static byte[] getChunk(long nativeNinePatch) {
171         NinePatch_Delegate delegate = sManager.getDelegate(nativeNinePatch);
172         if (delegate != null) {
173             return delegate.chunk;
174         }
175         return null;
176     }
177 
clearCache()178     public static void clearCache() {
179         sChunkCache.clear();
180     }
181 }
182