package com.huawei.hms.activity; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.text.TextUtils; import android.view.KeyEvent; import com.huawei.hms.activity.internal.BusResponseCallback; import com.huawei.hms.activity.internal.BusResponseResult; import com.huawei.hms.activity.internal.ForegroundBusResponseMgr; import com.huawei.hms.activity.internal.ForegroundInnerHeader; import com.huawei.hms.adapter.AvailableAdapter; import com.huawei.hms.api.HuaweiApiAvailability; import com.huawei.hms.common.internal.RequestHeader; import com.huawei.hms.common.internal.ResponseHeader; import com.huawei.hms.support.hianalytics.HiAnalyticsConstant; import com.huawei.hms.support.hianalytics.HiAnalyticsUtil; import com.huawei.hms.support.log.HMSLog; import com.huawei.hms.utils.HMSPackageManager; import com.huawei.hms.utils.IntentUtil; import com.huawei.hms.utils.JsonUtil; import com.huawei.hms.utils.UIUtil; import com.huawei.hms.utils.Util; import java.lang.ref.WeakReference; import java.util.Map; /* loaded from: classes2.dex */ public class ForegroundBusDelegate implements IBridgeActivityDelegate { public static final String HMS_FOREGROUND_REQ_BODY = "HMS_FOREGROUND_REQ_BODY"; public static final String HMS_FOREGROUND_REQ_HEADER = "HMS_FOREGROUND_REQ_HEADER"; public static final String HMS_FOREGROUND_REQ_INNER = "HMS_FOREGROUND_REQ_INNER"; public static final String HMS_FOREGROUND_RESP_HEADER = "HMS_FOREGROUND_RESP_HEADER"; public static final String INNER_PKG_NAME = "INNER_PACKAGE_NAME"; private RequestHeader a; private String b; private ForegroundInnerHeader c = new ForegroundInnerHeader(); private ResponseHeader d; private WeakReference e; private boolean f; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class b implements AvailableAdapter.AvailableCallBack { final ForegroundBusDelegate a; private b(ForegroundBusDelegate foregroundBusDelegate) { this.a = foregroundBusDelegate; } @Override // com.huawei.hms.adapter.AvailableAdapter.AvailableCallBack public void onComplete(int i) { if (i == 0) { this.a.h(); } else { HMSLog.i("ForegroundBusDelegate", "version check failed"); this.a.a(0, "apk version is invalid"); } } } private BusResponseCallback b(String str) { return ForegroundBusResponseMgr.getInstance().get(str); } private void d() { a(HiAnalyticsConstant.HMS_SDK_BASE_START_CORE_ACTIVITY); } private void e() { if (g() == null) { HMSLog.e("ForegroundBusDelegate", "checkMinVersion failed, activity must not be null."); a(0, "checkMinVersion failed, activity must not be null."); return; } if (this.f) { h(); return; } if (!Util.isAvailableLibExist(g().getApplicationContext())) { if (HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(g().getApplicationContext(), this.c.getApkVersion()) != 0) { HMSLog.e("ForegroundBusDelegate", "checkMinVersion failed, and no available lib exists."); a(0, "apk version is invalid"); return; } else { h(); return; } } b bVar = new b(); AvailableAdapter availableAdapter = new AvailableAdapter(this.c.getApkVersion()); int isHuaweiMobileServicesAvailable = availableAdapter.isHuaweiMobileServicesAvailable(g()); if (isHuaweiMobileServicesAvailable != 0 && availableAdapter.isUserResolvableError(isHuaweiMobileServicesAvailable)) { a(g(), availableAdapter, bVar); } else { bVar.onComplete(isHuaweiMobileServicesAvailable); } } private void f() { Activity g = g(); if (g == null || g.isFinishing()) { return; } g.finish(); } /* JADX INFO: Access modifiers changed from: private */ public void h() { String hMSPackageNameForMultiService; HMSLog.i("ForegroundBusDelegate", "startApkHubActivity"); Activity g = g(); if (g == null) { HMSLog.e("ForegroundBusDelegate", "startApkHubActivity but activity is null"); return; } if (this.f) { hMSPackageNameForMultiService = g.getPackageName(); } else { hMSPackageNameForMultiService = HMSPackageManager.getInstance(g.getApplicationContext()).getHMSPackageNameForMultiService(); } if (TextUtils.isEmpty(hMSPackageNameForMultiService)) { HMSLog.e("ForegroundBusDelegate", "hmsPackageName is null, Service is invalid."); a(0, "hmsPackageName is null, Service is invalid."); return; } Intent intent = new Intent(this.c.getAction()); intent.putExtra(HMS_FOREGROUND_REQ_BODY, this.b); try { intent.setPackage(hMSPackageNameForMultiService); } catch (IllegalArgumentException unused) { HMSLog.e("ForegroundBusDelegate", "IllegalArgumentException when startApkHubActivity intent.setPackage"); } intent.putExtra("intent.extra.isfullscreen", UIUtil.isActivityFullscreen(g)); intent.setClassName(hMSPackageNameForMultiService, "com.huawei.hms.core.activity.UiJumpActivity"); intent.putExtra(HMS_FOREGROUND_REQ_HEADER, this.a.toJson()); intent.putExtra("intent.extra.hms.core.DELEGATE_NAME", "com.huawei.hms.core.activity.ForegroundBus"); try { b(); g.startActivityForResult(intent, 431057); } catch (ActivityNotFoundException e) { HMSLog.e("ForegroundBusDelegate", "Launch sign in Intent failed. hms is probably being updated:", e); a(0, "launch bus intent failed"); } } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public int getRequestCode() { return 431057; } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public void onBridgeActivityCreate(Activity activity) { if (activity == null) { HMSLog.e("ForegroundBusDelegate", "activity is null"); a(0, "activity is null"); return; } if (activity.isFinishing()) { HMSLog.e("ForegroundBusDelegate", "activity is finishing"); a(0, "activity is finishing"); return; } this.e = new WeakReference<>(activity); try { Intent intent = activity.getIntent(); if (intent == null) { a(0, "intent is invalid"); return; } String stringExtra = intent.getStringExtra(HMS_FOREGROUND_REQ_HEADER); RequestHeader requestHeader = new RequestHeader(); this.a = requestHeader; if (!requestHeader.fromJson(stringExtra)) { a(0, "header is invalid"); return; } this.b = intent.getStringExtra(HMS_FOREGROUND_REQ_BODY); ForegroundInnerHeader foregroundInnerHeader = this.c; if (foregroundInnerHeader == null) { a(0, "inner header is invalid"); return; } foregroundInnerHeader.fromJson(intent.getStringExtra(HMS_FOREGROUND_REQ_INNER)); if (TextUtils.isEmpty(this.a.getApiName())) { a(0, "action is invalid"); return; } a(); if (!TextUtils.isEmpty(intent.getStringExtra(INNER_PKG_NAME))) { HMSLog.i("ForegroundBusDelegate", "isUseInnerHms: true"); this.f = true; } e(); } catch (Throwable th) { StringBuilder sb = new StringBuilder("ForegroundBusDelegate getStringExtra error:"); sb.append(th.getMessage()); HMSLog.e("ForegroundBusDelegate", sb.toString()); StringBuilder sb2 = new StringBuilder("ForegroundBusDelegate getStringExtra error:"); sb2.append(th.getMessage()); a(0, sb2.toString()); } } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public void onBridgeActivityDestroy() { c(); this.e = null; } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public boolean onBridgeActivityResult(int i, int i2, Intent intent) { String str; if (i != 431057) { return false; } if (intent != null && intent.hasExtra(HMS_FOREGROUND_RESP_HEADER)) { try { str = intent.getStringExtra(HMS_FOREGROUND_RESP_HEADER); } catch (Throwable unused) { HMSLog.w("ForegroundBusDelegate", "exception occur, HMS_FOREGROUND_RESP_HEADER get failed."); str = ""; } ResponseHeader responseHeader = new ResponseHeader(); this.d = responseHeader; JsonUtil.jsonToEntity(str, responseHeader); } d(); BusResponseCallback b2 = b(this.c.getResponseCallbackKey()); if (b2 == null) { a(i2, intent); return true; } BusResponseResult succeedReturn = b2.succeedReturn(this.e.get(), i2, intent); if (succeedReturn == null) { a(i2, intent); return true; } a(succeedReturn.getCode(), succeedReturn.getIntent()); return true; } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public void onBridgeConfigurationChanged() { } @Override // com.huawei.hms.activity.IBridgeActivityDelegate public void onKeyUp(int i, KeyEvent keyEvent) { } private void b() { Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.a); mapFromForegroundRequestHeader.put(HiAnalyticsConstant.HaKey.BI_KEY_DIRECTION, HiAnalyticsConstant.Direction.REQUEST); mapFromForegroundRequestHeader.put("version", HiAnalyticsUtil.versionCodeToName(String.valueOf(this.a.getKitSdkVersion()))); if (g() != null) { HiAnalyticsUtil.getInstance().onNewEvent(g().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_START_CORE_ACTIVITY, mapFromForegroundRequestHeader); } } private void c() { if (this.a != null) { a(HiAnalyticsConstant.HMS_SDK_BASE_ACTIVITY_STARTED); } } private void a(int i, Intent intent) { HMSLog.i("ForegroundBusDelegate", "succeedReturn"); Activity g = g(); if (g == null) { return; } g.setResult(i, IntentUtil.modifyIntentBehaviorsSafe(intent)); f(); } private Activity g() { WeakReference weakReference = this.e; if (weakReference == null) { return null; } return weakReference.get(); } /* JADX INFO: Access modifiers changed from: private */ public void a(int i, String str) { BusResponseResult innerError; HMSLog.e("ForegroundBusDelegate", str); Activity g = g(); if (g == null) { return; } BusResponseCallback b2 = b(this.c.getResponseCallbackKey()); if (b2 != null && (innerError = b2.innerError(this.e.get(), i, str)) != null) { g.setResult(innerError.getCode(), IntentUtil.modifyIntentBehaviorsSafe(innerError.getIntent())); } else { g.setResult(0); } f(); } private static void a(Activity activity, AvailableAdapter availableAdapter, AvailableAdapter.AvailableCallBack availableCallBack) { if (activity == null) { HMSLog.i("ForegroundBusDelegate", "null activity, could not start resolution intent"); } availableAdapter.startResolution(activity, availableCallBack); } private void a() { Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.a); mapFromForegroundRequestHeader.put(HiAnalyticsConstant.HaKey.BI_KEY_DIRECTION, HiAnalyticsConstant.Direction.REQUEST); mapFromForegroundRequestHeader.put("version", HiAnalyticsUtil.versionCodeToName(String.valueOf(this.a.getKitSdkVersion()))); if (g() != null) { HiAnalyticsUtil.getInstance().onNewEvent(g().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_ACTIVITY_STARTED, mapFromForegroundRequestHeader); } } private void a(String str) { Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.a); mapFromForegroundRequestHeader.put(HiAnalyticsConstant.HaKey.BI_KEY_DIRECTION, HiAnalyticsConstant.Direction.RESPONSE); mapFromForegroundRequestHeader.put("version", HiAnalyticsUtil.versionCodeToName(String.valueOf(this.a.getKitSdkVersion()))); ResponseHeader responseHeader = this.d; if (responseHeader != null) { mapFromForegroundRequestHeader.put(HiAnalyticsConstant.HaKey.BI_KEY_RESULT, String.valueOf(responseHeader.getStatusCode())); mapFromForegroundRequestHeader.put("result", String.valueOf(this.d.getErrorCode())); } if (g() != null) { HiAnalyticsUtil.getInstance().onNewEvent(g().getApplicationContext(), str, mapFromForegroundRequestHeader); } } }