From 378e6ad76b3fa295427c128b4cb6c3d789733107 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Mon, 17 Jul 2023 21:06:11 +0200 Subject: [PATCH] Clean up code, Close app properly on URI import --- .../urihandler/URIHandlerActivity.java | 49 ++++++------------- .../cringe_authenticator/util/DialogUtil.java | 2 - 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java index 9ec6410..2f1a84e 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java @@ -3,6 +3,7 @@ package com.cringe_studios.cringe_authenticator.urihandler; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; @@ -26,49 +27,28 @@ public class URIHandlerActivity extends AppCompatActivity { Intent intent = getIntent(); if(intent == null) { - finish(); + finishAndRemoveTask(); return; } - Uri uri = intent.getData(); - switch(uri.getScheme().toLowerCase()) { - case "otpauth": - importCode(uri); - break; - case "otpauth-migration": - importMigration(uri); - break; - } - } - - private void importCode(Uri uri) { try { - OTPData data = OTPParser.parse(uri); - importCodes(data); + Uri uri = intent.getData(); + switch(uri.getScheme().toLowerCase()) { + case "otpauth": + importCodes(OTPParser.parse(uri)); + break; + case "otpauth-migration": + importCodes(OTPParser.parseMigration(uri)); + break; + } }catch(IllegalArgumentException e) { AlertDialog dialog = new StyledDialogBuilder(this) .setTitle(R.string.uri_handler_failed_title) .setMessage(e.getMessage()) - .setPositiveButton(R.string.ok, (d, which) -> finish()) + .setPositiveButton(R.string.ok, (d, which) -> finishAndRemoveTask()) .create(); - dialog.setOnDismissListener(d -> finish()); - dialog.show(); - } - } - - private void importMigration(Uri uri) { - try { - OTPData[] data = OTPParser.parseMigration(uri); - importCodes(data); - }catch(IllegalArgumentException e) { - AlertDialog dialog = new StyledDialogBuilder(this) - .setTitle(R.string.uri_handler_failed_title) - .setMessage(e.getMessage()) - .setPositiveButton(R.string.ok, (d, which) -> finish()) - .create(); - - dialog.setOnDismissListener(d -> finish()); + dialog.setOnDismissListener(d -> finishAndRemoveTask()); dialog.show(); } } @@ -76,7 +56,8 @@ public class URIHandlerActivity extends AppCompatActivity { private void importCodes(OTPData... data) { DialogUtil.showImportCodeDialog(this, group -> { for(OTPData d : data) SettingsUtil.addOTP(this, group, d); - }, this::finish); + Toast.makeText(this, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show(); + }, this::finishAndRemoveTask); } } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/DialogUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/DialogUtil.java index db4a495..d0dd8aa 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/DialogUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/DialogUtil.java @@ -5,7 +5,6 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; @@ -247,7 +246,6 @@ public class DialogUtil { } callback.accept(groups.get(which - 1)); - Toast.makeText(context, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show(); if(onDismiss != null) onDismiss.run(); }) .setNegativeButton(R.string.cancel, (d, which) -> { if(onDismiss != null) onDismiss.run(); })