Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/ResConfigFlags.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.res.data;
18
19 import java.util.logging.Logger;
20
21 public class ResConfigFlags {
22 public final short mcc;
23 public final short mnc;
24
25 public final char[] language;
26 public final char[] region;
27
28 public final byte orientation;
29 public final byte touchscreen;
30 public final int density;
31
32 public final byte keyboard;
33 public final byte navigation;
34 public final byte inputFlags;
35
36 public final short screenWidth;
37 public final short screenHeight;
38
39 public final short sdkVersion;
40
41 public final byte screenLayout;
42 public final byte uiMode;
43 public final short smallestScreenWidthDp;
44
45 public final short screenWidthDp;
46 public final short screenHeightDp;
47
48 private final char[] localeScript;
49 private final char[] localeVariant;
50
51 private final byte screenLayout2;
52 private final byte colorMode;
53
54 public final boolean isInvalid;
55
56 private final String mQualifiers;
57
58 private final int size;
59
60 public ResConfigFlags() {
61 mcc = 0;
62 mnc = 0;
63 language = new char[] { '\00', '\00' };
64 region = new char[] { '\00', '\00' };
65 orientation = ORIENTATION_ANY;
66 touchscreen = TOUCHSCREEN_ANY;
67 density = DENSITY_DEFAULT;
68 keyboard = KEYBOARD_ANY;
69 navigation = NAVIGATION_ANY;
70 inputFlags = KEYSHIDDEN_ANY | NAVHIDDEN_ANY;
71 screenWidth = 0;
72 screenHeight = 0;
73 sdkVersion = 0;
74 screenLayout = SCREENLONG_ANY | SCREENSIZE_ANY;
75 uiMode = UI_MODE_TYPE_ANY | UI_MODE_NIGHT_ANY;
76 smallestScreenWidthDp = 0;
77 screenWidthDp = 0;
78 screenHeightDp = 0;
79 localeScript = null;
80 localeVariant = null;
81 screenLayout2 = 0;
82 colorMode = COLOR_WIDE_UNDEFINED;
83 isInvalid = false;
84 mQualifiers = "";
85 size = 0;
86 }
87
88 public ResConfigFlags(short mcc, short mnc, char[] language,
89 char[] region, byte orientation,
90 byte touchscreen, int density, byte keyboard, byte navigation,
91 byte inputFlags, short screenWidth, short screenHeight,
92 short sdkVersion, byte screenLayout, byte uiMode,
93 short smallestScreenWidthDp, short screenWidthDp,
94 short screenHeightDp, char[] localeScript, char[] localeVariant,
95 byte screenLayout2, byte colorMode, boolean isInvalid, int size) {
96 if (orientation < 0 || orientation > 3) {
97 LOGGER.warning("Invalid orientation value: " + orientation);
98 orientation = 0;
99 isInvalid = true;
100 }
101 if (touchscreen < 0 || touchscreen > 3) {
102 LOGGER.warning("Invalid touchscreen value: " + touchscreen);
103 touchscreen = 0;
104 isInvalid = true;
105 }
106 if (density < -1) {
107 LOGGER.warning("Invalid density value: " + density);
108 density = 0;
109 isInvalid = true;
110 }
111 if (keyboard < 0 || keyboard > 3) {
112 LOGGER.warning("Invalid keyboard value: " + keyboard);
113 keyboard = 0;
114 isInvalid = true;
115 }
116 if (navigation < 0 || navigation > 4) {
117 LOGGER.warning("Invalid navigation value: " + navigation);
118 navigation = 0;
119 isInvalid = true;
120 }
121
122 if (localeScript != null && localeScript.length != 0) {
123 if (localeScript[0] == '\00') {
124 localeScript = null;
125 }
126 } else {
127 localeScript = null;
128 }
129
130 if (localeVariant != null && localeVariant.length != 0) {
131 if (localeVariant[0] == '\00') {
132 localeVariant = null;
133 }
134 } else {
135 localeVariant = null;
136 }
137
138 this.mcc = mcc;
139 this.mnc = mnc;
140 this.language = language;
141 this.region = region;
142 this.orientation = orientation;
143 this.touchscreen = touchscreen;
144 this.density = density;
145 this.keyboard = keyboard;
146 this.navigation = navigation;
147 this.inputFlags = inputFlags;
148 this.screenWidth = screenWidth;
149 this.screenHeight = screenHeight;
150 this.sdkVersion = sdkVersion;
151 this.screenLayout = screenLayout;
152 this.uiMode = uiMode;
153 this.smallestScreenWidthDp = smallestScreenWidthDp;
154 this.screenWidthDp = screenWidthDp;
155 this.screenHeightDp = screenHeightDp;
156 this.localeScript = localeScript;
157 this.localeVariant = localeVariant;
158 this.screenLayout2 = screenLayout2;
159 this.colorMode = colorMode;
160 this.isInvalid = isInvalid;
161 this.size = size;
162 mQualifiers = generateQualifiers();
163 }
164
165 public String getQualifiers() {
166 return mQualifiers;
167 }
168
169 private String generateQualifiers() {
170 StringBuilder ret = new StringBuilder();
171 if (mcc != 0) {
172 ret.append("-mcc").append(String.format("%03d", mcc));
173 if (mnc != MNC_ZERO) {
174 if (mnc != 0) {
175 ret.append("-mnc");
176 if (size <= 32) {
177 if (mnc > 0 && mnc < 10) {
178 ret.append(String.format("%02d", mnc));
179 } else {
180 ret.append(String.format("%03d", mnc));
181 }
182 } else {
183 ret.append(mnc);
184 }
185 }
186 } else {
187 ret.append("-mnc00");
188 }
189 } else {
190 if (mnc != 0) {
191 ret.append("-mnc").append(mnc);
192 }
193 }
194 ret.append(getLocaleString());
195
196 switch (screenLayout & MASK_LAYOUTDIR) {
197 case SCREENLAYOUT_LAYOUTDIR_RTL:
198 ret.append("-ldrtl");
199 break;
200 case SCREENLAYOUT_LAYOUTDIR_LTR:
201 ret.append("-ldltr");
202 break;
203 }
204 if (smallestScreenWidthDp != 0) {
205 ret.append("-sw").append(smallestScreenWidthDp).append("dp");
206 }
207 if (screenWidthDp != 0) {
208 ret.append("-w").append(screenWidthDp).append("dp");
209 }
210 if (screenHeightDp != 0) {
211 ret.append("-h").append(screenHeightDp).append("dp");
212 }
213 switch (screenLayout & MASK_SCREENSIZE) {
214 case SCREENSIZE_SMALL:
215 ret.append("-small");
216 break;
217 case SCREENSIZE_NORMAL:
218 ret.append("-normal");
219 break;
220 case SCREENSIZE_LARGE:
221 ret.append("-large");
222 break;
223 case SCREENSIZE_XLARGE:
224 ret.append("-xlarge");
225 break;
226 }
227 switch (screenLayout & MASK_SCREENLONG) {
228 case SCREENLONG_YES:
229 ret.append("-long");
230 break;
231 case SCREENLONG_NO:
232 ret.append("-notlong");
233 break;
234 }
235 switch (screenLayout2 & MASK_SCREENROUND) {
236 case SCREENLAYOUT_ROUND_NO:
237 ret.append("-notround");
238 break;
239 case SCREENLAYOUT_ROUND_YES:
240 ret.append("-round");
241 break;
242 }
243 switch (colorMode & COLOR_HDR_MASK) {
244 case COLOR_HDR_YES:
245 ret.append("-highdr");
246 break;
247 case COLOR_HDR_NO:
248 ret.append("-lowdr");
249 break;
250 }
251 switch (colorMode & COLOR_WIDE_MASK) {
252 case COLOR_WIDE_YES:
253 ret.append("-widecg");
254 break;
255 case COLOR_WIDE_NO:
256 ret.append("-nowidecg");
257 break;
258 }
259 switch (orientation) {
260 case ORIENTATION_PORT:
261 ret.append("-port");
262 break;
263 case ORIENTATION_LAND:
264 ret.append("-land");
265 break;
266 case ORIENTATION_SQUARE:
267 ret.append("-square");
268 break;
269 }
270 switch (uiMode & MASK_UI_MODE_TYPE) {
271 case UI_MODE_TYPE_CAR:
272 ret.append("-car");
273 break;
274 case UI_MODE_TYPE_DESK:
275 ret.append("-desk");
276 break;
277 case UI_MODE_TYPE_TELEVISION:
278 ret.append("-television");
279 break;
280 case UI_MODE_TYPE_SMALLUI:
281 ret.append("-smallui");
282 break;
283 case UI_MODE_TYPE_MEDIUMUI:
284 ret.append("-mediumui");
285 break;
286 case UI_MODE_TYPE_LARGEUI:
287 ret.append("-largeui");
288 break;
289 case UI_MODE_TYPE_GODZILLAUI:
290 ret.append("-godzillaui");
291 break;
292 case UI_MODE_TYPE_HUGEUI:
293 ret.append("-hugeui");
294 break;
295 case UI_MODE_TYPE_APPLIANCE:
296 ret.append("-appliance");
297 break;
298 case UI_MODE_TYPE_WATCH:
299 ret.append("-watch");
300 break;
301 case UI_MODE_TYPE_VR_HEADSET:
302 ret.append("-vrheadset");
303 break;
304 }
305 switch (uiMode & MASK_UI_MODE_NIGHT) {
306 case UI_MODE_NIGHT_YES:
307 ret.append("-night");
308 break;
309 case UI_MODE_NIGHT_NO:
310 ret.append("-notnight");
311 break;
312 }
313 switch (density) {
314 case DENSITY_DEFAULT:
315 break;
316 case DENSITY_LOW:
317 ret.append("-ldpi");
318 break;
319 case DENSITY_MEDIUM:
320 ret.append("-mdpi");
321 break;
322 case DENSITY_HIGH:
323 ret.append("-hdpi");
324 break;
325 case DENSITY_TV:
326 ret.append("-tvdpi");
327 break;
328 case DENSITY_XHIGH:
329 ret.append("-xhdpi");
330 break;
331 case DENSITY_XXHIGH:
332 ret.append("-xxhdpi");
333 break;
334 case DENSITY_XXXHIGH:
335 ret.append("-xxxhdpi");
336 break;
337 case DENSITY_ANY:
338 ret.append("-anydpi");
339 break;
340 case DENSITY_NONE:
341 ret.append("-nodpi");
342 break;
343 default:
344 ret.append('-').append(density).append("dpi");
345 }
346 switch (touchscreen) {
347 case TOUCHSCREEN_NOTOUCH:
348 ret.append("-notouch");
349 break;
350 case TOUCHSCREEN_STYLUS:
351 ret.append("-stylus");
352 break;
353 case TOUCHSCREEN_FINGER:
354 ret.append("-finger");
355 break;
356 }
357 switch (inputFlags & MASK_KEYSHIDDEN) {
358 case KEYSHIDDEN_NO:
359 ret.append("-keysexposed");
360 break;
361 case KEYSHIDDEN_YES:
362 ret.append("-keyshidden");
363 break;
364 case KEYSHIDDEN_SOFT:
365 ret.append("-keyssoft");
366 break;
367 }
368 switch (keyboard) {
369 case KEYBOARD_NOKEYS:
370 ret.append("-nokeys");
371 break;
372 case KEYBOARD_QWERTY:
373 ret.append("-qwerty");
374 break;
375 case KEYBOARD_12KEY:
376 ret.append("-12key");
377 break;
378 }
379 switch (inputFlags & MASK_NAVHIDDEN) {
380 case NAVHIDDEN_NO:
381 ret.append("-navexposed");
382 break;
383 case NAVHIDDEN_YES:
384 ret.append("-navhidden");
385 break;
386 }
387 switch (navigation) {
388 case NAVIGATION_NONAV:
389 ret.append("-nonav");
390 break;
391 case NAVIGATION_DPAD:
392 ret.append("-dpad");
393 break;
394 case NAVIGATION_TRACKBALL:
395 ret.append("-trackball");
396 break;
397 case NAVIGATION_WHEEL:
398 ret.append("-wheel");
399 break;
400 }
401 if (screenWidth != 0 && screenHeight != 0) {
402 if (screenWidth > screenHeight) {
403 ret.append(String.format("-%dx%d", screenWidth, screenHeight));
404 } else {
405 ret.append(String.format("-%dx%d", screenHeight, screenWidth));
406 }
407 }
408 if (sdkVersion > 0 && sdkVersion >= getNaturalSdkVersionRequirement()) {
409 ret.append("-v").append(sdkVersion);
410 }
411 if (isInvalid) {
412 ret.append("-ERR").append(sErrCounter++);
413 }
414
415 return ret.toString();
416 }
417
418 private short getNaturalSdkVersionRequirement() {
419 if ((uiMode & MASK_UI_MODE_TYPE) == UI_MODE_TYPE_VR_HEADSET || (colorMode & COLOR_WIDE_MASK) != 0 || ((colorMode & COLOR_HDR_MASK) != 0)) {
420 return SDK_OREO;
421 }
422 if ((screenLayout2 & MASK_SCREENROUND) != 0) {
423 return SDK_MNC;
424 }
425 if (density == DENSITY_ANY) {
426 return SDK_LOLLIPOP;
427 }
428 if (smallestScreenWidthDp != 0 || screenWidthDp != 0 || screenHeightDp != 0) {
429 return SDK_HONEYCOMB_MR2;
430 }
431 if ((uiMode & (MASK_UI_MODE_TYPE | MASK_UI_MODE_NIGHT)) != UI_MODE_NIGHT_ANY) {
432 return SDK_FROYO;
433 }
434 if ((screenLayout & (MASK_SCREENSIZE | MASK_SCREENLONG)) != SCREENSIZE_ANY || density != DENSITY_DEFAULT) {
435 return SDK_DONUT;
436 }
437 return 0;
438 }
439
440 private String getLocaleString() {
441 StringBuilder sb = new StringBuilder();
442
443 // check for old style non BCP47 tags
444 // allows values-xx-rXX, values-xx, values-xxx-rXX
445 // denies values-xxx, anything else
446 if (localeVariant == null && localeScript == null && (region[0] != '\00' || language[0] != '\00') &&
447 region.length != 3) {
448 sb.append("-").append(language);
449 if (region[0] != '\00') {
450 sb.append("-r").append(region);
451 }
452 } else { // BCP47
453 if (language[0] == '\00' && region[0] == '\00') {
454 return sb.toString(); // early return, no language or region
455 }
456 sb.append("-b+");
457 if (language[0] != '\00') {
458 sb.append(language);
459 }
460 if (localeScript != null && localeScript.length == 4) {
461 sb.append("+").append(localeScript);
462 }
463 if ((region.length == 2 || region.length == 3) && region[0] != '\00') {
464 sb.append("+").append(region);
465 }
466 if (localeVariant != null && localeVariant.length >= 5) {
467 sb.append("+").append(toUpper(localeVariant));
468 }
469 }
470 return sb.toString();
471 }
472
473 private String toUpper(char[] character) {
474 StringBuilder sb = new StringBuilder();
475 for (char ch: character) {
476 sb.append(Character.toUpperCase(ch));
477 }
478 return sb.toString();
479 }
480
481
482 @Override
483 public String toString() {
484 return !getQualifiers().equals("") ? getQualifiers() : "[DEFAULT]";
485 }
486
487 @Override
488 public boolean equals(Object obj) {
489 if (obj == null) {
490 return false;
491 }
492 if (getClass() != obj.getClass()) {
493 return false;
494 }
495 final ResConfigFlags other = (ResConfigFlags) obj;
496 return this.mQualifiers.equals(other.mQualifiers);
497 }
498
499 @Override
500 public int hashCode() {
501 int hash = 17;
502 hash = 31 * hash + this.mQualifiers.hashCode();
503 return hash;
504 }
505
506 // TODO: Dirty static hack. This counter should be a part of ResPackage,
507 // but it would be hard right now and this feature is very rarely used.
508 private static int sErrCounter = 0;
509
510 public final static byte SDK_BASE = 1;
511 public final static byte SDK_BASE_1_1 = 2;
512 public final static byte SDK_CUPCAKE = 3;
513 public final static byte SDK_DONUT = 4;
514 public final static byte SDK_ECLAIR = 5;
515 public final static byte SDK_ECLAIR_0_1 = 6;
516 public final static byte SDK_ECLAIR_MR1 = 7;
517 public final static byte SDK_FROYO = 8;
518 public final static byte SDK_GINGERBREAD = 9;
519 public final static byte SDK_GINGERBREAD_MR1 = 10;
520 public final static byte SDK_HONEYCOMB = 11;
521 public final static byte SDK_HONEYCOMB_MR1 = 12;
522 public final static byte SDK_HONEYCOMB_MR2 = 13;
523 public final static byte SDK_ICE_CREAM_SANDWICH = 14;
524 public final static byte SDK_ICE_CREAM_SANDWICH_MR1 = 15;
525 public final static byte SDK_JELLY_BEAN = 16;
526 public final static byte SDK_JELLY_BEAN_MR1 = 17;
527 public final static byte SDK_JELLY_BEAN_MR2 = 18;
528 public final static byte SDK_KITKAT = 19;
529 public final static byte SDK_LOLLIPOP = 21;
530 public final static byte SDK_LOLLIPOP_MR1 = 22;
531 public final static byte SDK_MNC = 23;
532 public final static byte SDK_NOUGAT = 24;
533 public final static byte SDK_NOUGAT_MR1 = 25;
534 public final static byte SDK_OREO = 26;
535 public final static byte SDK_OREO_MR1 = 27;
536 public final static byte SDK_P = 28;
537 public final static byte SDK_Q = 29;
538 public final static byte SDK_R = 30;
539 public final static byte SDK_S = 31;
540 public final static byte SDK_S_V2 = 32;
541 public final static byte SDK_T = 33;
542
543 // AOSP has this as 10,000 for dev purposes.
544 // platform_frameworks_base/commit/c7a1109a1fe0771d4c9b572dcf178e2779fc4f2d
545 public final static int SDK_DEVELOPMENT = 10000;
546
547 public final static byte ORIENTATION_ANY = 0;
548 public final static byte ORIENTATION_PORT = 1;
549 public final static byte ORIENTATION_LAND = 2;
550 public final static byte ORIENTATION_SQUARE = 3;
551
552 public final static byte TOUCHSCREEN_ANY = 0;
553 public final static byte TOUCHSCREEN_NOTOUCH = 1;
554 public final static byte TOUCHSCREEN_STYLUS = 2;
555 public final static byte TOUCHSCREEN_FINGER = 3;
556
557 public final static int DENSITY_DEFAULT = 0;
558 public final static int DENSITY_LOW = 120;
559 public final static int DENSITY_MEDIUM = 160;
560 public final static int DENSITY_400 = 190;
561 public final static int DENSITY_TV = 213;
562 public final static int DENSITY_HIGH = 240;
563 public final static int DENSITY_XHIGH = 320;
564 public final static int DENSITY_XXHIGH = 480;
565 public final static int DENSITY_XXXHIGH = 640;
566 public final static int DENSITY_ANY = 0xFFFE;
567 public final static int DENSITY_NONE = 0xFFFF;
568
569 public final static int MNC_ZERO = -1;
570
571 public final static short MASK_LAYOUTDIR = 0xc0;
572 public final static short SCREENLAYOUT_LAYOUTDIR_ANY = 0x00;
573 public final static short SCREENLAYOUT_LAYOUTDIR_LTR = 0x40;
574 public final static short SCREENLAYOUT_LAYOUTDIR_RTL = 0x80;
575 public final static short SCREENLAYOUT_LAYOUTDIR_SHIFT = 0x06;
576
577 public final static short MASK_SCREENROUND = 0x03;
578 public final static short SCREENLAYOUT_ROUND_ANY = 0;
579 public final static short SCREENLAYOUT_ROUND_NO = 0x1;
580 public final static short SCREENLAYOUT_ROUND_YES = 0x2;
581
582 public final static byte KEYBOARD_ANY = 0;
583 public final static byte KEYBOARD_NOKEYS = 1;
584 public final static byte KEYBOARD_QWERTY = 2;
585 public final static byte KEYBOARD_12KEY = 3;
586
587 public final static byte NAVIGATION_ANY = 0;
588 public final static byte NAVIGATION_NONAV = 1;
589 public final static byte NAVIGATION_DPAD = 2;
590 public final static byte NAVIGATION_TRACKBALL = 3;
591 public final static byte NAVIGATION_WHEEL = 4;
592
593 public final static byte MASK_KEYSHIDDEN = 0x3;
594 public final static byte KEYSHIDDEN_ANY = 0x0;
595 public final static byte KEYSHIDDEN_NO = 0x1;
596 public final static byte KEYSHIDDEN_YES = 0x2;
597 public final static byte KEYSHIDDEN_SOFT = 0x3;
598
599 public final static byte MASK_NAVHIDDEN = 0xc;
600 public final static byte NAVHIDDEN_ANY = 0x0;
601 public final static byte NAVHIDDEN_NO = 0x4;
602 public final static byte NAVHIDDEN_YES = 0x8;
603
604 public final static byte MASK_SCREENSIZE = 0x0f;
605 public final static byte SCREENSIZE_ANY = 0x00;
606 public final static byte SCREENSIZE_SMALL = 0x01;
607 public final static byte SCREENSIZE_NORMAL = 0x02;
608 public final static byte SCREENSIZE_LARGE = 0x03;
609 public final static byte SCREENSIZE_XLARGE = 0x04;
610
611 public final static byte MASK_SCREENLONG = 0x30;
612 public final static byte SCREENLONG_ANY = 0x00;
613 public final static byte SCREENLONG_NO = 0x10;
614 public final static byte SCREENLONG_YES = 0x20;
615
616 public final static byte MASK_UI_MODE_TYPE = 0x0f;
617 public final static byte UI_MODE_TYPE_ANY = 0x00;
618 public final static byte UI_MODE_TYPE_NORMAL = 0x01;
619 public final static byte UI_MODE_TYPE_DESK = 0x02;
620 public final static byte UI_MODE_TYPE_CAR = 0x03;
621 public final static byte UI_MODE_TYPE_TELEVISION = 0x04;
622 public final static byte UI_MODE_TYPE_APPLIANCE = 0x05;
623 public final static byte UI_MODE_TYPE_WATCH = 0x06;
624 public final static byte UI_MODE_TYPE_VR_HEADSET = 0x07;
625
626 // start - miui
627 public final static byte UI_MODE_TYPE_GODZILLAUI = 0x0b;
628 public final static byte UI_MODE_TYPE_SMALLUI = 0x0c;
629 public final static byte UI_MODE_TYPE_MEDIUMUI = 0x0d;
630 public final static byte UI_MODE_TYPE_LARGEUI = 0x0e;
631 public final static byte UI_MODE_TYPE_HUGEUI = 0x0f;
632 // end - miui
633
634 public final static byte MASK_UI_MODE_NIGHT = 0x30;
635 public final static byte UI_MODE_NIGHT_ANY = 0x00;
636 public final static byte UI_MODE_NIGHT_NO = 0x10;
637 public final static byte UI_MODE_NIGHT_YES = 0x20;
638
639 public final static byte COLOR_HDR_MASK = 0xC;
640 public final static byte COLOR_HDR_NO = 0x4;
641 public final static byte COLOR_HDR_SHIFT = 0x2;
642 public final static byte COLOR_HDR_UNDEFINED = 0x0;
643 public final static byte COLOR_HDR_YES = 0x8;
644
645 public final static byte COLOR_UNDEFINED = 0x0;
646
647 public final static byte COLOR_WIDE_UNDEFINED = 0x0;
648 public final static byte COLOR_WIDE_NO = 0x1;
649 public final static byte COLOR_WIDE_YES = 0x2;
650 public final static byte COLOR_WIDE_MASK = 0x3;
651
652 private static final Logger LOGGER = Logger.getLogger(ResConfigFlags.class.getName());
653 }