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