|
| 1 | +package org.medicmobile.webapp.mobile; |
| 2 | + |
| 3 | +import static org.medicmobile.webapp.mobile.MedicLog.log; |
| 4 | + |
| 5 | +import android.Manifest; |
| 6 | +import android.app.Activity; |
| 7 | +import android.app.NotificationChannel; |
| 8 | +import android.app.NotificationManager; |
| 9 | +import android.app.PendingIntent; |
| 10 | +import android.content.Context; |
| 11 | +import android.content.Intent; |
| 12 | +import android.content.pm.PackageManager; |
| 13 | +import android.net.Uri; |
| 14 | +import android.os.Build; |
| 15 | +import android.text.TextUtils; |
| 16 | + |
| 17 | +import androidx.core.app.ActivityCompat; |
| 18 | +import androidx.core.app.NotificationCompat; |
| 19 | +import androidx.core.content.ContextCompat; |
| 20 | +import androidx.work.ExistingPeriodicWorkPolicy; |
| 21 | +import androidx.work.PeriodicWorkRequest; |
| 22 | +import androidx.work.WorkManager; |
| 23 | + |
| 24 | +import org.json.JSONArray; |
| 25 | +import org.json.JSONException; |
| 26 | +import org.json.JSONObject; |
| 27 | +import org.medicmobile.webapp.mobile.util.AppDataStore; |
| 28 | + |
| 29 | +import java.time.LocalDate; |
| 30 | +import java.time.ZoneId; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +public class AppNotificationManager { |
| 34 | + private static final String CHANNEL_ID = "cht_android_notifications"; |
| 35 | + private static final String CHANNEL_NAME = "CHT Android Notifications"; |
| 36 | + public static final int REQUEST_NOTIFICATION_PERMISSION = 1001; |
| 37 | + public static final String TASK_NOTIFICATIONS_KEY = "task_notifications"; |
| 38 | + public static final String TASK_NOTIFICATION_DAY_KEY = "cht_task_notification_day"; |
| 39 | + public static final String LATEST_NOTIFICATION_TIMESTAMP_KEY = "cht_task_notification_timestamp"; |
| 40 | + public static final String MAX_NOTIFICATIONS_TO_SHOW_KEY = "cht_max_task_notifications"; |
| 41 | + |
| 42 | + private final Context context; |
| 43 | + private final NotificationManager manager; |
| 44 | + private final String appUrl; |
| 45 | + private final AppDataStore appDataStore; |
| 46 | + |
| 47 | + public AppNotificationManager(Context context) { |
| 48 | + this.context = context.getApplicationContext(); |
| 49 | + SettingsStore settings = SettingsStore.in(context); |
| 50 | + this.appUrl = settings.getAppUrl(); |
| 51 | + manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 52 | + appDataStore = AppDataStore.getInstance(this.context); |
| 53 | + createNotificationChannel(); |
| 54 | + } |
| 55 | + |
| 56 | + public boolean hasNotificationPermission() { |
| 57 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 58 | + return ContextCompat.checkSelfPermission(context, |
| 59 | + Manifest.permission.POST_NOTIFICATIONS) |
| 60 | + == PackageManager.PERMISSION_GRANTED; |
| 61 | + } |
| 62 | + //versions below 13 |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + public void requestNotificationPermission(Activity activity) { |
| 67 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !hasNotificationPermission()) { |
| 68 | + ActivityCompat.requestPermissions(activity, |
| 69 | + new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATION_PERMISSION); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void startNotificationWorker() { |
| 74 | + if (hasNotificationPermission()) { |
| 75 | + PeriodicWorkRequest request = new PeriodicWorkRequest.Builder( |
| 76 | + NotificationWorker.class, |
| 77 | + NotificationWorker.WORKER_REPEAT_INTERVAL_MINS, |
| 78 | + TimeUnit.MINUTES |
| 79 | + ) |
| 80 | + .addTag(NotificationWorker.NOTIFICATION_WORK_REQUEST_TAG) |
| 81 | + .build(); |
| 82 | + |
| 83 | + WorkManager.getInstance(context).enqueueUniquePeriodicWork( |
| 84 | + NotificationWorker.NOTIFICATION_WORK_NAME, |
| 85 | + ExistingPeriodicWorkPolicy.KEEP, |
| 86 | + request |
| 87 | + ); |
| 88 | + log(context, "startNotificationWorker() :: Started Notification Work Manager..."); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void stopNotificationWorker() { |
| 93 | + WorkManager.getInstance(context).cancelAllWorkByTag(NotificationWorker.NOTIFICATION_WORK_REQUEST_TAG); |
| 94 | + log(context, "stopNotificationWorker() :: Stopped notification work manager"); |
| 95 | + } |
| 96 | + |
| 97 | + void cancelAllNotifications() { |
| 98 | + manager.cancelAll(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param jsArrayString string notifications sorted by readyAt in descending order |
| 103 | + * @throws JSONException throws JSONException |
| 104 | + * Method is blocking don't run on UI thread |
| 105 | + */ |
| 106 | + public void showNotificationsFromJsArray(String jsArrayString) throws JSONException { |
| 107 | + JSONArray dataArray = Utils.parseJSArrayData(jsArrayString); |
| 108 | + if (dataArray.length() == 0) { |
| 109 | + return; |
| 110 | + } |
| 111 | + showMultipleTaskNotifications(dataArray); |
| 112 | + } |
| 113 | + |
| 114 | + private void showMultipleTaskNotifications(JSONArray dataArray) throws JSONException { |
| 115 | + Intent intent = new Intent(context, EmbeddedBrowserActivity.class); |
| 116 | + intent.setData(Uri.parse(TextUtils.concat(appUrl, "/#/tasks").toString())); |
| 117 | + long maxNotifications = appDataStore.getLongBlocking(MAX_NOTIFICATIONS_TO_SHOW_KEY, 8L); |
| 118 | + long latestStoredTimestamp = getLatestStoredTimestamp(getStartOfDay()); |
| 119 | + int counter = 0; |
| 120 | + long latestReadyAt = 0; |
| 121 | + for (int i = 0; i < dataArray.length(); i++) { |
| 122 | + JSONObject notification = dataArray.getJSONObject(i); |
| 123 | + long readyAt = notification.getLong("readyAt"); |
| 124 | + latestReadyAt = Math.max(latestReadyAt, readyAt); |
| 125 | + if (counter >= maxNotifications) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + long endDate = notification.getLong("endDate"); |
| 130 | + long dueDate = notification.getLong("dueDate"); |
| 131 | + if (isValidNotification(latestStoredTimestamp, readyAt, dueDate, endDate)) { |
| 132 | + int notificationId = notification.getString("_id").hashCode(); |
| 133 | + String contentText = notification.getString("contentText"); |
| 134 | + String title = notification.getString("title"); |
| 135 | + showNotification(intent, notificationId, title, contentText); |
| 136 | + counter++; |
| 137 | + } |
| 138 | + } |
| 139 | + saveLatestNotificationTimestamp(latestReadyAt); |
| 140 | + } |
| 141 | + |
| 142 | + private boolean isValidNotification(long latestStoredTimestamp, long readyAt, long dueDate, long endDate) { |
| 143 | + long startOfDay = getStartOfDay(); |
| 144 | + return readyAt > latestStoredTimestamp && dueDate <= startOfDay && endDate >= startOfDay; |
| 145 | + } |
| 146 | + |
| 147 | + public void saveLatestNotificationTimestamp(long value) { |
| 148 | + appDataStore.saveLong(LATEST_NOTIFICATION_TIMESTAMP_KEY, value); |
| 149 | + } |
| 150 | + |
| 151 | + public long getStartOfDay() { |
| 152 | + return LocalDate.now() |
| 153 | + .atStartOfDay(ZoneId.systemDefault()) |
| 154 | + .toInstant().toEpochMilli(); |
| 155 | + } |
| 156 | + |
| 157 | + private long getLatestStoredTimestamp(long startOfDay) { |
| 158 | + if (isNewDay(startOfDay)) { |
| 159 | + return 0; |
| 160 | + } |
| 161 | + return appDataStore.getLongBlocking(LATEST_NOTIFICATION_TIMESTAMP_KEY, 0L); |
| 162 | + } |
| 163 | + |
| 164 | + private boolean isNewDay(long startOfDay) { |
| 165 | + long storedNotificationDay = appDataStore.getLongBlocking(TASK_NOTIFICATION_DAY_KEY, 0L); |
| 166 | + if (getStartOfDay() != storedNotificationDay) { |
| 167 | + appDataStore.saveLong(TASK_NOTIFICATION_DAY_KEY, startOfDay); |
| 168 | + return true; |
| 169 | + } |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + void showNotification(Intent intent, int id, String title, String contentText) { |
| 174 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 175 | + PendingIntent pendingIntent = PendingIntent.getActivity( |
| 176 | + context, |
| 177 | + 0, |
| 178 | + intent, |
| 179 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE |
| 180 | + ); |
| 181 | + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) |
| 182 | + .setSmallIcon(R.drawable.ic_notification) |
| 183 | + .setContentTitle(title) |
| 184 | + .setContentText(contentText) |
| 185 | + .setAutoCancel(true) |
| 186 | + .setContentIntent(pendingIntent) |
| 187 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT); |
| 188 | + manager.notify(id, builder.build()); |
| 189 | + } |
| 190 | + |
| 191 | + private void createNotificationChannel() { |
| 192 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 193 | + NotificationChannel notificationChannel = new NotificationChannel( |
| 194 | + CHANNEL_ID, |
| 195 | + CHANNEL_NAME, |
| 196 | + NotificationManager.IMPORTANCE_DEFAULT |
| 197 | + ); |
| 198 | + manager.createNotificationChannel(notificationChannel); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments