summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/AuthenticationDialog.java
blob: 160afcf86048b39daede38c193d48bfa4b23640a (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
43
44
45
46
47
48
49
50
51
52
53
54
package de.danoeh.antennapod.dialog;

import android.content.Context;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import androidx.appcompat.app.AlertDialog;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.databinding.AuthenticationDialogBinding;

/**
 * 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 {
    boolean passwordHidden = true;

    public AuthenticationDialog(Context context, int titleRes, boolean enableUsernameField,
                                String usernameInitialValue, String passwordInitialValue) {
        super(context);
        setTitle(titleRes);
        AuthenticationDialogBinding viewBinding = AuthenticationDialogBinding.inflate(LayoutInflater.from(context));
        setView(viewBinding.getRoot());

        viewBinding.usernameEditText.setEnabled(enableUsernameField);
        if (usernameInitialValue != null) {
            viewBinding.usernameEditText.setText(usernameInitialValue);
        }
        if (passwordInitialValue != null) {
            viewBinding.passwordEditText.setText(passwordInitialValue);
        }
        viewBinding.showPasswordButton.setOnClickListener(v -> {
            if (passwordHidden) {
                viewBinding.passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                viewBinding.showPasswordButton.setAlpha(1.0f);
            } else {
                viewBinding.passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                viewBinding.showPasswordButton.setAlpha(0.6f);
            }
            passwordHidden = !passwordHidden;
        });

        setOnCancelListener(dialog -> onCancelled());
        setNegativeButton(R.string.cancel_label, (dialog, which) -> onCancelled());
        setPositiveButton(R.string.confirm_label, (dialog, which)
                -> onConfirmed(viewBinding.usernameEditText.getText().toString(),
                        viewBinding.passwordEditText.getText().toString()));
    }

    protected void onCancelled() {

    }

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