From 8d1cf14565bc1a21951b1fece67d1a43790e6413 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 28 Feb 2023 22:21:18 -0500 Subject: [PATCH] Android: Convert ThreadUtil to Kotlin --- .../dolphinemu/utils/ThreadUtil.java | 61 ------------------- .../dolphinemu/dolphinemu/utils/ThreadUtil.kt | 43 +++++++++++++ 2 files changed, 43 insertions(+), 61 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.java deleted file mode 100644 index af464cf94b..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.java +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.utils; - -import android.app.Activity; -import android.content.DialogInterface; -import android.content.res.Resources; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; - -import com.google.android.material.dialog.MaterialAlertDialogBuilder; - -import org.dolphinemu.dolphinemu.R; - -import java.util.function.Supplier; - -public class ThreadUtil -{ - public static void runOnThreadAndShowResult(Activity activity, int progressTitle, - int progressMessage, @NonNull Supplier f) - { - runOnThreadAndShowResult(activity, progressTitle, progressMessage, f, null); - } - - public static void runOnThreadAndShowResult(Activity activity, int progressTitle, - int progressMessage, @NonNull Supplier f, - @Nullable DialogInterface.OnDismissListener onResultDismiss) - { - Resources resources = activity.getResources(); - AlertDialog progressDialog = new MaterialAlertDialogBuilder(activity) - .setTitle(progressTitle) - .setView(R.layout.dialog_indeterminate_progress) - .setCancelable(false) - .create(); - - if (progressMessage != 0) - progressDialog.setMessage(resources.getString(progressMessage)); - - progressDialog.show(); - - new Thread(() -> - { - String result = f.get(); - activity.runOnUiThread(() -> - { - progressDialog.dismiss(); - - if (result != null) - { - new MaterialAlertDialogBuilder(activity) - .setMessage(result) - .setPositiveButton(R.string.ok, (dialog, i) -> dialog.dismiss()) - .setOnDismissListener(onResultDismiss) - .show(); - } - }); - }, resources.getString(progressTitle)).start(); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.kt new file mode 100644 index 0000000000..12f93a8ab4 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ThreadUtil.kt @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.utils + +import android.app.Activity +import android.content.DialogInterface +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import org.dolphinemu.dolphinemu.R +import java.util.function.Supplier + +object ThreadUtil { + @JvmStatic + @JvmOverloads + fun runOnThreadAndShowResult( + activity: Activity, + progressTitle: Int, + progressMessage: Int, + f: Supplier, + onResultDismiss: DialogInterface.OnDismissListener? = null + ) { + val resources = activity.resources + val progressDialog = MaterialAlertDialogBuilder(activity) + .setTitle(progressTitle) + .setView(R.layout.dialog_indeterminate_progress) + .setCancelable(false) + .create() + if (progressMessage != 0) progressDialog.setMessage(resources.getString(progressMessage)) + progressDialog.show() + Thread({ + val result = f.get() + activity.runOnUiThread { + progressDialog.dismiss() + if (result != null) { + MaterialAlertDialogBuilder(activity) + .setMessage(result) + .setPositiveButton(R.string.ok) { dialog: DialogInterface, _: Int -> dialog.dismiss() } + .setOnDismissListener(onResultDismiss) + .show() + } + } + }, resources.getString(progressTitle)).start() + } +}