Repository navigation
Expand file tree
/
Copy pathXResources.java
More file actions
1251 lines (1100 loc) · 43.1 KB
/
Copy pathXResources.java
File metadata and controls
1251 lines (1100 loc) · 43.1 KB
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
package android.content.res;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.getIntField;
import static de.robv.android.xposed.XposedHelpers.getObjectField;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.WeakHashMap;
import org.xmlpull.v1.XmlPullParser;
import android.content.pm.PackageParser;
import android.graphics.Movie;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodHook.MethodHookParam;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedBridge.CopyOnWriteSortedSet;
import de.robv.android.xposed.callbacks.XC_LayoutInflated;
import de.robv.android.xposed.callbacks.XC_LayoutInflated.LayoutInflatedParam;
import de.robv.android.xposed.callbacks.XCallback;
/**
* Resources class that allows replacements for selected resources
*/
public class XResources extends MiuiResources {
private static final SparseArray<HashMap<String, Object>> sReplacements = new SparseArray<HashMap<String, Object>>();
private static final SparseArray<HashMap<String, ResourceNames>> sResourceNames
= new SparseArray<HashMap<String, ResourceNames>>();
private static final byte[] sSystemReplacementsCache = new byte[256]; // bitmask: 0x000700ff => 2048 bit => 256 bytes
private byte[] mReplacementsCache; // bitmask: 0x0007007f => 1024 bit => 128 bytes
private static final HashMap<String, byte[]> sReplacementsCacheMap = new HashMap<String, byte[]>();
private static final SparseArray<ColorStateList> sColorStateListCache = new SparseArray<ColorStateList>(0);
private static final SparseArray<HashMap<String, CopyOnWriteSortedSet<XC_LayoutInflated>>> sLayoutCallbacks
= new SparseArray<HashMap<String, CopyOnWriteSortedSet<XC_LayoutInflated>>>();
private static final WeakHashMap<XmlResourceParser, XMLInstanceDetails> sXmlInstanceDetails
= new WeakHashMap<XmlResourceParser, XMLInstanceDetails>();
private static final String EXTRA_XML_INSTANCE_DETAILS = "xmlInstanceDetails";
private static final ThreadLocal<LinkedList<MethodHookParam>> sIncludedLayouts = new ThreadLocal<LinkedList<MethodHookParam>>() {
@Override
protected LinkedList<MethodHookParam> initialValue() {
return new LinkedList<MethodHookParam>();
}
};
private static final HashMap<String, Long> sResDirLastModified = new HashMap<String, Long>();
private static final HashMap<String, String> sResDirPackageNames = new HashMap<String, String>();
private static ThreadLocal<Object> sLatestResKey = null;
private boolean mIsObjectInited;
private String mResDir;
private String mPackageName;
// Dummy, will never be called (objects are transferred to this class only).
private XResources() {
super(null, null, null);
throw new UnsupportedOperationException();
}
/** Framework only, don't call this from your module! */
public void initObject(String resDir) {
if (mIsObjectInited)
throw new IllegalStateException("Object has already been initialized");
this.mResDir = resDir;
this.mPackageName = getPackageName(resDir);
if (resDir != null) {
synchronized (sReplacementsCacheMap) {
mReplacementsCache = sReplacementsCacheMap.get(resDir);
if (mReplacementsCache == null) {
mReplacementsCache = new byte[128];
sReplacementsCacheMap.put(resDir, mReplacementsCache);
}
}
}
this.mIsObjectInited = true;
}
/** Framework only, don't call this from your module! */
public boolean isFirstLoad() {
synchronized (sReplacements) {
if (mResDir == null)
return false;
Long lastModification = new File(mResDir).lastModified();
Long oldModified = sResDirLastModified.get(mResDir);
if (lastModification.equals(oldModified))
return false;
sResDirLastModified.put(mResDir, lastModification);
if (oldModified == null)
return true;
// file was changed meanwhile => remove old replacements
for (int i = 0; i < sReplacements.size(); i++) {
sReplacements.valueAt(i).remove(mResDir);
}
Arrays.fill(mReplacementsCache, (byte) 0);
return true;
}
}
public String getResDir() {
return mResDir;
}
/** Framework only, don't call this from your module! */
public static void setPackageNameForResDir(String packageName, String resDir) {
synchronized (sResDirPackageNames) {
sResDirPackageNames.put(resDir, packageName);
}
}
/**
* Returns the name of the package that these resources belong to, or "android" for system resources.
*/
public String getPackageName() {
return mPackageName;
}
private static String getPackageName(String resDir) {
if (resDir == null)
return "android";
String packageName;
synchronized (sResDirPackageNames) {
packageName = sResDirPackageNames.get(resDir);
}
if (packageName != null)
return packageName;
PackageParser.PackageLite pkgInfo = PackageParser.parsePackageLite(resDir, 0);
if (pkgInfo != null && pkgInfo.packageName != null) {
Log.w("Xposed", "Package name for " + resDir + " had to be retrieved via parser");
packageName = pkgInfo.packageName;
setPackageNameForResDir(packageName, resDir);
return packageName;
}
throw new IllegalStateException("Could not determine package name for " + resDir);
}
/**
* For a short moment during/after the creation of a new {@code Resources} object, it isn't an
* instance of {@code XResources} yet. For any hooks that need information about the just created
* object during this particular stage, this method will return the package name.
*
* <p>Note: If you call this outside of {@code getTopLevelResources()}, it throws an
* {@code IllegalStateException}.
*/
public static String getPackageNameDuringConstruction() {
Object key;
if (sLatestResKey == null || (key = sLatestResKey.get()) == null)
throw new IllegalStateException("This method can only be called during getTopLevelResources()");
String resDir = (String) getObjectField(key, "mResDir");
return getPackageName(resDir);
}
/** Framework only, don't call this from your module! */
public static void init(ThreadLocal<Object> latestResKey) throws Exception {
sLatestResKey = latestResKey;
findAndHookMethod(LayoutInflater.class, "inflate", XmlPullParser.class, ViewGroup.class, boolean.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (param.hasThrowable())
return;
XMLInstanceDetails details;
synchronized (sXmlInstanceDetails) {
details = sXmlInstanceDetails.get(param.args[0]);
}
if (details != null) {
LayoutInflatedParam liparam = new LayoutInflatedParam(details.callbacks);
liparam.view = (View) param.getResult();
liparam.resNames = details.resNames;
liparam.variant = details.variant;
liparam.res = details.res;
XCallback.callAll(liparam);
}
}
});
findAndHookMethod(LayoutInflater.class, "parseInclude", XmlPullParser.class, View.class, AttributeSet.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
sIncludedLayouts.get().push(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
sIncludedLayouts.get().pop();
if (param.hasThrowable())
return;
// filled in by our implementation of getLayout()
XMLInstanceDetails details = (XMLInstanceDetails) param.getObjectExtra(EXTRA_XML_INSTANCE_DETAILS);
if (details != null) {
LayoutInflatedParam liparam = new LayoutInflatedParam(details.callbacks);
ViewGroup group = (ViewGroup) param.args[1];
liparam.view = group.getChildAt(group.getChildCount() - 1);
liparam.resNames = details.resNames;
liparam.variant = details.variant;
liparam.res = details.res;
XCallback.callAll(liparam);
}
}
});
}
public static class ResourceNames {
public final int id;
public final String pkg;
public final String name;
public final String type;
public final String fullName;
private ResourceNames(int id, String pkg, String name, String type) {
this.id = id;
this.pkg = pkg;
this.name = name;
this.type = type;
this.fullName = pkg + ":" + type + "/" + name;
}
/**
* Returns <code>true</code> if all non-null parameters match the values of this object.
*/
public boolean equals(String pkg, String name, String type, int id) {
return (pkg == null || pkg.equals(this.pkg))
&& (name == null || name.equals(this.name))
&& (type == null || type.equals(this.type))
&& (id == 0 || id == this.id);
}
}
private ResourceNames getResourceNames(int id) {
return new ResourceNames(
id,
getResourcePackageName(id),
getResourceTypeName(id),
getResourceEntryName(id));
}
private static ResourceNames getSystemResourceNames(int id) {
Resources sysRes = getSystem();
return new ResourceNames(
id,
sysRes.getResourcePackageName(id),
sysRes.getResourceTypeName(id),
sysRes.getResourceEntryName(id));
}
private static void putResourceNames(String resDir, ResourceNames resNames) {
int id = resNames.id;
synchronized (sResourceNames) {
HashMap<String, ResourceNames> inner = sResourceNames.get(id);
if (inner == null) {
inner = new HashMap<String, ResourceNames>();
sResourceNames.put(id, inner);
}
synchronized (inner) {
inner.put(resDir, resNames);
}
}
}
// =======================================================
// DEFINING REPLACEMENTS
// =======================================================
public void setReplacement(int id, Object replacement) {
setReplacement(id, replacement, this);
}
public void setReplacement(String fullName, Object replacement) {
int id = getIdentifier(fullName, null, null);
if (id == 0)
throw new NotFoundException(fullName);
setReplacement(id, replacement, this);
}
public void setReplacement(String pkg, String type, String name, Object replacement) {
int id = getIdentifier(name, type, pkg);
if (id == 0)
throw new NotFoundException(pkg + ":" + type + "/" + name);
setReplacement(id, replacement, this);
}
public static void setSystemWideReplacement(int id, Object replacement) {
setReplacement(id, replacement, null);
}
public static void setSystemWideReplacement(String fullName, Object replacement) {
int id = getSystem().getIdentifier(fullName, null, null);
if (id == 0)
throw new NotFoundException(fullName);
setReplacement(id, replacement, null);
}
public static void setSystemWideReplacement(String pkg, String type, String name, Object replacement) {
int id = getSystem().getIdentifier(name, type, pkg);
if (id == 0)
throw new NotFoundException(pkg + ":" + type + "/" + name);
setReplacement(id, replacement, null);
}
private static void setReplacement(int id, Object replacement, XResources res) {
String resDir = (res != null) ? res.mResDir : null;
if (id == 0)
throw new IllegalArgumentException("id 0 is not an allowed resource identifier");
else if (resDir == null && id >= 0x7f000000)
throw new IllegalArgumentException("ids >= 0x7f000000 are app specific and cannot be set for the framework");
if (replacement instanceof Drawable)
throw new IllegalArgumentException("Drawable replacements are deprecated since Xposed 2.1. Use DrawableLoader instead.");
// Cache that we have a replacement for this ID, false positives are accepted to save memory.
if (id < 0x7f000000) {
int cacheKey = (id & 0x00070000) >> 11 | (id & 0xf8) >> 3;
synchronized (sSystemReplacementsCache) {
sSystemReplacementsCache[cacheKey] |= 1 << (id & 7);
}
} else {
int cacheKey = (id & 0x00070000) >> 12 | (id & 0x78) >> 3;
synchronized (res.mReplacementsCache) {
res.mReplacementsCache[cacheKey] |= 1 << (id & 7);
}
}
synchronized (sReplacements) {
HashMap<String, Object> inner = sReplacements.get(id);
if (inner == null) {
inner = new HashMap<String, Object>();
sReplacements.put(id, inner);
}
inner.put(resDir, replacement);
}
}
// =======================================================
// RETURNING REPLACEMENTS
// =======================================================
private Object getReplacement(int id) {
if (id <= 0)
return null;
// Check the cache whether it's worth looking for replacements
if (id < 0x7f000000) {
int cacheKey = (id & 0x00070000) >> 11 | (id & 0xf8) >> 3;
if ((sSystemReplacementsCache[cacheKey] & (1 << (id & 7))) == 0)
return null;
} else if (mResDir != null) {
int cacheKey = (id & 0x00070000) >> 12 | (id & 0x78) >> 3;
if ((mReplacementsCache[cacheKey] & (1 << (id & 7))) == 0)
return null;
}
HashMap<String, Object> inner;
synchronized (sReplacements) {
inner = sReplacements.get(id);
}
if (inner == null)
return null;
synchronized (inner) {
Object result = inner.get(mResDir);
if (result != null || mResDir == null)
return result;
return inner.get(null);
}
}
@Override
public XmlResourceParser getAnimation(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
boolean loadedFromCache = isXmlCached(repRes, repId);
XmlResourceParser result = repRes.getAnimation(repId);
if (!loadedFromCache) {
int parseState = getIntField(result, "mParseState");
rewriteXmlReferencesNative(parseState, this, repRes);
}
return result;
}
return super.getAnimation(id);
}
@Override
public boolean getBoolean(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof Boolean) {
return (Boolean) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getBoolean(repId);
}
return super.getBoolean(id);
}
@Override
public int getColor(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof Integer) {
return (Integer) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getColor(repId);
}
return super.getColor(id);
}
@Override
public ColorStateList getColorStateList(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof ColorStateList) {
return (ColorStateList) replacement;
} else if (replacement instanceof Integer) {
int color = (Integer) replacement;
synchronized (sColorStateListCache) {
ColorStateList result = sColorStateListCache.get(color);
if (result == null) {
result = ColorStateList.valueOf(color);
sColorStateListCache.put(color, result);
}
return result;
}
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getColorStateList(repId);
}
return super.getColorStateList(id);
}
@Override
public float getDimension(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof DimensionReplacement) {
return ((DimensionReplacement) replacement).getDimension(getDisplayMetrics());
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimension(repId);
}
return super.getDimension(id);
}
@Override
public int getDimensionPixelOffset(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof DimensionReplacement) {
return ((DimensionReplacement) replacement).getDimensionPixelOffset(getDisplayMetrics());
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimensionPixelOffset(repId);
}
return super.getDimensionPixelOffset(id);
}
@Override
public int getDimensionPixelSize(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof DimensionReplacement) {
return ((DimensionReplacement) replacement).getDimensionPixelSize(getDisplayMetrics());
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimensionPixelSize(repId);
}
return super.getDimensionPixelSize(id);
}
@Override
public Drawable getDrawable(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof DrawableLoader) {
try {
Drawable result = ((DrawableLoader) replacement).newDrawable(this, id);
if (result != null)
return result;
} catch (Throwable t) { XposedBridge.log(t); }
} else if (replacement instanceof Integer) {
return new ColorDrawable((Integer) replacement);
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDrawable(repId);
}
return super.getDrawable(id);
}
@Override
public Drawable getDrawableForDensity(int id, int density) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof DrawableLoader) {
try {
Drawable result = ((DrawableLoader) replacement).newDrawableForDensity(this, id, density);
if (result != null)
return result;
} catch (Throwable t) { XposedBridge.log(t); }
} else if (replacement instanceof Integer) {
return new ColorDrawable((Integer) replacement);
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDrawableForDensity(repId, density);
}
return super.getDrawableForDensity(id, density);
}
@Override
public float getFraction(int id, int base, int pbase) {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getFraction(repId, base, pbase);
}
return super.getFraction(id, base, pbase);
}
@Override
public int getInteger(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof Integer) {
return (Integer) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getInteger(repId);
}
return super.getInteger(id);
}
@Override
public int[] getIntArray(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof int[]) {
return (int[]) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getIntArray(repId);
}
return super.getIntArray(id);
}
@Override
public XmlResourceParser getLayout(int id) throws NotFoundException {
XmlResourceParser result;
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
boolean loadedFromCache = isXmlCached(repRes, repId);
result = repRes.getLayout(repId);
if (!loadedFromCache) {
int parseState = getIntField(result, "mParseState");
rewriteXmlReferencesNative(parseState, this, repRes);
}
} else {
result = super.getLayout(id);
}
// Check whether this layout is hooked
HashMap<String, CopyOnWriteSortedSet<XC_LayoutInflated>> inner;
synchronized (sLayoutCallbacks) {
inner = sLayoutCallbacks.get(id);
}
if (inner != null) {
CopyOnWriteSortedSet<XC_LayoutInflated> callbacks;
synchronized (inner) {
callbacks = inner.get(mResDir);
if (callbacks == null && mResDir != null)
callbacks = inner.get(null);
}
if (callbacks != null) {
String variant = "layout";
TypedValue value = (TypedValue) getObjectField(this, "mTmpValue");
getValue(id, value, true);
if (value.type == TypedValue.TYPE_STRING) {
String[] components = value.string.toString().split("/", 3);
if (components.length == 3)
variant = components[1];
else
XposedBridge.log("Unexpected resource path \"" + value.string.toString()
+ "\" for resource id 0x" + Integer.toHexString(id));
} else {
XposedBridge.log(new NotFoundException("Could not find file name for resource id 0x") + Integer.toHexString(id));
}
synchronized (sXmlInstanceDetails) {
synchronized (sResourceNames) {
HashMap<String, ResourceNames> resNamesInner = sResourceNames.get(id);
if (resNamesInner != null) {
synchronized (resNamesInner) {
XMLInstanceDetails details = new XMLInstanceDetails(resNamesInner.get(mResDir), variant, callbacks);
sXmlInstanceDetails.put(result, details);
// if we were called inside LayoutInflater.parseInclude, store the details for it
MethodHookParam top = sIncludedLayouts.get().peek();
if (top != null)
top.setObjectExtra(EXTRA_XML_INSTANCE_DETAILS, details);
}
}
}
}
}
}
return result;
}
@Override
public Movie getMovie(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getMovie(repId);
}
return super.getMovie(id);
}
@Override
public CharSequence getQuantityText(int id, int quantity) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getQuantityText(repId, quantity);
}
return super.getQuantityText(id, quantity);
}
// these are handled by getQuantityText:
// public String getQuantityString(int id, int quantity);
// public String getQuantityString(int id, int quantity, Object... formatArgs);
@Override
public String[] getStringArray(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof String[]) {
return (String[]) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getStringArray(repId);
}
return super.getStringArray(id);
}
@Override
public CharSequence getText(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof CharSequence) {
return (CharSequence) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getText(repId);
}
return super.getText(id);
}
// these are handled by getText:
// public String getString(int id);
// public String getString(int id, Object... formatArgs);
@Override
public CharSequence getText(int id, CharSequence def) {
Object replacement = getReplacement(id);
if (replacement instanceof CharSequence) {
return (CharSequence) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getText(repId, def);
}
return super.getText(id, def);
}
@Override
public CharSequence[] getTextArray(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof CharSequence[]) {
return (CharSequence[]) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getTextArray(repId);
}
return super.getTextArray(id);
}
@Override
public XmlResourceParser getXml(int id) throws NotFoundException {
Object replacement = getReplacement(id);
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
boolean loadedFromCache = isXmlCached(repRes, repId);
XmlResourceParser result = repRes.getXml(repId);
if (!loadedFromCache) {
int parseState = getIntField(result, "mParseState");
rewriteXmlReferencesNative(parseState, this, repRes);
}
return result;
}
return super.getXml(id);
}
private static boolean isXmlCached(Resources res, int id) {
int[] mCachedXmlBlockIds = (int[]) getObjectField(res, "mCachedXmlBlockIds");
synchronized (mCachedXmlBlockIds) {
final int num = mCachedXmlBlockIds.length;
for (int i = 0; i < num; i++) {
if (mCachedXmlBlockIds[i] == id)
return true;
}
}
return false;
}
private static native void rewriteXmlReferencesNative(int parserPtr, XResources origRes, Resources repRes);
/**
* Used to replace reference IDs in XMLs.
*
* When resource requests are forwarded to modules, the may include references to resources with the same
* name as in the original resources, but the IDs generated by aapt will be different. rewriteXmlReferencesNative
* walks through all references and calls this function to find out the original ID, which it then writes to
* the compiled XML file in the memory.
*/
private static int translateResId(int id, XResources origRes, Resources repRes) {
try {
String entryName = repRes.getResourceEntryName(id);
String entryType = repRes.getResourceTypeName(id);
String origPackage = origRes.mPackageName;
int origResId = 0;
try {
// look for a resource with the same name and type in the original package
origResId = origRes.getIdentifier(entryName, entryType, origPackage);
} catch (NotFoundException ignored) {}
boolean repResDefined = false;
try {
final TypedValue tmpValue = new TypedValue();
repRes.getValue(id, tmpValue, false);
// if a resource has not been defined (i.e. only a resource ID has been created), it will equal "false"
// this means a boolean "false" value is not detected of it is directly referenced in an XML file
repResDefined = !(tmpValue.type == TypedValue.TYPE_INT_BOOLEAN && tmpValue.data == 0);
} catch (NotFoundException ignored) {}
if (!repResDefined && origResId == 0 && !entryType.equals("id")) {
XposedBridge.log(entryType + "/" + entryName + " is neither defined in module nor in original resources");
return 0;
}
// exists only in module, so create a fake resource id
if (origResId == 0)
origResId = getFakeResId(repRes, id);
// IDs will never be loaded, no need to set a replacement
if (repResDefined && !entryType.equals("id"))
origRes.setReplacement(origResId, new XResForwarder(repRes, id));
return origResId;
} catch (Exception e) {
XposedBridge.log(e);
return id;
}
}
public static int getFakeResId(String resName) {
return 0x7e000000 | (resName.hashCode() & 0x00ffffff);
}
public static int getFakeResId(Resources res, int id) {
return getFakeResId(res.getResourceName(id));
}
public int addResource(Resources res, int id) {
int fakeId = getFakeResId(res, id);
synchronized (sReplacements) {
if (sReplacements.indexOfKey(fakeId) < 0)
setReplacement(fakeId, new XResForwarder(res, id));
}
return fakeId;
}
/**
* Similar to {@link #translateResId}, but used to determine the original ID of attribute names
*/
private static int translateAttrId(String attrName, XResources origRes) {
String origPackage = origRes.mPackageName;
int origAttrId = 0;
try {
origAttrId = origRes.getIdentifier(attrName, "attr", origPackage);
} catch (NotFoundException e) {
XposedBridge.log("Attribute " + attrName + " not found in original resources");
}
return origAttrId;
}
// =======================================================
// XTypedArray class
// =======================================================
/**
* {@link TypedArray} replacement that replaces values on-the-fly.
* Mainly used when inflating layouts.
*/
public static class XTypedArray extends TypedArray {
private boolean mIsObjectInited;
private XResources mRes;
// Dummy, will never be called (objects are transferred to this class only).
private XTypedArray() {
super(null, null, null, 0);
throw new UnsupportedOperationException();
}
public void initObject(XResources res) {
if (mIsObjectInited)
throw new IllegalStateException("Object has already been initialized");
this.mRes = res;
this.mIsObjectInited = true;
}
@Override
public boolean getBoolean(int index, boolean defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof Boolean) {
return (Boolean) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getBoolean(repId);
}
return super.getBoolean(index, defValue);
}
@Override
public int getColor(int index, int defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof Integer) {
return (Integer) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getColor(repId);
}
return super.getColor(index, defValue);
}
@Override
public ColorStateList getColorStateList(int index) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof ColorStateList) {
return (ColorStateList) replacement;
} else if (replacement instanceof Integer) {
int color = (Integer) replacement;
synchronized (sColorStateListCache) {
ColorStateList result = sColorStateListCache.get(color);
if (result == null) {
result = ColorStateList.valueOf(color);
sColorStateListCache.put(color, result);
}
return result;
}
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getColorStateList(repId);
}
return super.getColorStateList(index);
}
@Override
public float getDimension(int index, float defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimension(repId);
}
return super.getDimension(index, defValue);
}
@Override
public int getDimensionPixelOffset(int index, int defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimensionPixelOffset(repId);
}
return super.getDimensionPixelOffset(index, defValue);
}
@Override
public int getDimensionPixelSize(int index, int defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDimensionPixelSize(repId);
}
return super.getDimensionPixelSize(index, defValue);
}
@Override
public Drawable getDrawable(int index) {
final int resId = getResourceId(index, 0);
Object replacement = mRes.getReplacement(resId);
if (replacement instanceof DrawableLoader) {
try {
Drawable result = ((DrawableLoader) replacement).newDrawable(mRes, resId);
if (result != null)
return result;
} catch (Throwable t) { XposedBridge.log(t); }
} else if (replacement instanceof Integer) {
return new ColorDrawable((Integer) replacement);
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getDrawable(repId);
}
return super.getDrawable(index);
}
@Override
public float getFloat(int index, float defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
// dimensions seem to be the only way to define floats by references
return repRes.getDimension(repId);
}
return super.getFloat(index, defValue);
}
@Override
public float getFraction(int index, int base, int pbase, float defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
// dimensions seem to be the only way to define floats by references
return repRes.getFraction(repId, base, pbase);
}
return super.getFraction(index, base, pbase, defValue);
}
@Override
public int getInt(int index, int defValue) {
Object replacement = mRes.getReplacement(getResourceId(index, 0));
if (replacement instanceof Integer) {
return (Integer) replacement;
} else if (replacement instanceof XResForwarder) {
Resources repRes = ((XResForwarder) replacement).getResources();
int repId = ((XResForwarder) replacement).getId();
return repRes.getInteger(repId);