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.annotation.SuppressLint; 0010 import android.app.Dialog; 0011 import android.content.DialogInterface; 0012 import android.os.Bundle; 0013 0014 import androidx.annotation.LayoutRes; 0015 import androidx.annotation.NonNull; 0016 import androidx.annotation.Nullable; 0017 import androidx.annotation.StringRes; 0018 import androidx.appcompat.app.AlertDialog; 0019 import androidx.fragment.app.DialogFragment; 0020 0021 import com.google.android.material.dialog.MaterialAlertDialogBuilder; 0022 0023 public class AlertDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { 0024 private static final String KEY_TITLE_RES_ID = "TitleResId"; 0025 private static final String KEY_TITLE = "Title"; 0026 private static final String KEY_MESSAGE_RES_ID = "MessageResId"; 0027 private static final String KEY_POSITIVE_BUTTON_TEXT_RES_ID = "PositiveButtonResId"; 0028 private static final String KEY_NEGATIVE_BUTTON_TEXT_RES_ID = "NegativeButtonResId"; 0029 private static final String KEY_CUSTOM_VIEW_RES_ID = "CustomViewResId"; 0030 0031 @StringRes private int titleResId; 0032 @Nullable private String title; 0033 @StringRes private int messageResId; 0034 @StringRes private int positiveButtonResId; 0035 @StringRes private int negativeButtonResId; 0036 @LayoutRes private int customViewResId; 0037 0038 @Nullable private Callback callback; 0039 0040 public AlertDialogFragment() { 0041 } 0042 0043 @Override 0044 public void onCreate(@Nullable Bundle savedInstanceState) { 0045 super.onCreate(savedInstanceState); 0046 0047 Bundle args = getArguments(); 0048 0049 if (args == null) { 0050 throw new RuntimeException("You need to instantiate a new AlertDialogFragment using AlertDialogFragment.Builder"); 0051 } 0052 0053 titleResId = args.getInt(KEY_TITLE_RES_ID); 0054 title = args.getString(KEY_TITLE); 0055 messageResId = args.getInt(KEY_MESSAGE_RES_ID); 0056 positiveButtonResId = args.getInt(KEY_POSITIVE_BUTTON_TEXT_RES_ID); 0057 negativeButtonResId = args.getInt(KEY_NEGATIVE_BUTTON_TEXT_RES_ID); 0058 customViewResId = args.getInt(KEY_CUSTOM_VIEW_RES_ID); 0059 } 0060 0061 @NonNull 0062 @Override 0063 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 0064 @SuppressLint("ResourceType") 0065 String titleString = titleResId > 0 ? getString(titleResId) : title; 0066 0067 AlertDialog.Builder builder = new MaterialAlertDialogBuilder(requireContext()) 0068 .setTitle(titleString) 0069 .setPositiveButton(positiveButtonResId, this); 0070 if (negativeButtonResId != 0) { 0071 builder.setNegativeButton(negativeButtonResId, this); 0072 } 0073 if (customViewResId != 0) { 0074 builder.setView(customViewResId); 0075 } else { 0076 builder.setMessage(messageResId); 0077 } 0078 0079 return builder.create(); 0080 } 0081 0082 public void setCallback(@Nullable Callback callback) { 0083 this.callback = callback; 0084 } 0085 0086 @Override 0087 public void onClick(DialogInterface dialog, int which) { 0088 if (callback == null) { 0089 return; 0090 } 0091 0092 switch (which) { 0093 case AlertDialog.BUTTON_POSITIVE: 0094 callback.onPositiveButtonClicked(); 0095 break; 0096 case AlertDialog.BUTTON_NEGATIVE: 0097 callback.onNegativeButtonClicked(); 0098 break; 0099 default: 0100 break; 0101 } 0102 } 0103 0104 @Override 0105 public void onCancel(@NonNull DialogInterface dialog) { 0106 super.onCancel(dialog); 0107 0108 if (callback != null) { 0109 callback.onCancel(); 0110 } 0111 } 0112 0113 @Override 0114 public void onDismiss(@NonNull DialogInterface dialog) { 0115 super.onDismiss(dialog); 0116 0117 if (callback != null) { 0118 callback.onDismiss(); 0119 } 0120 } 0121 0122 public static abstract class AbstractBuilder<B extends AbstractBuilder<B, F>, F extends DialogFragment> { 0123 final Bundle args; 0124 0125 AbstractBuilder() { 0126 args = new Bundle(); 0127 } 0128 0129 public abstract B getThis(); 0130 0131 public B setTitle(@StringRes int titleResId) { 0132 args.putInt(KEY_TITLE_RES_ID, titleResId); 0133 return getThis(); 0134 } 0135 0136 public B setTitle(@NonNull String title) { 0137 args.putString(KEY_TITLE, title); 0138 return getThis(); 0139 } 0140 0141 public B setMessage(@StringRes int messageResId) { 0142 args.putInt(KEY_MESSAGE_RES_ID, messageResId); 0143 return getThis(); 0144 } 0145 0146 public B setPositiveButton(@StringRes int positiveButtonResId) { 0147 args.putInt(KEY_POSITIVE_BUTTON_TEXT_RES_ID, positiveButtonResId); 0148 return getThis(); 0149 } 0150 0151 public B setNegativeButton(@StringRes int negativeButtonResId) { 0152 args.putInt(KEY_NEGATIVE_BUTTON_TEXT_RES_ID, negativeButtonResId); 0153 return getThis(); 0154 } 0155 0156 public B setView(@LayoutRes int customViewResId) { 0157 args.putInt(KEY_CUSTOM_VIEW_RES_ID, customViewResId); 0158 return getThis(); 0159 } 0160 0161 protected abstract F createFragment(); 0162 0163 public F create() { 0164 F fragment = createFragment(); 0165 fragment.setArguments(args); 0166 0167 return fragment; 0168 } 0169 } 0170 0171 public static class Builder extends AbstractBuilder<Builder, AlertDialogFragment> { 0172 @Override 0173 public Builder getThis() { 0174 return this; 0175 } 0176 0177 @Override 0178 protected AlertDialogFragment createFragment() { 0179 return new AlertDialogFragment(); 0180 } 0181 } 0182 0183 //TODO: Generify so the actual AlertDialogFragment subclass can be passed as an argument 0184 public static abstract class Callback { 0185 public void onPositiveButtonClicked() {} 0186 void onNegativeButtonClicked() {} 0187 public void onDismiss() {} 0188 void onCancel() {} 0189 } 0190 }