package com.firemonkeys.cloudcellapi; import android.annotation.TargetApi; import android.app.Activity; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import androidx.core.app.NotificationCompat; import com.google.android.gms.drive.DriveFile; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import org.json.JSONArray; import org.json.JSONObject; /* loaded from: classes2.dex */ public class LocalNotificationsCenter { private static String CLASSNAME = "LocalNotificationsCenter"; public static final String EXTRA_CHANNEL_ID = "channel"; public static final String EXTRA_FIRE_TIME = "firetime"; public static final String EXTRA_ID = "id"; public static final String EXTRA_LAUNCH_URL = "launchURL"; public static final String EXTRA_LAUNCH_URL_MP_INVITE = "MultiplayerInvite"; public static final String EXTRA_LAUNCH_URL_RT_ADMIN = "RaceTeamsAdmin"; public static final String EXTRA_MESSAGE = "message"; public static final String EXTRA_NAME = "name"; public static final String EXTRA_REMINDER = "reminder"; static final String FILENAME = ResourceHelper.GetNotificationsFilename(); private static HashMap mNotificationsIntent = new HashMap<>(); private static Context broadcastContext = null; public static class LocalNotification { public String mChannel; public long mFireTime; public int mId; public String mMessage; public String mName; public int mReminderRemain; public String mURL; public JSONObject Serialise() { JSONObject jSONObject = new JSONObject(); try { jSONObject.put("id", this.mId); jSONObject.put("name", this.mName); jSONObject.put("firetime", this.mFireTime); jSONObject.put("message", this.mMessage); jSONObject.put(LocalNotificationsCenter.EXTRA_LAUNCH_URL, this.mURL); jSONObject.put("reminder", this.mReminderRemain); jSONObject.put(LocalNotificationsCenter.EXTRA_CHANNEL_ID, this.mChannel); } catch (Exception e) { Logging.CC_ERROR(LocalNotificationsCenter.CLASSNAME, e.getMessage()); } return jSONObject; } public void Serialise(JSONObject jSONObject) { try { this.mId = jSONObject.optInt("id"); this.mName = jSONObject.optString("name"); this.mFireTime = jSONObject.optLong("firetime"); this.mMessage = jSONObject.optString("message"); this.mURL = jSONObject.optString(LocalNotificationsCenter.EXTRA_LAUNCH_URL); this.mReminderRemain = jSONObject.optInt("reminder"); this.mChannel = jSONObject.optString(LocalNotificationsCenter.EXTRA_CHANNEL_ID); } catch (Exception e) { Logging.CC_ERROR(LocalNotificationsCenter.CLASSNAME, e.getMessage()); } } public LocalNotification(JSONObject jSONObject) { this.mReminderRemain = -1; Serialise(jSONObject); } public LocalNotification(int i, String str, String str2, String str3) { this.mReminderRemain = -1; this.mId = i; this.mName = str; this.mMessage = str2; this.mURL = str3; this.mChannel = ""; } public LocalNotification(int i, String str, String str2, String str3, String str4) { this.mReminderRemain = -1; this.mId = i; this.mName = str; this.mMessage = str2; this.mURL = str3; this.mChannel = str4; } } private static Context getStaticContext() { Activity GetActivity = CC_Component.GetActivity(); return GetActivity != null ? GetActivity.getApplicationContext() : broadcastContext; } @TargetApi(19) public static PendingIntent CreateNotificationIntent(LocalNotification localNotification) { Context applicationContext = CC_Component.GetActivity().getApplicationContext(); Intent intent = new Intent(applicationContext, (Class) DelayedNotificationService.class); intent.setType("type" + localNotification.mName); intent.putExtra("message", localNotification.mMessage); intent.putExtra("id", localNotification.mId); int i = localNotification.mReminderRemain; if (i != -1) { intent.putExtra("reminder", i); } if (localNotification.mURL.length() > 0) { intent.putExtra(EXTRA_LAUNCH_URL, localNotification.mURL); } intent.putExtra(EXTRA_CHANNEL_ID, localNotification.mChannel); return PendingIntent.getService(applicationContext, 0, intent, 201326592); } @TargetApi(19) public static void showNotification(LocalNotification localNotification) { if (localNotification == null) { return; } PendingIntent CreateNotificationIntent = CreateNotificationIntent(localNotification); try { ((AlarmManager) CC_Component.GetActivity().getApplicationContext().getSystemService(NotificationCompat.CATEGORY_ALARM)).set(0, localNotification.mFireTime, CreateNotificationIntent); } catch (Exception e) { Logging.CC_ERROR(CLASSNAME, "showNotification: failed to set alarm: " + e.toString()); } mNotificationsIntent.put(localNotification, CreateNotificationIntent); SaveNotifications(); } @TargetApi(19) public static void showNotification(int i, String str, long j, String str2, String str3) { if (str3 == null) { str3 = ""; } LocalNotification localNotification = new LocalNotification(i, str, str2, str3); localNotification.mFireTime = j; Logging.CC_INFO(CLASSNAME, "showNotification id: " + i + " name: " + str + " delay: " + j + " msg: " + str2); String str4 = CLASSNAME; StringBuilder sb = new StringBuilder(); sb.append("showNotification current time: "); sb.append(System.currentTimeMillis()); Logging.CC_INFO(str4, sb.toString()); Logging.CC_INFO(CLASSNAME, "showNotification fire time: " + localNotification.mFireTime); Logging.CC_INFO(CLASSNAME, "showNotification url: " + str3); Logging.CC_INFO(CLASSNAME, "showNotification channel empty "); showNotification(localNotification); } @TargetApi(26) public static void showNotification(int i, String str, long j, String str2, String str3, String str4) { if (str3 == null) { str3 = ""; } LocalNotification localNotification = new LocalNotification(i, str, str2, str3, str4); localNotification.mFireTime = j; Logging.CC_INFO(CLASSNAME, "showNotification id: " + i + " name: " + str + " delay: " + j + " msg: " + str2); String str5 = CLASSNAME; StringBuilder sb = new StringBuilder(); sb.append("showNotification current time: "); sb.append(System.currentTimeMillis()); Logging.CC_INFO(str5, sb.toString()); Logging.CC_INFO(CLASSNAME, "showNotification fire time: " + localNotification.mFireTime); Logging.CC_INFO(CLASSNAME, "showNotification url: " + str3); Logging.CC_INFO(CLASSNAME, "showNotification channel: " + str4); showNotification(localNotification); } public static void CancelAllNotifications() { Logging.CC_TRACE(CLASSNAME, "CancelAllNotifications: start"); if (mNotificationsIntent.size() <= 0) { LoadNotifications(false); } Context staticContext = getStaticContext(); ((NotificationManager) staticContext.getSystemService("notification")).cancelAll(); Iterator it = mNotificationsIntent.keySet().iterator(); while (it.hasNext()) { CancelNotification((LocalNotification) it.next()); } mNotificationsIntent.clear(); SaveNotifications(); Logging.CC_TRACE(CLASSNAME, "CancelAllNotifications: end"); SerialiseNotificationsHelper.ClearAll(staticContext); } public static void CancelNotification(LocalNotification localNotification) { Context staticContext = getStaticContext(); NotificationManager notificationManager = (NotificationManager) staticContext.getSystemService("notification"); AlarmManager alarmManager = (AlarmManager) staticContext.getSystemService(NotificationCompat.CATEGORY_ALARM); PendingIntent pendingIntent = mNotificationsIntent.get(localNotification); if (pendingIntent == null) { pendingIntent = CreateNotificationIntent(localNotification); } try { Logging.CC_INFO(CLASSNAME, "Cancelling notification: " + localNotification.mId); notificationManager.cancel(localNotification.mId); if (pendingIntent != null) { alarmManager.cancel(pendingIntent); pendingIntent.cancel(); } } catch (Exception e) { Logging.CC_ERROR(CLASSNAME, "CancelNotification: failed to cancel alarm: " + e.toString()); } } public static void CancelNotification(int i, String str) { Context staticContext = getStaticContext(); AlarmManager alarmManager = (AlarmManager) staticContext.getSystemService(NotificationCompat.CATEGORY_ALARM); Intent intent = new Intent(staticContext, (Class) DelayedNotificationService.class); intent.setType("type" + str); PendingIntent service = PendingIntent.getService(staticContext, 0, intent, DriveFile.MODE_READ_ONLY); try { ((NotificationManager) staticContext.getSystemService("notification")).cancel(i); alarmManager.cancel(service); } catch (Exception e) { Logging.CC_ERROR(CLASSNAME, "CancelNotification: failed to cancel alarm: " + e.toString()); } } public static void SetAreNotificationsAllowed(boolean z) { SerialiseNotificationsHelper.SetAreNotificationsAllowed(getStaticContext(), z); if (z) { return; } CancelAllNotifications(); } public static void SaveNotifications() { SaveNotifications(mNotificationsIntent.keySet()); } public static void SaveNotifications(Iterable iterable) { Logging.CC_INFO(CLASSNAME, "SaveNotifications() Num:" + mNotificationsIntent.size()); try { JSONArray jSONArray = new JSONArray(); Iterator it = iterable.iterator(); while (it.hasNext()) { jSONArray.put(it.next().Serialise()); } FileOutputStream fileOutputStream = new FileOutputStream(new File(getStaticContext().getFilesDir(), FILENAME)); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(jSONArray.toString()); objectOutputStream.close(); fileOutputStream.close(); } catch (Exception e) { Logging.CC_ERROR(CLASSNAME, "SaveNotifications failed: " + e.toString()); } } public static void LoadNotifications(boolean z) { for (LocalNotification localNotification : LoadNotifications()) { if (z) { showNotification(localNotification); } else { mNotificationsIntent.put(localNotification, null); } } } public static Iterable LoadNotifications() { ArrayList arrayList = new ArrayList(); Logging.CC_INFO(CLASSNAME, "LoadNotifications()"); try { File file = new File(getStaticContext().getFilesDir(), FILENAME); if (file.exists()) { Logging.CC_INFO(CLASSNAME, "Found saved file. Loading information"); FileInputStream fileInputStream = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); String str = (String) objectInputStream.readObject(); objectInputStream.close(); fileInputStream.close(); JSONArray jSONArray = new JSONArray(str); for (int i = 0; i < jSONArray.length(); i++) { arrayList.add(new LocalNotification(jSONArray.getJSONObject(i))); } } else { Logging.CC_ERROR(CLASSNAME, "File not found"); } } catch (Exception e) { Logging.CC_ERROR(CLASSNAME, "LoadNotifications failed: " + e.toString()); } return arrayList; } }