Migrations: reuse SharedPreferences instances
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
3ac9c7d8e7
commit
32cb1b3af3
1 changed files with 20 additions and 18 deletions
|
@ -7,6 +7,7 @@
|
||||||
package com.zeapo.pwdstore
|
package com.zeapo.pwdstore
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import com.github.ajalt.timberkt.e
|
import com.github.ajalt.timberkt.e
|
||||||
import com.github.ajalt.timberkt.i
|
import com.github.ajalt.timberkt.i
|
||||||
|
@ -20,19 +21,20 @@ import java.io.File
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
|
||||||
fun runMigrations(context: Context) {
|
fun runMigrations(context: Context) {
|
||||||
migrateToGitUrlBasedConfig(context)
|
val sharedPrefs = context.sharedPrefs
|
||||||
migrateToHideAll(context)
|
migrateToGitUrlBasedConfig(sharedPrefs)
|
||||||
migrateToSshKey(context)
|
migrateToHideAll(sharedPrefs)
|
||||||
|
migrateToSshKey(context, sharedPrefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun migrateToGitUrlBasedConfig(context: Context) {
|
private fun migrateToGitUrlBasedConfig(sharedPrefs: SharedPreferences) {
|
||||||
val serverHostname = context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_SERVER)
|
val serverHostname = sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_SERVER)
|
||||||
?: return
|
?: return
|
||||||
i { "Migrating to URL-based Git config" }
|
i { "Migrating to URL-based Git config" }
|
||||||
val serverPort = context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_PORT) ?: ""
|
val serverPort = sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_PORT) ?: ""
|
||||||
val serverUser = context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_USERNAME) ?: ""
|
val serverUser = sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_USERNAME) ?: ""
|
||||||
val serverPath = context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_LOCATION) ?: ""
|
val serverPath = sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_LOCATION) ?: ""
|
||||||
val protocol = Protocol.fromString(context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL))
|
val protocol = Protocol.fromString(sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL))
|
||||||
|
|
||||||
// Whether we need the leading ssh:// depends on the use of a custom port.
|
// Whether we need the leading ssh:// depends on the use of a custom port.
|
||||||
val hostnamePart = serverHostname.removePrefix("ssh://")
|
val hostnamePart = serverHostname.removePrefix("ssh://")
|
||||||
|
@ -74,7 +76,7 @@ private fun migrateToGitUrlBasedConfig(context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.sharedPrefs.edit {
|
sharedPrefs.edit {
|
||||||
remove(PreferenceKeys.GIT_REMOTE_LOCATION)
|
remove(PreferenceKeys.GIT_REMOTE_LOCATION)
|
||||||
remove(PreferenceKeys.GIT_REMOTE_PORT)
|
remove(PreferenceKeys.GIT_REMOTE_PORT)
|
||||||
remove(PreferenceKeys.GIT_REMOTE_SERVER)
|
remove(PreferenceKeys.GIT_REMOTE_SERVER)
|
||||||
|
@ -89,26 +91,26 @@ private fun migrateToGitUrlBasedConfig(context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun migrateToHideAll(context: Context) {
|
private fun migrateToHideAll(sharedPrefs: SharedPreferences) {
|
||||||
context.sharedPrefs.all[PreferenceKeys.SHOW_HIDDEN_FOLDERS] ?: return
|
sharedPrefs.all[PreferenceKeys.SHOW_HIDDEN_FOLDERS] ?: return
|
||||||
val isHidden = context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)
|
val isHidden = sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)
|
||||||
context.sharedPrefs.edit {
|
sharedPrefs.edit {
|
||||||
remove(PreferenceKeys.SHOW_HIDDEN_FOLDERS)
|
remove(PreferenceKeys.SHOW_HIDDEN_FOLDERS)
|
||||||
putBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, isHidden)
|
putBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, isHidden)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun migrateToSshKey(context: Context) {
|
private fun migrateToSshKey(context: Context, sharedPrefs: SharedPreferences) {
|
||||||
val privateKeyFile = File(context.filesDir, ".ssh_key")
|
val privateKeyFile = File(context.filesDir, ".ssh_key")
|
||||||
if (context.sharedPrefs.contains(PreferenceKeys.USE_GENERATED_KEY) &&
|
if (sharedPrefs.contains(PreferenceKeys.USE_GENERATED_KEY) &&
|
||||||
!SshKey.exists &&
|
!SshKey.exists &&
|
||||||
privateKeyFile.exists()) {
|
privateKeyFile.exists()) {
|
||||||
// Currently uses a private key imported or generated with an old version of Password Store.
|
// Currently uses a private key imported or generated with an old version of Password Store.
|
||||||
// Generated keys come with a public key which the user should still be able to view after
|
// Generated keys come with a public key which the user should still be able to view after
|
||||||
// the migration (not possible for regular imported keys), hence the special case.
|
// the migration (not possible for regular imported keys), hence the special case.
|
||||||
val isGeneratedKey = context.sharedPrefs.getBoolean(PreferenceKeys.USE_GENERATED_KEY, false)
|
val isGeneratedKey = sharedPrefs.getBoolean(PreferenceKeys.USE_GENERATED_KEY, false)
|
||||||
SshKey.useLegacyKey(isGeneratedKey)
|
SshKey.useLegacyKey(isGeneratedKey)
|
||||||
context.sharedPrefs.edit {
|
sharedPrefs.edit {
|
||||||
remove(PreferenceKeys.USE_GENERATED_KEY)
|
remove(PreferenceKeys.USE_GENERATED_KEY)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue