package cn.jpush.android.p; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.text.TextUtils; import androidx.core.app.NotificationCompat; import cn.jpush.android.cache.Key; import cn.jpush.android.cache.Sp; import cn.jpush.android.d.d; import cn.jpush.android.helper.Logger; import cn.jpush.android.p.b; import cn.jpush.android.service.SchedulerReceiver; import cn.jpush.android.u.b; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import org.json.JSONArray; import org.json.JSONObject; public class e { private static volatile e a; private LinkedList<a> b; private final Comparator<a> c = new Comparator<a>() { /* class cn.jpush.android.p.e.AnonymousClass1 */ /* renamed from: a */ public int compare(a aVar, a aVar2) { if (aVar.a < aVar2.a) { return -1; } return aVar.a == aVar2.a ? 0 : 1; } }; /* access modifiers changed from: package-private */ public static class a { private long a; private boolean b; private d c; private int d; public a(long j, int i) { this.a = j; this.b = false; this.d = i; } public a(long j, d dVar) { this.a = j; this.b = true; this.c = dVar; } public a(JSONObject jSONObject) { try { this.a = jSONObject.getLong("operationTime"); this.b = jSONObject.optBoolean("showOrDismiss"); String optString = jSONObject.optString("pushEntity"); if (!TextUtils.isEmpty(optString)) { this.c = d.a(optString); } this.d = jSONObject.optInt("notifyId"); } catch (Throwable unused) { } } public JSONObject a() { try { JSONObject jSONObject = new JSONObject(); jSONObject.put("operationTime", this.a); jSONObject.put("showOrDismiss", this.b); if (this.b) { jSONObject.put("pushEntity", this.c != null ? this.c.c() : null); } else { jSONObject.put("notifyId", this.d); } return jSONObject; } catch (Throwable unused) { return null; } } public String toString() { return "Item{operationTime=" + this.a + ", showOrDismiss=" + this.b + ", pushEntity=" + this.c + ", notifyId=" + this.d + '}'; } } private e() { } public static e a() { if (a == null) { synchronized (e.class) { if (a == null) { a = new e(); } } } return a; } private static void a(Context context, LinkedList<a> linkedList, long j, a aVar) { if (aVar != null) { if (aVar.a > j) { linkedList.add(aVar); } else if (!aVar.b) { Logger.d("NotificationScheduler", "cancelNotification:" + aVar); b.d(context, aVar.d); } else if (aVar.c == null || !d.a(context, aVar.c.c, aVar.c.f)) { long b2 = b.b(aVar.c.ap); int a2 = b.a(aVar.c); if (b2 <= 0) { Logger.d("NotificationScheduler", "handleNotification:" + aVar); b.a.a(context, aVar.c); } else if (b2 <= j) { Logger.d("NotificationScheduler", "cancelNotification:" + aVar); b.d(context, a2); } else { Logger.d("NotificationScheduler", "handleNotification:" + aVar); b.a.a(context, aVar.c); linkedList.add(new a(b2, a2)); } } else { Logger.d("NotificationScheduler", "item: " + aVar + " already cancel"); } } } private void b(Context context, a aVar) { try { Logger.d("NotificationScheduler", "schedule item=" + aVar); Intent intent = new Intent(context, SchedulerReceiver.class); intent.setAction(SchedulerReceiver.DELAY_NOTIFY); PendingIntent broadcast = PendingIntent.getBroadcast(context, 1000, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(NotificationCompat.CATEGORY_ALARM); if (alarmManager != null) { if (Build.VERSION.SDK_INT >= 19) { alarmManager.setWindow(0, aVar.a, 300, broadcast); } else { alarmManager.set(0, aVar.a, broadcast); } Logger.d("NotificationScheduler", "setAlarm at=" + cn.jpush.android.u.b.a("yyyy-MM-dd HH:mm:ss").format(new Date(aVar.a))); } } catch (Throwable th) { Logger.ww("NotificationScheduler", "can't trigger alarm cause by exception:" + th); } } public void a(Context context) { this.b = new LinkedList<>(); String str = (String) Sp.get(context, Key.NotiSchedule()); if (!TextUtils.isEmpty(str)) { try { JSONArray jSONArray = new JSONArray(str); for (int i = 0; i < jSONArray.length(); i++) { this.b.add(new a(jSONArray.getJSONObject(i))); } } catch (Throwable unused) { } } } public synchronized void a(Context context, a aVar) { a(context); LinkedList<a> linkedList = new LinkedList<>(); long currentTimeMillis = System.currentTimeMillis(); boolean z = aVar != null; Iterator<a> it = this.b.iterator(); while (it.hasNext()) { a next = it.next(); if (z && next.a > aVar.a) { a(context, linkedList, currentTimeMillis, aVar); z = false; } a(context, linkedList, currentTimeMillis, next); } if (z) { a(context, linkedList, currentTimeMillis, aVar); } this.b = linkedList; b(context); if (!linkedList.isEmpty()) { b(context, linkedList.getFirst()); } } public void b(Context context) { String str; LinkedList<a> linkedList = this.b; if (linkedList == null || linkedList.isEmpty()) { str = null; } else { JSONArray jSONArray = new JSONArray(); Iterator<a> it = this.b.iterator(); while (it.hasNext()) { jSONArray.put(it.next().a()); } str = jSONArray.toString(); } Collections.sort(this.b, this.c); Sp.set(context, Key.NotiSchedule().set(str)); } }