File indexing completed on 2024-12-22 04:41:42
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.UserInterface; 0008 0009 import android.content.Intent; 0010 import android.net.Uri; 0011 import android.os.Bundle; 0012 0013 import androidx.annotation.NonNull; 0014 import androidx.annotation.Nullable; 0015 0016 import org.apache.commons.lang3.StringUtils; 0017 0018 public class StartActivityAlertDialogFragment extends AlertDialogFragment { 0019 private static final String KEY_INTENT_ACTION = "IntentAction"; 0020 private static final String KEY_INTENT_URL = "IntentUrl"; 0021 private static final String KEY_REQUEST_CODE = "RequestCode"; 0022 private static final String KEY_START_FOR_RESULT = "StartForResult"; 0023 0024 private String intentAction; 0025 private String intentUrl; 0026 private int requestCode; 0027 private boolean startForResult; 0028 0029 public StartActivityAlertDialogFragment() {} 0030 0031 @Override 0032 public void onCreate(@Nullable Bundle savedInstanceState) { 0033 super.onCreate(savedInstanceState); 0034 0035 Bundle args = getArguments(); 0036 0037 if (args == null || !args.containsKey(KEY_INTENT_ACTION)) { 0038 throw new RuntimeException("You must call Builder.setIntentAction() to set the intent action"); 0039 } 0040 0041 intentAction = args.getString(KEY_INTENT_ACTION); 0042 intentUrl = args.getString(KEY_INTENT_URL); 0043 requestCode = args.getInt(KEY_REQUEST_CODE, 0); 0044 startForResult = args.getBoolean(KEY_START_FOR_RESULT); 0045 0046 if (startForResult && !args.containsKey(KEY_REQUEST_CODE)) { 0047 throw new RuntimeException("You requested startForResult but you did not set the requestCode"); 0048 } 0049 0050 setCallback(new Callback() { 0051 @Override 0052 public void onPositiveButtonClicked() { 0053 Intent intent; 0054 if (StringUtils.isNotEmpty(intentUrl)) { 0055 Uri uri = Uri.parse(intentUrl); 0056 intent = new Intent(intentAction, uri); 0057 } else { 0058 intent = new Intent(intentAction); 0059 } 0060 if (startForResult) { 0061 requireActivity().startActivityForResult(intent, requestCode); 0062 } else { 0063 requireActivity().startActivity(intent); 0064 } 0065 } 0066 }); 0067 } 0068 0069 public static class Builder extends AlertDialogFragment.AbstractBuilder<StartActivityAlertDialogFragment.Builder, StartActivityAlertDialogFragment> { 0070 @Override 0071 public StartActivityAlertDialogFragment.Builder getThis() { 0072 return this; 0073 } 0074 0075 public StartActivityAlertDialogFragment.Builder setIntentAction(@NonNull String intentAction) { 0076 args.putString(KEY_INTENT_ACTION, intentAction); 0077 0078 return getThis(); 0079 } 0080 0081 public StartActivityAlertDialogFragment.Builder setIntentUrl(@NonNull String intentUrl) { 0082 args.putString(KEY_INTENT_URL, intentUrl); 0083 0084 return getThis(); 0085 } 0086 0087 public StartActivityAlertDialogFragment.Builder setRequestCode(int requestCode) { 0088 args.putInt(KEY_REQUEST_CODE, requestCode); 0089 0090 return getThis(); 0091 } 0092 0093 public StartActivityAlertDialogFragment.Builder setStartForResult(boolean startForResult) { 0094 args.putBoolean(KEY_START_FOR_RESULT, startForResult); 0095 0096 return getThis(); 0097 } 0098 0099 @Override 0100 protected StartActivityAlertDialogFragment createFragment() { 0101 return new StartActivityAlertDialogFragment(); 0102 } 0103 } 0104 }