From 4c461fb174428d0c96d80a634e01578e846eb44b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 8 May 2020 20:50:04 +0530 Subject: Introduce no-auth mode for connections (#768) Fixes #758 Fixes #526 --- .../java/com/zeapo/pwdstore/PasswordFragment.kt | 43 +++++++++++++++------- .../main/java/com/zeapo/pwdstore/PasswordStore.kt | 9 ++++- .../java/com/zeapo/pwdstore/git/GitOperation.kt | 3 ++ .../zeapo/pwdstore/git/GitServerConfigActivity.kt | 35 ++++++++++-------- .../zeapo/pwdstore/git/config/ConnectionMode.kt | 4 +- .../java/com/zeapo/pwdstore/git/config/Protocol.kt | 3 +- .../main/res/drawable/ic_refresh_white_24dp.xml | 9 ----- app/src/main/res/layout/activity_git_clone.xml | 43 ++++++++-------------- app/src/main/res/menu/main_menu_git.xml | 17 ++++----- app/src/main/res/menu/main_menu_no_auth.xml | 20 ++++++++++ app/src/main/res/menu/main_menu_non_git.xml | 9 ++--- app/src/main/res/values/strings.xml | 5 ++- 12 files changed, 114 insertions(+), 86 deletions(-) delete mode 100644 app/src/main/res/drawable/ic_refresh_white_24dp.xml create mode 100644 app/src/main/res/menu/main_menu_no_auth.xml diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index ecf1a887..33f3b32f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -6,6 +6,7 @@ package com.zeapo.pwdstore import android.content.Context import android.content.Intent +import android.content.SharedPreferences import android.os.Bundle import android.os.Parcelable import android.view.LayoutInflater @@ -19,12 +20,14 @@ import androidx.appcompat.view.ActionMode import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.lifecycle.observe +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.git.GitServerConfigActivity +import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.ui.OnOffItemAnimator import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet @@ -37,6 +40,7 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder class PasswordFragment : Fragment() { private lateinit var recyclerAdapter: PasswordItemRecyclerAdapter private lateinit var listener: OnFragmentInteractionListener + private lateinit var settings: SharedPreferences private var recyclerViewStateToRestore: Parcelable? = null private var actionMode: ActionMode? = null @@ -53,6 +57,7 @@ class PasswordFragment : Fragment() { savedInstanceState: Bundle? ): View? { _binding = PasswordRecyclerViewBinding.inflate(inflater, container, false) + settings = PreferenceManager.getDefaultSharedPreferences(requireContext()) initializePasswordList() binding.fab.setOnClickListener { ItemCreationBottomSheet().apply { @@ -63,20 +68,30 @@ class PasswordFragment : Fragment() { } private fun initializePasswordList() { - binding.swipeRefresher.setOnRefreshListener { - if (!PasswordRepository.isGitRepo()) { - Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE) - .setAction(R.string.clone_button) { - val intent = Intent(context, GitServerConfigActivity::class.java) - intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_CLONE) - startActivityForResult(intent, BaseGitActivity.REQUEST_CLONE) - } - .show() - binding.swipeRefresher.isRefreshing = false - } else { - val intent = Intent(context, GitOperationActivity::class.java) - intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_SYNC) - startActivityForResult(intent, BaseGitActivity.REQUEST_SYNC) + val gitDir = File(PasswordRepository.getRepositoryDirectory(requireContext()), ".git") + val hasGitDir = gitDir.exists() && gitDir.isDirectory && (gitDir.listFiles()?.isNotEmpty() == true) + if (hasGitDir) { + binding.swipeRefresher.setOnRefreshListener { + if (!PasswordRepository.isGitRepo()) { + Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE) + .setAction(R.string.clone_button) { + val intent = Intent(context, GitServerConfigActivity::class.java) + intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_CLONE) + startActivityForResult(intent, BaseGitActivity.REQUEST_CLONE) + } + .show() + binding.swipeRefresher.isRefreshing = false + } else { + // When authentication is set to ConnectionMode.None then the only git operation we + // can run is a pull, so automatically fallback to that. + val operationId = when (ConnectionMode.fromString(settings.getString("git_remote_auth", null))) { + ConnectionMode.None -> BaseGitActivity.REQUEST_PULL + else -> BaseGitActivity.REQUEST_SYNC + } + val intent = Intent(context, GitOperationActivity::class.java) + intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, operationId) + startActivityForResult(intent, operationId) + } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index cba24202..b47e2b8e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -53,6 +53,7 @@ import com.zeapo.pwdstore.git.GitAsyncTask import com.zeapo.pwdstore.git.GitOperation import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.git.GitServerConfigActivity +import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository @@ -201,7 +202,13 @@ class PasswordStore : AppCompatActivity() { } override fun onCreateOptionsMenu(menu: Menu?): Boolean { - menuInflater.inflate(if (PasswordRepository.isGitRepo()) R.menu.main_menu_git else R.menu.main_menu_non_git, menu) + val menuRes = when { + ConnectionMode.fromString(settings.getString("git_remote_auth", null)) + == ConnectionMode.None -> R.menu.main_menu_no_auth + PasswordRepository.isGitRepo() -> R.menu.main_menu_git + else -> R.menu.main_menu_non_git + } + menuInflater.inflate(menuRes, menu) return super.onCreateOptionsMenu(menu) } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt index 817d9dca..82f3d7f6 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt @@ -247,6 +247,9 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit dialog.show() } } + ConnectionMode.None -> { + execute() + } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt index a10f3460..14f9bb62 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt @@ -24,7 +24,7 @@ import java.io.IOException */ class GitServerConfigActivity : BaseGitActivity() { - lateinit var binding: ActivityGitCloneBinding + private lateinit var binding: ActivityGitCloneBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -36,11 +36,10 @@ class GitServerConfigActivity : BaseGitActivity() { setContentView(binding.root) supportActionBar?.setDisplayHomeAsUpEnabled(true) - val protocolIdToCheck = when (protocol) { + binding.cloneProtocolGroup.check(when (protocol) { Protocol.Ssh -> R.id.clone_protocol_ssh Protocol.Https -> R.id.clone_protocol_https - } - binding.cloneProtocolGroup.check(protocolIdToCheck) + }) binding.cloneProtocolGroup.addOnButtonCheckedListener { _, checkedId, checked -> if (checked) { when (checkedId) { @@ -51,19 +50,18 @@ class GitServerConfigActivity : BaseGitActivity() { } } - val connectionModeIdToCheck = when (connectionMode) { + binding.connectionModeGroup.check(when (connectionMode) { ConnectionMode.SshKey -> R.id.connection_mode_ssh_key ConnectionMode.Password -> R.id.connection_mode_password ConnectionMode.OpenKeychain -> R.id.connection_mode_open_keychain - } - binding.connectionModeGroup.check(connectionModeIdToCheck) - binding.connectionModeGroup.addOnButtonCheckedListener { _, checkedId, checked -> - if (checked) { - when (checkedId) { - R.id.connection_mode_ssh_key -> connectionMode = ConnectionMode.SshKey - R.id.connection_mode_open_keychain -> connectionMode = ConnectionMode.OpenKeychain - R.id.connection_mode_password -> connectionMode = ConnectionMode.Password - } + ConnectionMode.None -> R.id.connection_mode_none + }) + binding.connectionModeGroup.setOnCheckedChangeListener { group, _ -> + when (group.checkedRadioButtonId) { + R.id.connection_mode_ssh_key -> connectionMode = ConnectionMode.SshKey + R.id.connection_mode_open_keychain -> connectionMode = ConnectionMode.OpenKeychain + R.id.connection_mode_password -> connectionMode = ConnectionMode.Password + R.id.connection_mode_none -> connectionMode = ConnectionMode.None } } updateConnectionModeToggleGroup() @@ -121,15 +119,20 @@ class GitServerConfigActivity : BaseGitActivity() { private fun updateConnectionModeToggleGroup() { if (protocol == Protocol.Ssh) { + if (binding.connectionModeNone.isChecked) + binding.connectionModeGroup.check(R.id.connection_mode_ssh_key) binding.connectionModeSshKey.isEnabled = true binding.connectionModeOpenKeychain.isEnabled = true + binding.connectionModeNone.isEnabled = false } else { - // Reset connection mode to the only one possible via HTTPS: password. + // Reset connection mode to password if the current value is not valid for HTTPS // Important note: This has to happen before disabling the other toggle buttons or they // won't uncheck. - binding.connectionModeGroup.check(R.id.connection_mode_password) + if (connectionMode !in listOf(ConnectionMode.None, ConnectionMode.Password)) + binding.connectionModeGroup.check(R.id.connection_mode_password) binding.connectionModeSshKey.isEnabled = false binding.connectionModeOpenKeychain.isEnabled = false + binding.connectionModeNone.isEnabled = true } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/ConnectionMode.kt b/app/src/main/java/com/zeapo/pwdstore/git/config/ConnectionMode.kt index 9316e89f..8bc3fb18 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/config/ConnectionMode.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/config/ConnectionMode.kt @@ -7,7 +7,9 @@ package com.zeapo.pwdstore.git.config enum class ConnectionMode(val pref: String) { SshKey("ssh-key"), Password("username/password"), - OpenKeychain("OpenKeychain"); + OpenKeychain("OpenKeychain"), + None("None"), + ; companion object { private val map = values().associateBy(ConnectionMode::pref) diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/Protocol.kt b/app/src/main/java/com/zeapo/pwdstore/git/config/Protocol.kt index 1909fe85..eca23800 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/config/Protocol.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/config/Protocol.kt @@ -6,7 +6,8 @@ package com.zeapo.pwdstore.git.config enum class Protocol(val pref: String) { Ssh("ssh://"), - Https("https://"); + Https("https://"), + ; companion object { private val map = values().associateBy(Protocol::pref) diff --git a/app/src/main/res/drawable/ic_refresh_white_24dp.xml b/app/src/main/res/drawable/ic_refresh_white_24dp.xml deleted file mode 100644 index 7d391467..00000000 --- a/app/src/main/res/drawable/ic_refresh_white_24dp.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/layout/activity_git_clone.xml b/app/src/main/res/layout/activity_git_clone.xml index de9a240e..e5d41059 100644 --- a/app/src/main/res/layout/activity_git_clone.xml +++ b/app/src/main/res/layout/activity_git_clone.xml @@ -156,50 +156,39 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/label_server_path" /> - + app:layout_constraintStart_toStartOf="parent"> - + android:text="@string/connection_mode_ssh_key" /> - + android:text="@string/connection_mode_basic_authentication" /> - + android:text="@string/connection_mode_openkeychain" /> - + + + + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:title="@string/git_sync" /> + + android:title="@string/git_pull" /> + + android:title="@string/git_push" /> + android:title="@string/refresh_list" /> + android:orderInCategory="100" /> diff --git a/app/src/main/res/menu/main_menu_no_auth.xml b/app/src/main/res/menu/main_menu_no_auth.xml new file mode 100644 index 00000000..9036c261 --- /dev/null +++ b/app/src/main/res/menu/main_menu_no_auth.xml @@ -0,0 +1,20 @@ + + + + + + + + + + diff --git a/app/src/main/res/menu/main_menu_non_git.xml b/app/src/main/res/menu/main_menu_non_git.xml index dc7d20cc..7745b544 100644 --- a/app/src/main/res/menu/main_menu_non_git.xml +++ b/app/src/main/res/menu/main_menu_non_git.xml @@ -1,7 +1,6 @@ + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:title="@string/refresh_list" /> + android:orderInCategory="100" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d77ad6a..db8491e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -354,9 +354,10 @@ System default SSH HTTPS - SSH key - Password + SSH key + Password OpenKeychain + None Successfully saved configuration Configuration error: please verify your settings and try again Unable to open the ssh-key -- cgit v1.2.3