Convert 'DeleteConfirmationActivity' to Kotlin
This commit is contained in:
parent
871802fccc
commit
25c172c0fa
1 changed files with 109 additions and 144 deletions
|
@ -1,168 +1,133 @@
|
|||
package com.fsck.k9.ui.notification;
|
||||
package com.fsck.k9.ui.notification
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.fsck.k9.Account
|
||||
import com.fsck.k9.Preferences
|
||||
import com.fsck.k9.activity.ConfirmationDialog
|
||||
import com.fsck.k9.controller.MessageReference
|
||||
import com.fsck.k9.controller.MessageReferenceHelper
|
||||
import com.fsck.k9.controller.MessagingController
|
||||
import com.fsck.k9.notification.NotificationActionService
|
||||
import com.fsck.k9.ui.R
|
||||
import com.fsck.k9.ui.base.K9ActivityCommon
|
||||
import com.fsck.k9.ui.base.ThemeType
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
class DeleteConfirmationActivity : AppCompatActivity() {
|
||||
private val base = K9ActivityCommon(this, ThemeType.DIALOG)
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
private lateinit var account: Account
|
||||
private lateinit var messagesToDelete: List<MessageReference>
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.fsck.k9.Account;
|
||||
import com.fsck.k9.Preferences;
|
||||
import com.fsck.k9.activity.ConfirmationDialog;
|
||||
import com.fsck.k9.controller.MessageReference;
|
||||
import com.fsck.k9.ui.R;
|
||||
import com.fsck.k9.controller.MessagingController;
|
||||
import com.fsck.k9.notification.NotificationActionService;
|
||||
import com.fsck.k9.ui.base.K9ActivityCommon;
|
||||
import com.fsck.k9.ui.base.ThemeType;
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
base.preOnCreate()
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
import static com.fsck.k9.controller.MessageReferenceHelper.toMessageReferenceList;
|
||||
import static com.fsck.k9.controller.MessageReferenceHelper.toMessageReferenceStringList;
|
||||
extractExtras()
|
||||
|
||||
|
||||
public class DeleteConfirmationActivity extends AppCompatActivity {
|
||||
private final static String EXTRA_ACCOUNT_UUID = "accountUuid";
|
||||
private final static String EXTRA_MESSAGE_REFERENCES = "messageReferences";
|
||||
|
||||
private final static int DIALOG_CONFIRM = 1;
|
||||
|
||||
|
||||
private final K9ActivityCommon base = new K9ActivityCommon(this, ThemeType.DIALOG);
|
||||
|
||||
private Account account;
|
||||
private List<MessageReference> messagesToDelete;
|
||||
|
||||
|
||||
public static Intent getIntent(Context context, MessageReference messageReference) {
|
||||
return getIntent(context, Collections.singletonList(messageReference));
|
||||
showDialog(DIALOG_CONFIRM)
|
||||
}
|
||||
|
||||
public static Intent getIntent(Context context, List<MessageReference> messageReferences) {
|
||||
String accountUuid = messageReferences.get(0).getAccountUuid();
|
||||
|
||||
Intent intent = new Intent(context, DeleteConfirmationActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
intent.putExtra(EXTRA_ACCOUNT_UUID, accountUuid);
|
||||
intent.putExtra(EXTRA_MESSAGE_REFERENCES, toMessageReferenceStringList(messageReferences));
|
||||
|
||||
return intent;
|
||||
override fun onResume() {
|
||||
base.preOnResume()
|
||||
super.onResume()
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
base.preOnCreate();
|
||||
super.onCreate(icicle);
|
||||
private fun extractExtras() {
|
||||
val accountUuid = intent.getStringExtra(EXTRA_ACCOUNT_UUID)
|
||||
val messageReferenceStrings = intent.getStringArrayListExtra(EXTRA_MESSAGE_REFERENCES)
|
||||
val messagesToDelete = MessageReferenceHelper.toMessageReferenceList(messageReferenceStrings)
|
||||
|
||||
extractExtras();
|
||||
requireNotNull(accountUuid) { "$EXTRA_ACCOUNT_UUID can't be null" }
|
||||
requireNotNull(messagesToDelete) { "$EXTRA_MESSAGE_REFERENCES can't be null" }
|
||||
require(messagesToDelete.isNotEmpty()) { "$EXTRA_MESSAGE_REFERENCES can't be empty" }
|
||||
|
||||
showDialog(DIALOG_CONFIRM);
|
||||
val account = getAccountFromUuid(accountUuid) ?: error("$EXTRA_ACCOUNT_UUID couldn't be resolved to an account")
|
||||
|
||||
this.account = account
|
||||
this.messagesToDelete = messagesToDelete
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
base.preOnResume();
|
||||
super.onResume();
|
||||
public override fun onCreateDialog(dialogId: Int): Dialog {
|
||||
return when (dialogId) {
|
||||
DIALOG_CONFIRM -> createDeleteConfirmationDialog(dialogId)
|
||||
else -> super.onCreateDialog(dialogId)
|
||||
}
|
||||
}
|
||||
|
||||
private void extractExtras() {
|
||||
Intent intent = getIntent();
|
||||
String accountUuid = intent.getStringExtra(EXTRA_ACCOUNT_UUID);
|
||||
List<String> messageReferenceStrings = intent.getStringArrayListExtra(EXTRA_MESSAGE_REFERENCES);
|
||||
List<MessageReference> messagesToDelete = toMessageReferenceList(messageReferenceStrings);
|
||||
public override fun onPrepareDialog(dialogId: Int, dialog: Dialog) {
|
||||
when (dialogId) {
|
||||
DIALOG_CONFIRM -> {
|
||||
val alertDialog = dialog as AlertDialog
|
||||
val messageCount = messagesToDelete.size
|
||||
alertDialog.setMessage(
|
||||
resources.getQuantityString(R.plurals.dialog_confirm_delete_messages, messageCount, messageCount)
|
||||
)
|
||||
}
|
||||
else -> super.onPrepareDialog(dialogId, dialog)
|
||||
}
|
||||
}
|
||||
|
||||
if (accountUuid == null) {
|
||||
throw new IllegalArgumentException(EXTRA_ACCOUNT_UUID + " can't be null");
|
||||
private fun getAccountFromUuid(accountUuid: String): Account? {
|
||||
val preferences = Preferences.getPreferences(this)
|
||||
return preferences.getAccount(accountUuid)
|
||||
}
|
||||
|
||||
private fun createDeleteConfirmationDialog(dialogId: Int): Dialog {
|
||||
return ConfirmationDialog.create(
|
||||
this,
|
||||
dialogId,
|
||||
R.string.dialog_confirm_delete_title,
|
||||
"",
|
||||
R.string.dialog_confirm_delete_confirm_button,
|
||||
R.string.dialog_confirm_delete_cancel_button,
|
||||
{ deleteAndFinish() },
|
||||
{ finish() }
|
||||
)
|
||||
}
|
||||
|
||||
private fun deleteAndFinish() {
|
||||
cancelNotifications()
|
||||
triggerDelete()
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun cancelNotifications() {
|
||||
val controller = MessagingController.getInstance(this)
|
||||
for (messageReference in messagesToDelete) {
|
||||
controller.cancelNotificationForMessage(account, messageReference)
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerDelete() {
|
||||
val intent = NotificationActionService.createDeleteAllMessagesIntent(this, account.uuid, messagesToDelete)
|
||||
startService(intent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val EXTRA_ACCOUNT_UUID = "accountUuid"
|
||||
private const val EXTRA_MESSAGE_REFERENCES = "messageReferences"
|
||||
private const val DIALOG_CONFIRM = 1
|
||||
|
||||
@JvmStatic
|
||||
fun getIntent(context: Context, messageReference: MessageReference): Intent {
|
||||
return getIntent(context, listOf(messageReference))
|
||||
}
|
||||
|
||||
if (messagesToDelete == null) {
|
||||
throw new IllegalArgumentException(EXTRA_MESSAGE_REFERENCES + " can't be null");
|
||||
}
|
||||
@JvmStatic
|
||||
fun getIntent(context: Context, messageReferences: List<MessageReference>): Intent {
|
||||
val accountUuid = messageReferences[0].accountUuid
|
||||
val messageReferenceStrings = MessageReferenceHelper.toMessageReferenceStringList(messageReferences)
|
||||
|
||||
if (messagesToDelete.isEmpty()) {
|
||||
throw new IllegalArgumentException(EXTRA_MESSAGE_REFERENCES + " can't be empty");
|
||||
}
|
||||
|
||||
Account account = getAccountFromUuid(accountUuid);
|
||||
if (account == null) {
|
||||
throw new IllegalStateException(EXTRA_ACCOUNT_UUID + " couldn't be resolved to an account");
|
||||
}
|
||||
|
||||
this.account = account;
|
||||
this.messagesToDelete = messagesToDelete;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(int dialogId) {
|
||||
switch (dialogId) {
|
||||
case DIALOG_CONFIRM: {
|
||||
return createDeleteConfirmationDialog(dialogId);
|
||||
return Intent(context, DeleteConfirmationActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
putExtra(EXTRA_ACCOUNT_UUID, accountUuid)
|
||||
putExtra(EXTRA_MESSAGE_REFERENCES, messageReferenceStrings)
|
||||
}
|
||||
}
|
||||
|
||||
return super.onCreateDialog(dialogId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareDialog(int dialogId, @NonNull Dialog dialog) {
|
||||
AlertDialog alert = (AlertDialog) dialog;
|
||||
switch (dialogId) {
|
||||
case DIALOG_CONFIRM: {
|
||||
int messageCount = messagesToDelete.size();
|
||||
alert.setMessage(getResources().getQuantityString(
|
||||
R.plurals.dialog_confirm_delete_messages, messageCount, messageCount));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
super.onPrepareDialog(dialogId, dialog);
|
||||
}
|
||||
|
||||
private Account getAccountFromUuid(String accountUuid) {
|
||||
Preferences preferences = Preferences.getPreferences(this);
|
||||
return preferences.getAccount(accountUuid);
|
||||
}
|
||||
|
||||
private Dialog createDeleteConfirmationDialog(int dialogId) {
|
||||
return ConfirmationDialog.create(this, dialogId,
|
||||
R.string.dialog_confirm_delete_title, "",
|
||||
R.string.dialog_confirm_delete_confirm_button,
|
||||
R.string.dialog_confirm_delete_cancel_button,
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
deleteAndFinish();
|
||||
}
|
||||
},
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void deleteAndFinish() {
|
||||
cancelNotifications();
|
||||
triggerDelete();
|
||||
finish();
|
||||
}
|
||||
|
||||
private void cancelNotifications() {
|
||||
MessagingController controller = MessagingController.getInstance(this);
|
||||
for (MessageReference messageReference : messagesToDelete) {
|
||||
controller.cancelNotificationForMessage(account, messageReference);
|
||||
}
|
||||
}
|
||||
|
||||
private void triggerDelete() {
|
||||
String accountUuid = account.getUuid();
|
||||
Intent intent = NotificationActionService.createDeleteAllMessagesIntent(this, accountUuid, messagesToDelete);
|
||||
startService(intent);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue