diff options
author | Harsh Shandilya <msfjarvis@gmail.com> | 2020-04-29 11:01:22 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-29 11:01:22 +0530 |
commit | edc6dcda88a46cb3cd963d89145f48fb1ef0a189 (patch) | |
tree | ffc619d841e434bdd95743e34e52026f8988f968 /app/src/main/java | |
parent | f9c310e93a6c4ac820779b052c3be40199d9bc9c (diff) |
build: update to openpgp-ktx 2.0 (#749)
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt | 378 |
1 files changed, 186 insertions, 192 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index c73adc9d..cd16a09b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -365,175 +365,173 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { val oStream = ByteArrayOutputStream() lifecycleScope.launch(IO) { - api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback { - override fun onReturn(result: Intent?) { - when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { - RESULT_CODE_SUCCESS -> { - try { - val showPassword = settings.getBoolean("show_password", true) - val showExtraContent = settings.getBoolean("show_extra_content", true) + api?.executeApiAsync(data, iStream, oStream) { result -> + when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { + RESULT_CODE_SUCCESS -> { + try { + val showPassword = settings.getBoolean("show_password", true) + val showExtraContent = settings.getBoolean("show_extra_content", true) - crypto_container_decrypt.visibility = View.VISIBLE + crypto_container_decrypt.visibility = View.VISIBLE - val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") - val entry = PasswordEntry(oStream) + val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") + val entry = PasswordEntry(oStream) - passwordEntry = entry + passwordEntry = entry - if (intent.getStringExtra("OPERATION") == "EDIT") { - editPassword() - return - } + if (intent.getStringExtra("OPERATION") == "EDIT") { + editPassword() + return@executeApiAsync + } - if (entry.password.isEmpty()) { - crypto_password_show.visibility = View.GONE - crypto_password_show_label.visibility = View.GONE - } else { - crypto_password_show.visibility = View.VISIBLE - crypto_password_show_label.visibility = View.VISIBLE - crypto_password_show.typeface = monoTypeface - crypto_password_show.text = entry.password - } + if (entry.password.isEmpty()) { + crypto_password_show.visibility = View.GONE + crypto_password_show_label.visibility = View.GONE + } else { + crypto_password_show.visibility = View.VISIBLE + crypto_password_show_label.visibility = View.VISIBLE crypto_password_show.typeface = monoTypeface crypto_password_show.text = entry.password + } + crypto_password_show.typeface = monoTypeface + crypto_password_show.text = entry.password - crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE - crypto_password_show.transformationMethod = if (showPassword) { - null - } else { - HoldToShowPasswordTransformation( - crypto_password_toggle_show, - Runnable { crypto_password_show.text = entry.password } - ) - } + crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE + crypto_password_show.transformationMethod = if (showPassword) { + null + } else { + HoldToShowPasswordTransformation( + crypto_password_toggle_show, + Runnable { crypto_password_show.text = entry.password } + ) + } - if (entry.hasExtraContent()) { - crypto_extra_show.typeface = monoTypeface - crypto_extra_show.text = entry.extraContent - - if (showExtraContent) { - crypto_extra_show_layout.visibility = View.VISIBLE - crypto_extra_toggle_show.visibility = View.GONE - crypto_extra_show.transformationMethod = null - } else { - crypto_extra_show_layout.visibility = View.GONE - crypto_extra_toggle_show.visibility = View.VISIBLE - crypto_extra_toggle_show.setOnCheckedChangeListener { _, _ -> - crypto_extra_show.text = entry.extraContent - } + if (entry.hasExtraContent()) { + crypto_extra_show.typeface = monoTypeface + crypto_extra_show.text = entry.extraContent - crypto_extra_show.transformationMethod = object : PasswordTransformationMethod() { - override fun getTransformation(source: CharSequence, view: View): CharSequence { - return if (crypto_extra_toggle_show.isChecked) source else super.getTransformation(source, view) - } - } + if (showExtraContent) { + crypto_extra_show_layout.visibility = View.VISIBLE + crypto_extra_toggle_show.visibility = View.GONE + crypto_extra_show.transformationMethod = null + } else { + crypto_extra_show_layout.visibility = View.GONE + crypto_extra_toggle_show.visibility = View.VISIBLE + crypto_extra_toggle_show.setOnCheckedChangeListener { _, _ -> + crypto_extra_show.text = entry.extraContent } - if (entry.hasUsername()) { - crypto_username_show.visibility = View.VISIBLE - crypto_username_show_label.visibility = View.VISIBLE - crypto_copy_username.visibility = View.VISIBLE - - crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username!!) } - crypto_username_show.typeface = monoTypeface - crypto_username_show.text = entry.username - } else { - crypto_username_show.visibility = View.GONE - crypto_username_show_label.visibility = View.GONE - crypto_copy_username.visibility = View.GONE + crypto_extra_show.transformationMethod = object : PasswordTransformationMethod() { + override fun getTransformation(source: CharSequence, view: View): CharSequence { + return if (crypto_extra_toggle_show.isChecked) source else super.getTransformation(source, view) + } } } - if (entry.hasTotp() || entry.hasHotp()) { - crypto_extra_show_layout.visibility = View.VISIBLE - crypto_extra_show.typeface = monoTypeface - crypto_extra_show.text = entry.extraContent - - crypto_otp_show.visibility = View.VISIBLE - crypto_otp_show_label.visibility = View.VISIBLE - crypto_copy_otp.visibility = View.VISIBLE - - if (entry.hasTotp()) { - crypto_copy_otp.setOnClickListener { - copyOtpToClipBoard( - Otp.calculateCode( - entry.totpSecret, - Date().time / (1000 * entry.totpPeriod), - entry.totpAlgorithm, - entry.digits) - ) - } - crypto_otp_show.text = + if (entry.hasUsername()) { + crypto_username_show.visibility = View.VISIBLE + crypto_username_show_label.visibility = View.VISIBLE + crypto_copy_username.visibility = View.VISIBLE + + crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username!!) } + crypto_username_show.typeface = monoTypeface + crypto_username_show.text = entry.username + } else { + crypto_username_show.visibility = View.GONE + crypto_username_show_label.visibility = View.GONE + crypto_copy_username.visibility = View.GONE + } + } + + if (entry.hasTotp() || entry.hasHotp()) { + crypto_extra_show_layout.visibility = View.VISIBLE + crypto_extra_show.typeface = monoTypeface + crypto_extra_show.text = entry.extraContent + + crypto_otp_show.visibility = View.VISIBLE + crypto_otp_show_label.visibility = View.VISIBLE + crypto_copy_otp.visibility = View.VISIBLE + + if (entry.hasTotp()) { + crypto_copy_otp.setOnClickListener { + copyOtpToClipBoard( Otp.calculateCode( entry.totpSecret, Date().time / (1000 * entry.totpPeriod), entry.totpAlgorithm, entry.digits) - } else { - // we only want to calculate and show HOTP if the user requests it - crypto_copy_otp.setOnClickListener { - if (settings.getBoolean("hotp_remember_check", false)) { - if (settings.getBoolean("hotp_remember_choice", false)) { - calculateAndCommitHotp(entry) - } else { - calculateHotp(entry) - } + ) + } + crypto_otp_show.text = + Otp.calculateCode( + entry.totpSecret, + Date().time / (1000 * entry.totpPeriod), + entry.totpAlgorithm, + entry.digits) + } else { + // we only want to calculate and show HOTP if the user requests it + crypto_copy_otp.setOnClickListener { + if (settings.getBoolean("hotp_remember_check", false)) { + if (settings.getBoolean("hotp_remember_choice", false)) { + calculateAndCommitHotp(entry) } else { - // show a dialog asking permission to update the HOTP counter in the entry - val checkInflater = LayoutInflater.from(this@PgpActivity) - val checkLayout = checkInflater.inflate(R.layout.otp_confirm_layout, null) - val rememberCheck: CheckBox = - checkLayout.findViewById(R.id.hotp_remember_checkbox) - val dialogBuilder = MaterialAlertDialogBuilder(this@PgpActivity) - dialogBuilder.setView(checkLayout) - dialogBuilder.setMessage(R.string.dialog_update_body) - .setCancelable(false) - .setPositiveButton(R.string.dialog_update_positive) { _, _ -> - run { - calculateAndCommitHotp(entry) - if (rememberCheck.isChecked) { - settings.edit { - putBoolean("hotp_remember_check", true) - putBoolean("hotp_remember_choice", true) - } - } - } - } - .setNegativeButton(R.string.dialog_update_negative) { _, _ -> - run { - calculateHotp(entry) + calculateHotp(entry) + } + } else { + // show a dialog asking permission to update the HOTP counter in the entry + val checkInflater = LayoutInflater.from(this@PgpActivity) + val checkLayout = checkInflater.inflate(R.layout.otp_confirm_layout, null) + val rememberCheck: CheckBox = + checkLayout.findViewById(R.id.hotp_remember_checkbox) + val dialogBuilder = MaterialAlertDialogBuilder(this@PgpActivity) + dialogBuilder.setView(checkLayout) + dialogBuilder.setMessage(R.string.dialog_update_body) + .setCancelable(false) + .setPositiveButton(R.string.dialog_update_positive) { _, _ -> + run { + calculateAndCommitHotp(entry) + if (rememberCheck.isChecked) { settings.edit { putBoolean("hotp_remember_check", true) - putBoolean("hotp_remember_choice", false) + putBoolean("hotp_remember_choice", true) } } } - val updateDialog = dialogBuilder.create() - updateDialog.setTitle(R.string.dialog_update_title) - updateDialog.show() - } + } + .setNegativeButton(R.string.dialog_update_negative) { _, _ -> + run { + calculateHotp(entry) + settings.edit { + putBoolean("hotp_remember_check", true) + putBoolean("hotp_remember_choice", false) + } + } + } + val updateDialog = dialogBuilder.create() + updateDialog.setTitle(R.string.dialog_update_title) + updateDialog.show() } - crypto_otp_show.setText(R.string.hotp_pending) } - crypto_otp_show.typeface = monoTypeface - } else { - crypto_otp_show.visibility = View.GONE - crypto_otp_show_label.visibility = View.GONE - crypto_copy_otp.visibility = View.GONE + crypto_otp_show.setText(R.string.hotp_pending) } + crypto_otp_show.typeface = monoTypeface + } else { + crypto_otp_show.visibility = View.GONE + crypto_otp_show_label.visibility = View.GONE + crypto_copy_otp.visibility = View.GONE + } - if (settings.getBoolean("copy_on_decrypt", true)) { - copyPasswordToClipBoard() - } - } catch (e: Exception) { - e(e) { "An Exception occurred" } + if (settings.getBoolean("copy_on_decrypt", true)) { + copyPasswordToClipBoard() } + } catch (e: Exception) { + e(e) { "An Exception occurred" } } - RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_DECRYPT) - RESULT_CODE_ERROR -> handleError(result) } + RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_DECRYPT) + RESULT_CODE_ERROR -> handleError(result) } - }) + } } } @@ -591,48 +589,46 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { } lifecycleScope.launch(IO) { - api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback { - override fun onReturn(result: Intent?) { - when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { - RESULT_CODE_SUCCESS -> { - try { - // TODO This might fail, we should check that the write is successful - val file = File(path) - val outputStream = FileUtils.openOutputStream(file) - outputStream.write(oStream.toByteArray()) - outputStream.close() - - val returnIntent = Intent() - returnIntent.putExtra("CREATED_FILE", path) - returnIntent.putExtra("NAME", editName) - returnIntent.putExtra("LONG_NAME", getLongName(fullPath, repoPath, editName!!)) - - // if coming from decrypt screen->edit button - if (intent.getBooleanExtra("fromDecrypt", false)) { - returnIntent.putExtra("OPERATION", "EDIT") - returnIntent.putExtra("needCommit", true) - } + api?.executeApiAsync(data, iStream, oStream) { result -> + when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { + RESULT_CODE_SUCCESS -> { + try { + // TODO This might fail, we should check that the write is successful + val file = File(path) + val outputStream = FileUtils.openOutputStream(file) + outputStream.write(oStream.toByteArray()) + outputStream.close() + + val returnIntent = Intent() + returnIntent.putExtra("CREATED_FILE", path) + returnIntent.putExtra("NAME", editName) + returnIntent.putExtra("LONG_NAME", getLongName(fullPath, repoPath, editName!!)) + + // if coming from decrypt screen->edit button + if (intent.getBooleanExtra("fromDecrypt", false)) { + returnIntent.putExtra("OPERATION", "EDIT") + returnIntent.putExtra("needCommit", true) + } - if (shouldGeneratePassword) { - val directoryStructure = + if (shouldGeneratePassword) { + val directoryStructure = AutofillPreferences.directoryStructure(applicationContext) - val entry = PasswordEntry(content) - returnIntent.putExtra("PASSWORD", entry.password) - val username = PasswordEntry(content).username + val entry = PasswordEntry(content) + returnIntent.putExtra("PASSWORD", entry.password) + val username = PasswordEntry(content).username ?: directoryStructure.getUsernameFor(file) - returnIntent.putExtra("USERNAME", username) - } - - setResult(RESULT_OK, returnIntent) - finish() - } catch (e: Exception) { - e(e) { "An Exception occurred" } + returnIntent.putExtra("USERNAME", username) } + + setResult(RESULT_OK, returnIntent) + finish() + } catch (e: Exception) { + e(e) { "An Exception occurred" } } - RESULT_CODE_ERROR -> handleError(result) } + RESULT_CODE_ERROR -> handleError(result) } - }) + } } } @@ -715,31 +711,29 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { val data = receivedIntent ?: Intent() data.action = OpenPgpApi.ACTION_GET_KEY_IDS lifecycleScope.launch(IO) { - api?.executeApiAsync(data, null, null, object : OpenPgpApi.IOpenPgpCallback { - override fun onReturn(result: Intent?) { - when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { - RESULT_CODE_SUCCESS -> { - try { - val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS) - ?: LongArray(0) - val keys = ids.map { it.toString() }.toSet() - - // use Long - settings.edit { putStringSet("openpgp_key_ids_set", keys) } - - showSnackbar("PGP keys selected") - - setResult(RESULT_OK) - finish() - } catch (e: Exception) { - e(e) { "An Exception occurred" } - } + api?.executeApiAsync(data, null, null) { result -> + when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { + RESULT_CODE_SUCCESS -> { + try { + val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS) + ?: LongArray(0) + val keys = ids.map { it.toString() }.toSet() + + // use Long + settings.edit { putStringSet("openpgp_key_ids_set", keys) } + + showSnackbar("PGP keys selected") + + setResult(RESULT_OK) + finish() + } catch (e: Exception) { + e(e) { "An Exception occurred" } } - RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_KEY_ID) - RESULT_CODE_ERROR -> handleError(result) } + RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_KEY_ID) + RESULT_CODE_ERROR -> handleError(result) } - }) + } } } |