-
Notifications
You must be signed in to change notification settings - Fork 56
/
android_util_AssetManager.cpp
2118 lines (1798 loc) · 72.3 KB
/
android_util_AssetManager.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* //device/libs/android_runtime/android_util_AssetManager.cpp
**
** Copyright 2006, The Android Open Source Project
** This code has been modified. Portions copyright (C) 2010, T-Mobile USA, Inc.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define LOG_TAG "asset"
#define DEBUG_STYLES(x) //x
#define THROW_ON_BAD_ID 0
#include <android_runtime/android_util_AssetManager.h>
#include "jni.h"
#include "JNIHelp.h"
#include "ScopedStringChars.h"
#include "ScopedUtfChars.h"
#include "android_util_Binder.h"
#include <utils/misc.h>
#include <android_runtime/AndroidRuntime.h>
#include <utils/Log.h>
#include <androidfw/Asset.h>
#include <androidfw/AssetManager.h>
#include <androidfw/ResourceTypes.h>
#include <androidfw/PackageRedirectionMap.h>
#include <androidfw/ZipFile.h>
#include <stdio.h>
#define REDIRECT_NOISY(x) //x
namespace android {
// ----------------------------------------------------------------------------
static struct typedvalue_offsets_t
{
jfieldID mType;
jfieldID mData;
jfieldID mString;
jfieldID mAssetCookie;
jfieldID mResourceId;
jfieldID mChangingConfigurations;
jfieldID mDensity;
} gTypedValueOffsets;
static struct assetfiledescriptor_offsets_t
{
jfieldID mFd;
jfieldID mStartOffset;
jfieldID mLength;
} gAssetFileDescriptorOffsets;
static struct assetmanager_offsets_t
{
jfieldID mObject;
} gAssetManagerOffsets;
jclass g_stringClass = NULL;
// ----------------------------------------------------------------------------
enum {
STYLE_NUM_ENTRIES = 6,
STYLE_TYPE = 0,
STYLE_DATA = 1,
STYLE_ASSET_COOKIE = 2,
STYLE_RESOURCE_ID = 3,
STYLE_CHANGING_CONFIGURATIONS = 4,
STYLE_DENSITY = 5
};
static jint copyValue(JNIEnv* env, jobject outValue, const ResTable* table,
const Res_value& value, uint32_t ref, ssize_t block,
uint32_t typeSpecFlags, ResTable_config* config = NULL);
jint copyValue(JNIEnv* env, jobject outValue, const ResTable* table,
const Res_value& value, uint32_t ref, ssize_t block,
uint32_t typeSpecFlags, ResTable_config* config)
{
env->SetIntField(outValue, gTypedValueOffsets.mType, value.dataType);
env->SetIntField(outValue, gTypedValueOffsets.mAssetCookie,
(jint)table->getTableCookie(block));
env->SetIntField(outValue, gTypedValueOffsets.mData, value.data);
env->SetObjectField(outValue, gTypedValueOffsets.mString, NULL);
env->SetIntField(outValue, gTypedValueOffsets.mResourceId, ref);
env->SetIntField(outValue, gTypedValueOffsets.mChangingConfigurations,
typeSpecFlags);
if (config != NULL) {
env->SetIntField(outValue, gTypedValueOffsets.mDensity, config->density);
}
return block;
}
// ----------------------------------------------------------------------------
// this guy is exported to other jni routines
AssetManager* assetManagerForJavaObject(JNIEnv* env, jobject obj)
{
AssetManager* am = (AssetManager*)env->GetIntField(obj, gAssetManagerOffsets.mObject);
if (am != NULL) {
return am;
}
jniThrowException(env, "java/lang/IllegalStateException", "AssetManager has been finalized!");
return NULL;
}
static jint android_content_AssetManager_openAsset(JNIEnv* env, jobject clazz,
jstring fileName, jint mode)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
ALOGV("openAsset in %p (Java object %p)\n", am, clazz);
ScopedUtfChars fileName8(env, fileName);
if (fileName8.c_str() == NULL) {
return -1;
}
if (mode != Asset::ACCESS_UNKNOWN && mode != Asset::ACCESS_RANDOM
&& mode != Asset::ACCESS_STREAMING && mode != Asset::ACCESS_BUFFER) {
jniThrowException(env, "java/lang/IllegalArgumentException", "Bad access mode");
return -1;
}
Asset* a = am->open(fileName8.c_str(), (Asset::AccessMode)mode);
if (a == NULL) {
jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
return -1;
}
//printf("Created Asset Stream: %p\n", a);
return (jint)a;
}
static jobject returnParcelFileDescriptor(JNIEnv* env, Asset* a, jlongArray outOffsets)
{
off64_t startOffset, length;
int fd = a->openFileDescriptor(&startOffset, &length);
delete a;
if (fd < 0) {
jniThrowException(env, "java/io/FileNotFoundException",
"This file can not be opened as a file descriptor; it is probably compressed");
return NULL;
}
jlong* offsets = (jlong*)env->GetPrimitiveArrayCritical(outOffsets, 0);
if (offsets == NULL) {
close(fd);
return NULL;
}
offsets[0] = startOffset;
offsets[1] = length;
env->ReleasePrimitiveArrayCritical(outOffsets, offsets, 0);
jobject fileDesc = jniCreateFileDescriptor(env, fd);
if (fileDesc == NULL) {
close(fd);
return NULL;
}
return newParcelFileDescriptor(env, fileDesc);
}
static jobject android_content_AssetManager_openAssetFd(JNIEnv* env, jobject clazz,
jstring fileName, jlongArray outOffsets)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ALOGV("openAssetFd in %p (Java object %p)\n", am, clazz);
ScopedUtfChars fileName8(env, fileName);
if (fileName8.c_str() == NULL) {
return NULL;
}
Asset* a = am->open(fileName8.c_str(), Asset::ACCESS_RANDOM);
if (a == NULL) {
jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
return NULL;
}
//printf("Created Asset Stream: %p\n", a);
return returnParcelFileDescriptor(env, a, outOffsets);
}
static jint android_content_AssetManager_openNonAssetNative(JNIEnv* env, jobject clazz,
jint cookie,
jstring fileName,
jint mode)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
ALOGV("openNonAssetNative in %p (Java object %p)\n", am, clazz);
ScopedUtfChars fileName8(env, fileName);
if (fileName8.c_str() == NULL) {
return -1;
}
if (mode != Asset::ACCESS_UNKNOWN && mode != Asset::ACCESS_RANDOM
&& mode != Asset::ACCESS_STREAMING && mode != Asset::ACCESS_BUFFER) {
jniThrowException(env, "java/lang/IllegalArgumentException", "Bad access mode");
return -1;
}
Asset* a = cookie
? am->openNonAsset((void*)cookie, fileName8.c_str(), (Asset::AccessMode)mode)
: am->openNonAsset(fileName8.c_str(), (Asset::AccessMode)mode);
if (a == NULL) {
jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
return -1;
}
//printf("Created Asset Stream: %p\n", a);
return (jint)a;
}
static jobject android_content_AssetManager_openNonAssetFdNative(JNIEnv* env, jobject clazz,
jint cookie,
jstring fileName,
jlongArray outOffsets)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ALOGV("openNonAssetFd in %p (Java object %p)\n", am, clazz);
ScopedUtfChars fileName8(env, fileName);
if (fileName8.c_str() == NULL) {
return NULL;
}
Asset* a = cookie
? am->openNonAsset((void*)cookie, fileName8.c_str(), Asset::ACCESS_RANDOM)
: am->openNonAsset(fileName8.c_str(), Asset::ACCESS_RANDOM);
if (a == NULL) {
jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
return NULL;
}
//printf("Created Asset Stream: %p\n", a);
return returnParcelFileDescriptor(env, a, outOffsets);
}
static jobjectArray android_content_AssetManager_list(JNIEnv* env, jobject clazz,
jstring fileName)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ScopedUtfChars fileName8(env, fileName);
if (fileName8.c_str() == NULL) {
return NULL;
}
AssetDir* dir = am->openDir(fileName8.c_str());
if (dir == NULL) {
jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
return NULL;
}
jclass cls = env->FindClass("java/lang/String");
LOG_FATAL_IF(cls == NULL, "No string class?!?");
if (cls == NULL) {
delete dir;
return NULL;
}
size_t N = dir->getFileCount();
jobjectArray array = env->NewObjectArray(dir->getFileCount(),
cls, NULL);
if (array == NULL) {
delete dir;
return NULL;
}
for (size_t i=0; i<N; i++) {
const String8& name = dir->getFileName(i);
jstring str = env->NewStringUTF(name.string());
if (str == NULL) {
delete dir;
return NULL;
}
env->SetObjectArrayElement(array, i, str);
env->DeleteLocalRef(str);
}
delete dir;
return array;
}
static void android_content_AssetManager_destroyAsset(JNIEnv* env, jobject clazz,
jint asset)
{
Asset* a = (Asset*)asset;
//printf("Destroying Asset Stream: %p\n", a);
if (a == NULL) {
jniThrowNullPointerException(env, "asset");
return;
}
delete a;
}
static jint android_content_AssetManager_readAssetChar(JNIEnv* env, jobject clazz,
jint asset)
{
Asset* a = (Asset*)asset;
if (a == NULL) {
jniThrowNullPointerException(env, "asset");
return -1;
}
uint8_t b;
ssize_t res = a->read(&b, 1);
return res == 1 ? b : -1;
}
static jint android_content_AssetManager_readAsset(JNIEnv* env, jobject clazz,
jint asset, jbyteArray bArray,
jint off, jint len)
{
Asset* a = (Asset*)asset;
if (a == NULL || bArray == NULL) {
jniThrowNullPointerException(env, "asset");
return -1;
}
if (len == 0) {
return 0;
}
jsize bLen = env->GetArrayLength(bArray);
if (off < 0 || off >= bLen || len < 0 || len > bLen || (off+len) > bLen) {
jniThrowException(env, "java/lang/IndexOutOfBoundsException", "");
return -1;
}
jbyte* b = env->GetByteArrayElements(bArray, NULL);
ssize_t res = a->read(b+off, len);
env->ReleaseByteArrayElements(bArray, b, 0);
if (res > 0) return res;
if (res < 0) {
jniThrowException(env, "java/io/IOException", "");
}
return -1;
}
static jlong android_content_AssetManager_seekAsset(JNIEnv* env, jobject clazz,
jint asset,
jlong offset, jint whence)
{
Asset* a = (Asset*)asset;
if (a == NULL) {
jniThrowNullPointerException(env, "asset");
return -1;
}
return a->seek(
offset, (whence > 0) ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR));
}
static jlong android_content_AssetManager_getAssetLength(JNIEnv* env, jobject clazz,
jint asset)
{
Asset* a = (Asset*)asset;
if (a == NULL) {
jniThrowNullPointerException(env, "asset");
return -1;
}
return a->getLength();
}
static jlong android_content_AssetManager_getAssetRemainingLength(JNIEnv* env, jobject clazz,
jint asset)
{
Asset* a = (Asset*)asset;
if (a == NULL) {
jniThrowNullPointerException(env, "asset");
return -1;
}
return a->getRemainingLength();
}
static jint android_content_AssetManager_addAssetPath(JNIEnv* env, jobject clazz,
jstring path)
{
ScopedUtfChars path8(env, path);
if (path8.c_str() == NULL) {
return 0;
}
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
void* cookie;
bool res = am->addAssetPath(String8(path8.c_str()), &cookie);
return (res) ? (jint)cookie : 0;
}
static jboolean android_content_AssetManager_isUpToDate(JNIEnv* env, jobject clazz)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return JNI_TRUE;
}
return am->isUpToDate() ? JNI_TRUE : JNI_FALSE;
}
static void android_content_AssetManager_setLocale(JNIEnv* env, jobject clazz,
jstring locale)
{
ScopedUtfChars locale8(env, locale);
if (locale8.c_str() == NULL) {
return;
}
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return;
}
am->setLocale(locale8.c_str());
}
static jobjectArray android_content_AssetManager_getLocales(JNIEnv* env, jobject clazz)
{
Vector<String8> locales;
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
am->getLocales(&locales);
const int N = locales.size();
jobjectArray result = env->NewObjectArray(N, g_stringClass, NULL);
if (result == NULL) {
return NULL;
}
for (int i=0; i<N; i++) {
jstring str = env->NewStringUTF(locales[i].string());
if (str == NULL) {
return NULL;
}
env->SetObjectArrayElement(result, i, str);
env->DeleteLocalRef(str);
}
return result;
}
static void android_content_AssetManager_setConfiguration(JNIEnv* env, jobject clazz,
jint mcc, jint mnc,
jstring locale, jint orientation,
jint touchscreen, jint density,
jint keyboard, jint keyboardHidden,
jint navigation,
jint screenWidth, jint screenHeight,
jint smallestScreenWidthDp,
jint screenWidthDp, jint screenHeightDp,
jint screenLayout, jint uiMode,
jint sdkVersion)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return;
}
ResTable_config config;
memset(&config, 0, sizeof(config));
const char* locale8 = locale != NULL ? env->GetStringUTFChars(locale, NULL) : NULL;
config.mcc = (uint16_t)mcc;
config.mnc = (uint16_t)mnc;
config.orientation = (uint8_t)orientation;
config.touchscreen = (uint8_t)touchscreen;
config.density = (uint16_t)density;
config.keyboard = (uint8_t)keyboard;
config.inputFlags = (uint8_t)keyboardHidden;
config.navigation = (uint8_t)navigation;
config.screenWidth = (uint16_t)screenWidth;
config.screenHeight = (uint16_t)screenHeight;
config.smallestScreenWidthDp = (uint16_t)smallestScreenWidthDp;
config.screenWidthDp = (uint16_t)screenWidthDp;
config.screenHeightDp = (uint16_t)screenHeightDp;
config.screenLayout = (uint8_t)screenLayout;
config.uiMode = (uint8_t)uiMode;
config.sdkVersion = (uint16_t)sdkVersion;
config.minorVersion = 0;
am->setConfiguration(config, locale8);
if (locale != NULL) env->ReleaseStringUTFChars(locale, locale8);
}
static jint android_content_AssetManager_getResourceIdentifier(JNIEnv* env, jobject clazz,
jstring name,
jstring defType,
jstring defPackage)
{
ScopedStringChars name16(env, name);
if (name16.get() == NULL) {
return 0;
}
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
const char16_t* defType16 = defType
? env->GetStringChars(defType, NULL) : NULL;
jsize defTypeLen = defType
? env->GetStringLength(defType) : 0;
const char16_t* defPackage16 = defPackage
? env->GetStringChars(defPackage, NULL) : NULL;
jsize defPackageLen = defPackage
? env->GetStringLength(defPackage) : 0;
jint ident = am->getResources().identifierForName(
name16.get(), name16.size(), defType16, defTypeLen, defPackage16, defPackageLen);
if (defPackage16) {
env->ReleaseStringChars(defPackage, defPackage16);
}
if (defType16) {
env->ReleaseStringChars(defType, defType16);
}
return ident;
}
static jstring android_content_AssetManager_getResourceName(JNIEnv* env, jobject clazz,
jint resid)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ResTable::resource_name name;
if (!am->getResources().getResourceName(resid, &name)) {
return NULL;
}
String16 str;
if (name.package != NULL) {
str.setTo(name.package, name.packageLen);
}
if (name.type != NULL) {
if (str.size() > 0) {
char16_t div = ':';
str.append(&div, 1);
}
str.append(name.type, name.typeLen);
}
if (name.name != NULL) {
if (str.size() > 0) {
char16_t div = '/';
str.append(&div, 1);
}
str.append(name.name, name.nameLen);
}
return env->NewString((const jchar*)str.string(), str.size());
}
static jstring android_content_AssetManager_getResourcePackageName(JNIEnv* env, jobject clazz,
jint resid)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ResTable::resource_name name;
if (!am->getResources().getResourceName(resid, &name)) {
return NULL;
}
if (name.package != NULL) {
return env->NewString((const jchar*)name.package, name.packageLen);
}
return NULL;
}
static jstring android_content_AssetManager_getResourceTypeName(JNIEnv* env, jobject clazz,
jint resid)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ResTable::resource_name name;
if (!am->getResources().getResourceName(resid, &name)) {
return NULL;
}
if (name.type != NULL) {
return env->NewString((const jchar*)name.type, name.typeLen);
}
return NULL;
}
static jstring android_content_AssetManager_getResourceEntryName(JNIEnv* env, jobject clazz,
jint resid)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
ResTable::resource_name name;
if (!am->getResources().getResourceName(resid, &name)) {
return NULL;
}
if (name.name != NULL) {
return env->NewString((const jchar*)name.name, name.nameLen);
}
return NULL;
}
static jint android_content_AssetManager_loadResourceValue(JNIEnv* env, jobject clazz,
jint ident,
jshort density,
jobject outValue,
jboolean resolve)
{
if (outValue == NULL) {
jniThrowNullPointerException(env, "outValue");
return NULL;
}
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
const ResTable& res(am->getResources());
uint32_t ref = res.lookupRedirectionMap(ident);
if (ref == 0) {
ref = ident;
} else {
REDIRECT_NOISY(ALOGW("PERFORMED REDIRECT OF ident=0x%08x FOR ref=0x%08x\n", ident, ref));
}
Res_value value;
ResTable_config config;
uint32_t typeSpecFlags;
ssize_t block = res.getResource(ref, &value, false, density, &typeSpecFlags, &config);
#if THROW_ON_BAD_ID
if (block == BAD_INDEX) {
jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!");
return 0;
}
#endif
if (resolve) {
block = res.resolveReference(&value, block, &ref, &typeSpecFlags, &config);
#if THROW_ON_BAD_ID
if (block == BAD_INDEX) {
jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!");
return 0;
}
#endif
}
return block >= 0 ? copyValue(env, outValue, &res, value, ref, block, typeSpecFlags, &config) : block;
}
static jint android_content_AssetManager_loadResourceBagValue(JNIEnv* env, jobject clazz,
jint ident, jint bagEntryId,
jobject outValue, jboolean resolve)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
const ResTable& res(am->getResources());
// Now lock down the resource object and start pulling stuff from it.
res.lock();
ssize_t block = -1;
Res_value value;
const ResTable::bag_entry* entry = NULL;
uint32_t typeSpecFlags;
ssize_t entryCount = res.getBagLocked(ident, &entry, &typeSpecFlags);
for (ssize_t i=0; i<entryCount; i++) {
if (((uint32_t)bagEntryId) == entry->map.name.ident) {
block = entry->stringBlock;
value = entry->map.value;
}
entry++;
}
res.unlock();
if (block < 0) {
return block;
}
uint32_t ref = ident;
if (resolve) {
block = res.resolveReference(&value, block, &ref, &typeSpecFlags);
#if THROW_ON_BAD_ID
if (block == BAD_INDEX) {
jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!");
return 0;
}
#endif
}
return block >= 0 ? copyValue(env, outValue, &res, value, ref, block, typeSpecFlags) : block;
}
static jint android_content_AssetManager_getStringBlockCount(JNIEnv* env, jobject clazz)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
return am->getResources().getTableCount();
}
static jint android_content_AssetManager_getNativeStringBlock(JNIEnv* env, jobject clazz,
jint block)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
return (jint)am->getResources().getTableStringBlock(block);
}
static jstring android_content_AssetManager_getCookieName(JNIEnv* env, jobject clazz,
jint cookie)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return NULL;
}
String8 name(am->getAssetPath((void*)cookie));
if (name.length() == 0) {
jniThrowException(env, "java/lang/IndexOutOfBoundsException", "Empty cookie name");
return NULL;
}
jstring str = env->NewStringUTF(name.string());
return str;
}
static jint android_content_AssetManager_newTheme(JNIEnv* env, jobject clazz)
{
AssetManager* am = assetManagerForJavaObject(env, clazz);
if (am == NULL) {
return 0;
}
return (jint)(new ResTable::Theme(am->getResources()));
}
static void android_content_AssetManager_deleteTheme(JNIEnv* env, jobject clazz,
jint themeInt)
{
ResTable::Theme* theme = (ResTable::Theme*)themeInt;
delete theme;
}
static void android_content_AssetManager_applyThemeStyle(JNIEnv* env, jobject clazz,
jint themeInt,
jint styleRes,
jboolean force)
{
ResTable::Theme* theme = (ResTable::Theme*)themeInt;
theme->applyStyle(styleRes, force ? true : false);
}
static void android_content_AssetManager_copyTheme(JNIEnv* env, jobject clazz,
jint destInt, jint srcInt)
{
ResTable::Theme* dest = (ResTable::Theme*)destInt;
ResTable::Theme* src = (ResTable::Theme*)srcInt;
dest->setTo(*src);
}
static jint android_content_AssetManager_loadThemeAttributeValue(
JNIEnv* env, jobject clazz, jint themeInt, jint ident, jobject outValue, jboolean resolve)
{
ResTable::Theme* theme = (ResTable::Theme*)themeInt;
const ResTable& res(theme->getResTable());
Res_value value;
// XXX value could be different in different configs!
uint32_t typeSpecFlags = 0;
ssize_t block = theme->getAttribute(ident, &value, &typeSpecFlags);
uint32_t ref = 0;
if (resolve) {
block = res.resolveReference(&value, block, &ref, &typeSpecFlags);
#if THROW_ON_BAD_ID
if (block == BAD_INDEX) {
jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!");
return 0;
}
#endif
}
return block >= 0 ? copyValue(env, outValue, &res, value, ref, block, typeSpecFlags) : block;
}
static void android_content_AssetManager_dumpTheme(JNIEnv* env, jobject clazz,
jint themeInt, jint pri,
jstring tag, jstring prefix)
{
ResTable::Theme* theme = (ResTable::Theme*)themeInt;
const ResTable& res(theme->getResTable());
// XXX Need to use params.
theme->dumpToLog();
}
static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject clazz,
jint themeToken,
jint defStyleAttr,
jint defStyleRes,
jint xmlParserToken,
jintArray attrs,
jintArray outValues,
jintArray outIndices)
{
if (themeToken == 0) {
jniThrowNullPointerException(env, "theme token");
return JNI_FALSE;
}
if (attrs == NULL) {
jniThrowNullPointerException(env, "attrs");
return JNI_FALSE;
}
if (outValues == NULL) {
jniThrowNullPointerException(env, "out values");
return JNI_FALSE;
}
DEBUG_STYLES(ALOGI("APPLY STYLE: theme=0x%x defStyleAttr=0x%x defStyleRes=0x%x xml=0x%x",
themeToken, defStyleAttr, defStyleRes, xmlParserToken));
ResTable::Theme* theme = (ResTable::Theme*)themeToken;
const ResTable& res = theme->getResTable();
ResXMLParser* xmlParser = (ResXMLParser*)xmlParserToken;
ResTable_config config;
Res_value value;
const jsize NI = env->GetArrayLength(attrs);
const jsize NV = env->GetArrayLength(outValues);
if (NV < (NI*STYLE_NUM_ENTRIES)) {
jniThrowException(env, "java/lang/IndexOutOfBoundsException", "out values too small");
return JNI_FALSE;
}
jint* src = (jint*)env->GetPrimitiveArrayCritical(attrs, 0);
if (src == NULL) {
return JNI_FALSE;
}
jint* baseDest = (jint*)env->GetPrimitiveArrayCritical(outValues, 0);
jint* dest = baseDest;
if (dest == NULL) {
env->ReleasePrimitiveArrayCritical(attrs, src, 0);
return JNI_FALSE;
}
jint* indices = NULL;
int indicesIdx = 0;
if (outIndices != NULL) {
if (env->GetArrayLength(outIndices) > NI) {
indices = (jint*)env->GetPrimitiveArrayCritical(outIndices, 0);
}
}
// Load default style from attribute, if specified...
uint32_t defStyleBagTypeSetFlags = 0;
if (defStyleAttr != 0) {
Res_value value;
if (theme->getAttribute(defStyleAttr, &value, &defStyleBagTypeSetFlags) >= 0) {
if (value.dataType == Res_value::TYPE_REFERENCE) {
defStyleRes = value.data;
}
}
}
// Retrieve the style class associated with the current XML tag.
int style = 0;
uint32_t styleBagTypeSetFlags = 0;
if (xmlParser != NULL) {
ssize_t idx = xmlParser->indexOfStyle();
if (idx >= 0 && xmlParser->getAttributeValue(idx, &value) >= 0) {
if (value.dataType == value.TYPE_ATTRIBUTE) {
if (theme->getAttribute(value.data, &value, &styleBagTypeSetFlags) < 0) {
value.dataType = Res_value::TYPE_NULL;
}
}
if (value.dataType == value.TYPE_REFERENCE) {
style = value.data;
}
}
}
// Now lock down the resource object and start pulling stuff from it.
res.lock();
// Apply theme redirections to the referenced styles.
if (defStyleRes != 0) {
uint32_t ref = res.lookupRedirectionMap(defStyleRes);
if (ref != 0) {
defStyleRes = ref;
}
}
if (style != 0) {
uint32_t ref = res.lookupRedirectionMap(style);
if (ref != 0) {
style = ref;
}
}
// Retrieve the default style bag, if requested.
const ResTable::bag_entry* defStyleEnt = NULL;
uint32_t defStyleTypeSetFlags = 0;
ssize_t bagOff = defStyleRes != 0
? res.getBagLocked(defStyleRes, &defStyleEnt, &defStyleTypeSetFlags) : -1;
defStyleTypeSetFlags |= defStyleBagTypeSetFlags;
const ResTable::bag_entry* endDefStyleEnt = defStyleEnt +
(bagOff >= 0 ? bagOff : 0);
// Retrieve the style class bag, if requested.
const ResTable::bag_entry* styleEnt = NULL;
uint32_t styleTypeSetFlags = 0;
bagOff = style != 0 ? res.getBagLocked(style, &styleEnt, &styleTypeSetFlags) : -1;
styleTypeSetFlags |= styleBagTypeSetFlags;
const ResTable::bag_entry* endStyleEnt = styleEnt +
(bagOff >= 0 ? bagOff : 0);