summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/AuthenticationDialog.java
blob: 39d321f18be48925d2e0b8f03455fd8e730becf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package de.danoeh.antennapod.dialog;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AlertDialog;
import de.danoeh.antennapod.R;

/**
 * Displays a dialog with a username and password text field and an optional checkbox to save username and preferences.
 */
public abstract class AuthenticationDialog extends AlertDialog.Builder {

    public AuthenticationDialog(Context context, int titleRes, boolean enableUsernameField,
                                String usernameInitialValue, String passwordInitialValue) {
        super(context);
        setTitle(titleRes);
        View rootView = View.inflate(context, R.layout.authentication_dialog, null);
        setView(rootView);

        final EditText etxtUsername = rootView.findViewById(R.id.etxtUsername);
        final EditText etxtPassword = rootView.findViewById(R.id.etxtPassword);

        etxtUsername.setEnabled(enableUsernameField);
        if (usernameInitialValue != null) {
            etxtUsername.setText(usernameInitialValue);
        }
        if (passwordInitialValue != null) {
            etxtPassword.setText(passwordInitialValue);
        }
        setOnCancelListener(dialog -> onCancelled());
        setNegativeButton(R.string.cancel_label, null);
        setPositiveButton(R.string.confirm_label, (dialog, which)
                -> onConfirmed(etxtUsername.getText().toString(), etxtPassword.getText().toString()));
    }

    protected void onCancelled() {

    }

    protected abstract void onConfirmed(String username, String password);
}