• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.camera.ui;
2 
3 import android.graphics.Rect;
4 
5 import java.nio.ByteBuffer;
6 import java.nio.ByteOrder;
7 
8 // See "frameworks/base/include/utils/ResourceTypes.h" for the format of
9 // NinePatch chunk.
10 class NinePatchChunk {
11 
12     public static final int NO_COLOR = 0x00000001;
13     public static final int TRANSPARENT_COLOR = 0x00000000;
14 
15     public Rect mPaddings = new Rect();
16 
17     public int mDivX[];
18     public int mDivY[];
19     public int mColor[];
20 
readIntArray(int[] data, ByteBuffer buffer)21     private static void readIntArray(int[] data, ByteBuffer buffer) {
22         for (int i = 0, n = data.length; i < n; ++i) {
23             data[i] = buffer.getInt();
24         }
25     }
26 
checkDivCount(int length)27     private static void checkDivCount(int length) {
28         if (length == 0 || (length & 0x01) != 0) {
29             throw new RuntimeException("invalid nine-patch: " + length);
30         }
31     }
32 
deserialize(byte[] data)33     public static NinePatchChunk deserialize(byte[] data) {
34         ByteBuffer byteBuffer =
35                 ByteBuffer.wrap(data).order(ByteOrder.nativeOrder());
36 
37         byte wasSerialized = byteBuffer.get();
38         if (wasSerialized == 0) return null;
39 
40         NinePatchChunk chunk = new NinePatchChunk();
41         chunk.mDivX = new int[byteBuffer.get()];
42         chunk.mDivY = new int[byteBuffer.get()];
43         chunk.mColor = new int[byteBuffer.get()];
44 
45         checkDivCount(chunk.mDivX.length);
46         checkDivCount(chunk.mDivY.length);
47 
48         // skip 8 bytes
49         byteBuffer.getInt();
50         byteBuffer.getInt();
51 
52         chunk.mPaddings.left = byteBuffer.getInt();
53         chunk.mPaddings.right = byteBuffer.getInt();
54         chunk.mPaddings.top = byteBuffer.getInt();
55         chunk.mPaddings.bottom = byteBuffer.getInt();
56 
57         // skip 4 bytes
58         byteBuffer.getInt();
59 
60         readIntArray(chunk.mDivX, byteBuffer);
61         readIntArray(chunk.mDivY, byteBuffer);
62         readIntArray(chunk.mColor, byteBuffer);
63 
64         return chunk;
65     }
66 }