Clean up code, Close app properly on URI import

This commit is contained in:
MrLetsplay 2023-07-17 21:06:11 +02:00
parent a3ed96c6b0
commit 378e6ad76b
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 15 additions and 36 deletions

View File

@ -3,6 +3,7 @@ package com.cringe_studios.cringe_authenticator.urihandler;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -26,49 +27,28 @@ public class URIHandlerActivity extends AppCompatActivity {
Intent intent = getIntent(); Intent intent = getIntent();
if(intent == null) { if(intent == null) {
finish(); finishAndRemoveTask();
return; return;
} }
try {
Uri uri = intent.getData(); Uri uri = intent.getData();
switch(uri.getScheme().toLowerCase()) { switch(uri.getScheme().toLowerCase()) {
case "otpauth": case "otpauth":
importCode(uri); importCodes(OTPParser.parse(uri));
break; break;
case "otpauth-migration": case "otpauth-migration":
importMigration(uri); importCodes(OTPParser.parseMigration(uri));
break; break;
} }
}
private void importCode(Uri uri) {
try {
OTPData data = OTPParser.parse(uri);
importCodes(data);
}catch(IllegalArgumentException e) { }catch(IllegalArgumentException e) {
AlertDialog dialog = new StyledDialogBuilder(this) AlertDialog dialog = new StyledDialogBuilder(this)
.setTitle(R.string.uri_handler_failed_title) .setTitle(R.string.uri_handler_failed_title)
.setMessage(e.getMessage()) .setMessage(e.getMessage())
.setPositiveButton(R.string.ok, (d, which) -> finish()) .setPositiveButton(R.string.ok, (d, which) -> finishAndRemoveTask())
.create(); .create();
dialog.setOnDismissListener(d -> finish()); dialog.setOnDismissListener(d -> finishAndRemoveTask());
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.show(); dialog.show();
} }
} }
@ -76,7 +56,8 @@ public class URIHandlerActivity extends AppCompatActivity {
private void importCodes(OTPData... data) { private void importCodes(OTPData... data) {
DialogUtil.showImportCodeDialog(this, group -> { DialogUtil.showImportCodeDialog(this, group -> {
for(OTPData d : data) SettingsUtil.addOTP(this, group, d); 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);
} }
} }

View File

@ -5,7 +5,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -247,7 +246,6 @@ public class DialogUtil {
} }
callback.accept(groups.get(which - 1)); callback.accept(groups.get(which - 1));
Toast.makeText(context, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show();
if(onDismiss != null) onDismiss.run(); if(onDismiss != null) onDismiss.run();
}) })
.setNegativeButton(R.string.cancel, (d, which) -> { if(onDismiss != null) onDismiss.run(); }) .setNegativeButton(R.string.cancel, (d, which) -> { if(onDismiss != null) onDismiss.run(); })