Skip to content

Commit

Permalink
Merge pull request #1776 from OneSignal/feat/prefer_thread_for_callbacks
Browse files Browse the repository at this point in the history
Add public class CallbackThreadManager
  • Loading branch information
jkasten2 authored May 12, 2023
2 parents 631fa0f + 53e810e commit 36951ff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Modified MIT License
* <p>
* Copyright 2023 OneSignal
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.onesignal

import kotlin.concurrent.thread

/**
* Provides a public API to allow changing which thread callbacks and observers
* should fire on.
*
* Initial motivation for this is to allow the OneSignal-Unity-SDK to config
* the SDK to fire off the main thread. This is to avoid cases where Unity may
* cause the main UI thread to wait on a background thread when calling back
* into Unity.
*
* Usage: CallbackThreadManager.preference = UseThread.Background
*/
class CallbackThreadManager {
enum class UseThread {
MainUI,
Background
}

companion object {
var preference = UseThread.MainUI

fun runOnPreferred(runnable: Runnable) {
when (preference) {
UseThread.MainUI -> OSUtils.runOnMainUIThread(runnable)
UseThread.Background -> thread { runnable.run() }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private void firePublicClickHandler(@NonNull final String messageId, @NonNull fi
if (OneSignal.inAppMessageClickHandler == null)
return;

OSUtils.runOnMainUIThread(new Runnable() {
CallbackThreadManager.Companion.runOnPreferred(new Runnable() {
@Override
public void run() {
// Send public outcome from handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ boolean notifyChange(final StateType state) {
final Method method = clazz.getDeclaredMethod(methodName, state.getClass());
method.setAccessible(true);
if (fireOnMainThread) {
OSUtils.runOnMainUIThread(
CallbackThreadManager.Companion.runOnPreferred(
new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ private static void fireNotificationOpenedHandler(final OSNotificationOpenedResu

// TODO: Once the NotificationOpenedHandler gets a Worker, we should make sure we add a catch
// like we have implemented for the OSRemoteNotificationReceivedHandler and NotificationWillShowInForegroundHandlers
OSUtils.runOnMainUIThread(new Runnable() {
CallbackThreadManager.Companion.runOnPreferred(new Runnable() {
@Override
public void run() {
notificationOpenedHandler.notificationOpened(openedResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ public void onComplete(String channel, boolean success) {
}
}

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

0 comments on commit 36951ff

Please sign in to comment.