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
17package android.graphics;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.layoutlib.bridge.impl.DelegateManager;
22import com.android.ninepatch.NinePatchChunk;
23import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
24
25import android.graphics.drawable.NinePatchDrawable;
26
27import java.io.ByteArrayInputStream;
28import java.io.ByteArrayOutputStream;
29import java.io.IOException;
30import java.io.ObjectInputStream;
31import java.io.ObjectOutputStream;
32import java.lang.ref.SoftReference;
33import java.util.HashMap;
34import 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 */
46public 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     */
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     */
109    public static NinePatchChunk getChunk(byte[] array) {
110        SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array);
111        NinePatchChunk chunk = chunkRef.get();
112        if (chunk == null) {
113            ByteArrayInputStream bais = new ByteArrayInputStream(array);
114            ObjectInputStream ois = null;
115            try {
116                ois = new ObjectInputStream(bais);
117                chunk = (NinePatchChunk) ois.readObject();
118
119                // put back the chunk in the cache
120                if (chunk != null) {
121                    sChunkCache.put(array, new SoftReference<>(chunk));
122                }
123            } catch (IOException e) {
124                Bridge.getLog().error(LayoutLog.TAG_BROKEN,
125                        "Failed to deserialize NinePatchChunk content.", e, null /*data*/);
126                return null;
127            } catch (ClassNotFoundException e) {
128                Bridge.getLog().error(LayoutLog.TAG_BROKEN,
129                        "Failed to deserialize NinePatchChunk class.", e, null /*data*/);
130                return null;
131            } finally {
132                if (ois != null) {
133                    try {
134                        ois.close();
135                    } catch (IOException ignored) {
136                    }
137                }
138            }
139        }
140
141        return chunk;
142    }
143
144    // ---- native methods ----
145
146    @LayoutlibDelegate
147    /*package*/ static boolean isNinePatchChunk(byte[] chunk) {
148        NinePatchChunk chunkObject = getChunk(chunk);
149        return chunkObject != null;
150    }
151
152    @LayoutlibDelegate
153    /*package*/ static long validateNinePatchChunk(byte[] chunk) {
154        // the default JNI implementation only checks that the byte[] has the same
155        // size as the C struct it represent. Since we cannot do the same check (serialization
156        // will return different size depending on content), we do nothing.
157        NinePatch_Delegate newDelegate = new NinePatch_Delegate();
158        newDelegate.chunk = chunk;
159        return sManager.addNewDelegate(newDelegate);
160    }
161
162    @LayoutlibDelegate
163    /*package*/ static void nativeFinalize(long chunk) {
164        sManager.removeJavaReferenceFor(chunk);
165    }
166
167
168    @LayoutlibDelegate
169    /*package*/ static long nativeGetTransparentRegion(Bitmap bitmap, long chunk, Rect location) {
170        return 0;
171    }
172
173    static byte[] getChunk(long nativeNinePatch) {
174        NinePatch_Delegate delegate = sManager.getDelegate(nativeNinePatch);
175        if (delegate != null) {
176            return delegate.chunk;
177        }
178        return null;
179    }
180
181}
182