summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/activity/DownloadAuthenticationActivity.java
blob: 1d3d9bf11977df70de2fef33a3aed9e9d6ffb9ae (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package de.danoeh.antennapod.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.commons.lang3.Validate;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.storage.DownloadRequester;

/**
 * Shows a username and a password text field.
 * The activity MUST be started with the ARG_DOWNlOAD_REQUEST argument set to a non-null value.
 * Other arguments are optional.
 * The activity's result will be the same DownloadRequest with the entered username and password.
 */
public class DownloadAuthenticationActivity extends AppCompatActivity {

    /**
     * The download request object that contains information about the resource that requires a username and a password
     */
    public static final String ARG_DOWNLOAD_REQUEST = "request";
    /**
     * True if the request should be sent to the DownloadRequester when this activity is finished, false otherwise.
     * The default value is false.
     */
    public static final String ARG_SEND_TO_DOWNLOAD_REQUESTER_BOOL = "send_to_downloadrequester";

    private static final String RESULT_REQUEST = "request";

    private EditText etxtUsername;
    private EditText etxtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getNoTitleTheme());
        super.onCreate(savedInstanceState);

        setContentView(R.layout.download_authentication_activity);
        TextView txtvDescription = findViewById(R.id.txtvDescription);
        etxtUsername = findViewById(R.id.etxtUsername);
        etxtPassword = findViewById(R.id.etxtPassword);
        Button butConfirm = findViewById(R.id.butConfirm);
        Button butCancel = findViewById(R.id.butCancel);

        Validate.isTrue(getIntent().hasExtra(ARG_DOWNLOAD_REQUEST), "Download request missing");
        DownloadRequest request = getIntent().getParcelableExtra(ARG_DOWNLOAD_REQUEST);
        boolean sendToDownloadRequester = getIntent().getBooleanExtra(ARG_SEND_TO_DOWNLOAD_REQUESTER_BOOL, false);

        String newDescription = txtvDescription.getText() + ":\n\n" + request.getTitle();
        txtvDescription.setText(newDescription);

        if (savedInstanceState != null) {
            etxtUsername.setText(savedInstanceState.getString("username"));
            etxtPassword.setText(savedInstanceState.getString("password"));
        }

        butConfirm.setOnClickListener(v -> {
            String username = etxtUsername.getText().toString();
            String password = etxtPassword.getText().toString();
            request.setUsername(username);
            request.setPassword(password);
            Intent result = new Intent();
            result.putExtra(RESULT_REQUEST, request);
            setResult(Activity.RESULT_OK, result);

            if (sendToDownloadRequester) {
                DownloadRequester.getInstance().download(DownloadAuthenticationActivity.this, request);
            }
            finish();
        });

        butCancel.setOnClickListener(v -> {
            setResult(Activity.RESULT_CANCELED);
            finish();
        });

    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("username", etxtUsername.getText().toString());
        outState.putString("password", etxtPassword.getText().toString());
    }
}