Skip to content

Commit 5cca792

Browse files
committed
Added initialization check on Android.
1 parent 945b652 commit 5cca792

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

android/notification/src/main/java/org/godotengine/plugin/android/notification/NotificationSchedulerPlugin.java

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,20 @@ public class NotificationSchedulerPlugin extends GodotPlugin {
5959
private static final int NOTIFICATION_NOT_FOUND = -1;
6060

6161
private Activity activity;
62+
private boolean isInitialized;
6263

6364
public NotificationSchedulerPlugin(Godot godot) {
6465
super(godot);
66+
isInitialized = false;
6567
}
6668

6769
/**
6870
* Initializes plugin.
6971
*/
7072
@UsedByGodot
7173
public void initialize() {
74+
isInitialized = true;
75+
7276
// Nothing to do on Android version (implemented for platform parity)
7377
emitSignal(getGodot(), getPluginName(), INITIALIZATION_COMPLETED_SIGNAL);
7478
}
@@ -82,6 +86,11 @@ public void initialize() {
8286
@RequiresApi(api = Build.VERSION_CODES.O)
8387
@UsedByGodot
8488
public int create_notification_channel(Dictionary data) {
89+
if (!isInitialized) {
90+
Log.e(LOG_TAG, "create_notification_channel(): plugin is not initialized!");
91+
return Error.ERR_UNCONFIGURED.toNativeValue();
92+
}
93+
8594
ChannelData channelData = new ChannelData(data);
8695
if (channelData.validate()) {
8796
NotificationManager manager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE);
@@ -104,6 +113,7 @@ public int create_notification_channel(Dictionary data) {
104113
Log.e(LOG_TAG, "create_notification_channel(): invalid channel data object");
105114
return Error.ERR_INVALID_DATA.toNativeValue();
106115
}
116+
107117
return Error.OK.toNativeValue();
108118
}
109119

@@ -113,9 +123,13 @@ public int create_notification_channel(Dictionary data) {
113123
* @param data dictionary containing notification data, including delaySeconds that specifies
114124
* how many seconds from now to schedule the notification.
115125
*/
116-
@RequiresApi(api = Build.VERSION_CODES.N)
117126
@UsedByGodot
118-
public void schedule(Dictionary data) {
127+
public int schedule(Dictionary data) {
128+
if (!isInitialized) {
129+
Log.e(LOG_TAG, "schedule(): plugin is not initialized!");
130+
return Error.ERR_UNCONFIGURED.toNativeValue();
131+
}
132+
119133
NotificationData notificationData = new NotificationData(data);
120134
Log.d(LOG_TAG, "schedule():: notification id: " + notificationData.getId());
121135
if (notificationData.isValid()) {
@@ -148,23 +162,37 @@ public void schedule(Dictionary data) {
148162
} else {
149163
Log.e(LOG_TAG, "schedule(): invalid notification data object");
150164
}
165+
166+
return Error.OK.toNativeValue();
151167
}
152168

153169
/**
154170
* Cancel notification with given ID
155171
*
156172
* @param notificationId ID of notification to cancel
157173
*/
158-
@RequiresApi(api = Build.VERSION_CODES.M)
159174
@UsedByGodot
160-
public void cancel(int notificationId) {
175+
public int cancel(int notificationId) {
176+
if (!isInitialized) {
177+
Log.e(LOG_TAG, "cancel(): plugin is not initialized!");
178+
return Error.ERR_UNCONFIGURED.toNativeValue();
179+
}
180+
161181
cancelNotification(activity, notificationId);
162182
Log.d(LOG_TAG, "cancel():: notification id: " + notificationId);
183+
184+
return Error.OK.toNativeValue();
163185
}
164186

165187
@UsedByGodot
166-
public void set_badge_count(int badgeCount) {
188+
public int set_badge_count(int badgeCount) {
189+
if (!isInitialized) {
190+
Log.e(LOG_TAG, "set_badge_count(): plugin is not initialized!");
191+
return Error.ERR_UNCONFIGURED.toNativeValue();
192+
}
193+
167194
Log.e(LOG_TAG, "set_badge_count(): method not supported on Android");
195+
return Error.ERR_UNAVAILABLE.toNativeValue();
168196
}
169197

170198
/**
@@ -174,6 +202,11 @@ public void set_badge_count(int badgeCount) {
174202
*/
175203
@UsedByGodot
176204
public int get_notification_id(int defaultValue) {
205+
if (!isInitialized) {
206+
Log.e(LOG_TAG, "get_notification_id(): plugin is not initialized!");
207+
return defaultValue;
208+
}
209+
177210
int notificationId = defaultValue;
178211
Activity activity = getActivity();
179212
if (activity != null) {
@@ -193,6 +226,11 @@ public int get_notification_id(int defaultValue) {
193226
*/
194227
@UsedByGodot
195228
public boolean has_post_notifications_permission() {
229+
if (!isInitialized) {
230+
Log.e(LOG_TAG, "has_post_notifications_permission(): plugin is not initialized!");
231+
return false;
232+
}
233+
196234
boolean result = false;
197235
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) {
198236
if (NotificationManagerCompat.from(activity.getApplicationContext()).areNotificationsEnabled()) {
@@ -209,7 +247,12 @@ public boolean has_post_notifications_permission() {
209247
* Sends a request to acquire POST_NOTIFICATIONS permission for the app
210248
*/
211249
@UsedByGodot
212-
public void request_post_notifications_permission() {
250+
public int request_post_notifications_permission() {
251+
if (!isInitialized) {
252+
Log.e(LOG_TAG, "request_post_notifications_permission(): plugin is not initialized!");
253+
return Error.ERR_UNCONFIGURED.toNativeValue();
254+
}
255+
213256
try {
214257
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) {
215258
ActivityCompat.requestPermissions(activity, new String[]{ Manifest.permission.POST_NOTIFICATIONS },
@@ -220,13 +263,20 @@ public void request_post_notifications_permission() {
220263
} catch (Exception e) {
221264
Log.e(LOG_TAG, "request_post_notifications_permission():: Failed to request permission due to " + e.getMessage());
222265
}
266+
267+
return Error.OK.toNativeValue();
223268
}
224269

225270
/**
226271
* Opens APP INFO settings screen
227272
*/
228273
@UsedByGodot
229-
public void open_app_info_settings() {
274+
public int open_app_info_settings() {
275+
if (!isInitialized) {
276+
Log.e(LOG_TAG, "open_app_info_settings(): plugin is not initialized!");
277+
return Error.ERR_UNCONFIGURED.toNativeValue();
278+
}
279+
230280
Log.d(LOG_TAG, "open_app_info_settings()");
231281

232282
try {
@@ -238,6 +288,8 @@ public void open_app_info_settings() {
238288
} catch (Exception e) {
239289
Log.e(LOG_TAG, "open_app_info_settings():: Failed due to "+ e.getMessage());
240290
}
291+
292+
return Error.OK.toNativeValue();
241293
}
242294

243295
@NonNull
@@ -345,14 +397,12 @@ void handleNotificationDismissed(int notificationId) {
345397
emitSignal(getGodot(), getPluginName(), NOTIFICATION_DISMISSED_SIGNAL, notificationId);
346398
}
347399

348-
@RequiresApi(api = Build.VERSION_CODES.N)
349400
private long calculateTimeAfterDelay(int delaySeconds) {
350401
Calendar calendar = Calendar.getInstance();
351402
calendar.add(Calendar.SECOND, delaySeconds);
352403
return calendar.getTimeInMillis();
353404
}
354405

355-
@RequiresApi(api = Build.VERSION_CODES.N)
356406
private void scheduleNotification(Activity activity, int notificationId, Intent intent, int delaySeconds) {
357407
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(ALARM_SERVICE);
358408
long timeAfterDelay = calculateTimeAfterDelay(delaySeconds);
@@ -362,7 +412,6 @@ private void scheduleNotification(Activity activity, int notificationId, Intent
362412
Log.i(LOG_TAG, String.format("Scheduled notification '%d' to be delivered at %d.", notificationId, timeAfterDelay));
363413
}
364414

365-
@RequiresApi(api = Build.VERSION_CODES.N)
366415
private void scheduleRepeatingNotification(Activity activity, int notificationId, Intent intent, int delaySeconds, int intervalSeconds) {
367416
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(ALARM_SERVICE);
368417
long timeAfterDelay = calculateTimeAfterDelay(delaySeconds);
@@ -372,7 +421,6 @@ private void scheduleRepeatingNotification(Activity activity, int notificationId
372421
Log.i(LOG_TAG, String.format("Scheduled notification '%d' to be delivered at %d with %ds interval.", notificationId, timeAfterDelay, intervalSeconds));
373422
}
374423

375-
@RequiresApi(api = Build.VERSION_CODES.M)
376424
private void cancelNotification(Activity activity, int notificationId) {
377425
Context context = activity.getApplicationContext();
378426

0 commit comments

Comments
 (0)