(Context context, String name)
| 2603 | } |
| 2604 | |
| 2605 | static String getLocalizedAsset(Context context, String name) throws IOException { |
| 2606 | if (name == null || !name.contains(".")) |
| 2607 | throw new IllegalArgumentException(name); |
| 2608 | |
| 2609 | String[] list = context.getResources().getAssets().list(""); |
| 2610 | if (list == null) |
| 2611 | throw new IllegalArgumentException(); |
| 2612 | |
| 2613 | List<String> names = new ArrayList<>(); |
| 2614 | String[] c = name.split("\\."); |
| 2615 | List<String> assets = Arrays.asList(list); |
| 2616 | |
| 2617 | List<Locale> locales = new ArrayList<>(); |
| 2618 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) |
| 2619 | locales.add(Locale.getDefault()); |
| 2620 | else { |
| 2621 | LocaleList ll = context.getResources().getConfiguration().getLocales(); |
| 2622 | for (int i = 0; i < ll.size(); i++) |
| 2623 | locales.add(ll.get(i)); |
| 2624 | } |
| 2625 | |
| 2626 | for (Locale locale : locales) { |
| 2627 | String language = locale.getLanguage(); |
| 2628 | String country = locale.getCountry(); |
| 2629 | if ("en".equals(language) && "US".equals(country)) |
| 2630 | names.add(name); |
| 2631 | else { |
| 2632 | String localized = c[0] + "-" + language + "-r" + country + "." + c[1]; |
| 2633 | if (assets.contains(localized)) |
| 2634 | names.add(localized); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | for (Locale locale : locales) { |
| 2639 | String prefix = c[0] + "-" + locale.getLanguage(); |
| 2640 | for (String asset : assets) |
| 2641 | if (asset.startsWith(prefix)) |
| 2642 | names.add(asset); |
| 2643 | } |
| 2644 | |
| 2645 | names.add(name); |
| 2646 | |
| 2647 | String asset = names.get(0); |
| 2648 | Log.i("Using " + asset + |
| 2649 | " of " + TextUtils.join(",", names) + |
| 2650 | " (" + TextUtils.join(",", locales) + ")"); |
| 2651 | return asset; |
| 2652 | } |
| 2653 | |
| 2654 | static boolean containsWhiteSpace(String text) { |
| 2655 | return text.matches(".*\\s+.*"); |
no test coverage detected