package com.huawei.hms.utils; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.ComponentInfo; import android.content.pm.PackageItemInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Build; import android.os.Bundle; import android.text.TextUtils; import android.util.AndroidException; import android.util.Pair; import com.huawei.hms.android.SystemUtils; import com.huawei.hms.common.HmsCheckedState; import com.huawei.hms.common.PackageConstants; import com.huawei.hms.framework.common.ContainerUtils; import com.huawei.hms.support.api.entity.common.CommonConstant; import com.huawei.hms.support.log.HMSLog; import com.huawei.hms.utils.PackageManagerHelper; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; /* loaded from: classes2.dex */ public class HMSPackageManager { /* renamed from: o, reason: collision with root package name */ private static HMSPackageManager f6499o; private static final Object p = new Object(); private static final Object q = new Object(); private static final Object r = new Object(); private static final Map s; private final Context a; private final PackageManagerHelper b; private String c; private String d; private int e; private String f; private String g; private String h; private String i; private int j; private int k; private long l; private boolean m; private int n; /* loaded from: classes2.dex */ public static class PackagePriorityInfo implements Comparable { private String a; private String b; private String c; private String d; private String e; private Long f; public PackagePriorityInfo(String str, String str2, String str3, String str4, String str5, long j) { this.a = str; this.b = str2; this.c = str3; this.d = str4; this.e = str5; this.f = Long.valueOf(j); } @Override // java.lang.Comparable public int compareTo(PackagePriorityInfo packagePriorityInfo) { if (TextUtils.equals(this.e, packagePriorityInfo.e)) { return this.f.compareTo(packagePriorityInfo.f); } return this.e.compareTo(packagePriorityInfo.e); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class a implements Comparator { final HMSPackageManager a; a(HMSPackageManager hMSPackageManager) { this.a = hMSPackageManager; } @Override // java.util.Comparator /* renamed from: a, reason: merged with bridge method [inline-methods] */ public int compare(ResolveInfo resolveInfo, ResolveInfo resolveInfo2) { String str = (String) PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(ComponentInfo.class.getField("applicationInfo").get(resolveInfo.serviceInfo)); String str2 = (String) PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(ComponentInfo.class.getField("applicationInfo").get(resolveInfo2.serviceInfo)); if (HMSPackageManager.s.containsKey(str) && HMSPackageManager.s.containsKey(str2)) { return str.compareTo(str2); } if (HMSPackageManager.s.containsKey(str)) { return -1; } return HMSPackageManager.s.containsKey(str2) ? 1 : 0; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class b implements Runnable { final HMSPackageManager a; b(HMSPackageManager hMSPackageManager) { this.a = hMSPackageManager; } @Override // java.lang.Runnable public void run() { HMSLog.i("HMSPackageManager", "enter asyncOnceCheckMDMState"); try { List queryIntentServices = this.a.a.getPackageManager().queryIntentServices(new Intent("com.huawei.hms.core.aidlservice"), 128); if (queryIntentServices == null || queryIntentServices.size() == 0) { return; } Iterator it = queryIntentServices.iterator(); while (it.hasNext()) { if ("com.huawei.hwid".equals(PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(ComponentInfo.class.getField("applicationInfo").get(it.next().serviceInfo)))) { this.a.d(); } } HMSLog.i("HMSPackageManager", "quit asyncOnceCheckMDMState"); } catch (Exception e) { StringBuilder sb = new StringBuilder("asyncOnceCheckMDMState query hms action failed. "); sb.append(e.getMessage()); HMSLog.e("HMSPackageManager", sb.toString()); } } } static { HashMap hashMap = new HashMap(); s = hashMap; hashMap.put("com.huawei.hwid", "B92825C2BD5D6D6D1E7F39EECD17843B7D9016F611136B75441BC6F4D3F00F05"); hashMap.put("com.huawei.hwid.tv", "3517262215D8D3008CBF888750B6418EDC4D562AC33ED6874E0D73ABA667BC3C"); } private HMSPackageManager(Context context) { this.a = context; this.b = new PackageManagerHelper(context); } private boolean c(String str, String str2) { return Objects.equals(str2, this.b.getPackageSigningCertificate(str)) || Objects.equals(str2, this.b.getPackageSignature(str)); } private Pair d(String str, String str2) { if (!s.containsKey(str) || !PackageConstants.SERVICES_SIGNATURE_V3.equalsIgnoreCase(str2)) { return null; } this.n = 3; return new Pair<>(str, str2); } private void e(String str) { if (SystemUtils.isHuawei() || SystemUtils.isSystemApp(this.a, str) || Build.VERSION.SDK_INT < 28 || b(str)) { AgHmsUpdateState.getInstance().setCheckedState(HmsCheckedState.NOT_NEED_UPDATE); } } private void f() { synchronized (q) { this.c = null; this.d = null; this.e = 0; } } public static HMSPackageManager getInstance(Context context) { synchronized (p) { if (f6499o == null && context != null) { if (context.getApplicationContext() != null) { f6499o = new HMSPackageManager(context.getApplicationContext()); } else { f6499o = new HMSPackageManager(context); } f6499o.k(); f6499o.b(); } } return f6499o; } private Pair h() { Pair g = g(); if (g != null) { StringBuilder sb = new StringBuilder("aidlService pkgName: "); sb.append((String) g.first); HMSLog.i("HMSPackageManager", sb.toString()); this.h = "com.huawei.hms.core.aidlservice"; this.i = null; return g; } ArrayList i = i(); if (i == null) { HMSLog.e("HMSPackageManager", "PackagePriorityInfo list is null"); return null; } Iterator it = i.iterator(); while (it.hasNext()) { PackagePriorityInfo next = it.next(); String str = next.a; String str2 = next.b; String str3 = next.c; String str4 = next.d; String packageSignature = this.b.getPackageSignature(str); StringBuilder sb2 = new StringBuilder(); sb2.append(str); sb2.append(ContainerUtils.FIELD_DELIMITER); sb2.append(packageSignature); sb2.append(ContainerUtils.FIELD_DELIMITER); sb2.append(str2); if (a(sb2.toString(), str3, str4)) { StringBuilder sb3 = new StringBuilder("result: "); sb3.append(str); sb3.append(", "); sb3.append(str2); sb3.append(", "); sb3.append(next.f); HMSLog.i("HMSPackageManager", sb3.toString()); this.h = PackageConstants.GENERAL_SERVICES_ACTION; d(str2); return new Pair<>(str, packageSignature); } } return null; } private ArrayList i() { try { List queryIntentServices = this.a.getPackageManager().queryIntentServices(new Intent(PackageConstants.GENERAL_SERVICES_ACTION), 128); if (queryIntentServices != null && !queryIntentServices.isEmpty()) { ArrayList arrayList = new ArrayList<>(); for (ResolveInfo resolveInfo : queryIntentServices) { String str = (String) PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(ComponentInfo.class.getField("applicationInfo").get(resolveInfo.serviceInfo)); long packageFirstInstallTime = this.b.getPackageFirstInstallTime(str); Bundle bundle = (Bundle) PackageItemInfo.class.getField("metaData").get(resolveInfo.serviceInfo); if (bundle == null) { StringBuilder sb = new StringBuilder("package "); sb.append(str); sb.append(" get metaData is null"); HMSLog.e("HMSPackageManager", sb.toString()); } else { String a2 = a(bundle, "hms_app_checker_config"); String a3 = a(a2); if (TextUtils.isEmpty(a3)) { HMSLog.i("HMSPackageManager", "get priority fail. hmsCheckerCfg: ".concat(String.valueOf(a2))); } else { String a4 = a(bundle, "hms_app_signer_v2"); if (TextUtils.isEmpty(a4)) { HMSLog.i("HMSPackageManager", "get signerV2 fail."); } else { String a5 = a(bundle, "hms_app_cert_chain"); if (TextUtils.isEmpty(a5)) { HMSLog.i("HMSPackageManager", "get certChain fail."); } else { StringBuilder sb2 = new StringBuilder("add: "); sb2.append(str); sb2.append(", "); sb2.append(a2); sb2.append(", "); sb2.append(packageFirstInstallTime); HMSLog.i("HMSPackageManager", sb2.toString()); arrayList.add(new PackagePriorityInfo(str, a2, a4, a5, a3, packageFirstInstallTime)); } } } } } Collections.sort(arrayList); return arrayList; } HMSLog.e("HMSPackageManager", "query aglite action, resolveInfoList is null or empty"); return null; } catch (Exception e) { StringBuilder sb3 = new StringBuilder("query aglite action failed. "); sb3.append(e.getMessage()); HMSLog.e("HMSPackageManager", sb3.toString()); return null; } } private void j() { synchronized (q) { Pair g = g(); if (g == null) { HMSLog.e("HMSPackageManager", " Failed to find HMS apk"); f(); return; } String str = (String) g.first; this.c = str; this.d = (String) g.second; this.e = this.b.getPackageVersionCode(str); StringBuilder sb = new StringBuilder(" Succeed to find HMS apk: "); sb.append(this.c); sb.append(" version: "); sb.append(this.e); HMSLog.i("HMSPackageManager", sb.toString()); } } private void k() { synchronized (q) { Pair h = h(); if (h == null) { HMSLog.e("HMSPackageManager", " Failed to find HMS apk"); e(); AgHmsUpdateState.getInstance().setCheckedState(HmsCheckedState.NOT_NEED_UPDATE); return; } this.f = (String) h.first; this.g = (String) h.second; this.j = this.b.getPackageVersionCode(getHMSPackageNameForMultiService()); e(this.f); StringBuilder sb = new StringBuilder(" Succeed to find HMS apk: "); sb.append(this.f); sb.append(" version: "); sb.append(this.j); HMSLog.i("HMSPackageManager", sb.toString()); } } private boolean l() { Bundle bundle; PackageManager packageManager = this.a.getPackageManager(); if (packageManager == null) { HMSLog.e("HMSPackageManager", "In isMinApkVersionEffective, Failed to get 'PackageManager' instance."); return true; } try { } catch (AndroidException unused) { HMSLog.e("HMSPackageManager", "In isMinApkVersionEffective, Failed to read meta data for HMSCore API level."); } catch (RuntimeException e) { HMSLog.e("HMSPackageManager", "In isMinApkVersionEffective, Failed to read meta data for HMSCore API level.", e); } if (!TextUtils.isEmpty(this.h) && (this.h.equals(PackageConstants.GENERAL_SERVICES_ACTION) || this.h.equals(PackageConstants.INTERNAL_SERVICES_ACTION))) { StringBuilder sb = new StringBuilder("action = "); sb.append(this.h); sb.append(" exist"); HMSLog.i("HMSPackageManager", sb.toString()); return false; } ApplicationInfo applicationInfo = packageManager.getPackageInfo(getHMSPackageName(), 128).applicationInfo; if (applicationInfo != null && (bundle = (Bundle) PackageItemInfo.class.getField("metaData").get(applicationInfo)) != null && bundle.containsKey("com.huawei.hms.kit.api_level:hmscore") && (getHmsVersionCode() >= 50000000 || getHmsVersionCode() <= 19999999)) { HMSLog.i("HMSPackageManager", "MinApkVersion is disabled."); return false; } return true; } public String getHMSPackageName() { HMSLog.i("HMSPackageManager", "Enter getHMSPackageName"); refresh(); String str = this.c; if (str != null) { if (PackageManagerHelper.PackageStates.NOT_INSTALLED.equals(this.b.getPackageStates(str))) { HMSLog.i("HMSPackageManager", "The package name is not installed and needs to be refreshed again"); j(); } String str2 = this.c; if (str2 != null) { return str2; } } HMSLog.i("HMSPackageManager", "return default packageName: com.huawei.hwid"); return "com.huawei.hwid"; } public String getHMSPackageNameForMultiService() { HMSLog.i("HMSPackageManager", "Enter getHMSPackageNameForMultiService"); refreshForMultiService(); String str = this.f; if (str != null) { if (PackageManagerHelper.PackageStates.NOT_INSTALLED.equals(this.b.getPackageStates(str))) { HMSLog.i("HMSPackageManager", "The package name is not installed and needs to be refreshed again"); k(); } String str2 = this.f; if (str2 != null) { return str2; } } HMSLog.i("HMSPackageManager", "return default packageName: com.huawei.hwid"); return "com.huawei.hwid"; } public PackageManagerHelper.PackageStates getHMSPackageStates() { synchronized (p) { refresh(); PackageManagerHelper.PackageStates packageStates = this.b.getPackageStates(this.c); PackageManagerHelper.PackageStates packageStates2 = PackageManagerHelper.PackageStates.NOT_INSTALLED; if (packageStates == packageStates2) { f(); return packageStates2; } if ("com.huawei.hwid".equals(this.c) && d() == 1) { return PackageManagerHelper.PackageStates.SPOOF; } return (packageStates != PackageManagerHelper.PackageStates.ENABLED || c(this.c, this.d)) ? packageStates : packageStates2; } } public PackageManagerHelper.PackageStates getHMSPackageStatesForMultiService() { synchronized (p) { refreshForMultiService(); PackageManagerHelper.PackageStates packageStates = this.b.getPackageStates(this.f); PackageManagerHelper.PackageStates packageStates2 = PackageManagerHelper.PackageStates.NOT_INSTALLED; if (packageStates == packageStates2) { e(); return packageStates2; } if ("com.huawei.hwid".equals(this.f) && d() == 1) { return PackageManagerHelper.PackageStates.SPOOF; } return (packageStates != PackageManagerHelper.PackageStates.ENABLED || c(this.f, this.g)) ? packageStates : packageStates2; } } public int getHmsMultiServiceVersion() { return this.b.getPackageVersionCode(getHMSPackageNameForMultiService()); } public int getHmsVersionCode() { return this.b.getPackageVersionCode(getHMSPackageName()); } public String getServiceAction() { return !TextUtils.isEmpty(this.h) ? this.h : "com.huawei.hms.core.aidlservice"; } public boolean isApkNeedUpdate(int i) { int hmsVersionCode = getHmsVersionCode(); StringBuilder sb = new StringBuilder("current versionCode:"); sb.append(hmsVersionCode); sb.append(", target version requirements: "); sb.append(i); HMSLog.i("HMSPackageManager", sb.toString()); return hmsVersionCode < i; } public boolean isApkUpdateNecessary(int i) { if (isUpdateHmsForThirdPartyDevice()) { return true; } int hmsVersionCode = getHmsVersionCode(); StringBuilder sb = new StringBuilder("current versionCode:"); sb.append(hmsVersionCode); sb.append(", minimum version requirements: "); sb.append(i); HMSLog.i("HMSPackageManager", sb.toString()); return l() && hmsVersionCode < i; } public boolean isUpdateHmsForThirdPartyDevice() { return "com.huawei.hwid".equals(this.f) && AgHmsUpdateState.getInstance().isUpdateHms(); } public void refresh() { if (TextUtils.isEmpty(this.c) || TextUtils.isEmpty(this.d)) { j(); } c(this.c); } public void refreshForMultiService() { if (TextUtils.isEmpty(this.f) || TextUtils.isEmpty(this.g)) { k(); } c(this.f); } public void resetMultiServiceState() { e(); } private boolean b(String str) { return !"com.huawei.hwid".equals(str) || this.n == 3; } private Pair g() { try { List queryIntentServices = this.a.getPackageManager().queryIntentServices(new Intent("com.huawei.hms.core.aidlservice"), 128); if (queryIntentServices != null && queryIntentServices.size() != 0) { a(queryIntentServices); for (ResolveInfo resolveInfo : queryIntentServices) { String str = (String) PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(ComponentInfo.class.getField("applicationInfo").get(resolveInfo.serviceInfo)); String packageSigningCertificate = this.b.getPackageSigningCertificate(str); String packageSignature = this.b.getPackageSignature(str); Pair d = d(str, packageSigningCertificate); if (d != null) { HMSLog.i("HMSPackageManager", "signature V3 check success"); return d; } Pair a2 = a((Bundle) PackageItemInfo.class.getField("metaData").get(resolveInfo.serviceInfo), str, packageSigningCertificate, packageSignature); if (a2 != null) { HMSLog.i("HMSPackageManager", "DSS signature check success"); return a2; } Pair a3 = a(str, packageSignature); if (a3 != null) { HMSLog.i("HMSPackageManager", "signature V2 check success"); return a3; } } return null; } HMSLog.e("HMSPackageManager", "query hms action, resolveInfoList is null or empty."); return null; } catch (Exception e) { StringBuilder sb = new StringBuilder("getHmsPackageName query hms action failed. "); sb.append(e.getMessage()); HMSLog.e("HMSPackageManager", sb.toString()); return null; } } private void a(List list) { if (list.size() <= 1) { return; } Collections.sort(list, new a(this)); } private boolean b(String str, String str2) { Map map = s; return map.containsKey(str) && map.get(str).equalsIgnoreCase(str2); } private void c(String str) { if ("com.huawei.hwid".equals(str) && AgHmsUpdateState.getInstance().isUpdateHms() && this.b.getPackageVersionCode(str) >= AgHmsUpdateState.getInstance().getTargetVersionCode()) { AgHmsUpdateState.getInstance().resetUpdateState(); HMSLog.i("HMSPackageManager", "refresh update state for HMS V3"); } } public boolean hmsVerHigherThan(int i) { if (this.e >= i || !l()) { return true; } int packageVersionCode = this.b.getPackageVersionCode(getHMSPackageName()); this.e = packageVersionCode; return packageVersionCode >= i; } private void b() { new Thread(new b(this), "Thread-asyncOnceCheckMDMState").start(); } private void d(String str) { String a2 = a(str); if (TextUtils.isEmpty(a2)) { return; } this.i = a2.substring(9); } private void e() { synchronized (q) { this.f = null; this.g = null; this.h = null; this.i = null; this.j = 0; } } private Pair a(Bundle bundle, String str, String str2, String str3) { String str4; if (bundle == null) { StringBuilder sb = new StringBuilder("DSS check: "); sb.append(str); sb.append(" for metadata is null"); HMSLog.e("HMSPackageManager", sb.toString()); return null; } this.n = 2; if (a(bundle, str, str2)) { HMSLog.i("HMSPackageManager", "support DSS V3 check"); str3 = str2; str4 = "hms_app_signer_v3"; } else { str4 = "hms_app_signer"; } if (!bundle.containsKey(str4)) { StringBuilder sb2 = new StringBuilder("skip package "); sb2.append(str); sb2.append(" for no "); sb2.append(str4); HMSLog.e("HMSPackageManager", sb2.toString()); return null; } if (!bundle.containsKey("hms_app_cert_chain")) { StringBuilder sb3 = new StringBuilder("skip package "); sb3.append(str); sb3.append(" for no cert chain"); HMSLog.e("HMSPackageManager", sb3.toString()); return null; } StringBuilder sb4 = new StringBuilder(); sb4.append(str); sb4.append(ContainerUtils.FIELD_DELIMITER); sb4.append(str3); if (!a(sb4.toString(), bundle.getString(str4), bundle.getString("hms_app_cert_chain"))) { HMSLog.e("HMSPackageManager", "checkSigner failed"); return null; } if (str4.equals("hms_app_signer_v3")) { this.n = 3; } return new Pair<>(str, str3); } /* JADX INFO: Access modifiers changed from: private */ public int d() { synchronized (r) { HMSLog.i("HMSPackageManager", "enter checkHmsIsSpoof"); long packageFirstInstallTime = this.b.getPackageFirstInstallTime("com.huawei.hwid"); if (this.k != 3 && this.l == packageFirstInstallTime) { StringBuilder sb = new StringBuilder("quit checkHmsIsSpoof cached state: "); sb.append(a(this.k)); HMSLog.i("HMSPackageManager", sb.toString()); return this.k; } this.k = c() ? 2 : 1; this.l = this.b.getPackageFirstInstallTime("com.huawei.hwid"); StringBuilder sb2 = new StringBuilder("quit checkHmsIsSpoof state: "); sb2.append(a(this.k)); HMSLog.i("HMSPackageManager", sb2.toString()); return this.k; } } private boolean c() { String hmsPath = ReadApkFileUtil.getHmsPath(this.a); if (hmsPath == null) { HMSLog.i("HMSPackageManager", "hmsPath is null!"); return false; } if (!ReadApkFileUtil.isCertFound(hmsPath)) { HMSLog.i("HMSPackageManager", "NO huawer.cer in HMS!"); return false; } if (!ReadApkFileUtil.checkSignature()) { HMSLog.i("HMSPackageManager", "checkSignature fail!"); return false; } if (ReadApkFileUtil.verifyApkHash(hmsPath)) { return true; } HMSLog.i("HMSPackageManager", "verifyApkHash fail!"); return false; } private Pair a(String str, String str2) { if (b(str, str2)) { return new Pair<>(str, str2); } StringBuilder sb = new StringBuilder("check sign fail: "); sb.append(str); sb.append("_"); sb.append(str2); HMSLog.w("HMSPackageManager", sb.toString()); return null; } private boolean a(Bundle bundle, String str, String str2) { return bundle.containsKey("hms_app_signer_v3") && !b(str, str2) && Build.VERSION.SDK_INT >= 28; } private String a(Bundle bundle, String str) { if (!bundle.containsKey(str)) { StringBuilder sb = new StringBuilder("no "); sb.append(str); sb.append(" in metaData"); HMSLog.e("HMSPackageManager", sb.toString()); return null; } return bundle.getString(str); } private String a(String str) { if (TextUtils.isEmpty(str)) { return null; } int indexOf = str.indexOf("priority="); if (indexOf == -1) { HMSLog.e("HMSPackageManager", "get indexOfIdentifier -1"); return null; } int indexOf2 = str.indexOf(",", indexOf); if (indexOf2 == -1) { indexOf2 = str.length(); } return str.substring(indexOf, indexOf2); } private boolean a(String str, String str2, String str3) { if (!TextUtils.isEmpty(str2) && !TextUtils.isEmpty(str3)) { List b2 = com.huawei.hms.device.a.b(str3); if (b2.size() == 0) { HMSLog.e("HMSPackageManager", "certChain is empty"); return false; } if (!com.huawei.hms.device.a.a(com.huawei.hms.device.a.a(this.a), b2)) { HMSLog.e("HMSPackageManager", "failed to verify cert chain"); return false; } X509Certificate x509Certificate = b2.get(b2.size() - 1); if (!com.huawei.hms.device.a.a(x509Certificate, "Huawei CBG HMS")) { HMSLog.e("HMSPackageManager", "CN is invalid"); return false; } if (!com.huawei.hms.device.a.b(x509Certificate, "Huawei CBG Cloud Security Signer")) { HMSLog.e("HMSPackageManager", "OU is invalid"); return false; } if (com.huawei.hms.device.a.a(x509Certificate, str, str2)) { return true; } HMSLog.e("HMSPackageManager", "signature is invalid: ".concat(String.valueOf(str))); return false; } HMSLog.e("HMSPackageManager", "args is invalid"); return false; } private static String a(int i) { if (i == 1) { return "SPOOFED"; } if (i == 2) { return "SUCCESS"; } if (i == 3) { return "UNCHECKED"; } HMSLog.e("HMSPackageManager", "invalid checkMDM state: ".concat(String.valueOf(i))); return ""; } public void setUseOldCertificate(boolean z) { this.m = z; } public boolean isUseOldCertificate() { return this.m; } public String getInnerServiceAction() { return PackageConstants.INTERNAL_SERVICES_ACTION; } public String getHMSFingerprint() { String str = this.d; return str == null ? "B92825C2BD5D6D6D1E7F39EECD17843B7D9016F611136B75441BC6F4D3F00F05" : str; } }