diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2020-10-19 00:00:52 +0530 |
|---|---|---|
| committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-10-19 00:11:28 +0530 |
| commit | 30c8c27770c3d901ac5f1930da55a85c321e5369 (patch) | |
| tree | 21d2ebe8a6b9994e6eb3f371d22b975db0293a5d /app/src/main/java/com/zeapo/pwdstore/utils | |
| parent | 95d53e495c66fd0c785397c64991a0d17509b767 (diff) | |
| download | HardenedPass-30c8c27770c3d901ac5f1930da55a85c321e5369.tar.gz HardenedPass-30c8c27770c3d901ac5f1930da55a85c321e5369.zip | |
Convert PasswordRepository to an object
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt | 5 | ||||
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt | 315 |
2 files changed, 158 insertions, 162 deletions
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 10a29cf0..7a439e94 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -6,7 +6,6 @@ package com.zeapo.pwdstore.utils import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.runCatching -import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory import java.io.File import java.util.Date import org.eclipse.jgit.lib.ObjectId @@ -50,10 +49,10 @@ fun File.contains(other: File): Boolean { /** * Checks if this [File] is in the password repository directory as given - * by [getRepositoryDirectory] + * by [PasswordRepository.getRepositoryDirectory] */ fun File.isInsideRepository(): Boolean { - return canonicalPath.contains(getRepositoryDirectory().canonicalPath) + return canonicalPath.contains(PasswordRepository.getRepositoryDirectory().canonicalPath) } /** diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt index beb1c7b4..8b403a56 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -7,9 +7,9 @@ package com.zeapo.pwdstore.utils import android.os.Build import androidx.annotation.RequiresApi import androidx.core.content.edit -import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.onFailure +import com.github.michaelbull.result.runCatching import com.zeapo.pwdstore.Application import java.io.File import java.io.FileFilter @@ -24,7 +24,7 @@ import org.eclipse.jgit.transport.URIish import org.eclipse.jgit.util.FS import org.eclipse.jgit.util.FS_POSIX_Java6 -open class PasswordRepository protected constructor() { +object PasswordRepository { @RequiresApi(Build.VERSION_CODES.O) private class FS_POSIX_Java6_with_optional_symlinks : FS_POSIX_Java6() { @@ -51,191 +51,188 @@ open class PasswordRepository protected constructor() { } } - companion object { - - private var repository: Repository? = null - private val settings by lazy(LazyThreadSafetyMode.NONE) { Application.instance.sharedPrefs } - private val filesDir - get() = Application.instance.filesDir - - /** - * Returns the git repository - * - * @param localDir needed only on the creation - * @return the git repository - */ - @JvmStatic - fun getRepository(localDir: File?): Repository? { - if (repository == null && localDir != null) { - val builder = FileRepositoryBuilder() - repository = runCatching { - builder.run { - gitDir = localDir - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - fs = Java7FSFactory().detect(null) - } - readEnvironment() - }.build() - }.getOrElse { e -> - e.printStackTrace() - null - } + private var repository: Repository? = null + private val settings by lazy(LazyThreadSafetyMode.NONE) { Application.instance.sharedPrefs } + private val filesDir + get() = Application.instance.filesDir + + /** + * Returns the git repository + * + * @param localDir needed only on the creation + * @return the git repository + */ + @JvmStatic + fun getRepository(localDir: File?): Repository? { + if (repository == null && localDir != null) { + val builder = FileRepositoryBuilder() + repository = runCatching { + builder.run { + gitDir = localDir + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + fs = Java7FSFactory().detect(null) + } + readEnvironment() + }.build() + }.getOrElse { e -> + e.printStackTrace() + null } - return repository } + return repository + } - @JvmStatic - val isInitialized: Boolean - get() = repository != null + @JvmStatic + val isInitialized: Boolean + get() = repository != null - @JvmStatic - fun isGitRepo(): Boolean { - if (repository != null) { - return repository!!.objectDatabase.exists() - } - return false + @JvmStatic + fun isGitRepo(): Boolean { + if (repository != null) { + return repository!!.objectDatabase.exists() } + return false + } - @JvmStatic - @Throws(Exception::class) - fun createRepository(localDir: File) { - localDir.delete() + @JvmStatic + @Throws(Exception::class) + fun createRepository(localDir: File) { + localDir.delete() - Git.init().setDirectory(localDir).call() - getRepository(localDir) - } + Git.init().setDirectory(localDir).call() + getRepository(localDir) + } - // TODO add multiple remotes support for pull/push - @JvmStatic - fun addRemote(name: String, url: String, replace: Boolean = false) { - val storedConfig = repository!!.config - val remotes = storedConfig.getSubsections("remote") + // TODO add multiple remotes support for pull/push + @JvmStatic + fun addRemote(name: String, url: String, replace: Boolean = false) { + val storedConfig = repository!!.config + val remotes = storedConfig.getSubsections("remote") - if (!remotes.contains(name)) { - runCatching { - val uri = URIish(url) - val refSpec = RefSpec("+refs/head/*:refs/remotes/$name/*") + if (!remotes.contains(name)) { + runCatching { + val uri = URIish(url) + val refSpec = RefSpec("+refs/head/*:refs/remotes/$name/*") - val remoteConfig = RemoteConfig(storedConfig, name) - remoteConfig.addFetchRefSpec(refSpec) - remoteConfig.addPushRefSpec(refSpec) - remoteConfig.addURI(uri) - remoteConfig.addPushURI(uri) + val remoteConfig = RemoteConfig(storedConfig, name) + remoteConfig.addFetchRefSpec(refSpec) + remoteConfig.addPushRefSpec(refSpec) + remoteConfig.addURI(uri) + remoteConfig.addPushURI(uri) - remoteConfig.update(storedConfig) + remoteConfig.update(storedConfig) - storedConfig.save() - }.onFailure { e -> - e.printStackTrace() + storedConfig.save() + }.onFailure { e -> + e.printStackTrace() + } + } else if (replace) { + runCatching { + val uri = URIish(url) + + val remoteConfig = RemoteConfig(storedConfig, name) + // remove the first and eventually the only uri + if (remoteConfig.urIs.size > 0) { + remoteConfig.removeURI(remoteConfig.urIs[0]) + } + if (remoteConfig.pushURIs.size > 0) { + remoteConfig.removePushURI(remoteConfig.pushURIs[0]) } - } else if (replace) { - runCatching { - val uri = URIish(url) - - val remoteConfig = RemoteConfig(storedConfig, name) - // remove the first and eventually the only uri - if (remoteConfig.urIs.size > 0) { - remoteConfig.removeURI(remoteConfig.urIs[0]) - } - if (remoteConfig.pushURIs.size > 0) { - remoteConfig.removePushURI(remoteConfig.pushURIs[0]) - } - remoteConfig.addURI(uri) - remoteConfig.addPushURI(uri) + remoteConfig.addURI(uri) + remoteConfig.addPushURI(uri) - remoteConfig.update(storedConfig) + remoteConfig.update(storedConfig) - storedConfig.save() - }.onFailure { e -> - e.printStackTrace() - } + storedConfig.save() + }.onFailure { e -> + e.printStackTrace() } } + } - @JvmStatic - fun closeRepository() { - if (repository != null) repository!!.close() - repository = null + @JvmStatic + fun closeRepository() { + if (repository != null) repository!!.close() + repository = null + } + + @JvmStatic + fun getRepositoryDirectory(): File { + return if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) { + val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO) + if (externalRepo != null) + File(externalRepo) + else + File(filesDir.toString(), "/store") + } else { + File(filesDir.toString(), "/store") } + } - @JvmStatic - fun getRepositoryDirectory(): File { - return if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) { - val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO) - if (externalRepo != null) - File(externalRepo) - else - File(filesDir.toString(), "/store") + @JvmStatic + fun initialize(): Repository? { + val dir = getRepositoryDirectory() + // uninitialize the repo if the dir does not exist or is absolutely empty + settings.edit { + if (!dir.exists() || !dir.isDirectory || requireNotNull(dir.listFiles()).isEmpty()) { + putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) } else { - File(filesDir.toString(), "/store") + putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } } - @JvmStatic - fun initialize(): Repository? { - val dir = getRepositoryDirectory() - // uninitialize the repo if the dir does not exist or is absolutely empty - settings.edit { - if (!dir.exists() || !dir.isDirectory || requireNotNull(dir.listFiles()).isEmpty()) { - putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) - } else { - putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) - } - } + // create the repository static variable in PasswordRepository + return getRepository(File(dir.absolutePath + "/.git")) + } - // create the repository static variable in PasswordRepository - return getRepository(File(dir.absolutePath + "/.git")) - } + /** + * Gets the .gpg files in a directory + * + * @param path the directory path + * @return the list of gpg files in that directory + */ + @JvmStatic + fun getFilesList(path: File?): ArrayList<File> { + if (path == null || !path.exists()) return ArrayList() + + val directories = (path.listFiles(FileFilter { pathname -> pathname.isDirectory }) + ?: emptyArray()).toList() + val files = (path.listFiles(FileFilter { pathname -> pathname.extension == "gpg" }) + ?: emptyArray()).toList() + + val items = ArrayList<File>() + items.addAll(directories) + items.addAll(files) + + return items + } - /** - * Gets the .gpg files in a directory - * - * @param path the directory path - * @return the list of gpg files in that directory - */ - @JvmStatic - fun getFilesList(path: File?): ArrayList<File> { - if (path == null || !path.exists()) return ArrayList() - - val directories = (path.listFiles(FileFilter { pathname -> pathname.isDirectory }) - ?: emptyArray()).toList() - val files = (path.listFiles(FileFilter { pathname -> pathname.extension == "gpg" }) - ?: emptyArray()).toList() - - val items = ArrayList<File>() - items.addAll(directories) - items.addAll(files) - - return items + /** + * Gets the passwords (PasswordItem) in a directory + * + * @param path the directory path + * @return a list of password items + */ + @JvmStatic + fun getPasswords(path: File, rootDir: File, sortOrder: PasswordSortOrder): ArrayList<PasswordItem> { + // We need to recover the passwords then parse the files + val passList = getFilesList(path).also { it.sortBy { f -> f.name } } + val passwordList = ArrayList<PasswordItem>() + val showHidden = settings.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false) + + if (passList.size == 0) return passwordList + if (!showHidden) { + passList.filter { !it.isHidden }.toCollection(passList.apply { clear() }) } - - /** - * Gets the passwords (PasswordItem) in a directory - * - * @param path the directory path - * @return a list of password items - */ - @JvmStatic - fun getPasswords(path: File, rootDir: File, sortOrder: PasswordSortOrder): ArrayList<PasswordItem> { - // We need to recover the passwords then parse the files - val passList = getFilesList(path).also { it.sortBy { f -> f.name } } - val passwordList = ArrayList<PasswordItem>() - val showHidden = settings.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false) - - if (passList.size == 0) return passwordList - if (!showHidden) { - passList.filter { !it.isHidden }.toCollection(passList.apply { clear() }) - } - passList.forEach { file -> - passwordList.add(if (file.isFile) { - PasswordItem.newPassword(file.name, file, rootDir) - } else { - PasswordItem.newCategory(file.name, file, rootDir) - }) - } - passwordList.sortWith(sortOrder.comparator) - return passwordList + passList.forEach { file -> + passwordList.add(if (file.isFile) { + PasswordItem.newPassword(file.name, file, rootDir) + } else { + PasswordItem.newCategory(file.name, file, rootDir) + }) } + passwordList.sortWith(sortOrder.comparator) + return passwordList } } |
