diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt | 49 |
1 files changed, 45 insertions, 4 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 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 |
