From b94b52a42ddc2325b539d0956fd0adcf35308b52 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 17 Apr 2020 18:36:07 +0530 Subject: Refactor Git related activities (#685) * Refactor git logic into separate parts * Extract hardcoded strings * Add KDoc to updateHostname, remove unused field * Cleanups * Fix dialog message * Wire in repository clone flow * spotless * Remove unused method * Cleanup GitActivity - Rename to GitOperationActivity. - Ensure identityBuilder is always closed regardless of what fragment uses it. - Remove hardcoded "Operation" strings and replace with REQUEST_ARG_OP. - Apply a transparent theme to GitOperationActivity make the UI less jarring. * Tweak some stupidly worded dialog messages As pointed out in #629, these strings are shoddily worded and do not express any clear intent to the user, leaving them confused and angry. * GitOperationActivity: wrap Context to ensure right theme is used * spotless * undo build.gradle change * Use correct parent theme, remove now useless wrapping * GitServerConfigActivity: fix repository clone flow * temp: disable leakcanary framework leaks on Samsung are pissing me off * Make system bars transparent in git activity * Tweak HTTPS password layout * Unhardcode wrong passphrase string * Store SSH passphrase in EncryptedSharedPreferences Also revamp the dialog to look a bit better * Implement support for remembering HTTPS password Fixes #521 * Try to patch HTTPS remote creation logic * Update security-crypto * Clear saved passphrase/password on auth failure * Revert "Update security-crypto" Broken on R DP2.1 This reverts commit 4b20371dd42c512a3dd3b759859abb6c1ffd2961. * Revert "temp: disable leakcanary" This reverts commit 2db7d41bd67b79c6dc8c5b359a7b27100379f45f. * Update CHANGELOG * Remove spacer * Remove useless override * Wrap git server activity in a ScrollView * GitOperation: always finish calling activity when dialogs are dismissed * Wipe saved password/passphrase when hostname changes * Don't commit prefs updates * Don't call listFiles excessively * Finish activity after saving configuration * Make ConnectionMode and Protocol enum classes * Change SSH key passphrase key, don't wipe on host change * Reimplement BaseGitActivity.updateUrl (was updateHostname) * Use SharedPreferences.edit KTX extension * Disable inapplicable connection modes depending on scheme * BaseGitActivity: annotate onDestroy with CallSuper We'll leak the identityBuilder connection otherwise * Move input hack for AlertDialog into an extension function We re-use this in many places * Fix protocol/mode toggle issue and consistenly name options * Fix a crash when opening GitServerConfigActivity without a repo * Fix OpenKeychain callbacks by moving onActivityResult to BaseGitActivity * Run spotlessApply Signed-off-by: Harsh Shandilya Co-authored-by: Fabian Henneke --- .../java/com/zeapo/pwdstore/utils/Extensions.kt | 39 ++++++++++++++++++++++ .../com/zeapo/pwdstore/utils/PasswordRepository.kt | 14 ++------ 2 files changed, 41 insertions(+), 12 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/utils') 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 f7a637c3..6f4ebab1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -5,10 +5,18 @@ package com.zeapo.pwdstore.utils import android.content.Context +import android.content.SharedPreferences import android.os.Build import android.util.TypedValue +import android.view.View import android.view.autofill.AutofillManager +import android.view.inputmethod.InputMethodManager +import androidx.annotation.IdRes import androidx.annotation.RequiresApi +import androidx.appcompat.app.AlertDialog +import androidx.core.content.getSystemService +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys infix fun Int.hasFlag(flag: Int): Boolean { return this and flag == flag @@ -24,6 +32,37 @@ fun Context.resolveAttribute(attr: Int): Int { return typedValue.data } +fun Context.getEncryptedPrefs(fileName: String): SharedPreferences { + val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC + val masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec) + return EncryptedSharedPreferences.create( + fileName, + masterKeyAlias, + this, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) +} + +/** + * Extension function for [AlertDialog] that requests focus for the + * view whose id is [id]. Solution based on a StackOverflow + * answer: https://stackoverflow.com/a/13056259/297261 + */ +fun AlertDialog.requestInputFocusOnView(@IdRes id: Int) { + setOnShowListener { + findViewById(id)?.apply { + setOnFocusChangeListener { v, _ -> + v.post { + context.getSystemService() + ?.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT) + } + } + requestFocus() + } + } +} + val Context.autofillManager: AutofillManager? @RequiresApi(Build.VERSION_CODES.O) get() = getSystemService(AutofillManager::class.java) 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 1cf8fea0..2cc19ae3 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -95,7 +95,7 @@ open class PasswordRepository protected constructor() { // TODO add multiple remotes support for pull/push @JvmStatic - fun addRemote(name: String, url: String, replace: Boolean?) { + fun addRemote(name: String, url: String, replace: Boolean = false) { val storedConfig = repository!!.config val remotes = storedConfig.getSubsections("remote") @@ -116,7 +116,7 @@ open class PasswordRepository protected constructor() { } catch (e: Exception) { e.printStackTrace() } - } else if (replace!!) { + } else if (replace) { try { val uri = URIish(url) @@ -180,16 +180,6 @@ open class PasswordRepository protected constructor() { return getRepository(File(dir.absolutePath + "/.git")) } - /** - * Gets the password items in the root directory - * - * @return a list of passwords in the root directory - */ - @JvmStatic - fun getPasswords(rootDir: File, sortOrder: PasswordSortOrder): ArrayList { - return getPasswords(rootDir, rootDir, sortOrder) - } - /** * Gets the .gpg files in a directory * -- cgit v1.2.3