Better handle non-git repositories (#756)

This commit is contained in:
Harsh Shandilya 2020-05-03 01:39:16 +05:30 committed by GitHub
parent ced8bcca01
commit 69e887f3d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 68 additions and 56 deletions

View file

@ -201,11 +201,14 @@ class PasswordStore : AppCompatActivity() {
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean { override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu) menuInflater.inflate(if (PasswordRepository.isGitRepo()) R.menu.main_menu_git else R.menu.main_menu_non_git, menu)
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)
} }
override fun onPrepareOptionsMenu(menu: Menu): Boolean { override fun onPrepareOptionsMenu(menu: Menu): Boolean {
// Invalidation forces onCreateOptionsMenu to be called again. This is cheap and quick so
// we can get by without any noticeable difference in performance.
invalidateOptionsMenu()
searchItem = menu.findItem(R.id.action_search) searchItem = menu.findItem(R.id.action_search)
searchView = searchItem.actionView as SearchView searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener( searchView.setOnQueryTextListener(
@ -256,8 +259,8 @@ class PasswordStore : AppCompatActivity() {
val id = item.itemId val id = item.itemId
val intent: Intent val intent: Intent
val initBefore = MaterialAlertDialogBuilder(this) val initBefore = MaterialAlertDialogBuilder(this)
.setMessage(this.resources.getString(R.string.creation_dialog_text)) .setMessage(resources.getString(R.string.creation_dialog_text))
.setPositiveButton(this.resources.getString(R.string.dialog_ok), null) .setPositiveButton(resources.getString(R.string.dialog_ok), null)
when (id) { when (id) {
R.id.user_pref -> { R.id.user_pref -> {
try { try {
@ -378,12 +381,12 @@ class PasswordStore : AppCompatActivity() {
val keyIds = settings.getStringSet("openpgp_key_ids_set", HashSet()) val keyIds = settings.getStringSet("openpgp_key_ids_set", HashSet())
if (keyIds != null && keyIds.isEmpty()) { if (keyIds != null && keyIds.isEmpty()) {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setMessage(this.resources.getString(R.string.key_dialog_text)) .setMessage(resources.getString(R.string.key_dialog_text))
.setPositiveButton(this.resources.getString(R.string.dialog_positive)) { _, _ -> .setPositiveButton(resources.getString(R.string.dialog_positive)) { _, _ ->
val intent = Intent(activity, UserPreference::class.java) val intent = Intent(activity, UserPreference::class.java)
startActivityForResult(intent, BaseGitActivity.REQUEST_INIT) startActivityForResult(intent, BaseGitActivity.REQUEST_INIT)
} }
.setNegativeButton(this.resources.getString(R.string.dialog_negative), null) .setNegativeButton(resources.getString(R.string.dialog_negative), null)
.show() .show()
} }
createRepository() createRepository()
@ -540,16 +543,16 @@ class PasswordStore : AppCompatActivity() {
private fun validateState(): Boolean { private fun validateState(): Boolean {
if (!isInitialized) { if (!isInitialized) {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setMessage(this.resources.getString(R.string.creation_dialog_text)) .setMessage(resources.getString(R.string.creation_dialog_text))
.setPositiveButton(this.resources.getString(R.string.dialog_ok), null) .setPositiveButton(resources.getString(R.string.dialog_ok), null)
.show() .show()
return false return false
} }
if (settings.getStringSet("openpgp_key_ids_set", HashSet()).isNullOrEmpty()) { if (settings.getStringSet("openpgp_key_ids_set", HashSet()).isNullOrEmpty()) {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(this.resources.getString(R.string.no_key_selected_dialog_title)) .setTitle(resources.getString(R.string.no_key_selected_dialog_title))
.setMessage(this.resources.getString(R.string.no_key_selected_dialog_text)) .setMessage(resources.getString(R.string.no_key_selected_dialog_text))
.setPositiveButton(this.resources.getString(R.string.dialog_ok)) { _, _ -> .setPositiveButton(resources.getString(R.string.dialog_ok)) { _, _ ->
val intent = Intent(activity, UserPreference::class.java) val intent = Intent(activity, UserPreference::class.java)
startActivity(intent) startActivity(intent)
} }
@ -595,7 +598,7 @@ class PasswordStore : AppCompatActivity() {
commitChange(resources.getString(R.string.git_commit_remove_text, item.longName)) commitChange(resources.getString(R.string.git_commit_remove_text, item.longName))
deletePasswords(selectedItems) deletePasswords(selectedItems)
} }
.setNegativeButton(this.resources.getString(R.string.dialog_no)) { _, _ -> .setNegativeButton(resources.getString(R.string.dialog_no)) { _, _ ->
deletePasswords(selectedItems) deletePasswords(selectedItems)
} }
.show() .show()
@ -637,7 +640,7 @@ class PasswordStore : AppCompatActivity() {
get() = plist?.currentDir ?: getRepositoryDirectory(applicationContext) get() = plist?.currentDir ?: getRepositoryDirectory(applicationContext)
private fun commitChange(message: String) { private fun commitChange(message: String) {
commitChange(activity, message) commitChange(this, message)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -650,32 +653,23 @@ class PasswordStore : AppCompatActivity() {
REQUEST_CODE_DECRYPT_AND_VERIFY -> { REQUEST_CODE_DECRYPT_AND_VERIFY -> {
if (data != null && data.getBooleanExtra("needCommit", false)) { if (data != null && data.getBooleanExtra("needCommit", false)) {
if (data.getStringExtra("OPERATION") == "EDIT") { if (data.getStringExtra("OPERATION") == "EDIT") {
commitChange(this.resources commitChange(resources.getString(R.string.git_commit_edit_text,
.getString( data.extras!!.getString("LONG_NAME")))
R.string.git_commit_edit_text,
data.extras!!.getString("LONG_NAME")))
} else { } else {
commitChange(this.resources commitChange(resources.getString(R.string.git_commit_increment_text,
.getString( data.extras!!.getString("LONG_NAME")))
R.string.git_commit_increment_text,
data.extras!!.getString("LONG_NAME")))
} }
} }
refreshPasswordList() refreshPasswordList()
} }
REQUEST_CODE_ENCRYPT -> { REQUEST_CODE_ENCRYPT -> {
commitChange(this.resources commitChange(resources.getString(R.string.git_commit_add_text,
.getString( data!!.extras!!.getString("LONG_NAME")))
R.string.git_commit_add_text,
data!!.extras!!.getString("LONG_NAME")))
refreshPasswordList() refreshPasswordList()
} }
REQUEST_CODE_EDIT -> { REQUEST_CODE_EDIT -> {
commitChange( commitChange(resources.getString(R.string.git_commit_edit_text,
this.resources data!!.extras!!.getString("LONG_NAME")))
.getString(
R.string.git_commit_edit_text,
data!!.extras!!.getString("LONG_NAME")))
refreshPasswordList() refreshPasswordList()
} }
BaseGitActivity.REQUEST_INIT, NEW_REPO_BUTTON -> initializeRepositoryInfo() BaseGitActivity.REQUEST_INIT, NEW_REPO_BUTTON -> initializeRepositoryInfo()
@ -771,7 +765,7 @@ class PasswordStore : AppCompatActivity() {
e { "Something went wrong while moving." } e { "Something went wrong while moving." }
} else { } else {
AutofillMatcher.updateMatches(this, sourceDestinationMap) AutofillMatcher.updateMatches(this, sourceDestinationMap)
commitChange(this.resources commitChange(resources
.getString( .getString(
R.string.git_commit_move_text, R.string.git_commit_move_text,
sourceLongName, sourceLongName,
@ -782,9 +776,9 @@ class PasswordStore : AppCompatActivity() {
private fun initRepository(operation: Int) { private fun initRepository(operation: Int) {
closeRepository() closeRepository()
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(this.resources.getString(R.string.location_dialog_title)) .setTitle(resources.getString(R.string.location_dialog_title))
.setMessage(this.resources.getString(R.string.location_dialog_text)) .setMessage(resources.getString(R.string.location_dialog_text))
.setPositiveButton(this.resources.getString(R.string.location_hidden)) { _, _ -> .setPositiveButton(resources.getString(R.string.location_hidden)) { _, _ ->
settings.edit { putBoolean("git_external", false) } settings.edit { putBoolean("git_external", false) }
when (operation) { when (operation) {
NEW_REPO_BUTTON -> initializeRepositoryInfo() NEW_REPO_BUTTON -> initializeRepositoryInfo()
@ -795,7 +789,7 @@ class PasswordStore : AppCompatActivity() {
} }
} }
} }
.setNegativeButton(this.resources.getString(R.string.location_sdcard)) { _, _ -> .setNegativeButton(resources.getString(R.string.location_sdcard)) { _, _ ->
settings.edit { putBoolean("git_external", true) } settings.edit { putBoolean("git_external", true) }
val externalRepo = settings.getString("git_external_repo", null) val externalRepo = settings.getString("git_external_repo", null)
if (externalRepo == null) { if (externalRepo == null) {
@ -865,6 +859,7 @@ class PasswordStore : AppCompatActivity() {
private const val PREFERENCE_SEEN_AUTOFILL_ONBOARDING = "seen_autofill_onboarding" private const val PREFERENCE_SEEN_AUTOFILL_ONBOARDING = "seen_autofill_onboarding"
fun commitChange(activity: Activity, message: String, finishWithResultOnEnd: Intent? = null) { fun commitChange(activity: Activity, message: String, finishWithResultOnEnd: Intent? = null) {
if (!PasswordRepository.isGitRepo()) return
object : GitOperation(getRepositoryDirectory(activity), activity) { object : GitOperation(getRepositoryDirectory(activity), activity) {
override fun execute() { override fun execute() {
tag(TAG).d { "Committing with message $message" } tag(TAG).d { "Committing with message $message" }

View file

@ -96,6 +96,15 @@ class UserPreference : AppCompatActivity() {
val externalGitRepositoryPreference = findPreference<Preference>("git_external") val externalGitRepositoryPreference = findPreference<Preference>("git_external")
val selectExternalGitRepositoryPreference = findPreference<Preference>("pref_select_external") val selectExternalGitRepositoryPreference = findPreference<Preference>("pref_select_external")
if (!PasswordRepository.isGitRepo()) {
listOfNotNull(
gitServerPreference, gitConfigPreference, sshKeyPreference,
sshKeygenPreference, viewSshKeyPreference, clearSavedPassPreference
).forEach {
it.parent?.removePreference(it)
}
}
// Crypto preferences // Crypto preferences
val keyPreference = findPreference<Preference>("openpgp_key_id_pref") val keyPreference = findPreference<Preference>("openpgp_key_id_pref")

View file

@ -1,16 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:pwstore="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".pwdstore" > tools:context=".pwdstore" >
<item android:id="@+id/action_search" <item android:id="@+id/action_search"
android:title="@string/action_search" android:title="@string/action_search"
android:icon="@drawable/ic_search_white_24dp" android:icon="@drawable/ic_search_white_24dp"
pwstore:showAsAction="always|collapseActionView" app:showAsAction="always|collapseActionView"
pwstore:actionViewClass="androidx.appcompat.widget.SearchView" /> app:actionViewClass="androidx.appcompat.widget.SearchView" />
<!--<item android:id="@+id/menu_add_category"-->
<!--android:title="New category"/>-->
<item android:id="@+id/git_sync" <item android:id="@+id/git_sync"
android:title="@string/git_sync"/> android:title="@string/git_sync"/>
@ -21,8 +18,8 @@
<item android:id="@+id/refresh" <item android:id="@+id/refresh"
android:title="@string/refresh_list" android:title="@string/refresh_list"
pwstore:showAsAction="never" android:icon="@drawable/ic_refresh_white_24dp"
android:icon="@drawable/ic_refresh_white_24dp"/> app:showAsAction="never"/>
<item android:id="@+id/user_pref" <item android:id="@+id/user_pref"
android:title="@string/action_settings" android:title="@string/action_settings"

View file

@ -0,0 +1,20 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".pwdstore" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/refresh"
android:title="@string/refresh_list"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="never"/>
<item android:id="@+id/user_pref"
android:title="@string/action_settings"
android:orderInCategory="100"/>
</menu>

View file

@ -63,7 +63,6 @@
<string name="share_as_plaintext">شارك كنص مجرد</string> <string name="share_as_plaintext">شارك كنص مجرد</string>
<string name="last_changed">آخِر تعديل %s</string> <string name="last_changed">آخِر تعديل %s</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">جيت</string>
<string name="pref_edit_server_info">تعديل إعدادات خادوم جيت</string> <string name="pref_edit_server_info">تعديل إعدادات خادوم جيت</string>
<string name="pref_ssh_title">إستيراد مفتاح الـ SSH</string> <string name="pref_ssh_title">إستيراد مفتاح الـ SSH</string>
<string name="pref_ssh_keygen_title">توليد مفتاحَي الـ SSH</string> <string name="pref_ssh_keygen_title">توليد مفتاحَي الـ SSH</string>

View file

@ -102,7 +102,6 @@
<string name="share_as_plaintext">Sdílet v nešifrované podobě</string> <string name="share_as_plaintext">Sdílet v nešifrované podobě</string>
<string name="last_changed">Naposled změněno %s</string> <string name="last_changed">Naposled změněno %s</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">Upravit nastavení git serveru</string> <string name="pref_edit_server_info">Upravit nastavení git serveru</string>
<string name="pref_ssh_title">Importovat SSH klíč</string> <string name="pref_ssh_title">Importovat SSH klíč</string>
<string name="pref_ssh_keygen_title">Generovat SSH klíč</string> <string name="pref_ssh_keygen_title">Generovat SSH klíč</string>

View file

@ -76,7 +76,6 @@
<string name="share_as_plaintext">Als Klartext teilen</string> <string name="share_as_plaintext">Als Klartext teilen</string>
<string name="last_changed">Zuletzt geändert %s</string> <string name="last_changed">Zuletzt geändert %s</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">Git-Server Einstellungen</string> <string name="pref_edit_server_info">Git-Server Einstellungen</string>
<string name="pref_edit_git_config">Git Konfiguration bearbeiten</string> <string name="pref_edit_git_config">Git Konfiguration bearbeiten</string>
<string name="pref_ssh_title">Importiere SSH-Key</string> <string name="pref_ssh_title">Importiere SSH-Key</string>

View file

@ -101,7 +101,6 @@
<string name="dialog_update_check">Recordar elección</string> <string name="dialog_update_check">Recordar elección</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">Editar ajustes del servidor Git</string> <string name="pref_edit_server_info">Editar ajustes del servidor Git</string>
<string name="pref_edit_git_config">Preferencias de Git</string> <string name="pref_edit_git_config">Preferencias de Git</string>
<string name="pref_ssh_title">Importar llave SSH</string> <string name="pref_ssh_title">Importar llave SSH</string>

View file

@ -109,7 +109,6 @@
<string name="dialog_update_check">Se souvenir de mon choix</string> <string name="dialog_update_check">Se souvenir de mon choix</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">Editer les paramètres du serveur Git</string> <string name="pref_edit_server_info">Editer les paramètres du serveur Git</string>
<string name="pref_edit_git_config">Éditer la configuration</string> <string name="pref_edit_git_config">Éditer la configuration</string>
<string name="pref_ssh_title">Importer une clef SSH</string> <string name="pref_ssh_title">Importer une clef SSH</string>

View file

@ -63,7 +63,6 @@
<!-- DECRYPT Layout --> <!-- DECRYPT Layout -->
<string name="action_search">検索</string> <string name="action_search">検索</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">git サーバー設定を編集</string> <string name="pref_edit_server_info">git サーバー設定を編集</string>
<string name="pref_ssh_title">SSH 鍵のインポート</string> <string name="pref_ssh_title">SSH 鍵のインポート</string>
<string name="pref_ssh_keygen_title">SSH 鍵ペアを生成</string> <string name="pref_ssh_keygen_title">SSH 鍵ペアを生成</string>

View file

@ -114,7 +114,6 @@
<string name="dialog_update_check">Запомнить мой выбор</string> <string name="dialog_update_check">Запомнить мой выбор</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">Изменить настройки сервера git</string> <string name="pref_edit_server_info">Изменить настройки сервера git</string>
<string name="pref_edit_git_config">Утилиты Git</string> <string name="pref_edit_git_config">Утилиты Git</string>
<string name="pref_ssh_title">Импортировать SSH ключ</string> <string name="pref_ssh_title">Импортировать SSH ключ</string>

View file

@ -63,7 +63,6 @@
<!-- DECRYPT Layout --> <!-- DECRYPT Layout -->
<string name="action_search">搜索</string> <string name="action_search">搜索</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">修改 Git 服务器设置</string> <string name="pref_edit_server_info">修改 Git 服务器设置</string>
<string name="pref_ssh_title">导入 SSH 密钥</string> <string name="pref_ssh_title">导入 SSH 密钥</string>
<string name="pref_ssh_keygen_title">生成 SSH 密钥对</string> <string name="pref_ssh_keygen_title">生成 SSH 密钥对</string>

View file

@ -60,7 +60,6 @@
<!-- DECRYPT Layout --> <!-- DECRYPT Layout -->
<string name="action_search">搜尋</string> <string name="action_search">搜尋</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string>
<string name="pref_edit_server_info">修改 Git 伺服器設定</string> <string name="pref_edit_server_info">修改 Git 伺服器設定</string>
<string name="pref_ssh_title">匯入 SSH 私鑰</string> <string name="pref_ssh_title">匯入 SSH 私鑰</string>
<string name="pref_ssh_keygen_title">生成 SSH 金鑰</string> <string name="pref_ssh_keygen_title">生成 SSH 金鑰</string>

View file

@ -131,7 +131,7 @@
<string name="dialog_update_check">Remember my choice</string> <string name="dialog_update_check">Remember my choice</string>
<!-- Preferences --> <!-- Preferences -->
<string name="pref_git_title">Git</string> <string name="pref_repository_title">Repository</string>
<string name="pref_edit_server_info">Edit git server settings</string> <string name="pref_edit_server_info">Edit git server settings</string>
<string name="pref_edit_git_config">Git utils</string> <string name="pref_edit_git_config">Git utils</string>
<string name="pref_ssh_title">Import SSH key</string> <string name="pref_ssh_title">Import SSH key</string>

View file

@ -32,7 +32,7 @@
app:title="@string/pref_autofill_full_path_title"/> app:title="@string/pref_autofill_full_path_title"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/pref_git_title"> <PreferenceCategory app:title="@string/pref_repository_title">
<Preference <Preference
app:key="git_server_info" app:key="git_server_info"
app:title="@string/pref_edit_server_info" /> app:title="@string/pref_edit_server_info" />