diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5883981b..82dffe73 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -276,6 +276,15 @@ + + + + + + + + + + if (password != null) { + val salt = jsonString.copyOfRange(0, 16) + val encrypted = jsonString.copyOfRange(16, jsonString.size) + val decryptedJson = try { + PreferenceKeystore.decryptWithPassword( + password, + encrypted, + salt + ) + } catch (e: Exception) { + toast("Incorrect password") + return@passwordAlertDialog + } + if (PreferencePackager.unpack(decryptedJson)) { + val intent = Intent(this, this.javaClass) + this.finish() + startActivity(intent) + } + } else { + toast("Password cannot be empty") + } + } + } else if (name.endsWith(".ani")) { + val decryptedJson = jsonString.toString(Charsets.UTF_8) + if (PreferencePackager.unpack(decryptedJson)) { + val intent = Intent(this, this.javaClass) + this.finish() + startActivity(intent) + } + } else { + toast("Invalid file type") + } + } catch (e: Exception) { + e.printStackTrace() + toast("Error importing settings") + } + } + val _bottomBar = findViewById(R.id.navbar) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { @@ -398,6 +458,44 @@ class MainActivity : AppCompatActivity() { params.updateMargins(bottom = 32.toPx) } + private fun passwordAlertDialog(callback: (CharArray?) -> Unit) { + val password = CharArray(16).apply { fill('0') } + + // Inflate the dialog layout + val dialogView = + LayoutInflater.from(this).inflate(R.layout.dialog_user_agent, null) + dialogView.findViewById(R.id.userAgentTextBox)?.hint = "Password" + val subtitleTextView = dialogView.findViewById(R.id.subtitle) + subtitleTextView?.visibility = View.VISIBLE + subtitleTextView?.text = "Enter your password to decrypt the file" + + val dialog = AlertDialog.Builder(this, R.style.MyPopup) + .setTitle("Enter Password") + .setView(dialogView) + .setPositiveButton("OK", null) + .setNegativeButton("Cancel") { dialog, _ -> + password.fill('0') + dialog.dismiss() + callback(null) + } + .create() + + dialog.window?.setDimAmount(0.8f) + dialog.show() + + // Override the positive button here + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + val editText = dialog.findViewById(R.id.userAgentTextBox) + if (editText?.text?.isNotBlank() == true) { + editText.text?.toString()?.trim()?.toCharArray(password) + dialog.dismiss() + callback(password) + } else { + toast("Password cannot be empty") + } + } + } + //ViewPager private class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) { diff --git a/app/src/main/java/ani/dantotsu/home/LoginFragment.kt b/app/src/main/java/ani/dantotsu/home/LoginFragment.kt index 8efa8ac3..5f89464e 100644 --- a/app/src/main/java/ani/dantotsu/home/LoginFragment.kt +++ b/app/src/main/java/ani/dantotsu/home/LoginFragment.kt @@ -17,6 +17,7 @@ import ani.dantotsu.openLinkInBrowser import ani.dantotsu.settings.saving.internal.PreferenceKeystore import ani.dantotsu.settings.saving.internal.PreferencePackager import ani.dantotsu.toast +import ani.dantotsu.util.Logger import com.google.android.material.textfield.TextInputEditText class LoginFragment : Fragment() { @@ -50,7 +51,7 @@ class LoginFragment : Fragment() { DocumentFile.fromSingleUri(requireActivity(), uri)?.name ?: "settings" //.sani is encrypted, .ani is not if (name.endsWith(".sani")) { - passwordAlertDialog() { password -> + passwordAlertDialog { password -> if (password != null) { val salt = jsonString.copyOfRange(0, 16) val encrypted = jsonString.copyOfRange(16, jsonString.size) @@ -78,7 +79,7 @@ class LoginFragment : Fragment() { toast("Invalid file type") } } catch (e: Exception) { - e.printStackTrace() + Logger.log(e) toast("Error importing settings") } }