-
Notifications
You must be signed in to change notification settings - Fork 83
/
table.go
1093 lines (978 loc) · 24.9 KB
/
table.go
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 androidbinary
import (
"encoding/binary"
"fmt"
"io"
"strconv"
"strings"
"unsafe"
)
// ResID is ID for resources.
type ResID uint32
// TableFile is a resource table file.
type TableFile struct {
stringPool *ResStringPool
tablePackages map[uint32]*TablePackage
}
// ResTableHeader is a header of TableFile.
type ResTableHeader struct {
Header ResChunkHeader
PackageCount uint32
}
// ResTablePackage is a header of table packages.
type ResTablePackage struct {
Header ResChunkHeader
ID uint32
Name [128]uint16
TypeStrings uint32
LastPublicType uint32
KeyStrings uint32
LastPublicKey uint32
}
// TablePackage is a table package.
type TablePackage struct {
Header ResTablePackage
TypeStrings *ResStringPool
KeyStrings *ResStringPool
TableTypes []*TableType
}
// ResTableType is a type of a table.
type ResTableType struct {
Header ResChunkHeader
ID uint8
Res0 uint8
Res1 uint16
EntryCount uint32
EntriesStart uint32
Config ResTableConfig
}
// ScreenLayout describes screen layout.
type ScreenLayout uint8
// ScreenLayout bits
const (
MaskScreenSize ScreenLayout = 0x0f
ScreenSizeAny ScreenLayout = 0x01
ScreenSizeSmall ScreenLayout = 0x02
ScreenSizeNormal ScreenLayout = 0x03
ScreenSizeLarge ScreenLayout = 0x04
ScreenSizeXLarge ScreenLayout = 0x05
MaskScreenLong ScreenLayout = 0x30
ShiftScreenLong = 4
ScreenLongAny ScreenLayout = 0x00
ScreenLongNo ScreenLayout = 0x10
ScreenLongYes ScreenLayout = 0x20
MaskLayoutDir ScreenLayout = 0xC0
ShiftLayoutDir = 6
LayoutDirAny ScreenLayout = 0x00
LayoutDirLTR ScreenLayout = 0x40
LayoutDirRTL ScreenLayout = 0x80
)
// UIMode describes UI mode.
type UIMode uint8
// UIMode bits
const (
MaskUIModeType UIMode = 0x0f
UIModeTypeAny UIMode = 0x01
UIModeTypeNormal UIMode = 0x02
UIModeTypeDesk UIMode = 0x03
UIModeTypeCar UIMode = 0x04
MaskUIModeNight UIMode = 0x30
ShiftUIModeNight = 4
UIModeNightAny UIMode = 0x00
UIModeNightNo UIMode = 0x10
UIModeNightYes UIMode = 0x20
)
// InputFlags are input flags.
type InputFlags uint8
// input flags
const (
MaskKeysHidden InputFlags = 0x03
KeysHiddenAny InputFlags = 0x00
KeysHiddenNo InputFlags = 0x01
KeysHiddenYes InputFlags = 0x02
KeysHiddenSoft InputFlags = 0x03
MaskNavHidden InputFlags = 0x0c
NavHiddenAny InputFlags = 0x00
NavHiddenNo InputFlags = 0x04
NavHiddenYes InputFlags = 0x08
)
// ResTableConfig is a configuration of a table.
type ResTableConfig struct {
Size uint32
// imsi
Mcc uint16
Mnc uint16
// locale
Language [2]uint8
Country [2]uint8
// screen type
Orientation uint8
Touchscreen uint8
Density uint16
// inout
Keyboard uint8
Navigation uint8
InputFlags InputFlags
InputPad0 uint8
// screen size
ScreenWidth uint16
ScreenHeight uint16
// version
SDKVersion uint16
MinorVersion uint16
// screen config
ScreenLayout ScreenLayout
UIMode UIMode
SmallestScreenWidthDp uint16
// screen size dp
ScreenWidthDp uint16
ScreenHeightDp uint16
}
// TableType is a collection of resource entries for a particular resource data type.
type TableType struct {
Header *ResTableType
Entries []TableEntry
}
// ResTableEntry is the beginning of information about an entry in the resource table.
type ResTableEntry struct {
Size uint16
Flags uint16
Key ResStringPoolRef
}
// TableEntry is a entry in a resource table.
type TableEntry struct {
Key *ResTableEntry
Value *ResValue
Flags uint32
}
// ResTableTypeSpec is specification of the resources defined by a particular type.
type ResTableTypeSpec struct {
Header ResChunkHeader
ID uint8
Res0 uint8
Res1 uint16
EntryCount uint32
}
// IsResID returns whether s is ResId.
func IsResID(s string) bool {
return strings.HasPrefix(s, "@0x")
}
// ParseResID parses ResId.
func ParseResID(s string) (ResID, error) {
if !IsResID(s) {
return 0, fmt.Errorf("androidbinary: %s is not ResID", s)
}
id, err := strconv.ParseUint(s[3:], 16, 32)
if err != nil {
return 0, err
}
return ResID(id), nil
}
func (id ResID) String() string {
return fmt.Sprintf("@0x%08X", uint32(id))
}
// Package returns the package index of id.
func (id ResID) Package() uint32 {
return uint32(id) >> 24
}
// Type returns the type index of id.
func (id ResID) Type() int {
return (int(id) >> 16) & 0xFF
}
// Entry returns the entry index of id.
func (id ResID) Entry() int {
return int(id) & 0xFFFF
}
// NewTableFile returns new TableFile.
func NewTableFile(r io.ReaderAt) (*TableFile, error) {
f := new(TableFile)
sr := io.NewSectionReader(r, 0, 1<<63-1)
header := new(ResTableHeader)
binary.Read(sr, binary.LittleEndian, header)
f.tablePackages = make(map[uint32]*TablePackage)
offset := int64(header.Header.HeaderSize)
for offset < int64(header.Header.Size) {
chunkHeader, err := f.readChunk(sr, offset)
if err != nil {
return nil, err
}
offset += int64(chunkHeader.Size)
}
return f, nil
}
func (f *TableFile) findPackage(id uint32) *TablePackage {
if f == nil {
return nil
}
return f.tablePackages[id]
}
func (p *TablePackage) findEntry(typeIndex, entryIndex int, config *ResTableConfig) TableEntry {
var best *TableType
for _, t := range p.TableTypes {
switch {
case int(t.Header.ID) != typeIndex:
// nothing to do
case !t.Header.Config.Match(config):
// nothing to do
case entryIndex >= len(t.Entries):
// nothing to do
case t.Entries[entryIndex].Value == nil:
// nothing to do
case best == nil || t.Header.Config.IsBetterThan(&best.Header.Config, config):
best = t
}
}
if best == nil || entryIndex >= len(best.Entries) {
return TableEntry{}
}
return best.Entries[entryIndex]
}
// GetResource returns a resource referenced by id.
func (f *TableFile) GetResource(id ResID, config *ResTableConfig) (interface{}, error) {
p := f.findPackage(id.Package())
if p == nil {
return nil, fmt.Errorf("androidbinary: package 0x%02X not found", id.Package())
}
e := p.findEntry(id.Type(), id.Entry(), config)
v := e.Value
if v == nil {
return nil, fmt.Errorf("androidbinary: entry 0x%04X not found", id.Entry())
}
switch v.DataType {
case TypeNull:
return nil, nil
case TypeString:
return f.GetString(ResStringPoolRef(v.Data)), nil
case TypeIntDec:
return v.Data, nil
case TypeIntHex:
return v.Data, nil
case TypeIntBoolean:
return v.Data != 0, nil
}
return v.Data, nil
}
// GetString returns a string referenced by ref.
func (f *TableFile) GetString(ref ResStringPoolRef) string {
return f.stringPool.GetString(ref)
}
func (f *TableFile) readChunk(r io.ReaderAt, offset int64) (*ResChunkHeader, error) {
sr := io.NewSectionReader(r, offset, 1<<63-1-offset)
chunkHeader := &ResChunkHeader{}
if _, err := sr.Seek(0, io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
return nil, err
}
var err error
if _, err := sr.Seek(0, io.SeekStart); err != nil {
return nil, err
}
switch chunkHeader.Type {
case ResStringPoolChunkType:
f.stringPool, err = readStringPool(sr)
case ResTablePackageType:
var tablePackage *TablePackage
tablePackage, err = readTablePackage(sr)
f.tablePackages[tablePackage.Header.ID] = tablePackage
}
if err != nil {
return nil, err
}
return chunkHeader, nil
}
func readTablePackage(sr *io.SectionReader) (*TablePackage, error) {
tablePackage := new(TablePackage)
header := new(ResTablePackage)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return nil, err
}
tablePackage.Header = *header
srTypes := io.NewSectionReader(sr, int64(header.TypeStrings), int64(header.Header.Size-header.TypeStrings))
if typeStrings, err := readStringPool(srTypes); err == nil {
tablePackage.TypeStrings = typeStrings
} else {
return nil, err
}
srKeys := io.NewSectionReader(sr, int64(header.KeyStrings), int64(header.Header.Size-header.KeyStrings))
if keyStrings, err := readStringPool(srKeys); err == nil {
tablePackage.KeyStrings = keyStrings
} else {
return nil, err
}
offset := int64(header.Header.HeaderSize)
for offset < int64(header.Header.Size) {
chunkHeader := &ResChunkHeader{}
if _, err := sr.Seek(offset, io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
return nil, err
}
var err error
chunkReader := io.NewSectionReader(sr, offset, int64(chunkHeader.Size))
if _, err := sr.Seek(offset, io.SeekStart); err != nil {
return nil, err
}
switch chunkHeader.Type {
case ResTableTypeType:
var tableType *TableType
tableType, err = readTableType(chunkHeader, chunkReader)
tablePackage.TableTypes = append(tablePackage.TableTypes, tableType)
case ResTableTypeSpecType:
_, err = readTableTypeSpec(chunkReader)
}
if err != nil {
return nil, err
}
offset += int64(chunkHeader.Size)
}
return tablePackage, nil
}
func readTableType(chunkHeader *ResChunkHeader, sr *io.SectionReader) (*TableType, error) {
// TableType header may be omitted
header := new(ResTableType)
if _, err := sr.Seek(0, io.SeekStart); err != nil {
return nil, err
}
buf, err := newZeroFilledReader(sr, int64(chunkHeader.HeaderSize), int64(unsafe.Sizeof(*header)))
if err != nil {
return nil, err
}
if err := binary.Read(buf, binary.LittleEndian, header); err != nil {
return nil, err
}
entryIndexes := make([]uint32, header.EntryCount)
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, entryIndexes); err != nil {
return nil, err
}
entries := make([]TableEntry, header.EntryCount)
for i, index := range entryIndexes {
if index == 0xFFFFFFFF {
continue
}
if _, err := sr.Seek(int64(header.EntriesStart+index), io.SeekStart); err != nil {
return nil, err
}
var key ResTableEntry
binary.Read(sr, binary.LittleEndian, &key)
entries[i].Key = &key
var val ResValue
binary.Read(sr, binary.LittleEndian, &val)
entries[i].Value = &val
}
return &TableType{
header,
entries,
}, nil
}
func readTableTypeSpec(sr *io.SectionReader) ([]uint32, error) {
header := new(ResTableTypeSpec)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return nil, err
}
flags := make([]uint32, header.EntryCount)
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, flags); err != nil {
return nil, err
}
return flags, nil
}
// IsMoreSpecificThan returns true if c is more specific than o.
func (c *ResTableConfig) IsMoreSpecificThan(o *ResTableConfig) bool {
// nil ResTableConfig is never more specific than any ResTableConfig
if c == nil {
return false
}
if o == nil {
return false
}
// imsi
if c.Mcc != o.Mcc {
if c.Mcc == 0 {
return false
}
if o.Mnc == 0 {
return true
}
}
if c.Mnc != o.Mnc {
if c.Mnc == 0 {
return false
}
if o.Mnc == 0 {
return true
}
}
// locale
if diff := c.IsLocaleMoreSpecificThan(o); diff < 0 {
return false
} else if diff > 0 {
return true
}
// screen layout
if c.ScreenLayout != 0 || o.ScreenLayout != 0 {
if ((c.ScreenLayout ^ o.ScreenLayout) & MaskLayoutDir) != 0 {
if (c.ScreenLayout & MaskLayoutDir) == 0 {
return false
}
if (o.ScreenLayout & MaskLayoutDir) == 0 {
return true
}
}
}
// smallest screen width dp
if c.SmallestScreenWidthDp != 0 || o.SmallestScreenWidthDp != 0 {
if c.SmallestScreenWidthDp != o.SmallestScreenWidthDp {
if c.SmallestScreenWidthDp == 0 {
return false
}
if o.SmallestScreenWidthDp == 0 {
return true
}
}
}
// screen size dp
if c.ScreenWidthDp != 0 || o.ScreenWidthDp != 0 ||
c.ScreenHeightDp != 0 || o.ScreenHeightDp != 0 {
if c.ScreenWidthDp != o.ScreenWidthDp {
if c.ScreenWidthDp == 0 {
return false
}
if o.ScreenWidthDp == 0 {
return true
}
}
if c.ScreenHeightDp != o.ScreenHeightDp {
if c.ScreenHeightDp == 0 {
return false
}
if o.ScreenHeightDp == 0 {
return true
}
}
}
// screen layout
if c.ScreenLayout != 0 || o.ScreenLayout != 0 {
if ((c.ScreenLayout ^ o.ScreenLayout) & MaskScreenSize) != 0 {
if (c.ScreenLayout & MaskScreenSize) == 0 {
return false
}
if (o.ScreenLayout & MaskScreenSize) == 0 {
return true
}
}
if ((c.ScreenLayout ^ o.ScreenLayout) & MaskScreenLong) != 0 {
if (c.ScreenLayout & MaskScreenLong) == 0 {
return false
}
if (o.ScreenLayout & MaskScreenLong) == 0 {
return true
}
}
}
// orientation
if c.Orientation != o.Orientation {
if c.Orientation == 0 {
return false
}
if o.Orientation == 0 {
return true
}
}
// uimode
if c.UIMode != 0 || o.UIMode != 0 {
diff := c.UIMode ^ o.UIMode
if (diff & MaskUIModeType) != 0 {
if (c.UIMode & MaskUIModeType) == 0 {
return false
}
if (o.UIMode & MaskUIModeType) == 0 {
return true
}
}
if (diff & MaskUIModeNight) != 0 {
if (c.UIMode & MaskUIModeNight) == 0 {
return false
}
if (o.UIMode & MaskUIModeNight) == 0 {
return true
}
}
}
// touchscreen
if c.Touchscreen != o.Touchscreen {
if c.Touchscreen == 0 {
return false
}
if o.Touchscreen == 0 {
return true
}
}
// input
if c.InputFlags != 0 || o.InputFlags != 0 {
myKeysHidden := c.InputFlags & MaskKeysHidden
oKeysHidden := o.InputFlags & MaskKeysHidden
if (myKeysHidden ^ oKeysHidden) != 0 {
if myKeysHidden == 0 {
return false
}
if oKeysHidden == 0 {
return true
}
}
myNavHidden := c.InputFlags & MaskNavHidden
oNavHidden := o.InputFlags & MaskNavHidden
if (myNavHidden ^ oNavHidden) != 0 {
if myNavHidden == 0 {
return false
}
if oNavHidden == 0 {
return true
}
}
}
if c.Keyboard != o.Keyboard {
if c.Keyboard == 0 {
return false
}
if o.Keyboard == 0 {
return true
}
}
if c.Navigation != o.Navigation {
if c.Navigation == 0 {
return false
}
if o.Navigation == 0 {
return true
}
}
// screen size
if c.ScreenWidth != 0 || o.ScreenWidth != 0 ||
c.ScreenHeight != 0 || o.ScreenHeight != 0 {
if c.ScreenWidth != o.ScreenWidth {
if c.ScreenWidth == 0 {
return false
}
if o.ScreenWidth == 0 {
return true
}
}
if c.ScreenHeight != o.ScreenHeight {
if c.ScreenHeight == 0 {
return false
}
if o.ScreenHeight == 0 {
return true
}
}
}
//version
if c.SDKVersion != o.SDKVersion {
if c.SDKVersion == 0 {
return false
}
if o.SDKVersion == 0 {
return true
}
}
if c.MinorVersion != o.MinorVersion {
if c.MinorVersion == 0 {
return false
}
if o.MinorVersion == 0 {
return true
}
}
return false
}
// IsBetterThan returns true if c is better than o for the r configuration.
func (c *ResTableConfig) IsBetterThan(o *ResTableConfig, r *ResTableConfig) bool {
if r == nil {
return c.IsMoreSpecificThan(o)
}
// nil ResTableConfig is never better than any ResTableConfig
if c == nil {
return false
}
if o == nil {
return false
}
// imsi
if c.Mcc != 0 || c.Mnc != 0 || o.Mcc != 0 || o.Mnc != 0 {
if c.Mcc != o.Mcc && r.Mcc != 0 {
return c.Mcc != 0
}
if c.Mnc != o.Mnc && r.Mnc != 0 {
return c.Mnc != 0
}
}
// locale
if c.IsLocaleBetterThan(o, r) {
return true
}
// screen layout
if c.ScreenLayout != 0 || o.ScreenLayout != 0 {
myLayoutdir := c.ScreenLayout & MaskLayoutDir
oLayoutdir := o.ScreenLayout & MaskLayoutDir
if (myLayoutdir^oLayoutdir) != 0 && (r.ScreenLayout&MaskLayoutDir) != 0 {
return myLayoutdir > oLayoutdir
}
}
// smallest screen width dp
if c.SmallestScreenWidthDp != 0 || o.SmallestScreenWidthDp != 0 {
if c.SmallestScreenWidthDp != o.SmallestScreenWidthDp {
return c.SmallestScreenWidthDp > o.SmallestScreenWidthDp
}
}
// screen size dp
if c.ScreenWidthDp != 0 || c.ScreenHeightDp != 0 || o.ScreenWidthDp != 0 || o.ScreenHeightDp != 0 {
myDelta := 0
otherDelta := 0
if r.ScreenWidthDp != 0 {
myDelta += int(r.ScreenWidthDp) - int(c.ScreenWidthDp)
otherDelta += int(r.ScreenWidthDp) - int(o.ScreenWidthDp)
}
if r.ScreenHeightDp != 0 {
myDelta += int(r.ScreenHeightDp) - int(c.ScreenHeightDp)
otherDelta += int(r.ScreenHeightDp) - int(o.ScreenHeightDp)
}
if myDelta != otherDelta {
return myDelta < otherDelta
}
}
// screen layout
if c.ScreenLayout != 0 || o.ScreenLayout != 0 {
mySL := c.ScreenLayout & MaskScreenSize
oSL := o.ScreenLayout & MaskScreenSize
if (mySL^oSL) != 0 && (r.ScreenLayout&MaskScreenSize) != 0 {
fixedMySL := mySL
fixedOSL := oSL
if (r.ScreenLayout & MaskScreenSize) >= ScreenSizeNormal {
if fixedMySL == 0 {
fixedMySL = ScreenSizeNormal
}
if fixedOSL == 0 {
fixedOSL = ScreenSizeNormal
}
}
if fixedMySL == fixedOSL {
return mySL != 0
}
return fixedMySL > fixedOSL
}
if ((c.ScreenLayout^o.ScreenLayout)&MaskScreenLong) != 0 &&
(r.ScreenLayout&MaskScreenLong) != 0 {
return (c.ScreenLayout & MaskScreenLong) != 0
}
}
// orientation
if c.Orientation != o.Orientation && r.Orientation != 0 {
return c.Orientation != 0
}
// uimode
if c.UIMode != 0 || o.UIMode != 0 {
diff := c.UIMode ^ o.UIMode
if (diff&MaskUIModeType) != 0 && (r.UIMode&MaskUIModeType) != 0 {
return (c.UIMode & MaskUIModeType) != 0
}
if (diff&MaskUIModeNight) != 0 && (r.UIMode&MaskUIModeNight) != 0 {
return (c.UIMode & MaskUIModeNight) != 0
}
}
// screen type
if c.Density != o.Density {
h := int(c.Density)
if h == 0 {
h = 160
}
l := int(o.Density)
if l == 0 {
l = 160
}
blmBigger := true
if l > h {
h, l = l, h
blmBigger = false
}
reqValue := int(r.Density)
if reqValue == 0 {
reqValue = 160
}
if reqValue >= h {
return blmBigger
}
if l >= reqValue {
return !blmBigger
}
if (2*l-reqValue)*h > reqValue*reqValue {
return !blmBigger
}
return blmBigger
}
if c.Touchscreen != o.Touchscreen && r.Touchscreen != 0 {
return c.Touchscreen != 0
}
// input
if c.InputFlags != 0 || o.InputFlags != 0 {
myKeysHidden := c.InputFlags & MaskKeysHidden
oKeysHidden := o.InputFlags & MaskKeysHidden
reqKeysHidden := r.InputFlags & MaskKeysHidden
if myKeysHidden != oKeysHidden && reqKeysHidden != 0 {
switch {
case myKeysHidden == 0:
return false
case oKeysHidden == 0:
return true
case reqKeysHidden == myKeysHidden:
return true
case reqKeysHidden == oKeysHidden:
return false
}
}
myNavHidden := c.InputFlags & MaskNavHidden
oNavHidden := o.InputFlags & MaskNavHidden
reqNavHidden := r.InputFlags & MaskNavHidden
if myNavHidden != oNavHidden && reqNavHidden != 0 {
switch {
case myNavHidden == 0:
return false
case oNavHidden == 0:
return true
}
}
}
if c.Keyboard != o.Keyboard && r.Keyboard != 0 {
return c.Keyboard != 0
}
if c.Navigation != o.Navigation && r.Navigation != 0 {
return c.Navigation != 0
}
// screen size
if c.ScreenWidth != 0 || c.ScreenHeight != 0 || o.ScreenWidth != 0 || o.ScreenHeight != 0 {
myDelta := 0
otherDelta := 0
if r.ScreenWidth != 0 {
myDelta += int(r.ScreenWidth) - int(c.ScreenWidth)
otherDelta += int(r.ScreenWidth) - int(o.ScreenWidth)
}
if r.ScreenHeight != 0 {
myDelta += int(r.ScreenHeight) - int(c.ScreenHeight)
otherDelta += int(r.ScreenHeight) - int(o.ScreenHeight)
}
if myDelta != otherDelta {
return myDelta < otherDelta
}
}
// version
if c.SDKVersion != 0 || o.MinorVersion != 0 {
if c.SDKVersion != o.SDKVersion && r.SDKVersion != 0 {
return c.SDKVersion > o.SDKVersion
}
if c.MinorVersion != o.MinorVersion && r.MinorVersion != 0 {
return c.MinorVersion != 0
}
}
return false
}
// IsLocaleMoreSpecificThan a positive integer if this config is more specific than o,
// a negative integer if |o| is more specific
// and 0 if they're equally specific.
func (c *ResTableConfig) IsLocaleMoreSpecificThan(o *ResTableConfig) int {
if (c.Language != [2]uint8{} || c.Country != [2]uint8{}) || (o.Language != [2]uint8{} || o.Country != [2]uint8{}) {
if c.Language != o.Language {
if c.Language == [2]uint8{} {
return -1
}
if o.Language == [2]uint8{} {
return 1
}
}
if c.Country != o.Country {
if c.Country == [2]uint8{} {
return -1
}
if o.Country == [2]uint8{} {
return 1
}
}
}
return 0
}
// IsLocaleBetterThan returns true if c is a better locale match than o for the r configuration.
func (c *ResTableConfig) IsLocaleBetterThan(o *ResTableConfig, r *ResTableConfig) bool {
if r.Language == [2]uint8{} && r.Country == [2]uint8{} {
// The request doesn't have a locale, so no resource is better
// than the other.
return false
}
if c.Language == [2]uint8{} && c.Country == [2]uint8{} && o.Language == [2]uint8{} && o.Country == [2]uint8{} {
// The locales parts of both resources are empty, so no one is better
// than the other.
return false
}
if c.Language != o.Language {
// The languages of the two resources are not the same.
// the US English resource have traditionally lived for most apps.
if r.Language == [2]uint8{'e', 'n'} {
if r.Country == [2]uint8{'U', 'S'} {
if c.Language != [2]uint8{} {
return c.Country == [2]uint8{} || c.Country == [2]uint8{'U', 'S'}
}
return !(c.Country == [2]uint8{} || c.Country == [2]uint8{'U', 'S'})
}
}
return c.Language != [2]uint8{}
}
if c.Country != o.Country {
return c.Country != [2]uint8{}
}
return false
}
// Match returns true if c can be considered a match for the parameters in settings.
func (c *ResTableConfig) Match(settings *ResTableConfig) bool {
// nil ResTableConfig always matches.
if settings == nil {
return true
} else if c == nil {
return *settings == ResTableConfig{}
}
// match imsi
if settings.Mcc == 0 {
if c.Mcc != 0 {
return false
}
} else {
if c.Mcc != 0 && c.Mcc != settings.Mcc {
return false
}
}
if settings.Mnc == 0 {
if c.Mnc != 0 {
return false
}
} else {
if c.Mnc != 0 && c.Mnc != settings.Mnc {
return false
}
}
// match locale
if c.Language != [2]uint8{0, 0} {
// Don't consider country and variants when deciding matches.
// If two configs differ only in their country and variant,
// they can be weeded out in the isMoreSpecificThan test.
if c.Language != settings.Language {
return false
}
if c.Country != [2]uint8{0, 0} {
if c.Country != settings.Country {
return false
}
}
}
// screen layout
layoutDir := c.ScreenLayout & MaskLayoutDir
setLayoutDir := settings.ScreenLayout & MaskLayoutDir
if layoutDir != 0 && layoutDir != setLayoutDir {
return false
}
screenSize := c.ScreenLayout & MaskScreenSize
setScreenSize := settings.ScreenLayout & MaskScreenSize
if screenSize != 0 && screenSize > setScreenSize {
return false
}
screenLong := c.ScreenLayout & MaskScreenLong
setScreenLong := settings.ScreenLayout & MaskScreenLong
if screenLong != 0 && screenLong != setScreenLong {
return false
}