mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-25 10:25:03 +01:00
Imporved unsubscribe dialog
This commit is contained in:
@@ -25,6 +25,8 @@ import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -33,6 +35,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
@@ -52,65 +55,86 @@ public class FragmentDialogUnsubscribe extends FragmentDialogBase {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.dialog_unsubscribe, null);
|
||||
final TextView tvSender = view.findViewById(R.id.tvSender);
|
||||
final TextView tvUri = view.findViewById(R.id.tvUri);
|
||||
final Button btnUnsubscribe = view.findViewById(R.id.btnUnsubscribe);
|
||||
final ProgressBar pbUnsubscribe = view.findViewById(R.id.pbUnsubscribe);
|
||||
|
||||
tvSender.setText(from);
|
||||
tvUri.setText(uri);
|
||||
pbUnsubscribe.setVisibility(View.GONE);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
||||
AlertDialog dialog = new AlertDialog.Builder(context)
|
||||
.setView(view)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
.setNeutralButton(android.R.string.cancel, null)
|
||||
.create();
|
||||
|
||||
btnUnsubscribe.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
new SimpleTask<String>() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
new SimpleTask<String>() {
|
||||
@Override
|
||||
protected String onExecute(Context context, Bundle args) throws Throwable {
|
||||
final String uri = args.getString("uri");
|
||||
final String request = "List-Unsubscribe=One-Click";
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc8058
|
||||
|
||||
URL url = new URL(uri);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
connection.setReadTimeout(UNSUBSCRIBE_TIMEOUT);
|
||||
connection.setConnectTimeout(UNSUBSCRIBE_TIMEOUT);
|
||||
ConnectionHelper.setUserAgent(context, connection);
|
||||
connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
connection.connect();
|
||||
|
||||
try {
|
||||
connection.getOutputStream().write(request.getBytes());
|
||||
|
||||
int status = connection.getResponseCode();
|
||||
if (status != HttpsURLConnection.HTTP_OK) {
|
||||
String error = "Error " + status + ": " + connection.getResponseMessage();
|
||||
String detail = Helper.readStream(connection.getErrorStream());
|
||||
throw new IOException(error + " " + detail);
|
||||
}
|
||||
|
||||
return Helper.readStream(connection.getInputStream());
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, String output) {
|
||||
ToastEx.makeText(context, R.string.title_completed, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(getParentFragment(), ex);
|
||||
}
|
||||
}.execute(FragmentDialogUnsubscribe.this, args, "unsubscribe");
|
||||
protected void onPreExecute(Bundle args) {
|
||||
btnUnsubscribe.setEnabled(false);
|
||||
pbUnsubscribe.setVisibility(View.VISIBLE);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, null);
|
||||
|
||||
return builder.create();
|
||||
@Override
|
||||
protected void onPostExecute(Bundle args) {
|
||||
btnUnsubscribe.setEnabled(true);
|
||||
pbUnsubscribe.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String onExecute(Context context, Bundle args) throws Throwable {
|
||||
final String uri = args.getString("uri");
|
||||
final String request = "List-Unsubscribe=One-Click";
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc8058
|
||||
|
||||
URL url = new URL(uri);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
connection.setReadTimeout(UNSUBSCRIBE_TIMEOUT);
|
||||
connection.setConnectTimeout(UNSUBSCRIBE_TIMEOUT);
|
||||
ConnectionHelper.setUserAgent(context, connection);
|
||||
connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
connection.connect();
|
||||
|
||||
try {
|
||||
connection.getOutputStream().write(request.getBytes());
|
||||
|
||||
int status = connection.getResponseCode();
|
||||
if (status < 200 || status > 299) {
|
||||
String error = "Error " + status + ": " + connection.getResponseMessage();
|
||||
InputStream stream = connection.getErrorStream();
|
||||
String detail = (stream == null ? null : Helper.readStream(stream));
|
||||
throw new IOException(error + " " + detail);
|
||||
}
|
||||
|
||||
return Helper.readStream(connection.getInputStream());
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, String output) {
|
||||
ToastEx.makeText(context, R.string.title_completed, Toast.LENGTH_LONG).show();
|
||||
dialog.dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
dialog.dismiss();
|
||||
Log.unexpectedError(getParentFragmentManager(), ex);
|
||||
}
|
||||
}.execute(FragmentDialogUnsubscribe.this, args, "unsubscribe");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,5 +41,29 @@
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvSender" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnUnsubscribe"
|
||||
style="?android:attr/buttonStyleSmall"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:drawableEnd="@drawable/twotone_unsubscribe_24"
|
||||
android:drawablePadding="6dp"
|
||||
android:text="@string/title_unsubscribe"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvUri" />
|
||||
|
||||
<eu.faircode.email.ContentLoadingProgressBar
|
||||
android:id="@+id/pbUnsubscribe"
|
||||
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_marginStart="12dp"
|
||||
android:indeterminate="true"
|
||||
app:layout_constraintBottom_toBottomOf="@id/btnUnsubscribe"
|
||||
app:layout_constraintStart_toEndOf="@id/btnUnsubscribe"
|
||||
app:layout_constraintTop_toTopOf="@id/btnUnsubscribe" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</ScrollView>
|
||||
Reference in New Issue
Block a user