Added long press to delete account/identity

This commit is contained in:
M66B 2021-10-24 18:28:40 +02:00
parent a570896632
commit 35c9a0f60a
2 changed files with 117 additions and 12 deletions

View File

@ -26,6 +26,7 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
@ -51,6 +52,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.Fragment;
@ -350,14 +352,15 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
int order = 0;
SpannableString ss = new SpannableString(account.name);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
popupMenu.getMenu().add(Menu.NONE, 0, order++, ss).setEnabled(false);
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, 1, R.string.title_enabled)
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, order++, R.string.title_enabled)
.setCheckable(true).setChecked(account.synchronize);
popupMenu.getMenu().add(Menu.NONE, R.string.title_primary, 2, R.string.title_primary)
popupMenu.getMenu().add(Menu.NONE, R.string.title_primary, order++, R.string.title_primary)
.setCheckable(true).setChecked(account.primary);
if (account.notify &&
@ -366,16 +369,19 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = nm.getNotificationChannel(channelId);
if (channel != null)
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_channel, 3, R.string.title_edit_channel);
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_channel, order++, R.string.title_edit_channel);
}
if (account.protocol == EntityAccount.TYPE_IMAP && settings)
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 4, R.string.title_copy);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, order++, R.string.title_copy);
popupMenu.getMenu().add(Menu.NONE, R.string.title_log, 5, R.string.title_log);
if (settings)
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, order++, R.string.title_delete);
popupMenu.getMenu().add(Menu.NONE, R.string.title_log, order++, R.string.title_log);
if (debug)
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset, 6, R.string.title_reset);
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset, order++, R.string.title_reset);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
@ -393,6 +399,9 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
} else if (itemId == R.string.title_copy) {
onActionCopy();
return true;
} else if (itemId == R.string.title_delete) {
onActionDelete();
return true;
} else if (itemId == R.string.title_log) {
onActionLog();
return true;
@ -499,6 +508,26 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
.putExtra("copy", true));
}
private void onActionDelete() {
new AlertDialog.Builder(view.getContext())
.setIcon(R.drawable.twotone_warning_24)
.setTitle(account.name)
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onDelete();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
}
private void onActionLog() {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
parentFragment.getParentFragmentManager().popBackStack("logs", FragmentManager.POP_BACK_STACK_INCLUSIVE);
@ -540,6 +569,30 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
}
}.execute(context, owner, args, "account:reset");
}
private void onDelete() {
Bundle args = new Bundle();
args.putLong("id", account.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().setAccountTbd(id);
ServiceSynchronize.eval(context, "delete account");
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:delete");
}
});
popupMenu.show();

View File

@ -20,11 +20,13 @@ package eu.faircode.email;
*/
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
@ -36,6 +38,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
@ -199,20 +202,22 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
int order = 0;
SpannableString ss = new SpannableString(identity.email);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
popupMenu.getMenu().add(Menu.NONE, 0, order++, ss).setEnabled(false);
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, 1, R.string.title_enabled)
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, order++, R.string.title_enabled)
.setCheckable(true).setChecked(identity.synchronize);
popupMenu.getMenu().add(Menu.NONE, R.string.title_primary, 2, R.string.title_primary)
popupMenu.getMenu().add(Menu.NONE, R.string.title_primary, order++, R.string.title_primary)
.setCheckable(true).setChecked(identity.primary);
if (identity.sign_key != null || identity.sign_key_alias != null)
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset_sign_key, 3, R.string.title_reset_sign_key);
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset_sign_key, order++, R.string.title_reset_sign_key);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 4, R.string.title_copy);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, order++, R.string.title_copy);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, order++, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
@ -230,6 +235,9 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
} else if (itemId == R.string.title_copy) {
onActionCopy();
return true;
} else if (itemId == R.string.title_delete) {
onActionDelete();
return true;
}
return false;
}
@ -336,6 +344,50 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
.putExtra("id", identity.id)
.putExtra("copy", true));
}
private void onActionDelete() {
new AlertDialog.Builder(view.getContext())
.setIcon(R.drawable.twotone_warning_24)
.setTitle(identity.email)
.setMessage(R.string.title_identity_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onDelete();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
}
private void onDelete() {
Bundle args = new Bundle();
args.putLong("id", identity.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.identity().deleteIdentity(id);
Core.clearIdentities();
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "identity:delete");
}
});
popupMenu.show();