diff options
| author | Harsh Shandilya <msfjarvis@gmail.com> | 2020-09-14 22:37:22 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-14 22:37:22 +0530 |
| commit | a34f749e9ac1d6112a42b0f4ded3ae801d8c583c (patch) | |
| tree | 0bab8a806c64af04fe7422dcb6f906515278cdb7 /app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt | |
| parent | eef809760c7359a34b414af45165b157813e78af (diff) | |
| download | HardenedPass-a34f749e9ac1d6112a42b0f4ded3ae801d8c583c.tar.gz HardenedPass-a34f749e9ac1d6112a42b0f4ded3ae801d8c583c.zip | |
Add tentative workaround for dialog crashes and refactor Git-related code (#1100)
* Ensure we're creating dialogs on the main thread
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* Remove unused operation type
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* Refactor launchGitOperation to use an enum
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt | 69 |
1 files changed, 33 insertions, 36 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt index 19413efd..6a975f6c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt @@ -6,9 +6,7 @@ package com.zeapo.pwdstore.git import androidx.appcompat.app.AppCompatActivity import androidx.core.content.edit -import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.d -import com.github.ajalt.timberkt.e import com.github.michaelbull.result.Err import com.github.michaelbull.result.Result import com.github.michaelbull.result.andThen @@ -18,7 +16,6 @@ import com.zeapo.pwdstore.R import com.zeapo.pwdstore.git.config.GitSettings import com.zeapo.pwdstore.git.operation.BreakOutOfDetached import com.zeapo.pwdstore.git.operation.CloneOperation -import com.zeapo.pwdstore.git.operation.GitOperation import com.zeapo.pwdstore.git.operation.PullOperation import com.zeapo.pwdstore.git.operation.PushOperation import com.zeapo.pwdstore.git.operation.ResetToRemoteOperation @@ -26,6 +23,8 @@ import com.zeapo.pwdstore.git.operation.SyncOperation import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.getEncryptedPrefs import com.zeapo.pwdstore.utils.sharedPrefs +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import net.schmizz.sshj.common.DisconnectReason import net.schmizz.sshj.common.SSHException import net.schmizz.sshj.userauth.UserAuthException @@ -37,29 +36,37 @@ import net.schmizz.sshj.userauth.UserAuthException abstract class BaseGitActivity : AppCompatActivity() { /** + * Enum of possible Git operations than can be run through [launchGitOperation]. + */ + enum class GitOp { + BREAK_OUT_OF_DETACHED, + CLONE, + PULL, + PUSH, + RESET, + SYNC, + } + + /** * Attempt to launch the requested Git operation. * @param operation The type of git operation to launch */ - suspend fun launchGitOperation(operation: Int): Result<Unit, Throwable> { + suspend fun launchGitOperation(operation: GitOp): Result<Unit, Throwable> { if (GitSettings.url == null) { return Err(IllegalStateException("Git url is not set!")) } - if (operation == REQUEST_SYNC && !GitSettings.useMultiplexing) { + if (operation == GitOp.SYNC && !GitSettings.useMultiplexing) { // If the server does not support multiple SSH channels per connection, we cannot run // a sync operation without reconnecting and thus break sync into its two parts. - return launchGitOperation(REQUEST_PULL).andThen { launchGitOperation(REQUEST_PUSH) } + return launchGitOperation(GitOp.PULL).andThen { launchGitOperation(GitOp.PUSH) } } val op = when (operation) { - REQUEST_CLONE, GitOperation.GET_SSH_KEY_FROM_CLONE -> CloneOperation(this, GitSettings.url!!) - REQUEST_PULL -> PullOperation(this) - REQUEST_PUSH -> PushOperation(this) - REQUEST_SYNC -> SyncOperation(this) - BREAK_OUT_OF_DETACHED -> BreakOutOfDetached(this) - REQUEST_RESET -> ResetToRemoteOperation(this) - else -> { - tag(TAG).e { "Operation not recognized : $operation" } - return Err(IllegalArgumentException("$operation is not a valid Git operation")) - } + GitOp.CLONE -> CloneOperation(this, GitSettings.url!!) + GitOp.PULL -> PullOperation(this) + GitOp.PUSH -> PushOperation(this) + GitOp.SYNC -> SyncOperation(this) + GitOp.BREAK_OUT_OF_DETACHED -> BreakOutOfDetached(this) + GitOp.RESET -> ResetToRemoteOperation(this) } return op.executeAfterAuthentication(GitSettings.authMode).mapError { throwable -> val err = rootCauseException(throwable) @@ -76,7 +83,7 @@ abstract class BaseGitActivity : AppCompatActivity() { finish() } - fun promptOnErrorHandler(err: Throwable, onPromptDone: () -> Unit = {}) { + suspend fun promptOnErrorHandler(err: Throwable, onPromptDone: () -> Unit = {}) { val error = rootCauseException(err) if (!isExplicitlyUserInitiatedError(error)) { getEncryptedPrefs("git_operation").edit { @@ -84,14 +91,16 @@ abstract class BaseGitActivity : AppCompatActivity() { } sharedPrefs.edit { remove(PreferenceKeys.SSH_OPENKEYSTORE_KEYID) } d(error) - MaterialAlertDialogBuilder(this).run { - setTitle(resources.getString(R.string.jgit_error_dialog_title)) - setMessage(ErrorMessages[error]) - setPositiveButton(resources.getString(R.string.dialog_ok)) { _, _ -> } - setOnDismissListener { - onPromptDone() + withContext(Dispatchers.Main) { + MaterialAlertDialogBuilder(this@BaseGitActivity).run { + setTitle(resources.getString(R.string.jgit_error_dialog_title)) + setMessage(ErrorMessages[error]) + setPositiveButton(resources.getString(R.string.dialog_ok)) { _, _ -> } + setOnDismissListener { + onPromptDone() + } + show() } - show() } } else { onPromptDone() @@ -130,16 +139,4 @@ abstract class BaseGitActivity : AppCompatActivity() { } return rootCause } - - companion object { - - const val REQUEST_ARG_OP = "OPERATION" - const val REQUEST_PULL = 101 - const val REQUEST_PUSH = 102 - const val REQUEST_CLONE = 103 - const val REQUEST_SYNC = 104 - const val BREAK_OUT_OF_DETACHED = 105 - const val REQUEST_RESET = 106 - const val TAG = "AbstractGitActivity" - } } |
