/* * 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.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; } public 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(decodeColorMode(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); encodeColorMode(resConfig, split); encodeMcc(resConfig, split); encodeMnc(resConfig, split); encodeScreenSize(resConfig, split); encodeLanguageAndCountry(resConfig, split); encodeScriptAndVariant(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); } private static void encodeScriptAndVariant(ResConfig resConfig, String[] split){ if(split==null){ return; } 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 isLocaleScript(String str){ if(str==null){ return false; } Matcher matcher=PATTERN_LOCALE_SCRIPT.matcher(str); return matcher.find(); } private static boolean isLocaleVariant(String str){ if(str==null){ return false; } Matcher matcher=PATTERN_LOCALE_VARIANT.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 boolean isNull(String[] split){ if(split==null){ return true; } if(split.length==0){ return true; } boolean result=true; for(int i=0;i