From 8d7b86a6671941121974a6c80893c175c1ced147 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Tue, 6 Feb 2024 01:53:49 -0600 Subject: [PATCH] fix: crash on incorrect password --- .../java/ani/dantotsu/home/LoginFragment.kt | 20 ++++++++++++------- .../ani/dantotsu/settings/SettingsActivity.kt | 13 +++++++++--- .../saving/internal/PreferencePackager.kt | 15 +++++++++++++- app/src/main/res/layout/fragment_login.xml | 6 +++--- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/home/LoginFragment.kt b/app/src/main/java/ani/dantotsu/home/LoginFragment.kt index fdddeef109..7bd8865d5a 100644 --- a/app/src/main/java/ani/dantotsu/home/LoginFragment.kt +++ b/app/src/main/java/ani/dantotsu/home/LoginFragment.kt @@ -53,22 +53,28 @@ class LoginFragment : Fragment() { if (password != null) { val salt = jsonString.copyOfRange(0, 16) val encrypted = jsonString.copyOfRange(16, jsonString.size) - val decryptedJson = PreferenceKeystore.decryptWithPassword( - password, - encrypted, - salt - ) + val decryptedJson = try { + PreferenceKeystore.decryptWithPassword( + password, + encrypted, + salt + ) + } catch (e: Exception) { + toast("Incorrect password") + return@passwordAlertDialog + } if(PreferencePackager.unpack(decryptedJson)) - println("Settings imported") restartApp() } else { toast("Password cannot be empty") } } - } else { + } else if (name.endsWith(".ani")) { val decryptedJson = jsonString.toString(Charsets.UTF_8) if(PreferencePackager.unpack(decryptedJson)) restartApp() + } else { + toast("Invalid file type") } } catch (e: Exception) { e.printStackTrace() diff --git a/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt b/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt index 976846861b..dbefc1a007 100644 --- a/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt +++ b/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt @@ -97,21 +97,28 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene if (password != null) { val salt = jsonString.copyOfRange(0, 16) val encrypted = jsonString.copyOfRange(16, jsonString.size) - val decryptedJson = PreferenceKeystore.decryptWithPassword( + val decryptedJson = try { + PreferenceKeystore.decryptWithPassword( password, encrypted, salt - ) + ) + } catch (e: Exception) { + toast("Incorrect password") + return@passwordAlertDialog + } if(PreferencePackager.unpack(decryptedJson)) restartApp() } else { toast("Password cannot be empty") } } - } else { + } else if (name.endsWith(".ani")) { val decryptedJson = jsonString.toString(Charsets.UTF_8) if(PreferencePackager.unpack(decryptedJson)) restartApp() + } else { + toast("Unknown file type") } } catch (e: Exception) { e.printStackTrace() diff --git a/app/src/main/java/ani/dantotsu/settings/saving/internal/PreferencePackager.kt b/app/src/main/java/ani/dantotsu/settings/saving/internal/PreferencePackager.kt index c873bc55fa..6ad2456f96 100644 --- a/app/src/main/java/ani/dantotsu/settings/saving/internal/PreferencePackager.kt +++ b/app/src/main/java/ani/dantotsu/settings/saving/internal/PreferencePackager.kt @@ -1,7 +1,11 @@ package ani.dantotsu.settings.saving.internal import android.content.SharedPreferences +import android.net.Uri +import androidx.documentfile.provider.DocumentFile +import ani.dantotsu.connections.discord.serializers.Activity import ani.dantotsu.settings.saving.PrefManager +import ani.dantotsu.toast import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -10,11 +14,18 @@ class PreferencePackager { companion object { + /** + * @return a json string of the packed preferences + */ fun pack(map: Map): String { val prefsMap = packagePreferences(map) val gson = Gson() return gson.toJson(prefsMap) } + + /** + * @return true if successful, false if error + */ fun unpack(decryptedJson: String): Boolean { val gson = Gson() val type = object : TypeToken>>>() {}.type //oh god... @@ -47,6 +58,9 @@ class PreferencePackager { return unpackagePreferences(deserializedMap) } + /** + * @return a map of location names to a map of preference names to their values + */ private fun packagePreferences(map: Map): Map> { val result = mutableMapOf>() for ((location, preferences) in map) { @@ -63,7 +77,6 @@ class PreferencePackager { return result } - /** * @return true if successful, false if error */ diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index 60fab75494..0e3d0cab1c 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -42,13 +42,13 @@ android:layout_gravity="bottom|center_horizontal" android:layout_marginTop="32dp" android:layout_marginBottom="16dp" - android:backgroundTint="?attr/colorPrimaryContainer" + android:backgroundTint="?attr/colorPrimary" android:fontFamily="@font/poppins_bold" android:text="@string/login" - android:textColor="?attr/colorOnPrimaryContainer" + android:textColor="?attr/colorOnPrimary" app:cornerRadius="16dp" app:icon="@drawable/ic_anilist" - app:iconTint="?attr/colorOnPrimaryContainer" /> + app:iconTint="?attr/colorOnPrimary" />