Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public class CallbackThreadManager #1776

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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