package com.kwad.sdk.core.download.a; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import android.util.SparseArray; import android.widget.RemoteViews; import com.ksad.download.DownloadTask; import com.ksad.download.d; import com.kwad.sdk.api.push.KsNotificationCompat; import com.kwad.sdk.core.download.DownloadParams; import com.kwad.sdk.core.response.model.AdTemplate; import com.kwad.sdk.service.ServiceProvider; import com.kwad.sdk.service.kwai.e; import com.kwad.sdk.utils.ar; import com.kwad.sdk.utils.bo; import java.io.File; import java.lang.ref.WeakReference; import java.util.HashMap; public final class a implements d { private static HashMap<String, WeakReference<Bitmap>> aVI = new HashMap<>(); private static c aVJ; private static final Handler aVK = new HandlerC0148a(); /* renamed from: com.kwad.sdk.core.download.a.a$a reason: collision with other inner class name */ static class HandlerC0148a extends Handler { private final SparseArray<Long> aVL = new SparseArray<>(); HandlerC0148a() { super(Looper.getMainLooper()); } public final void handleMessage(Message message) { boolean z = false; boolean z2 = message.arg1 == 1; boolean z3 = message.arg2 == 1; if (message.arg2 == 2) { z = true; } Long l = this.aVL.get(message.what); NotificationManager notificationManager = (NotificationManager) com.ksad.download.c.Q().getContext().getSystemService("notification"); if (notificationManager != null) { if (com.ksad.download.c.Q().s(message.what) == null && !z) { removeMessages(message.what); notificationManager.cancel(message.what); } else if (z2 || l == null || System.currentTimeMillis() - l.longValue() >= 110) { if (z3) { notificationManager.cancel(message.what); } a.a(message.what, (Notification) message.obj); this.aVL.put(message.what, Long.valueOf(System.currentTimeMillis())); } else { sendMessageDelayed(Message.obtain(message), (l.longValue() + 110) - System.currentTimeMillis()); } } } } /* access modifiers changed from: package-private */ public static class b { private static String aVM = "ksad_notification_default_icon"; private String Rq; private String aVN; private String aVO; private File aVP = null; private String aVQ; private boolean aVR = false; private String name; private int progress; private b() { } public static String Mc() { return aVM; } /* access modifiers changed from: private */ public static b a(DownloadTask downloadTask, String str, String str2) { b bVar = new b(); Object tag = downloadTask.getTag(); if (tag instanceof DownloadParams) { DownloadParams downloadParams = (DownloadParams) tag; File cm = ((com.kwad.sdk.service.kwai.c) ServiceProvider.get(com.kwad.sdk.service.kwai.c.class)).cm(downloadParams.mAppIcon); if (cm != null && cm.exists()) { bVar.aVP = cm; } bVar.name = downloadParams.mAppName; } bVar.aVR = downloadTask.isPaused(); bVar.Rq = str; bVar.aVQ = str2; bVar.aVN = a.U((long) downloadTask.getSmallFileSoFarBytes()) + " / " + a.U((long) downloadTask.getSmallFileTotalBytes()); bVar.aVO = a.U((long) downloadTask.getSmallFileTotalBytes()); bVar.progress = (int) ((((float) downloadTask.getSmallFileSoFarBytes()) * 100.0f) / ((float) downloadTask.getSmallFileTotalBytes())); return bVar; } /* access modifiers changed from: private */ public static b a(DownloadParams downloadParams, String str, String str2) { File cm; b bVar = new b(); bVar.name = downloadParams.mAppName; if (!TextUtils.isEmpty(downloadParams.mAppIcon) && (cm = ((com.kwad.sdk.service.kwai.c) ServiceProvider.get(com.kwad.sdk.service.kwai.c.class)).cm(downloadParams.mAppIcon)) != null && cm.exists()) { bVar.aVP = cm; } bVar.Rq = str; bVar.aVO = a.U(downloadParams.mAppSize); bVar.aVQ = str2; return bVar; } public final String Md() { return this.aVN; } public final String Me() { return this.aVO; } public final String Mf() { return this.Rq; } public final File Mg() { return this.aVP; } public final String Mh() { return "正在下载 " + this.progress + "%"; } public final String Mi() { return this.aVQ; } public final String getName() { String str = this.name; return str == null ? "" : str; } public final int getProgress() { return this.progress; } public final boolean isPaused() { return this.aVR; } } /* access modifiers changed from: package-private */ public static class c extends BroadcastReceiver { private static void g(Intent intent) { DownloadTask i = i(intent); if (i != null) { com.ksad.download.c.Q().u(i.getId()); } } private static void h(Intent intent) { DownloadTask i = i(intent); if (i != null) { i.setNotificationRemoved(true); } } private static DownloadTask i(Intent intent) { int i = intent.getExtras().getInt("taskId", 0); if (i == 0) { return null; } return com.ksad.download.c.Q().s(i); } public final void onReceive(Context context, Intent intent) { if (intent != null && intent.getExtras() != null) { String action = intent.getAction(); if ("com.ksad.action.ACTION_NOTIFICATION_CLICK_CONTROL_BTN".equals(action)) { g(intent); } else if ("com.ksad.action.ACTION_NOTIFICATION_REMOVED".equals(action)) { h(intent); } } } } private static Bitmap K(Context context, String str) { WeakReference<Bitmap> weakReference = aVI.get(str); Bitmap bitmap = weakReference != null ? weakReference.get() : null; if (bitmap != null && !bitmap.isRecycled()) { return bitmap; } Bitmap decodeResource = BitmapFactory.decodeResource(ar.dn(context), ar.aB(context, str)); aVI.put(str, new WeakReference<>(decodeResource)); return decodeResource; } /* access modifiers changed from: private */ public static String U(long j) { return String.format("%.2fMB", Float.valueOf((((float) j) / 1000.0f) / 1000.0f)); } /* access modifiers changed from: private */ public static void a(int i, Notification notification) { Context context = com.ksad.download.c.Q().getContext(); if (context != null) { bq(bo.getApplicationContext(context)); NotificationManager notificationManager = (NotificationManager) context.getSystemService("notification"); try { if (Build.VERSION.SDK_INT >= 26) { NotificationChannel notificationChannel = new NotificationChannel("download_channel", "ksad", 3); notificationChannel.enableLights(false); notificationChannel.enableVibration(false); notificationChannel.setSound(null, null); notificationChannel.setShowBadge(false); notificationManager.createNotificationChannel(notificationChannel); } notificationManager.notify(i, notification); } catch (Exception e) { com.kwad.sdk.core.e.b.printStackTrace(e); } } } private static void a(Context context, RemoteViews remoteViews, boolean z, boolean z2, PendingIntent pendingIntent, int i, int i2, int i3) { KsNotificationCompat.Builder builder = new KsNotificationCompat.Builder(context, "download_channel"); builder.setContent(remoteViews).setWhen(System.currentTimeMillis()).setOngoing(false).setAutoCancel(false).setOnlyAlertOnce(true).setPriority(-1).setContentIntent(pendingIntent).setSmallIcon(ar.aB(context, "ksad_notification_small_icon")); if (z2) { builder.setDeleteIntent(c(context, "com.ksad.action.ACTION_NOTIFICATION_REMOVED", i)); } Handler handler = aVK; handler.removeMessages(i); handler.obtainMessage(i, i2, i3, builder.build()).sendToTarget(); } private void a(Context context, b bVar, b bVar2) { bVar.setName(bVar2.getName()); File Mg = bVar2.Mg(); if (!((Mg == null || !Mg.exists()) ? false : a(bVar, Mg))) { a(context, bVar, b.Mc()); } bVar.setStatus(bVar2.Mf()); bVar.setSize(bVar2.Me()); bVar.setInstallText(bVar2.Mi()); } private void a(Context context, c cVar, b bVar) { cVar.setName(bVar.getName()); File Mg = bVar.Mg(); if (!((Mg == null || !Mg.exists()) ? false : a(cVar, Mg))) { a(context, cVar, b.Mc()); } cVar.setStatus(bVar.Mf()); cVar.setSize(bVar.Md()); cVar.setPercentNum(bVar.Mh()); cVar.setProgress(100, bVar.getProgress(), false); cVar.setControlBtnPaused(bVar.isPaused()); } private boolean a(Context context, b bVar, String str) { try { bVar.setIcon(K(context, str)); return true; } catch (Exception e) { com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.service.b.gatherException(e); return false; } } private boolean a(Context context, c cVar, String str) { try { cVar.setIcon(K(context, str)); return true; } catch (Exception e) { com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.service.b.gatherException(e); return false; } } private boolean a(b bVar, File file) { try { bVar.setIcon(l(file)); return true; } catch (Exception e) { com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.service.b.gatherException(e); return false; } } private boolean a(c cVar, File file) { try { cVar.setIcon(l(file)); return true; } catch (Exception e) { com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.service.b.gatherException(e); return false; } } private static void bq(Context context) { if (aVJ == null && context != null) { aVJ = new c(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.ksad.action.ACTION_NOTIFICATION_CLICK_CONTROL_BTN"); intentFilter.addAction("com.ksad.action.ACTION_NOTIFICATION_REMOVED"); context.registerReceiver(aVJ, intentFilter); } } private static PendingIntent c(Context context, String str, int i) { Intent intent = new Intent(str); intent.putExtra("taskId", i); return PendingIntent.getBroadcast(context, i, intent, 0); } private static Bitmap l(File file) { String absolutePath = file.getAbsolutePath(); WeakReference<Bitmap> weakReference = aVI.get(absolutePath); Bitmap bitmap = weakReference != null ? weakReference.get() : null; if (bitmap != null && !bitmap.isRecycled()) { return bitmap; } Bitmap decodeFile = BitmapFactory.decodeFile(absolutePath); aVI.put(absolutePath, new WeakReference<>(decodeFile)); return decodeFile; } private static DownloadParams m(DownloadTask downloadTask) { if (downloadTask == null) { return null; } Object tag = downloadTask.getTag(); DownloadParams downloadParams = tag instanceof DownloadParams ? (DownloadParams) tag : new DownloadParams(); downloadParams.mAppSize = (long) downloadTask.getSmallFileTotalBytes(); downloadParams.mTaskId = downloadTask.getId(); downloadParams.filePath = downloadTask.getTargetFilePath(); return downloadParams; } @Override // com.ksad.download.d public final void a(DownloadTask downloadTask, boolean z) { c a; Context context = ((e) ServiceProvider.get(e.class)).getContext(); if (context != null && !downloadTask.isNotificationRemoved() && (a = c.a(context, downloadTask.getId(), downloadTask.downloadEnablePause)) != null) { a(context, a, b.a(downloadTask, "正在下载", (String) null)); a(context, a.build(), false, true, null, downloadTask.getId(), z ? 1 : 0, downloadTask.isCompleted() ? 1 : 0); } } @Override // com.ksad.download.d public final void b(File file) { Context context = ((e) ServiceProvider.get(e.class)).getContext(); DownloadParams cE = com.kwad.sdk.core.a.Ks().cE(file.getAbsolutePath()); com.kwad.sdk.core.a.Ks().cF(file.getAbsolutePath()); if (context != null && cE != null) { AdTemplate cG = com.kwad.sdk.core.a.Ks().cG(cE.mDownloadid); if (cG != null) { cG.installFrom = "recall"; com.kwad.sdk.core.download.d.LX().aA(cG); } b a = b.a(cE, "下载完成", "立即安装"); b br = b.br(context); a(context, br, a); a(context, br.build(), false, false, com.ksad.download.e.a(file, cE.mTaskId, cE.requestInstallPermission), cE.mTaskId, 1, 2); } } @Override // com.ksad.download.d public final void h(DownloadTask downloadTask) { Object tag = downloadTask.getTag(); if (tag instanceof DownloadParams) { String str = ((DownloadParams) tag).mAppIcon; if (!TextUtils.isEmpty(str)) { File cm = ((com.kwad.sdk.service.kwai.c) ServiceProvider.get(com.kwad.sdk.service.kwai.c.class)).cm(str); if (cm == null || !cm.exists()) { ((com.kwad.sdk.service.kwai.c) ServiceProvider.get(com.kwad.sdk.service.kwai.c.class)).a(true, str, "", ""); } } } } @Override // com.ksad.download.d public final void i(DownloadTask downloadTask) { Context context = ((e) ServiceProvider.get(e.class)).getContext(); if (context != null && !downloadTask.isNotificationRemoved()) { b a = b.a(downloadTask, downloadTask.getSmallFileSoFarBytes() > 0 && downloadTask.getSmallFileTotalBytes() > 0 ? "正在下载" : "准备下载", (String) null); c a2 = c.a(context, downloadTask.getId(), downloadTask.downloadEnablePause); if (a2 != null) { a(context, a2, a); a(context, a2.build(), false, true, null, downloadTask.getId(), 1, downloadTask.isCompleted() ? 1 : 0); } } } @Override // com.ksad.download.d public final void j(DownloadTask downloadTask) { Context context = ((e) ServiceProvider.get(e.class)).getContext(); if (context != null) { DownloadParams m = m(downloadTask); b br = b.br(context); a(context, br, b.a(downloadTask, "下载完成", "立即安装")); com.kwad.sdk.core.a.Ks().a(downloadTask.getTargetFilePath(), m); com.kwad.sdk.core.a.Ks().a(m.mPkgname, m); a(context, br.build(), false, false, com.ksad.download.e.k(downloadTask), downloadTask.getId(), 1, 1); } } @Override // com.ksad.download.d public final void n(String str) { Context context = ((e) ServiceProvider.get(e.class)).getContext(); DownloadParams cE = com.kwad.sdk.core.a.Ks().cE(str); com.kwad.sdk.core.a.Ks().cF(str); if (context != null && cE != null) { com.kwad.sdk.core.a.Ks().cF(cE.filePath); b a = b.a(cE, "安装完成", "立刻打开"); b br = b.br(context); a(context, br, a); a(context, br.build(), false, false, com.ksad.download.e.e(cE.mPkgname, cE.mTaskId), cE.mTaskId, 1, 2); } } @Override // com.ksad.download.d public final void v(int i) { Context context = ((e) ServiceProvider.get(e.class)).getContext(); if (context != null) { ((NotificationManager) context.getSystemService("notification")).cancel(i); } } }