/* * Copyright (C) 2022 github.com/REAndroid * * 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. */ package com.reandroid.lib.arsc.value; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ResConfigHelper { public static ResConfig fromQualifiers(String qualifiers){ ResConfig result=new ResConfig(); result.setConfigSize(ResConfig.SIZE_64); parseQualifiers(result, qualifiers); result.refresh(); return result; } static String toQualifier(ResConfig resConfig){ StringBuilder builder=new StringBuilder(); builder.append(decodeLanguageAndCountry(resConfig)); builder.append(decodeOrientation(resConfig)); builder.append(decodeScreenLayout(resConfig)); builder.append(decodeScreenLayout2(resConfig)); builder.append(decodeScreenSize(resConfig)); builder.append(decodeDensity(resConfig)); builder.append(decodeScreenHeightDp(resConfig)); builder.append(decodeSmallestScreenWidthDp(resConfig)); builder.append(decodeScreenWidthDp(resConfig)); builder.append(decodeNavigation(resConfig)); builder.append(decodeUiMode(resConfig)); builder.append(decodeTouchscreen(resConfig)); builder.append(decodeKeyboard(resConfig)); builder.append(decodeInputFlags(resConfig)); builder.append(decodeMccMnc(resConfig)); builder.append(decodeSdkVersion(resConfig)); return sortQualifiers(builder.toString()); } static void parseQualifiers(ResConfig resConfig, String name){ if(resConfig == null || name==null){ return; } String[] split=name.split("-"); if(isNull(split)){ return; } encode(resConfig, split); } public static String sortQualifiers(String qualifiers){ if(!qualifiers.startsWith("-")){ return qualifiers; } String[] split=qualifiers.split("-"); if(isNull(split)){ return qualifiers; } List qList=new ArrayList<>(); for(int i=0;i cmp=new Comparator() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } }; qList.sort(cmp); StringBuilder builder=new StringBuilder(); for(String q:qList){ builder.append('-'); builder.append(q); } return builder.toString(); } private static void encode(ResConfig resConfig, String[] split){ encodeDensity(resConfig, split); encodeScreenHeightDp(resConfig, split); encodeScreenWidthDp(resConfig, split); encodeSmallestScreenWidthDp(resConfig, split); encodeNavigation(resConfig, split); encodeInputFlags(resConfig, split); encodeSdkVersion(resConfig, split); encodeKeyboard(resConfig, split); encodeTouchscreen(resConfig, split); encodeUiMode(resConfig, split); encodeOrientation(resConfig, split); encodeScreenLayout(resConfig, split); encodeScreenLayout2(resConfig, split); encodeMcc(resConfig, split); encodeMnc(resConfig, split); encodeScreenSize(resConfig, split); encodeLanguageAndCountry(resConfig, split); } private static void encodeLanguageAndCountry(ResConfig resConfig, String[] split){ if(split==null){ return; } String lang=null; String country=null; for(int i=0;i2 && country.startsWith("r")){ country=country.substring(1); } char[] chs=country.toCharArray(); resConfig.setRegion(chs); } public static String decodeLanguage(char[] language){ StringBuilder builder=new StringBuilder(); if(language[0]!=0){ builder.append(language[0]).append(language[1]); } if(builder.length()==0){ return null; } return builder.toString(); } public static String decodeRegion(char[] region){ StringBuilder builder=new StringBuilder(); if(region[0]!=0){ builder.append(region[0]).append(region[1]); } if(builder.length()==0){ return null; } return builder.toString(); } public static String decodeLocale(ResConfig resConfig){ char[] region=resConfig.getRegionChars(); char[] language=resConfig.getLanguageChars(); StringBuilder builder=new StringBuilder(); if(language[0]!=0){ builder.append(language[0]).append(language[1]); } if(region[0]!=0){ if(language[0]!=0){ builder.append('-'); } builder.append(region[0]).append(region[1]); } if(builder.length()==0){ return null; } return builder.toString(); } private static String decodeLanguageAndCountry(ResConfig resConfig) { StringBuilder builder = new StringBuilder(); char[] localeVariant=resConfig.getLocaleVariant(); char[] localeScript=resConfig.getLocaleScript(); char[] region=resConfig.getRegionChars(); char[] language=resConfig.getLanguageChars(); if (localeVariant == null && localeScript == null && (region[0] != '\00' || language[0] != '\00') && region.length != 3) { builder.append("-").append(language); if (region[0] != '\00') { builder.append("-r").append(region); } } else { if (language[0] == '\00' && region[0] == '\00') { return builder.toString(); } builder.append("-b+"); if (language[0] != '\00') { builder.append(language); } if (localeScript != null && localeScript.length == 4) { builder.append("+").append(localeScript); } if ((region.length == 2 || region.length == 3) && region[0] != '\00') { builder.append("+").append(region); } if (localeVariant != null && localeVariant.length >= 5) { builder.append("+").append(toUpper(localeVariant)); } } return builder.toString(); } private static void encodeNavigation(ResConfig resConfig, String[] split){ byte navigation=0; for(int i=0;i 0 && mnc < 10) { ret.append(String.format("%02d", mnc)); } else { ret.append(String.format("%03d", mnc)); } } else { ret.append(mnc); } } } else { ret.append("-mnc00"); } } else { if (mnc != 0) { ret.append("-mnc").append(mnc); } } return ret.toString(); } private static void encodeSmallestScreenWidthDp(ResConfig resConfig, String[] split){ int val=0; for(int i=0;i height) { return String.format("%dx%d", width, height); } else { return String.format("%dx%d", height, width); } } private static void encodeScreenSize(ResConfig resConfig, String[] split){ for(int i=0;ib){ w=(short)a; h=(short)b; }else { w=(short)b; h=(short)a; } resConfig.setScreenSize(w, h); split[i]=null; break; } } private static boolean isLanguageName(String str){ if(str==null){ return false; } Matcher matcher=PATTERN_LANG_NAME.matcher(str); return matcher.find(); } private static boolean isCountryName(String str){ if(str==null){ return false; } Matcher matcher=PATTERN_COUNTRY_NAME.matcher(str); return matcher.find(); } private static char[] toUpper(char[] chs){ for(int i=0;i