package com.ss.android.downloadlib.addownload; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import com.ss.android.download.api.config.IDownloadButtonClickListener; import com.ss.android.download.api.config.s; import com.ss.android.download.api.config.u; import com.ss.android.download.api.download.DownloadController; import com.ss.android.download.api.download.DownloadEventConfig; import com.ss.android.download.api.download.DownloadModel; import com.ss.android.download.api.download.DownloadStatusChangeListener; import com.ss.android.download.api.download.c; import com.ss.android.download.api.model.DownloadShortInfo; import com.ss.android.downloadad.api.download.AdDownloadModel; import com.ss.android.downloadlib.addownload.b.f; import com.ss.android.downloadlib.addownload.b.i; import com.ss.android.downloadlib.addownload.g; import com.ss.android.downloadlib.c.h; import com.ss.android.downloadlib.g.j; import com.ss.android.downloadlib.g.k; import com.ss.android.downloadlib.g.l; import com.ss.android.downloadlib.g.m; import com.ss.android.socialbase.appdownloader.DownloadHandlerService; import com.ss.android.socialbase.appdownloader.c.d; import com.ss.android.socialbase.downloader.depend.IDownloadListener; import com.ss.android.socialbase.downloader.downloader.Downloader; import com.ss.android.socialbase.downloader.exception.BaseException; import com.ss.android.socialbase.downloader.model.DownloadInfo; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.json.JSONObject; /* compiled from: CommonDownloadHandler */ public class e implements f, m.a { private static final String a = "e"; private final m b; private g c; private d d; private WeakReference<Context> e; private final Map<Integer, Object> f = new ConcurrentHashMap(); private DownloadShortInfo g; private DownloadInfo h; private c i; private final IDownloadListener j; private boolean k; private long l; private long m; private DownloadModel n; private DownloadEventConfig o; private DownloadController p; private SoftReference<u> q; private boolean r; private final boolean s; private SoftReference<IDownloadButtonClickListener> t; /* access modifiers changed from: package-private */ /* compiled from: CommonDownloadHandler */ public interface a { void a(); } /* access modifiers changed from: package-private */ /* compiled from: CommonDownloadHandler */ public interface b { void a(long j); } public e() { m mVar = new m(Looper.getMainLooper(), this); this.b = mVar; this.j = new g.a(mVar); this.m = -1; this.n = null; this.o = null; this.p = null; this.c = new g(this); this.d = new d(mVar); this.s = com.ss.android.socialbase.downloader.g.a.c().a("ttdownloader_callback_twice"); } /* renamed from: a */ public e b(Context context) { if (context != null) { this.e = new WeakReference<>(context); } j.b(context); return this; } /* renamed from: a */ public e b(int i2, DownloadStatusChangeListener downloadStatusChangeListener) { if (downloadStatusChangeListener != null) { if (j.i().optInt("back_use_softref_listener") == 1) { this.f.put(Integer.valueOf(i2), downloadStatusChangeListener); } else { this.f.put(Integer.valueOf(i2), new SoftReference(downloadStatusChangeListener)); } } return this; } /* renamed from: a */ public e b(DownloadModel downloadModel) { if (downloadModel != null) { if (downloadModel.isAd()) { if (downloadModel.getId() <= 0 || TextUtils.isEmpty(downloadModel.getLogExtra())) { com.ss.android.downloadlib.e.c.a().a("setDownloadModel ad error"); } } else if (downloadModel.getId() == 0 && (downloadModel instanceof AdDownloadModel)) { com.ss.android.downloadlib.e.c.a().a(false, "setDownloadModel id=0"); if (com.ss.android.socialbase.downloader.g.a.c().a("fix_model_id")) { ((AdDownloadModel) downloadModel).setId((long) downloadModel.getDownloadUrl().hashCode()); } } f.a().a(downloadModel); this.m = downloadModel.getId(); this.n = downloadModel; if (h.a(downloadModel)) { ((AdDownloadModel) downloadModel).setExtraValue(3); com.ss.android.downloadad.api.a.b d2 = f.a().d(this.m); if (!(d2 == null || d2.l() == 3)) { d2.e(3L); i.a().a(d2); } } } return this; } /* renamed from: a */ public e b(DownloadController downloadController) { JSONObject extra; this.p = downloadController; if (com.ss.android.downloadlib.g.e.b(this.n).b("force_auto_open") == 1) { m().setLinkMode(1); } if (com.ss.android.socialbase.downloader.g.a.c().a("fix_show_dialog") && (extra = this.n.getExtra()) != null && extra.optInt("subprocess") > 0) { m().setEnableNewActivity(false); } f.a().a(this.m, m()); return this; } /* renamed from: a */ public e b(DownloadEventConfig downloadEventConfig) { this.o = downloadEventConfig; this.r = l().getDownloadScene() == 0; f.a().a(this.m, l()); return this; } @Override // com.ss.android.downloadlib.addownload.f public f a(u uVar) { if (uVar == null) { this.q = null; } else { this.q = new SoftReference<>(uVar); } return this; } @Override // com.ss.android.downloadlib.addownload.f public void a() { this.k = true; f.a().a(this.m, l()); f.a().a(this.m, m()); this.c.a(this.m); p(); if (j.i().optInt("enable_empty_listener", 1) == 1 && this.f.get(Integer.MIN_VALUE) == null) { b(Integer.MIN_VALUE, new com.ss.android.download.api.config.a()); } } @Override // com.ss.android.downloadlib.addownload.f public boolean a(int i2) { if (i2 == 0) { this.f.clear(); } else { this.f.remove(Integer.valueOf(i2)); } if (this.f.isEmpty()) { this.k = false; this.l = System.currentTimeMillis(); if (this.h != null) { Downloader.getInstance(j.getContext()).removeTaskMainListener(this.h.getId()); } c cVar = this.i; if (!(cVar == null || cVar.getStatus() == AsyncTask.Status.FINISHED)) { this.i.cancel(true); } this.c.a(this.h); String str = a; StringBuilder sb = new StringBuilder(); sb.append("onUnbind removeCallbacksAndMessages, downloadUrl:"); DownloadInfo downloadInfo = this.h; sb.append(downloadInfo == null ? "" : downloadInfo.getUrl()); k.a(str, sb.toString(), null); this.b.removeCallbacksAndMessages(null); this.g = null; this.h = null; return true; } if (this.f.size() == 1 && this.f.containsKey(Integer.MIN_VALUE)) { this.c.b(this.h); } return false; } @Override // com.ss.android.downloadlib.addownload.f public void a(boolean z) { if (this.h == null) { return; } if (z) { d b2 = com.ss.android.socialbase.appdownloader.d.j().b(); if (b2 != null) { b2.a(this.h); } Downloader.getInstance(com.ss.android.socialbase.downloader.downloader.c.N()).cancel(this.h.getId(), true); return; } Intent intent = new Intent(j.getContext(), DownloadHandlerService.class); intent.setAction("android.ss.intent.action.DOWNLOAD_DELETE"); intent.putExtra("extra_click_download_ids", this.h.getId()); j.getContext().startService(intent); } @Override // com.ss.android.downloadlib.addownload.f public boolean b() { return this.k; } public boolean c() { DownloadInfo downloadInfo = this.h; return (downloadInfo == null || downloadInfo.getStatus() == 0) ? false : true; } @Override // com.ss.android.downloadlib.addownload.f public long d() { return this.l; } @Override // com.ss.android.downloadlib.addownload.f public f a(long j2) { if (j2 != 0) { DownloadModel a2 = f.a().a(j2); if (a2 != null) { this.n = a2; this.m = j2; this.c.a(j2); } } else { com.ss.android.downloadlib.e.c.a().a(false, "setModelId"); } return this; } @Override // com.ss.android.downloadlib.addownload.f public void b(int i2) { if (i2 == 1 || i2 == 2) { this.c.a(this.m); if (!f.a().e(this.m).y()) { com.ss.android.downloadlib.e.c.a().a("handleDownload ModelBox !isStrictValid"); } if (!this.c.a(getContext(), i2, this.r)) { boolean c2 = c(i2); if (i2 != 1) { if (i2 == 2 && !c2) { String str = a; k.a(str, "handleDownload id:" + this.m + ",pBC:", null); b(true); } } else if (!c2) { String str2 = a; k.a(str2, "handleDownload id:" + this.m + ",pIC:", null); c(true); } } } else { throw new IllegalArgumentException("error actionType"); } } public boolean e() { if (j.i().optInt("quick_app_enable_switch", 0) == 0 && this.n.getQuickAppModel() != null && !TextUtils.isEmpty(this.n.getQuickAppModel().a()) && c.a(this.h) && l.a(getContext(), new Intent("android.intent.action.VIEW", Uri.parse(this.n.getQuickAppModel().a())))) { return true; } return false; } private boolean c(int i2) { if (!e()) { return false; } int i3 = -1; String a2 = this.n.getQuickAppModel().a(); if (i2 == 1) { i3 = 5; } else if (i2 == 2) { i3 = 4; } DownloadModel downloadModel = this.n; if (downloadModel instanceof AdDownloadModel) { ((AdDownloadModel) downloadModel).setFunnelType(3); } boolean c2 = com.ss.android.downloadlib.g.i.c(j.getContext(), a2); if (c2) { com.ss.android.downloadlib.d.a.a().a(this.m, i2); Message obtain = Message.obtain(); obtain.what = i3; obtain.obj = Long.valueOf(this.n.getId()); c.a().a(this, i3, this.n); } else { com.ss.android.downloadlib.d.a.a().a(this.m, false, 0); } return c2; } public void b(boolean z) { e(z); } private void k() { SoftReference<u> softReference = this.q; if (softReference == null || softReference.get() == null) { j.b().a(getContext(), this.n, m(), l()); return; } this.q.get().a(this.n, l(), m()); this.q = null; } public void c(boolean z) { if (z) { com.ss.android.downloadlib.d.a.a().a(this.m, 1); } n(); } private void e(boolean z) { if (com.ss.android.downloadlib.g.e.b(this.n).b("notification_opt_2") == 1 && this.h != null) { com.ss.android.socialbase.downloader.notification.b.a().f(this.h.getId()); } f(z); } public void f() { this.b.post(new Runnable() { /* class com.ss.android.downloadlib.addownload.e.AnonymousClass1 */ public void run() { for (DownloadStatusChangeListener downloadStatusChangeListener : g.a(e.this.f)) { downloadStatusChangeListener.onInstalled(e.this.q()); } } }); } @Override // com.ss.android.downloadlib.g.m.a public void a(Message message) { if (message != null && this.k && message.what == 3) { this.h = (DownloadInfo) message.obj; this.c.a(message, q(), this.f); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private Context getContext() { WeakReference<Context> weakReference = this.e; if (weakReference == null || weakReference.get() == null) { return j.getContext(); } return this.e.get(); } private DownloadEventConfig l() { DownloadEventConfig downloadEventConfig = this.o; return downloadEventConfig == null ? new c.a().a() : downloadEventConfig; } private DownloadController m() { if (this.p == null) { this.p = new com.ss.android.download.api.download.b(); } return this.p; } private void n() { String str = a; k.a(str, "pICD", null); if (this.c.d(this.h)) { k.a(str, "pICD BC", null); f(false); return; } k.a(str, "pICD IC", null); k(); } private void f(final boolean z) { DownloadModel downloadModel; String str = a; k.a(str, "pBCD", null); if (o()) { com.ss.android.downloadlib.addownload.b.e e2 = f.a().e(this.m); if (this.r) { if (!i()) { a(z, true); } else if (d(false) && e2.d != null && e2.d.isAutoDownloadOnCardShow()) { a(z, true); } } else if (!this.n.isAd() || e2.d == null || !e2.d.enableShowComplianceDialog() || e2.b == null || !com.ss.android.downloadlib.addownload.compliance.b.a().a(e2.b) || !com.ss.android.downloadlib.addownload.compliance.b.a().a(e2)) { a(z, true); } } else { k.a(str, "pBCD continue download, status:" + this.h.getStatus(), null); DownloadInfo downloadInfo = this.h; if (!(downloadInfo == null || (downloadModel = this.n) == null)) { downloadInfo.setOnlyWifi(downloadModel.isNeedWifi()); } final int status = this.h.getStatus(); final int id = this.h.getId(); final com.ss.android.downloadad.api.a.b a2 = f.a().a(this.h); if (status == -2 || status == -1) { this.c.a(this.h, z); if (a2 != null) { a2.h(System.currentTimeMillis()); a2.i(this.h.getCurBytes()); } this.h.setDownloadFromReserveWifi(false); this.d.a(new com.ss.android.downloadlib.addownload.b.e(this.m, this.n, l(), m())); this.d.a(id, this.h.getCurBytes(), this.h.getTotalBytes(), new a() { /* class com.ss.android.downloadlib.addownload.e.AnonymousClass2 */ @Override // com.ss.android.downloadlib.addownload.e.a public void a() { if (!e.this.d.a()) { e eVar = e.this; eVar.a(id, status, eVar.h); } } }); } else if (!l.a(status)) { this.c.a(this.h, z); a(id, status, this.h); } else if (this.n.enablePause()) { this.d.a(true); h.a().b(f.a().d(this.m)); com.ss.android.downloadlib.addownload.d.f.a().a(a2, status, new com.ss.android.downloadlib.addownload.d.c() { /* class com.ss.android.downloadlib.addownload.e.AnonymousClass3 */ @Override // com.ss.android.downloadlib.addownload.d.c public void a(com.ss.android.downloadad.api.a.b bVar) { if (e.this.h == null && com.ss.android.socialbase.downloader.g.a.c().a("fix_handle_pause")) { e.this.h = Downloader.getInstance(j.getContext()).getDownloadInfo(id); } e.this.c.a(e.this.h, z); if (e.this.h == null || !com.ss.android.socialbase.downloader.i.f.b(j.getContext()) || !e.this.h.isPauseReserveOnWifi()) { e eVar = e.this; eVar.a(id, status, eVar.h); return; } e.this.h.stopPauseReserveOnWifi(); com.ss.android.downloadlib.d.a.a().b("pause_reserve_wifi_cancel_on_wifi", a2); } }); } } } public void a(boolean z, final boolean z2) { if (z) { com.ss.android.downloadlib.d.a.a().a(this.m, 2); } if (!j.b(com.kuaishou.weapon.p0.h.j) && !m().enableNewActivity()) { this.n.setFilePath(this.c.b()); } if (com.ss.android.downloadlib.g.e.c(this.n) == 0) { k.a(a, "pBCD not start", null); this.c.a(new s() { /* class com.ss.android.downloadlib.addownload.e.AnonymousClass4 */ @Override // com.ss.android.download.api.config.s public void a() { k.a(e.a, "pBCD start download", null); e.this.g((e) z2); } @Override // com.ss.android.download.api.config.s public void a(String str) { k.a(e.a, "pBCD onDenied", null); } }); return; } g(z2); } private boolean o() { if (com.ss.android.socialbase.downloader.g.a.c().a("fix_click_start")) { DownloadInfo downloadInfo = this.h; if (downloadInfo == null) { return true; } if ((downloadInfo.getStatus() == -3 && this.h.getCurBytes() <= 0) || this.h.getStatus() == 0 || this.h.getStatus() == -4) { return true; } try { return com.ss.android.socialbase.downloader.i.f.a(this.h.getStatus(), this.h.getSavePath(), this.h.getName()); } catch (Exception unused) { } } else { DownloadInfo downloadInfo2 = this.h; if (downloadInfo2 == null) { return true; } return (downloadInfo2.getStatus() != -3 && !Downloader.getInstance(j.getContext()).canResume(this.h.getId())) || this.h.getStatus() == 0; } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void a(int i2, int i3, DownloadInfo downloadInfo) { if (!com.ss.android.socialbase.downloader.g.a.c().a("fix_click_start")) { com.ss.android.socialbase.appdownloader.d.j().a(j.getContext(), i2, i3); } else if (i3 == -3 || com.ss.android.socialbase.downloader.downloader.d.a().e(i2)) { com.ss.android.socialbase.appdownloader.d.j().a(j.getContext(), i2, i3); } else { a(false, false); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void g(final boolean z) { this.d.a(new com.ss.android.downloadlib.addownload.b.e(this.m, this.n, l(), m())); this.d.a(0, 0, 0, new a() { /* class com.ss.android.downloadlib.addownload.e.AnonymousClass5 */ @Override // com.ss.android.downloadlib.addownload.e.a public void a() { if (!e.this.d.a()) { e.this.h((e) z); } } }); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void h(boolean z) { for (DownloadStatusChangeListener downloadStatusChangeListener : g.a(this.f)) { downloadStatusChangeListener.onDownloadStart(this.n, m()); } int a2 = this.c.a(j.getContext(), this.j); String str = a; k.a(str, "beginDown id:" + a2, null); if (a2 == 0) { DownloadInfo a3 = new DownloadInfo.a(this.n.getDownloadUrl()).a(); a3.setStatus(-1); a(a3); com.ss.android.downloadlib.d.a.a().a(this.m, new BaseException(2, "start download failed, id=0")); com.ss.android.downloadlib.e.c.a().b("beginDown"); } else if (this.h != null && !com.ss.android.socialbase.downloader.g.a.c().a("fix_click_start")) { this.c.a(this.h, false); } else if (z) { this.c.a(); } if (this.c.a(c())) { k.a(str, "beginDown IC id:" + a2, null); k(); } } public void g() { if (this.f.size() != 0) { for (DownloadStatusChangeListener downloadStatusChangeListener : g.a(this.f)) { downloadStatusChangeListener.onIdle(); } DownloadInfo downloadInfo = this.h; if (downloadInfo != null) { downloadInfo.setStatus(-4); } } } /* access modifiers changed from: private */ /* compiled from: CommonDownloadHandler */ public class c extends AsyncTask<String, Void, DownloadInfo> { private c() { } /* access modifiers changed from: protected */ /* renamed from: a */ public DownloadInfo doInBackground(String... strArr) { DownloadInfo downloadInfo = null; if (strArr == null) { return null; } if (strArr.length >= 1 && TextUtils.isEmpty(strArr[0])) { return null; } String str = strArr[0]; if (e.this.n != null && !TextUtils.isEmpty(e.this.n.getFilePath())) { downloadInfo = Downloader.getInstance(j.getContext()).getDownloadInfo(str, e.this.n.getFilePath()); } return downloadInfo == null ? com.ss.android.socialbase.appdownloader.d.j().a(j.getContext(), str) : downloadInfo; } /* access modifiers changed from: protected */ /* renamed from: a */ public void onPostExecute(DownloadInfo downloadInfo) { super.onPostExecute(downloadInfo); if (!isCancelled() && e.this.n != null) { try { com.ss.android.downloadlib.addownload.b.c a2 = l.a(e.this.n.getPackageName(), e.this.n.getVersionCode(), e.this.n.getVersionName()); com.ss.android.downloadlib.addownload.b.h.a().a(e.this.n.getVersionCode(), a2.b(), f.a().a(downloadInfo)); boolean a3 = a2.a(); if (downloadInfo == null || downloadInfo.getId() == 0 || (!a3 && Downloader.getInstance(j.getContext()).isDownloadSuccessAndFileNotExist(downloadInfo))) { if (downloadInfo != null && Downloader.getInstance(j.getContext()).isDownloadSuccessAndFileNotExist(downloadInfo)) { com.ss.android.socialbase.downloader.notification.b.a().f(downloadInfo.getId()); e.this.h = null; } if (e.this.h != null) { Downloader.getInstance(j.getContext()).removeTaskMainListener(e.this.h.getId()); if (e.this.s) { Downloader.getInstance(e.this.getContext()).setMainThreadListener(e.this.h.getId(), e.this.j, false); } else { Downloader.getInstance(e.this.getContext()).setMainThreadListener(e.this.h.getId(), e.this.j); } } if (a3) { e eVar = e.this; eVar.h = new DownloadInfo.a(eVar.n.getDownloadUrl()).a(); e.this.h.setStatus(-3); e.this.c.a(e.this.h, e.this.q(), g.a(e.this.f)); } else { for (DownloadStatusChangeListener downloadStatusChangeListener : g.a(e.this.f)) { downloadStatusChangeListener.onIdle(); } e.this.h = null; } } else { Downloader.getInstance(j.getContext()).removeTaskMainListener(downloadInfo.getId()); if (e.this.h == null || e.this.h.getStatus() != -4) { e.this.h = downloadInfo; if (e.this.s) { Downloader.getInstance(j.getContext()).setMainThreadListener(e.this.h.getId(), e.this.j, false); } else { Downloader.getInstance(j.getContext()).setMainThreadListener(e.this.h.getId(), e.this.j); } } else { e.this.h = null; } e.this.c.a(e.this.h, e.this.q(), g.a(e.this.f)); } e.this.c.c(e.this.h); } catch (Exception e) { e.printStackTrace(); } } } } private void a(DownloadInfo downloadInfo) { Message obtain = Message.obtain(); obtain.what = 3; obtain.obj = downloadInfo; this.b.sendMessage(obtain); } private void p() { c cVar = this.i; if (!(cVar == null || cVar.getStatus() == AsyncTask.Status.FINISHED)) { this.i.cancel(true); } c cVar2 = new c(); this.i = cVar2; com.ss.android.downloadlib.g.b.a(cVar2, this.n.getDownloadUrl(), this.n.getPackageName()); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private DownloadShortInfo q() { if (this.g == null) { this.g = new DownloadShortInfo(); } return this.g; } @Override // com.ss.android.downloadlib.addownload.f public void h() { f.a().f(this.m); } @Override // com.ss.android.downloadlib.addownload.f public f a(IDownloadButtonClickListener iDownloadButtonClickListener) { if (iDownloadButtonClickListener == null) { this.t = null; } else { this.t = new SoftReference<>(iDownloadButtonClickListener); } return this; } public boolean i() { return h.a(this.n, this.t); } public boolean d(boolean z) { SoftReference<IDownloadButtonClickListener> softReference = this.t; if (softReference == null || softReference.get() == null) { com.ss.android.downloadlib.e.c.a().b("mDownloadButtonClickListener has recycled"); return false; } if (!z) { this.t.get().handleComplianceDialog(true); } else { this.t.get().handleMarketFailedComplianceDialog(); } this.t = null; return true; } }