mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
refactor: migrate StartActivityAlertDialogFragment
to Kotlin
This commit is contained in:
parent
9a196543ff
commit
9f15a9a3ab
@ -1,104 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com>
|
||||
*
|
||||
* 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<StartActivityAlertDialogFragment.Builder, StartActivityAlertDialogFragment> {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com>
|
||||
*
|
||||
* 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<Builder, StartActivityAlertDialogFragment>() {
|
||||
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"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user