Merge pull request #4890 from ByteHamster/authentication

Make authentication dialog easier to use
This commit is contained in:
ByteHamster 2021-01-22 15:11:42 +01:00 committed by GitHub
commit 176e8e7a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 162 deletions

View File

@ -88,6 +88,7 @@
<activity
android:name=".activity.DownloadAuthenticationActivity"
android:theme="@style/Theme.AntennaPod.Dark.Translucent"
android:launchMode="singleInstance"/>
<activity

View File

@ -1,11 +1,7 @@
package de.danoeh.antennapod.activity;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedMedia;
@ -15,6 +11,7 @@ import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.dialog.AuthenticationDialog;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
@ -32,71 +29,48 @@ public class DownloadAuthenticationActivity extends AppCompatActivity {
*/
public static final String ARG_DOWNLOAD_REQUEST = "request";
private EditText etxtUsername;
private EditText etxtPassword;
private DownloadRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getNoTitleTheme());
setTheme(UserPreferences.getTranslucentTheme());
super.onCreate(savedInstanceState);
setContentView(R.layout.download_authentication_activity);
etxtUsername = findViewById(R.id.etxtUsername);
etxtPassword = findViewById(R.id.etxtPassword);
Validate.isTrue(getIntent().hasExtra(ARG_DOWNLOAD_REQUEST), "Download request missing");
request = getIntent().getParcelableExtra(ARG_DOWNLOAD_REQUEST);
DownloadRequest request = getIntent().getParcelableExtra(ARG_DOWNLOAD_REQUEST);
TextView txtvDescription = findViewById(R.id.txtvDescription);
String newDescription = txtvDescription.getText() + ":\n\n" + request.getTitle();
txtvDescription.setText(newDescription);
new AuthenticationDialog(this, R.string.authentication_label, true, "", "") {
@Override
protected void onConfirmed(String username, String password) {
Completable.fromAction(
() -> {
request.setUsername(username);
request.setPassword(password);
if (savedInstanceState != null) {
etxtUsername.setText(savedInstanceState.getString("username"));
etxtPassword.setText(savedInstanceState.getString("password"));
}
findViewById(R.id.butConfirm).setOnClickListener(v ->
Completable.fromAction(this::updatePassword)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
DownloadRequester.getInstance().download(DownloadAuthenticationActivity.this, request);
finish();
}));
findViewById(R.id.butCancel).setOnClickListener(v -> {
setResult(Activity.RESULT_CANCELED);
finish();
});
}
private void updatePassword() {
String username = etxtUsername.getText().toString();
String password = etxtPassword.getText().toString();
request.setUsername(username);
request.setPassword(password);
if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
long mediaId = request.getFeedfileId();
FeedMedia media = DBReader.getFeedMedia(mediaId);
if (media != null) {
FeedPreferences preferences = media.getItem().getFeed().getPreferences();
if (TextUtils.isEmpty(preferences.getPassword()) || TextUtils.isEmpty(preferences.getUsername())) {
preferences.setUsername(username);
preferences.setPassword(password);
DBWriter.setFeedPreferences(preferences);
}
if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
long mediaId = request.getFeedfileId();
FeedMedia media = DBReader.getFeedMedia(mediaId);
if (media != null) {
FeedPreferences preferences = media.getItem().getFeed().getPreferences();
if (TextUtils.isEmpty(preferences.getPassword())
|| TextUtils.isEmpty(preferences.getUsername())) {
preferences.setUsername(username);
preferences.setPassword(password);
DBWriter.setFeedPreferences(preferences);
}
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
DownloadRequester.getInstance().download(DownloadAuthenticationActivity.this, request);
finish();
});
}
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("username", etxtUsername.getText().toString());
outState.putString("password", etxtPassword.getText().toString());
@Override
protected void onCancelled() {
finish();
}
}.show();
}
}

View File

@ -30,6 +30,7 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
@Override
public PendingIntent getAuthentificationNotificationContentIntent(Context context, DownloadRequest request) {
final Intent activityIntent = new Intent(context.getApplicationContext(), DownloadAuthenticationActivity.class);
activityIntent.setAction("request" + request.getFeedfileId());
activityIntent.putExtra(DownloadAuthenticationActivity.ARG_DOWNLOAD_REQUEST, request);
return PendingIntent.getActivity(context.getApplicationContext(),
R.id.pending_intent_download_service_auth, activityIntent, PendingIntent.FLAG_ONE_SHOT);

View File

@ -1,37 +1,50 @@
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
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);
View rootView = View.inflate(context, R.layout.authentication_dialog, null);
setView(rootView);
AuthenticationDialogBinding viewBinding = AuthenticationDialogBinding.inflate(LayoutInflater.from(context));
setView(viewBinding.getRoot());
final EditText etxtUsername = rootView.findViewById(R.id.etxtUsername);
final EditText etxtPassword = rootView.findViewById(R.id.etxtPassword);
etxtUsername.setEnabled(enableUsernameField);
viewBinding.usernameEditText.setEnabled(enableUsernameField);
if (usernameInitialValue != null) {
etxtUsername.setText(usernameInitialValue);
viewBinding.usernameEditText.setText(usernameInitialValue);
}
if (passwordInitialValue != null) {
etxtPassword.setText(passwordInitialValue);
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());
setOnDismissListener(dialog -> onCancelled());
setNegativeButton(R.string.cancel_label, null);
setPositiveButton(R.string.confirm_label, (dialog, which)
-> onConfirmed(etxtUsername.getText().toString(), etxtPassword.getText().toString()));
-> onConfirmed(viewBinding.usernameEditText.getText().toString(),
viewBinding.passwordEditText.getText().toString()));
}
protected void onCancelled() {

View File

@ -1,30 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etxtUsername"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"
android:hint="@string/username_label"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etxtPassword"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"
android:inputType="textPassword"
android:hint="@string/password_label"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username_label"
android:lines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_label"
android:inputType="textPassword"
android:lines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.joanzapata.iconify.widget.IconTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showPasswordButton"
android:text="{fa-eye}"
android:padding="8dp"
android:textColor="?android:attr/textColorPrimary"
android:background="?attr/selectableItemBackgroundBorderless"
android:alpha="0.6"
android:textSize="20sp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"/>
</LinearLayout>
</LinearLayout>

View File

@ -1,64 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/txtvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/authentication_notification_title"
android:textSize="@dimen/text_size_large"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:id="@+id/txtvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/authentication_notification_msg"
android:textColor="?android:attr/textColorSecondary"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etxtUsername"
android:hint="@string/username_label"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etxtPassword"
android:hint="@string/password_label"
android:inputType="textPassword"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:gravity="end">
<Button
android:id="@+id/butCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel_label"
style="@style/Widget.MaterialComponents.Button.TextButton"/>
<Button
android:id="@+id/butConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/confirm_label"
style="@style/Widget.MaterialComponents.Button.TextButton"/>
</LinearLayout>
</LinearLayout>

View File

@ -95,7 +95,6 @@
<string name="description_label">Description</string>
<string name="episodes_suffix">\u0020episodes</string>
<string name="processing_label">Processing</string>
<string name="save_username_password_label">Save username and password</string>
<string name="close_label">Close</string>
<string name="retry_label">Retry</string>
<string name="auto_download_label">Include in auto downloads</string>