From 9f15a9a3ab9b118b8b12cc877f71125e96d1ce35 Mon Sep 17 00:00:00 2001 From: ShellWen Chen Date: Mon, 1 Apr 2024 16:42:49 +0000 Subject: [PATCH] refactor: migrate `StartActivityAlertDialogFragment` to Kotlin --- .../StartActivityAlertDialogFragment.java | 104 ------------------ .../StartActivityAlertDialogFragment.kt | 94 ++++++++++++++++ 2 files changed, 94 insertions(+), 104 deletions(-) delete mode 100644 src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java create mode 100644 src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.kt diff --git a/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java b/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java deleted file mode 100644 index aa47326b..00000000 --- a/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2019 Erik Duisters - * - * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL - */ - -package org.kde.kdeconnect.UserInterface; - -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.apache.commons.lang3.StringUtils; - -public class StartActivityAlertDialogFragment extends AlertDialogFragment { - private static final String KEY_INTENT_ACTION = "IntentAction"; - private static final String KEY_INTENT_URL = "IntentUrl"; - private static final String KEY_REQUEST_CODE = "RequestCode"; - private static final String KEY_START_FOR_RESULT = "StartForResult"; - - private String intentAction; - private String intentUrl; - private int requestCode; - private boolean startForResult; - - public StartActivityAlertDialogFragment() {} - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Bundle args = getArguments(); - - if (args == null || !args.containsKey(KEY_INTENT_ACTION)) { - throw new RuntimeException("You must call Builder.setIntentAction() to set the intent action"); - } - - intentAction = args.getString(KEY_INTENT_ACTION); - intentUrl = args.getString(KEY_INTENT_URL); - requestCode = args.getInt(KEY_REQUEST_CODE, 0); - startForResult = args.getBoolean(KEY_START_FOR_RESULT); - - if (startForResult && !args.containsKey(KEY_REQUEST_CODE)) { - throw new RuntimeException("You requested startForResult but you did not set the requestCode"); - } - - setCallback(new Callback() { - @Override - public void onPositiveButtonClicked() { - Intent intent; - if (StringUtils.isNotEmpty(intentUrl)) { - Uri uri = Uri.parse(intentUrl); - intent = new Intent(intentAction, uri); - } else { - intent = new Intent(intentAction); - } - if (startForResult) { - requireActivity().startActivityForResult(intent, requestCode); - } else { - requireActivity().startActivity(intent); - } - } - }); - } - - public static class Builder extends AlertDialogFragment.AbstractBuilder { - @Override - public StartActivityAlertDialogFragment.Builder getThis() { - return this; - } - - public StartActivityAlertDialogFragment.Builder setIntentAction(@NonNull String intentAction) { - args.putString(KEY_INTENT_ACTION, intentAction); - - return getThis(); - } - - public StartActivityAlertDialogFragment.Builder setIntentUrl(@NonNull String intentUrl) { - args.putString(KEY_INTENT_URL, intentUrl); - - return getThis(); - } - - public StartActivityAlertDialogFragment.Builder setRequestCode(int requestCode) { - args.putInt(KEY_REQUEST_CODE, requestCode); - - return getThis(); - } - - public StartActivityAlertDialogFragment.Builder setStartForResult(boolean startForResult) { - args.putBoolean(KEY_START_FOR_RESULT, startForResult); - - return getThis(); - } - - @Override - protected StartActivityAlertDialogFragment createFragment() { - return new StartActivityAlertDialogFragment(); - } - } -} diff --git a/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.kt b/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.kt new file mode 100644 index 00000000..bbca4ba3 --- /dev/null +++ b/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.kt @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2019 Erik Duisters + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ +package org.kde.kdeconnect.UserInterface + +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import org.apache.commons.lang3.StringUtils + +class StartActivityAlertDialogFragment : AlertDialogFragment() { + private var intentAction: String? = null + private var intentUrl: String? = null + private var requestCode = 0 + private var startForResult = false + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + val args = arguments + + if (args == null || !args.containsKey(KEY_INTENT_ACTION)) { + throw RuntimeException("You must call Builder.setIntentAction() to set the intent action") + } + + intentAction = args.getString(KEY_INTENT_ACTION) + intentUrl = args.getString(KEY_INTENT_URL) + requestCode = args.getInt(KEY_REQUEST_CODE, 0) + startForResult = args.getBoolean(KEY_START_FOR_RESULT) + + check(!startForResult || args.containsKey(KEY_REQUEST_CODE)) { + "You requested startForResult but you did not set the requestCode" + } + + setCallback(object : Callback() { + override fun onPositiveButtonClicked() { + val intent = if (!intentUrl.isNullOrEmpty()) { + Intent(intentAction, Uri.parse(intentUrl)) + } else { + Intent(intentAction) + } + + if (startForResult) { + requireActivity().startActivityForResult(intent, requestCode) + } else { + requireActivity().startActivity(intent) + } + } + }) + } + + class Builder : AbstractBuilder() { + override fun getThis(): Builder { + return this + } + + fun setIntentAction(intentAction: String): Builder { + args.putString(KEY_INTENT_ACTION, intentAction) + + return getThis() + } + + fun setIntentUrl(intentUrl: String): Builder { + args.putString(KEY_INTENT_URL, intentUrl) + + return getThis() + } + + fun setRequestCode(requestCode: Int): Builder { + args.putInt(KEY_REQUEST_CODE, requestCode) + + return getThis() + } + + fun setStartForResult(startForResult: Boolean): Builder { + args.putBoolean(KEY_START_FOR_RESULT, startForResult) + + return getThis() + } + + override fun createFragment(): StartActivityAlertDialogFragment { + return StartActivityAlertDialogFragment() + } + } + + companion object { + private const val KEY_INTENT_ACTION = "IntentAction" + private const val KEY_INTENT_URL = "IntentUrl" + private const val KEY_REQUEST_CODE = "RequestCode" + private const val KEY_START_FOR_RESULT = "StartForResult" + } +}