From 5715b59ed424a8aee963aa7f4857bba118d20ede Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 10 Aug 2020 19:19:01 +0530 Subject: Replicate key selection flow for directory creation (#999) * Replicate key selection flow Signed-off-by: Harsh Shandilya * Review fixes Signed-off-by: Harsh Shandilya * Set --user 0 in adb options to prevent automatically installing to work profile Signed-off-by: Harsh Shandilya * Fix committing regression Signed-off-by: Harsh Shandilya * Update changelog Signed-off-by: Harsh Shandilya --- app/build.gradle.kts | 2 + .../com/zeapo/pwdstore/git/GitCommandExecutor.kt | 11 +--- .../zeapo/pwdstore/git/operation/SyncOperation.kt | 1 - .../ui/dialogs/FolderCreationDialogFragment.kt | 63 +++++++++++++++++++--- .../java/com/zeapo/pwdstore/utils/Extensions.kt | 1 - app/src/main/res/layout/folder_dialog_fragment.xml | 18 +++++-- app/src/main/res/values/strings.xml | 5 ++ 7 files changed, 79 insertions(+), 22 deletions(-) (limited to 'app') diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 19d9ca7a..d814e992 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -24,6 +24,8 @@ android { } } + adbOptions.installOptions("--user 0") + buildFeatures.viewBinding = true defaultConfig { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt index 11b06b8e..24c42df2 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt @@ -26,7 +26,6 @@ import org.eclipse.jgit.api.CommitCommand import org.eclipse.jgit.api.PullCommand import org.eclipse.jgit.api.PushCommand import org.eclipse.jgit.api.RebaseResult -import org.eclipse.jgit.api.StatusCommand import org.eclipse.jgit.transport.RemoteRefUpdate import org.eclipse.jgit.transport.SshSessionFactory @@ -43,22 +42,14 @@ class GitCommandExecutor( message = activity.resources.getString(R.string.git_operation_running), length = Snackbar.LENGTH_INDEFINITE, ) - var nbChanges = 0 var operationResult: Result = Result.Ok for (command in operation.commands) { try { when (command) { - is StatusCommand -> { - // in case we have changes, we want to keep track of it - val status = withContext(Dispatchers.IO) { - command.call() - } - nbChanges = status.changed.size + status.missing.size - } is CommitCommand -> { // the previous status will eventually be used to avoid a commit withContext(Dispatchers.IO) { - if (nbChanges > 0) command.call() + command.call() } } is PullCommand -> { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt index 6edf2994..223f2cd8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt @@ -18,7 +18,6 @@ class SyncOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOper override val commands = arrayOf( git.add().addFilepattern("."), - git.status(), git.commit().setAll(true).setMessage("[Android Password Store] Sync"), git.pull().setRebase(true).setRemote("origin"), git.push().setPushAll().setRemote("origin"), diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt index 25a935bb..b5295f17 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt @@ -5,15 +5,29 @@ package com.zeapo.pwdstore.ui.dialogs import android.app.Dialog +import android.content.Intent import android.os.Bundle +import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment +import androidx.lifecycle.lifecycleScope +import com.google.android.material.checkbox.MaterialCheckBox import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.textfield.TextInputEditText +import com.google.android.material.textfield.TextInputLayout import com.zeapo.pwdstore.PasswordStore import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.crypto.BasePgpActivity +import com.zeapo.pwdstore.crypto.GetKeyIdsActivity +import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory +import com.zeapo.pwdstore.utils.commitChange import com.zeapo.pwdstore.utils.requestInputFocusOnView import java.io.File +import kotlinx.coroutines.launch +import me.msfjarvis.openpgpktx.util.OpenPgpApi class FolderCreationDialogFragment : DialogFragment() { @@ -21,25 +35,60 @@ class FolderCreationDialogFragment : DialogFragment() { val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext()) alertDialogBuilder.setTitle(R.string.title_create_folder) alertDialogBuilder.setView(R.layout.folder_dialog_fragment) - alertDialogBuilder.setPositiveButton(getString(R.string.button_create)) { _, _ -> - createDirectory(requireArguments().getString(CURRENT_DIR_EXTRA)!!) - } + alertDialogBuilder.setPositiveButton(getString(R.string.button_create), null) alertDialogBuilder.setNegativeButton(getString(android.R.string.cancel)) { _, _ -> dismiss() } val dialog = alertDialogBuilder.create() dialog.requestInputFocusOnView(R.id.folder_name_text) + dialog.setOnShowListener { + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + createDirectory(requireArguments().getString(CURRENT_DIR_EXTRA)!!) + } + } return dialog } private fun createDirectory(currentDir: String) { val dialog = requireDialog() - val materialTextView = dialog.findViewById(R.id.folder_name_text) - val folderName = materialTextView.text.toString() - val newFolder = File("$currentDir/$folderName") + val folderNameView = dialog.findViewById(R.id.folder_name_text) + val folderNameViewContainer = dialog.findViewById(R.id.folder_name_container) + val newFolder = File("$currentDir/${folderNameView.text}") + folderNameViewContainer.error = when { + newFolder.isFile -> getString(R.string.folder_creation_err_file_exists) + newFolder.isDirectory -> getString(R.string.folder_creation_err_folder_exists) + else -> null + } + if (folderNameViewContainer.error != null) return newFolder.mkdirs() (requireActivity() as PasswordStore).refreshPasswordList(newFolder) - dismiss() + if (dialog.findViewById(R.id.set_gpg_key).isChecked) { + val gpgIdentifierFile = File(newFolder, ".gpg-id") + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == AppCompatActivity.RESULT_OK) { + result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> + gpgIdentifierFile.writeText(keyIds.joinToString("\n")) + val repo = PasswordRepository.getRepository(null) + if (repo != null) { + lifecycleScope.launch { + val repoPath = getRepositoryDirectory().absolutePath + requireActivity().commitChange( + getString( + R.string.git_commit_gpg_id, + BasePgpActivity.getLongName(gpgIdentifierFile.parentFile!!.absolutePath, repoPath, gpgIdentifierFile.name) + ), + finishActivityOnEnd = false, + ) + dismiss() + } + } + } + } + }.launch(Intent(requireContext(), GetKeyIdsActivity::class.java)) + return + } else { + dismiss() + } } companion object { diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt index b5cb4f8c..a3ddc170 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -116,7 +116,6 @@ suspend fun FragmentActivity.commitChange( object : GitOperation(getRepositoryDirectory(), this@commitChange) { override val commands = arrayOf( git.add().addFilepattern("."), - git.status(), git.commit().setAll(true).setMessage(message), ) diff --git a/app/src/main/res/layout/folder_dialog_fragment.xml b/app/src/main/res/layout/folder_dialog_fragment.xml index 338d62fb..e426af53 100644 --- a/app/src/main/res/layout/folder_dialog_fragment.xml +++ b/app/src/main/res/layout/folder_dialog_fragment.xml @@ -3,17 +3,21 @@ ~ SPDX-License-Identifier: GPL-3.0-only --> - + android:hint="@string/crypto_name_hint" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> - + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8303e18d..9251d91e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -367,6 +367,7 @@ A key ID in .gpg-id is too short, please use either long key IDs (16 characters) or fingerprints (40 characters) File name must not contain \'/\', set directory above Directory + Set GPG key for directory Unknown error @@ -383,4 +384,8 @@ https://play.google.com/store/apps/details?id=%1$s F-Droid https://f-droid.org/en/packages/%1$s/ + + + A file by that name already exists + A folder by that name already exists -- cgit v1.2.3