diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils')
3 files changed, 47 insertions, 34 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/ClipboardUtils.kt b/app/src/main/java/com/zeapo/pwdstore/utils/ClipboardUtils.kt deleted file mode 100644 index 2e408bfd..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/utils/ClipboardUtils.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -package com.zeapo.pwdstore.utils - -import android.content.ClipData -import android.content.ClipboardManager -import com.github.ajalt.timberkt.d -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext - -object ClipboardUtils { - - suspend fun clearClipboard(clipboard: ClipboardManager, deepClear: Boolean = false) { - d { "Clearing the clipboard" } - val clip = ClipData.newPlainText("pgp_handler_result_pm", "") - clipboard.setPrimaryClip(clip) - if (deepClear) { - withContext(Dispatchers.IO) { - repeat(20) { - val count = (it * 500).toString() - clipboard.setPrimaryClip(ClipData.newPlainText(count, count)) - } - } - } - } -} 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 d10bdaab..ff108b30 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -4,7 +4,10 @@ */ package com.zeapo.pwdstore.utils +import android.app.Activity +import android.content.ClipboardManager import android.content.Context +import android.content.Intent import android.content.SharedPreferences import android.os.Build import android.util.TypedValue @@ -16,7 +19,13 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog import androidx.core.content.getSystemService import androidx.security.crypto.EncryptedSharedPreferences -import androidx.security.crypto.MasterKeys +import androidx.security.crypto.MasterKey +import com.github.ajalt.timberkt.d +import com.google.android.material.snackbar.Snackbar +import com.zeapo.pwdstore.git.GitAsyncTask +import com.zeapo.pwdstore.git.GitOperation +import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory +import org.eclipse.jgit.api.Git import java.io.File infix fun Int.hasFlag(flag: Int): Boolean { @@ -33,6 +42,16 @@ fun CharArray.clear() { } } +val Context.clipboard get() = getSystemService<ClipboardManager>() + +fun Activity.snackbar( + view: View = findViewById(android.R.id.content), + message: String, + length: Int = Snackbar.LENGTH_SHORT +) { + Snackbar.make(view, message, length).show() +} + fun File.listFilesRecursively() = walkTopDown().filter { !it.isDirectory }.toList() fun Context.resolveAttribute(attr: Int): Int { @@ -42,17 +61,39 @@ fun Context.resolveAttribute(attr: Int): Int { } fun Context.getEncryptedPrefs(fileName: String): SharedPreferences { - val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC - val masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec) + val masterKeyAlias = MasterKey.Builder(applicationContext) + .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) + .build() return EncryptedSharedPreferences.create( + applicationContext, fileName, masterKeyAlias, - this, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) } +fun Activity.commitChange(message: String, finishWithResultOnEnd: Intent? = null) { + if (!PasswordRepository.isGitRepo()) { + if (finishWithResultOnEnd != null) { + setResult(Activity.RESULT_OK, finishWithResultOnEnd) + finish() + } + return + } + object : GitOperation(getRepositoryDirectory(this@commitChange), this@commitChange) { + override fun execute() { + d { "Comitting with message: '$message'" } + val git = Git(repository) + val task = GitAsyncTask(this@commitChange, true, this, finishWithResultOnEnd, silentlyExecute = true) + task.execute( + git.add().addFilepattern("."), + git.commit().setAll(true).setMessage(message) + ) + } + }.execute() +} + /** * Extension function for [AlertDialog] that requests focus for the * view whose id is [id]. Solution based on a StackOverflow diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.kt index 903f6402..5ca95d31 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.kt @@ -4,7 +4,7 @@ */ package com.zeapo.pwdstore.utils -import com.zeapo.pwdstore.crypto.PgpActivity +import com.zeapo.pwdstore.crypto.BasePgpActivity import java.io.File data class PasswordItem( @@ -19,7 +19,7 @@ data class PasswordItem( .replace(rootDir.absolutePath, "") .replace(file.name, "") - val longName = PgpActivity.getLongName( + val longName = BasePgpActivity.getLongName( fullPathToParent, rootDir.absolutePath, toString()) |
