|
| 1 | +package com.iterable.iterableapi; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | + |
| 6 | +import androidx.annotation.NonNull; |
| 7 | +import androidx.annotation.Nullable; |
| 8 | +import androidx.annotation.VisibleForTesting; |
| 9 | +import androidx.work.OneTimeWorkRequest; |
| 10 | +import androidx.work.OutOfQuotaPolicy; |
| 11 | +import androidx.work.WorkManager; |
| 12 | + |
| 13 | +import java.util.UUID; |
| 14 | + |
| 15 | +/** |
| 16 | + * Manages scheduling of notification processing work using WorkManager. |
| 17 | + * This class is responsible for: |
| 18 | + * - Creating WorkManager requests for notification processing |
| 19 | + * - Enqueueing work with expedited execution for high-priority notifications |
| 20 | + * - Providing callback interface for success/failure handling |
| 21 | + * - Comprehensive logging of scheduling operations |
| 22 | + */ |
| 23 | +class IterableNotificationWorkScheduler { |
| 24 | + |
| 25 | + private static final String TAG = "IterableNotificationWorkScheduler"; |
| 26 | + |
| 27 | + private final Context context; |
| 28 | + private final WorkManager workManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * Callback interface for work scheduling results. |
| 32 | + * Allows caller to handle success/failure appropriately. |
| 33 | + */ |
| 34 | + public interface SchedulerCallback { |
| 35 | + /** |
| 36 | + * Called when work is successfully scheduled. |
| 37 | + * @param workId UUID of the scheduled work |
| 38 | + */ |
| 39 | + void onScheduleSuccess(UUID workId); |
| 40 | + |
| 41 | + /** |
| 42 | + * Called when work scheduling fails. |
| 43 | + * @param exception The exception that caused the failure |
| 44 | + * @param notificationData The original notification data (for fallback) |
| 45 | + */ |
| 46 | + void onScheduleFailure(Exception exception, Bundle notificationData); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor for production use. |
| 51 | + * Initializes with application context and default WorkManager instance. |
| 52 | + * |
| 53 | + * @param context Application or service context |
| 54 | + */ |
| 55 | + public IterableNotificationWorkScheduler(@NonNull Context context) { |
| 56 | + this(context, WorkManager.getInstance(context)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor for testing. |
| 61 | + * Allows injection of mock WorkManager for unit testing. |
| 62 | + * |
| 63 | + * @param context Application or service context |
| 64 | + * @param workManager WorkManager instance (can be mocked for tests) |
| 65 | + */ |
| 66 | + @VisibleForTesting |
| 67 | + IterableNotificationWorkScheduler(@NonNull Context context, @NonNull WorkManager workManager) { |
| 68 | + this.context = context.getApplicationContext(); |
| 69 | + this.workManager = workManager; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Schedules notification processing work using WorkManager. |
| 74 | + * |
| 75 | + * Creates an expedited OneTimeWorkRequest and enqueues it with WorkManager. |
| 76 | + * Expedited execution ensures high-priority notifications are processed promptly, |
| 77 | + * with quota exemption when called from FCM onMessageReceived. |
| 78 | + * |
| 79 | + * @param notificationData Bundle containing notification data |
| 80 | + * @param isGhostPush Whether this is a ghost/silent push |
| 81 | + * @param callback Optional callback for success/failure (can be null) |
| 82 | + */ |
| 83 | + public void scheduleNotificationWork( |
| 84 | + @NonNull Bundle notificationData, |
| 85 | + boolean isGhostPush, |
| 86 | + @Nullable SchedulerCallback callback) { |
| 87 | + |
| 88 | + IterableLogger.d(TAG, "========================================"); |
| 89 | + IterableLogger.d(TAG, "Scheduling notification work"); |
| 90 | + IterableLogger.d(TAG, "Bundle keys: " + notificationData.keySet().size()); |
| 91 | + IterableLogger.d(TAG, "Is ghost push: " + isGhostPush); |
| 92 | + |
| 93 | + try { |
| 94 | + IterableLogger.d(TAG, "Step 1: Creating Worker input data"); |
| 95 | + androidx.work.Data inputData = IterableNotificationWorker.createInputData( |
| 96 | + notificationData, |
| 97 | + isGhostPush |
| 98 | + ); |
| 99 | + IterableLogger.d(TAG, " ✓ Worker input data created successfully"); |
| 100 | + |
| 101 | + IterableLogger.d(TAG, "Step 2: Building expedited WorkRequest"); |
| 102 | + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(IterableNotificationWorker.class) |
| 103 | + .setInputData(inputData) |
| 104 | + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) |
| 105 | + .build(); |
| 106 | + IterableLogger.d(TAG, " ✓ WorkRequest built with expedited execution"); |
| 107 | + |
| 108 | + IterableLogger.d(TAG, "Step 3: Enqueueing work with WorkManager"); |
| 109 | + workManager.enqueue(workRequest); |
| 110 | + |
| 111 | + UUID workId = workRequest.getId(); |
| 112 | + IterableLogger.d(TAG, " ✓ Work enqueued successfully"); |
| 113 | + IterableLogger.d(TAG, ""); |
| 114 | + IterableLogger.d(TAG, "✓ NOTIFICATION WORK SCHEDULED"); |
| 115 | + IterableLogger.d(TAG, " Work ID: " + workId); |
| 116 | + IterableLogger.d(TAG, " Priority: EXPEDITED (high-priority notification)"); |
| 117 | + IterableLogger.d(TAG, " Worker: " + IterableNotificationWorker.class.getSimpleName()); |
| 118 | + IterableLogger.d(TAG, "========================================"); |
| 119 | + |
| 120 | + if (callback != null) { |
| 121 | + callback.onScheduleSuccess(workId); |
| 122 | + } |
| 123 | + |
| 124 | + } catch (Exception e) { |
| 125 | + IterableLogger.e(TAG, "========================================"); |
| 126 | + IterableLogger.e(TAG, "✗ FAILED TO SCHEDULE NOTIFICATION WORK"); |
| 127 | + IterableLogger.e(TAG, "Error type: " + e.getClass().getSimpleName()); |
| 128 | + IterableLogger.e(TAG, "Error message: " + e.getMessage()); |
| 129 | + IterableLogger.e(TAG, "========================================"); |
| 130 | + |
| 131 | + if (callback != null) { |
| 132 | + callback.onScheduleFailure(e, notificationData); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @VisibleForTesting |
| 138 | + Context getContext() { |
| 139 | + return context; |
| 140 | + } |
| 141 | + |
| 142 | + @VisibleForTesting |
| 143 | + WorkManager getWorkManager() { |
| 144 | + return workManager; |
| 145 | + } |
| 146 | +} |
0 commit comments