diff options
author | Harsh Shandilya <msfjarvis@gmail.com> | 2020-05-08 20:50:04 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-08 20:50:04 +0530 |
commit | 4c461fb174428d0c96d80a634e01578e846eb44b (patch) | |
tree | c0e7e4c3b603943baaac22334925d9410961486d /app/src/main/java/com | |
parent | ed3312b3032ede9786d049706e74a60c973b7b72 (diff) |
Introduce no-auth mode for connections (#768)
Fixes #758
Fixes #526
Diffstat (limited to 'app/src/main/java/com')
6 files changed, 64 insertions, 33 deletions
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) |