package com.tencent.klevin.c.e; import android.content.Context; import android.location.Location; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import com.baidu.mobads.sdk.internal.by; import com.sigmob.sdk.base.mta.PointCategory; import com.tencent.klevin.KlevinConfig; import com.tencent.klevin.KlevinManager; import com.tencent.klevin.ads.ad.AdRequest; import com.tencent.klevin.ads.bean.AdInfo; import com.tencent.klevin.ads.widget.g.g; import com.tencent.klevin.c.b.i; import com.tencent.klevin.e.f.a0; import com.tencent.klevin.e.f.b0; import com.tencent.klevin.e.f.c0; import com.tencent.klevin.e.f.f; import com.tencent.klevin.e.f.u; import com.tencent.klevin.e.f.w; import com.tencent.klevin.e.f.z; import com.tencent.klevin.listener.AdLoadListener; import com.tencent.klevin.protocol.sspservice.nano.Sspservice; import com.tencent.klevin.utils.h; import com.tencent.klevin.utils.n; import com.tencent.klevin.utils.v; import java.io.IOException; import java.io.InterruptedIOException; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; public abstract class a { private final Context a; protected AdRequest b; protected AdLoadListener<?> c; protected Handler d; /* renamed from: e reason: collision with root package name */ private long f16177e = 0; /* renamed from: f reason: collision with root package name */ private final KlevinConfig f16178f; /* renamed from: g reason: collision with root package name */ private long f16179g; /* renamed from: h reason: collision with root package name */ private int f16180h; /* renamed from: i reason: collision with root package name */ protected Sspservice.SspRequestComp f16181i; /* renamed from: com.tencent.klevin.c.e.a$a reason: collision with other inner class name */ class C0476a implements f { C0476a() { } @Override // com.tencent.klevin.e.f.f public void onFailure(com.tencent.klevin.e.f.e eVar, IOException iOException) { a.this.f16180h = (int) (System.currentTimeMillis() - a.this.f16179g); a aVar = a.this; aVar.d.removeCallbacksAndMessages(aVar.b); a.this.a((a) iOException); } @Override // com.tencent.klevin.e.f.f public void onResponse(com.tencent.klevin.e.f.e eVar, c0 c0Var) { a.this.f16180h = (int) (System.currentTimeMillis() - a.this.f16179g); try { com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", "接收到广告请求, code=" + c0Var.o()); byte[] m = c0Var.l() != null ? c0Var.l().m() : null; if (m == null) { a.this.e(); } else { Sspservice.SspResponseComp parseFrom = Sspservice.SspResponseComp.parseFrom(m); com.tencent.klevin.base.log.a.e("KLEVINSDK_adLoad", "ticket: " + parseFrom.ticket); if (parseFrom != null && !TextUtils.isEmpty(parseFrom.ticket)) { h.a(parseFrom.ticket); } a.this.a((a) parseFrom); } a aVar = a.this; aVar.d.removeCallbacksAndMessages(aVar.b); return; } catch (InterruptedIOException e2) { a.this.b((a) e2); } catch (Exception e3) { a.this.c((a) e3); } catch (Throwable th) { a aVar2 = a.this; aVar2.d.removeCallbacksAndMessages(aVar2.b); throw th; } a aVar3 = a.this; aVar3.d.removeCallbacksAndMessages(aVar3.b); } } class b implements Runnable { final /* synthetic */ com.tencent.klevin.e.f.e a; b(com.tencent.klevin.e.f.e eVar) { this.a = eVar; } public void run() { try { if (this.a.n()) { this.a.cancel(); a.this.c(); } } catch (Throwable th) { KlevinManager.reportException(th); } } } /* access modifiers changed from: package-private */ public class c implements Runnable { final /* synthetic */ String a; final /* synthetic */ Exception b; final /* synthetic */ int c; c(String str, Exception exc, int i2) { this.a = str; this.b = exc; this.c = i2; } public void run() { try { StringBuilder sb = new StringBuilder(); sb.append(this.a); sb.append(", "); sb.append(this.b != null ? this.b.getMessage() : ""); com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", sb.toString()); if (a.this.c != null) { a.this.c.onAdLoadError(this.c, this.a); } } catch (Throwable th) { KlevinManager.reportException(th); } } } private static class e implements i.c { private String a; private List<AdInfo> b; /* synthetic */ e(String str, List list, C0476a aVar) { this(str, list); } private void b(String str) { List<AdInfo> list = this.b; if (list != null) { for (AdInfo adInfo : list) { if (adInfo != null) { adInfo.setWebTemplate(str); } } } } @Override // com.tencent.klevin.c.b.i.c public void a() { com.tencent.klevin.base.log.a.a("KLEVINSDK_adLoad", "onGetTemplateFailed"); String a2 = i.a().a(this.a); if (!TextUtils.isEmpty(a2)) { b(a2); } } private e(String str, List<AdInfo> list) { this.a = str; this.b = list; } @Override // com.tencent.klevin.c.b.i.c public void a(String str) { com.tencent.klevin.base.log.a.a("KLEVINSDK_adLoad", "onGetTemplateSuccess"); b(str); } } public a(AdRequest adRequest, AdLoadListener<?> adLoadListener) { this.b = adRequest; this.c = adLoadListener; this.a = com.tencent.klevin.b.m().c(); this.f16178f = com.tencent.klevin.b.m().e(); this.d = new Handler(Looper.getMainLooper()); } private String d() { String language = Locale.getDefault().getLanguage(); if (!language.toLowerCase().equals("zh")) { return language.toLowerCase().equals("en") ? "en" : "unknown"; } if (Locale.getDefault().getCountry().toLowerCase().equals("cn")) { return "zh"; } return "tc"; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void e() { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.AD_REQUEST_NULL_RESPONSE; a(aVar.a, aVar.b); String a2 = com.tencent.klevin.c.d.c.a(this.b.getAdType()); Sspservice.SspRequestComp sspRequestComp = this.f16181i; com.tencent.klevin.e.b.c.a(a2, sspRequestComp.requestId, "ssp_fail", aVar.a, aVar.b, "", 0, "", "error", sspRequestComp.position[0], this.f16180h); } /* access modifiers changed from: package-private */ public abstract void a(List<AdInfo> list); /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void c(Exception exc) { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.AD_REQUEST_RESPONSE_PARSE_EXCEPTION; a(aVar.a, aVar.b, exc); com.tencent.klevin.e.b.c.a(com.tencent.klevin.c.d.c.a(this.b.getAdType()), this.f16181i.requestId, "ssp_fail", aVar.a, exc.toString(), "", 0, "", "error", this.f16181i.position[0], this.f16180h); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void b(Exception exc) { com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", "timeout interrupted, " + exc.getMessage()); com.tencent.klevin.e.b.c.a(com.tencent.klevin.c.d.c.a(this.b.getAdType()), this.f16181i.requestId, "ssp_fail", com.tencent.klevin.c.d.a.AD_REQUEST_INTERRUPTED.a, exc.toString(), "", 0, "", "error", this.f16181i.position[0], this.f16180h); } class d implements i.c { final /* synthetic */ String a; final /* synthetic */ AdInfo b; d(a aVar, String str, AdInfo adInfo) { this.a = str; this.b = adInfo; } @Override // com.tencent.klevin.c.b.i.c public void a() { AdInfo adInfo; com.tencent.klevin.base.log.a.a("KLEVINSDK_adLoad", "onGetTemplateFailed"); String a2 = i.a().a(this.a); if (!TextUtils.isEmpty(a2) && (adInfo = this.b) != null) { adInfo.setWebTemplate(a2); } } @Override // com.tencent.klevin.c.b.i.c public void a(String str) { com.tencent.klevin.base.log.a.a("KLEVINSDK_adLoad", "onGetTemplateSuccess"); AdInfo adInfo = this.b; if (adInfo != null) { adInfo.setWebTemplate(str); } } } public void a(long j) { this.f16177e = j; } public void a() { com.tencent.klevin.base.log.a.e("KLEVINSDK_adLoad", "call load ad"); if (!com.tencent.klevin.b.m().j().booleanValue()) { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.SDK_INIT_UNINITIALIZED; a(aVar.a, aVar.b); } else if (!com.tencent.klevin.e.d.e.e().d()) { com.tencent.klevin.c.d.a aVar2 = com.tencent.klevin.c.d.a.SDK_NETWORK_NOTREACHABLE; a(aVar2.a, aVar2.b); } else { this.f16181i = a(this.a, this.b); try { com.tencent.klevin.base.log.a.e("KLEVINSDK_adLoad", "load ad " + this.b.getAdType() + " sspRequest->\n" + this.f16181i.toString()); com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", "开始请求广告:appid=" + this.f16178f.getAppId() + ", requestId=" + this.f16181i.requestId); com.tencent.klevin.e.b.c.a(com.tencent.klevin.c.d.c.a(this.b.getAdType()), this.f16181i.requestId, "ssp_request", 0, "", "", 0, "", "start", this.f16181i.position[0], 0); StringBuilder sb = new StringBuilder(); sb.append(this.f16178f.isTestEnv() ? "https://test.api.yky.qq.com/" : "https://api.yky.qq.com/"); sb.append("v2/ssp/get_ads_comp"); z a2 = new z.a().b(sb.toString()).a(a0.a(u.b("application/x-protobuf"), com.tencent.klevin.e.i.a.e.toByteArray(this.f16181i))).a(); b0.a(a2, 5); com.tencent.klevin.e.f.e a3 = w.a(a2); this.f16179g = System.currentTimeMillis(); a3.a(new C0476a()); if (this.f16177e > 0 && this.d != null) { com.tencent.klevin.base.log.a.e("KLEVINSDK_adLoad", "load ad waitTime: " + this.f16177e); Message obtain = Message.obtain(this.d, new b(a3)); obtain.obj = this.b; this.d.sendMessageDelayed(obtain, this.f16177e); } } catch (Exception e2) { this.d.removeCallbacksAndMessages(this.b); com.tencent.klevin.c.d.a aVar3 = com.tencent.klevin.c.d.a.SDK_INTERNAL_ERROR; a(aVar3.a, aVar3.b, e2); } } } private void b(Sspservice.SspResponseComp sspResponseComp) { Sspservice.PosAd[] posAdArr; if (!(sspResponseComp == null || (posAdArr = sspResponseComp.posAd) == null || posAdArr.length <= 0)) { ArrayList arrayList = new ArrayList(); Sspservice.PosAd[] posAdArr2 = sspResponseComp.posAd; for (Sspservice.PosAd posAd : posAdArr2) { if (posAd == null || posAd.code != 0) { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.AD_REQUEST_NOADRESPONSE; a(aVar.a, aVar.b); } else { Sspservice.Ad[] adArr = posAd.ad; for (Sspservice.Ad ad : adArr) { if (ad != null) { AdInfo adInfo = new AdInfo(sspResponseComp.requestId, posAd.posId, ad); if (adInfo.parse()) { arrayList.add(adInfo); adInfo.getAdStat().c(this.f16179g); adInfo.getAdStat().b(this.f16179g + ((long) this.f16180h)); } else { com.tencent.klevin.c.d.a aVar2 = com.tencent.klevin.c.d.a.AD_REQUEST_PARSEDATA_FAIL; a(aVar2.a, aVar2.b); } } } } } if (arrayList.size() > 0) { String a2 = com.tencent.klevin.c.d.c.a(this.b.getAdType()); Sspservice.SspRequestComp sspRequestComp = this.f16181i; com.tencent.klevin.e.b.c.a(a2, sspRequestComp.requestId, "ssp_success", 0, "", "", 0, "", by.o, sspRequestComp.position[0], this.f16180h); a(arrayList); } } } private String c(Context context) { return com.tencent.klevin.utils.f.c(context) == 2 ? "horizontal" : "vertical"; } /* access modifiers changed from: package-private */ public void c() { com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", "加载超时: " + this.b.getAdType()); } private int b(Context context) { return (context.getResources().getConfiguration().screenLayout & 15) >= 3 ? 5 : 4; } /* access modifiers changed from: protected */ public void b() { g.a().b(this.a); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void a(Sspservice.SspResponseComp sspResponseComp) { if (sspResponseComp == null) { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.AD_REQUEST_INVALID_RESPONSE; a(aVar.a, aVar.b); String a2 = com.tencent.klevin.c.d.c.a(this.b.getAdType()); Sspservice.SspRequestComp sspRequestComp = this.f16181i; com.tencent.klevin.e.b.c.a(a2, sspRequestComp.requestId, "ssp_fail", aVar.a, aVar.b, "", 0, "", "error", sspRequestComp.position[0], this.f16180h); } else if (sspResponseComp.code != 0) { com.tencent.klevin.base.log.a.d("KLEVINSDK_adLoad", "加载广告失败, code=" + sspResponseComp.code); a(sspResponseComp.code, sspResponseComp.errMsg); String a3 = com.tencent.klevin.c.d.c.a(this.b.getAdType()); Sspservice.SspRequestComp sspRequestComp2 = this.f16181i; com.tencent.klevin.e.b.c.a(a3, sspRequestComp2.requestId, "ssp_fail", sspResponseComp.code, sspResponseComp.errMsg, "", 0, "", "error", sspRequestComp2.position[0], this.f16180h); } else { b(sspResponseComp); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void a(Exception exc) { com.tencent.klevin.c.d.a aVar = com.tencent.klevin.c.d.a.AD_REQUEST_FAILED; if (exc instanceof SocketTimeoutException) { aVar = com.tencent.klevin.c.d.a.AD_REQUEST_TIMEOUT; } else if (exc instanceof UnknownHostException) { aVar = com.tencent.klevin.c.d.a.AD_REQUEST_UNKNOWN_HOST; } else if (exc instanceof ConnectException) { aVar = com.tencent.klevin.c.d.a.AD_REQUEST_CONNECT_EXCEPTION; } a(aVar.a, aVar.b, exc); com.tencent.klevin.e.b.c.a(com.tencent.klevin.c.d.c.a(this.b.getAdType()), this.f16181i.requestId, "ssp_fail", aVar.a, exc.toString(), "", 0, "", "error", this.f16181i.position[0], this.f16180h); } /* JADX WARNING: Removed duplicated region for block: B:17:0x0038 */ /* JADX WARNING: Removed duplicated region for block: B:22:0x0040 A[RETURN] */ private int a(String str) { char c2; int hashCode = str.hashCode(); if (hashCode != -792039641) { if (hashCode != 102570) { if (hashCode == 1843485230 && str.equals(PointCategory.NETWORK)) { c2 = 1; if (c2 != 0) { return 1; } if (c2 != 1) { return c2 != 2 ? 0 : 3; } return 2; } } else if (str.equals("gps")) { c2 = 0; if (c2 != 0) { } } } else if (str.equals("passive")) { c2 = 2; if (c2 != 0) { } } c2 = 65535; if (c2 != 0) { } } private Sspservice.DeviceComp a(Context context) { Sspservice.DeviceComp deviceComp = new Sspservice.DeviceComp(); com.tencent.klevin.d.b bVar = (com.tencent.klevin.d.b) com.tencent.klevin.base.router.a.a().a(com.tencent.klevin.d.b.class); if (bVar != null) { deviceComp.dc1 = bVar.e(); } deviceComp.dc2 = d(); if (bVar != null) { Sspservice.GeoComp geoComp = new Sspservice.GeoComp(); Location h2 = bVar.h(); if (h2 != null) { geoComp.gc1 = h2.getLatitude(); geoComp.gc2 = h2.getLongitude(); geoComp.gc3 = a(h2.getProvider()); geoComp.gc4 = Locale.getDefault().getISO3Country(); geoComp.gc5 = Locale.getDefault().getISO3Language(); } else { geoComp.gc1 = 0.0d; geoComp.gc2 = 0.0d; geoComp.gc3 = 0; geoComp.gc4 = ""; geoComp.gc5 = ""; } deviceComp.dc3 = geoComp; } if (bVar != null) { deviceComp.dc4 = bVar.c(); deviceComp.dc5 = bVar.d(); deviceComp.dc6 = bVar.g(); deviceComp.dc7 = bVar.b(); } deviceComp.dc8 = com.tencent.klevin.utils.f.b(context); deviceComp.dc9 = com.tencent.klevin.utils.f.a(context); deviceComp.dc10 = c(context); deviceComp.dc11 = com.tencent.klevin.e.d.e.e().a().b; deviceComp.dc12 = n.a(context); deviceComp.dc13 = b(context); if (bVar != null) { String i2 = bVar.i(); if (TextUtils.isEmpty(i2) || i2.length() < 6) { deviceComp.dc15 = bVar.f(); } else { deviceComp.dc16 = i2; } } return deviceComp; } private Sspservice.SspRequestComp a(Context context, AdRequest adRequest) { Sspservice.SspRequestComp sspRequestComp = new Sspservice.SspRequestComp(); try { Sspservice.App app = new Sspservice.App(); app.appId = Long.parseLong(this.f16178f.getAppId()); app.appVer = com.tencent.klevin.utils.a.b(context); app.appPkg = com.tencent.klevin.utils.a.a(context); app.sdkVer = "2.11.0.4"; app.personalRecommendSwitch = this.f16178f.isPersonalizeEnabled() ? 1 : 2; String valueOf = String.valueOf(System.currentTimeMillis() * 1000); Sspservice.DeviceComp a2 = a(context); if (TextUtils.isEmpty(h.b())) { sspRequestComp.device = a2; h.b(a2); } else { sspRequestComp.sdkTicket = v.a(h.b(), valueOf); sspRequestComp.device = h.a(a2); } ArrayList arrayList = new ArrayList(); Sspservice.Position position = new Sspservice.Position(); position.posId = adRequest.getPosId(); position.adCount = adRequest.getAdCount(); arrayList.add(position); sspRequestComp.app = app; sspRequestComp.position = (Sspservice.Position[]) arrayList.toArray(new Sspservice.Position[arrayList.size()]); sspRequestComp.requestId = h.a(); sspRequestComp.timestamp = valueOf; return sspRequestComp; } catch (Exception e2) { com.tencent.klevin.base.log.a.b("KLEVINSDK_adLoad", "buildSspRequest exception: " + e2.getMessage()); return null; } } /* access modifiers changed from: package-private */ public void a(int i2, String str, Exception exc) { this.d.post(new c(str, exc, i2)); } /* access modifiers changed from: package-private */ public void a(int i2, String str) { a(i2, str, (Exception) null); } /* access modifiers changed from: protected */ public void a(AdInfo adInfo) { if (adInfo != null) { long template = adInfo.getTemplate(); com.tencent.klevin.d.a aVar = (com.tencent.klevin.d.a) com.tencent.klevin.base.router.a.a().a(com.tencent.klevin.d.a.class); String c2 = aVar != null ? aVar.c("template_url", template) : ""; adInfo.setWebTemplateUrl(c2); if (!TextUtils.isEmpty(c2)) { i.a().a(c2, adInfo.getRequestId(), adInfo.getPosition(), new d(this, c2, adInfo)); } } } /* access modifiers changed from: protected */ public void a(String str, List<AdInfo> list, Sspservice.Position position) { if (list != null && list.size() > 0) { HashMap hashMap = new HashMap(list.size()); for (AdInfo adInfo : list) { if (adInfo != null) { long template = adInfo.getTemplate(); com.tencent.klevin.d.a aVar = (com.tencent.klevin.d.a) com.tencent.klevin.base.router.a.a().a(com.tencent.klevin.d.a.class); String c2 = aVar != null ? aVar.c("template_url", template) : ""; adInfo.setWebTemplateUrl(c2); if (!TextUtils.isEmpty(c2)) { if (!hashMap.containsKey(c2)) { ArrayList arrayList = new ArrayList(); arrayList.add(adInfo); hashMap.put(c2, arrayList); } else { ((List) hashMap.get(c2)).add(adInfo); } } } } for (String str2 : hashMap.keySet()) { e eVar = new e(str2, (List) hashMap.get(str2), null); com.tencent.klevin.base.log.a.a("KLEVINSDK_adLoad", "cacheWebTemplatesAsync templateUrl: " + str2); i.a().a(str2, str, position, eVar); } } } }