Skip to content

Commit 53e810e

Browse files
committed
add public CallbackThreadManager class
This new public API allows changing the default thread used to fire aync events, observers, and callbacks for the rest of the OneSignal public API. This is intended for advanced use-case, motivated mostly a need of the OneSignal Unity wrapper SDK. Specifically to avoid cases where Unity may cause the main UI thread to wait on a background thread when calling back into Unity. Given how the binding between C# and Java works here, there is no way for the C# code to switch threads itself, so this API has to be available to do so.
1 parent 631fa0f commit 53e810e

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Modified MIT License
3+
* <p>
4+
* Copyright 2023 OneSignal
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
* <p>
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
* <p>
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal
29+
30+
import kotlin.concurrent.thread
31+
32+
/**
33+
* Provides a public API to allow changing which thread callbacks and observers
34+
* should fire on.
35+
*
36+
* Initial motivation for this is to allow the OneSignal-Unity-SDK to config
37+
* the SDK to fire off the main thread. This is to avoid cases where Unity may
38+
* cause the main UI thread to wait on a background thread when calling back
39+
* into Unity.
40+
*
41+
* Usage: CallbackThreadManager.preference = UseThread.Background
42+
*/
43+
class CallbackThreadManager {
44+
enum class UseThread {
45+
MainUI,
46+
Background
47+
}
48+
49+
companion object {
50+
var preference = UseThread.MainUI
51+
52+
fun runOnPreferred(runnable: Runnable) {
53+
when (preference) {
54+
UseThread.MainUI -> OSUtils.runOnMainUIThread(runnable)
55+
UseThread.Background -> thread { runnable.run() }
56+
}
57+
}
58+
}
59+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ private void firePublicClickHandler(@NonNull final String messageId, @NonNull fi
479479
if (OneSignal.inAppMessageClickHandler == null)
480480
return;
481481

482-
OSUtils.runOnMainUIThread(new Runnable() {
482+
CallbackThreadManager.Companion.runOnPreferred(new Runnable() {
483483
@Override
484484
public void run() {
485485
// Send public outcome from handler

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSObservable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ boolean notifyChange(final StateType state) {
7979
final Method method = clazz.getDeclaredMethod(methodName, state.getClass());
8080
method.setAccessible(true);
8181
if (fireOnMainThread) {
82-
OSUtils.runOnMainUIThread(
82+
CallbackThreadManager.Companion.runOnPreferred(
8383
new Runnable() {
8484
@Override
8585
public void run() {

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ private static void fireNotificationOpenedHandler(final OSNotificationOpenedResu
23322332

23332333
// TODO: Once the NotificationOpenedHandler gets a Worker, we should make sure we add a catch
23342334
// like we have implemented for the OSRemoteNotificationReceivedHandler and NotificationWillShowInForegroundHandlers
2335-
OSUtils.runOnMainUIThread(new Runnable() {
2335+
CallbackThreadManager.Companion.runOnPreferred(new Runnable() {
23362336
@Override
23372337
public void run() {
23382338
notificationOpenedHandler.notificationOpened(openedResult);

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalStateSynchronizer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ public void onComplete(String channel, boolean success) {
306306
}
307307
}
308308

309-
// Need to call completion handler on main thread since the request response came from an async PUT
310-
OSUtils.runOnMainUIThread(new Runnable() {
309+
CallbackThreadManager.Companion.runOnPreferred(new Runnable() {
311310
@Override
312311
public void run() {
313312
if (completionHandler != null)

0 commit comments

Comments
 (0)