summaryrefslogtreecommitdiffstats
path: root/Ministro/jni/extract.cpp
blob: a125bfc268d25f7f9adc9689750ffe88d133eb77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
    Copyright (c) 2011, BogDan Vatra <bog_dan_ro@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <jni.h>
#include <android/log.h>
#include <extract.h>
#include <alloca.h>

#define LOG_TAG    "extractSyleInfo"
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define LOGF(...)  __android_log_print(ANDROID_LOG_FATAL,LOG_TAG,__VA_ARGS__)

const char * const NinePatchDrawableClassName = "android/graphics/drawable/NinePatchDrawable";
const char * const NinePatchFieldIDName = "mNinePatch";
static jfieldID m_ninePatchFieldID=0;

const char * const NinePatchClassName = "android/graphics/NinePatch";
const char * const ChunkFieldIDName = "mChunk";
static jfieldID m_chunkFieldID=0;

const char * const ClipStateClassName = "android/graphics/drawable/ClipDrawable$ClipState";
const char * const ClipStateDrawableFieldIDName = "mDrawable";
static jfieldID m_clipStateDrawableFieldID=0;

bool setup(JNIEnv * env, jobject /*obj*/)
{
    jclass ninePatchClass = env->FindClass(NinePatchClassName);
    if (!ninePatchClass)
    {
        LOGF("Unable to find class '%s'", NinePatchClassName);
        return JNI_FALSE;
    }

    m_chunkFieldID = env->GetFieldID(ninePatchClass, ChunkFieldIDName, "[B");
    if(!m_chunkFieldID)
    {
        LOGF("Unable to find field '%s'", ChunkFieldIDName);
        return JNI_FALSE;
    }

    jclass ninePatchDrawableClass = env->FindClass(NinePatchDrawableClassName);
    if (!ninePatchDrawableClass)
    {
        LOGF("Unable to find class '%s'", NinePatchDrawableClassName);
        return JNI_FALSE;
    }

    m_ninePatchFieldID = env->GetFieldID(ninePatchDrawableClass, NinePatchFieldIDName, "Landroid/graphics/NinePatch;");
    if(!m_ninePatchFieldID)
    {
        LOGF("Unable to find field '%s'", NinePatchFieldIDName);
        return JNI_FALSE;
    }

    jclass clipStateDrawableClass = env->FindClass(ClipStateClassName);
    if (!clipStateDrawableClass)
    {
        LOGF("Unable to find class '%s'", ClipStateClassName);
        return JNI_FALSE;
    }

    m_clipStateDrawableFieldID = env->GetFieldID(clipStateDrawableClass, ClipStateDrawableFieldIDName, "Landroid/graphics/drawable/Drawable;");
    if(!m_ninePatchFieldID)
    {
        LOGF("Unable to find field '%s'", NinePatchFieldIDName);
        return JNI_FALSE;
    }

    return JNI_TRUE;
}

static void printChunkInformation(Res_png_9patch* chunk)
{
    LOGI("printChunkInformation x:%d , y:%d",chunk->numXDivs, chunk->numYDivs);
    for (int x = 0; x < chunk->numXDivs; x ++)
        LOGI("X CHUNK %d %d", x, chunk->xDivs[x]);
    for (int y = 0; y < chunk->numYDivs; y ++)
        LOGI("Y CHUNK %d %d", y, chunk->yDivs[y]);
    LOGI("----");
}

extern "C" JNIEXPORT jintArray JNICALL Java_org_kde_necessitas_ministro_ExtractStyle_extractChunkInfo(JNIEnv * env, jobject  obj, jbyteArray chunkObj)
{
        size_t chunkSize = env->GetArrayLength(chunkObj);
        void* storage = alloca(chunkSize);
        env->GetByteArrayRegion(chunkObj, 0, chunkSize,
                                reinterpret_cast<jbyte*>(storage));

        if (!env->ExceptionCheck())
        {
            // need to deserialize the chunk
            Res_png_9patch* chunk = static_cast<Res_png_9patch*>(storage);
            Res_png_9patch::deserialize(chunk);
//            printChunkInformation(chunk);
            jintArray result;
            size_t size = 3+chunk->numXDivs+chunk->numYDivs+chunk->numColors;
            result = env->NewIntArray(size);
            if (!result)
                return 0;

            jint *data = (jint*)malloc(sizeof(jint)*size);
            size_t pos = 0;
            data[pos++]=chunk->numXDivs;
            data[pos++]=chunk->numYDivs;
            data[pos++]=chunk->numColors;
            for (int x = 0; x <chunk->numXDivs; x ++)
                data[pos++]=chunk->xDivs[x];
            for (int y = 0; y <chunk->numYDivs; y ++)
                data[pos++]=chunk->yDivs[y];
            for (int c = 0; c <chunk->numColors; c ++)
                data[pos++]=chunk->colors[c];
            env->SetIntArrayRegion(result, 0, size, data);
            free(data);
            return result;
        }
        return 0;
}

extern "C" JNIEXPORT jintArray JNICALL Java_org_kde_necessitas_ministro_ExtractStyle_extract9PatchInfo(JNIEnv * env, jobject obj, jobject ninePatchObject)
{
    if (!m_ninePatchFieldID || !m_chunkFieldID)
        if (!setup(env, obj))
            return 0;
    return Java_org_kde_necessitas_ministro_ExtractStyle_extractChunkInfo(env, obj
                                                                                ,reinterpret_cast<jbyteArray>(env->GetObjectField(
                                                                                        env->GetObjectField(ninePatchObject, m_ninePatchFieldID)
                                                                                        , m_chunkFieldID)) );
}

extern "C" JNIEXPORT jobject JNICALL Java_org_kde_necessitas_ministro_ExtractStyle_getClipStateDrawableObject(JNIEnv * env, jobject obj, jobject clipStateObject)
{
    if (!m_ninePatchFieldID || !m_chunkFieldID || !m_clipStateDrawableFieldID)
        if (!setup(env, obj))
            return 0;
    return env->GetObjectField(clipStateObject, m_clipStateDrawableFieldID);
}

// The following part was shamelessly stolen from ResourceTypes.cpp Android's sources
static void deserializeInternal(const void* inData, Res_png_9patch* outData) {
    char* patch = (char*) inData;
    if (inData != outData) {
        memmove(&outData->wasDeserialized, patch, 4);     // copy  wasDeserialized, numXDivs, numYDivs, numColors
        memmove(&outData->paddingLeft, patch + 12, 4);     // copy  wasDeserialized, numXDivs, numYDivs, numColors
    }
    outData->wasDeserialized = true;
    char* data = (char*)outData;
    data +=  sizeof(Res_png_9patch);
    outData->xDivs = (int32_t*) data;
    data +=  outData->numXDivs * sizeof(int32_t);
    outData->yDivs = (int32_t*) data;
    data +=  outData->numYDivs * sizeof(int32_t);
    outData->colors = (uint32_t*) data;
}

Res_png_9patch* Res_png_9patch::deserialize(const void* inData)
{
    if (sizeof(void*) != sizeof(int32_t)) {
        LOGE("Cannot deserialize on non 32-bit system\n");
        return NULL;
    }
    deserializeInternal(inData, (Res_png_9patch*) inData);
    return (Res_png_9patch*) inData;
}