From 92f1aab25da12411aa256c3b763e0cb6182e9320 Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Thu, 15 Oct 2020 20:46:39 +0530 Subject: Add option to automatically sync repository on app launch (#1137) * Add option to automatically sync repository on app launch Signed-off-by: Aditya Wasan * Pull repo if user is not authenticated Signed-off-by: Aditya Wasan * Address review comments Signed-off-by: Harsh Shandilya * Hijack incoming intent rather than copying it Signed-off-by: Harsh Shandilya * Initialize password repository before using it Signed-off-by: Aditya Wasan Co-authored-by: Harsh Shandilya --- app/src/main/AndroidManifest.xml | 3 +- .../main/java/com/zeapo/pwdstore/LaunchActivity.kt | 58 ++++++++++++++++----- .../main/java/com/zeapo/pwdstore/UserPreference.kt | 3 ++ .../com/zeapo/pwdstore/utils/PreferenceKeys.kt | 1 + app/src/main/res/layout/activity_launch.xml | 59 ++++++++++++++++++++++ app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/preference.xml | 6 +++ 7 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/layout/activity_launch.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6cad066e..a0a0e464 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -42,8 +42,7 @@ + android:label="@string/app_name"> diff --git a/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt b/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt index 268698e7..1003167f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt @@ -4,20 +4,34 @@ */ package com.zeapo.pwdstore +import android.content.ComponentName import android.content.Intent import android.os.Bundle import android.os.Handler -import androidx.appcompat.app.AppCompatActivity import androidx.core.content.edit +import androidx.core.view.isVisible +import androidx.lifecycle.lifecycleScope +import com.github.michaelbull.result.fold import com.zeapo.pwdstore.crypto.DecryptActivity +import com.zeapo.pwdstore.databinding.ActivityLaunchBinding +import com.zeapo.pwdstore.git.BaseGitActivity +import com.zeapo.pwdstore.git.config.AuthMode +import com.zeapo.pwdstore.git.config.GitSettings import com.zeapo.pwdstore.utils.BiometricAuthenticator +import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.sharedPrefs +import com.zeapo.pwdstore.utils.viewBinding +import kotlinx.coroutines.launch -class LaunchActivity : AppCompatActivity() { +class LaunchActivity : BaseGitActivity() { + + private val binding by viewBinding(ActivityLaunchBinding::inflate) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + setContentView(binding.root) + supportActionBar?.hide() val prefs = sharedPrefs if (prefs.getBoolean(PreferenceKeys.BIOMETRIC_AUTH, false)) { BiometricAuthenticator.authenticate(this) { @@ -40,18 +54,38 @@ class LaunchActivity : AppCompatActivity() { } private fun startTargetActivity(noAuth: Boolean) { - val intentToStart = if (intent.action == ACTION_DECRYPT_PASS) - Intent(this, DecryptActivity::class.java).apply { - putExtra("NAME", intent.getStringExtra("NAME")) - putExtra("FILE_PATH", intent.getStringExtra("FILE_PATH")) - putExtra("REPO_PATH", intent.getStringExtra("REPO_PATH")) - putExtra("LAST_CHANGED_TIMESTAMP", intent.getLongExtra("LAST_CHANGED_TIMESTAMP", 0L)) + if (intent.action == ACTION_DECRYPT_PASS) { + intent.component = ComponentName(this, DecryptActivity::class.java) + startPasswordStoreActivity(intent, noAuth) + } else { + val intent = Intent(this, PasswordStore::class.java) + val repo = PasswordRepository.initialize() + // if repo is null, let PasswordStore handle it. + if (repo != null && + !GitSettings.url.isNullOrEmpty() && + sharedPrefs.getBoolean(PreferenceKeys.SYNC_ON_LAUNCH, false)) { + binding.progressBar.isVisible = true + binding.sync.isVisible = true + runGitOperation { startPasswordStoreActivity(intent, noAuth) } + } else { + startPasswordStoreActivity(intent, noAuth) } - else - Intent(this, PasswordStore::class.java) - startActivity(intentToStart) + } + } + + private fun startPasswordStoreActivity(intent: Intent, noAuth: Boolean) { + Handler().postDelayed({ + startActivity(intent) + finish() + }, if (noAuth) 500L else 0L) + } - Handler().postDelayed({ finish() }, if (noAuth) 0L else 500L) + private fun runGitOperation(onCompletion: () -> Unit) = lifecycleScope.launch { + val gitOp = if (GitSettings.authMode == AuthMode.None) GitOp.PULL else GitOp.SYNC + launchGitOperation(gitOp).fold( + success = { onCompletion.invoke() }, + failure = { promptOnErrorHandler(it) { onCompletion.invoke() } }, + ) } companion object { diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 7f6727f2..6edd9ceb 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -46,6 +46,7 @@ import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity import com.zeapo.pwdstore.crypto.BasePgpActivity import com.zeapo.pwdstore.git.GitConfigActivity import com.zeapo.pwdstore.git.GitServerConfigActivity +import com.zeapo.pwdstore.git.config.GitSettings import com.zeapo.pwdstore.git.sshj.SshKey import com.zeapo.pwdstore.pwgenxkpwd.XkpwdDictionary import com.zeapo.pwdstore.sshkeygen.ShowSshKeyFragment @@ -424,6 +425,8 @@ class UserPreference : AppCompatActivity() { true } + findPreference(PreferenceKeys.SYNC_ON_LAUNCH)?.isVisible = !GitSettings.url.isNullOrEmpty() + val prefCustomXkpwdDictionary = findPreference(PreferenceKeys.PREF_KEY_CUSTOM_DICT) prefCustomXkpwdDictionary?.onPreferenceClickListener = ClickListener { prefsActivity.storeCustomDictionaryPath() diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt index 5ec40639..33130f78 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt @@ -77,6 +77,7 @@ object PreferenceKeys { const val SSH_OPENKEYSTORE_CLEAR_KEY_ID = "ssh_openkeystore_clear_keyid" const val SSH_OPENKEYSTORE_KEYID = "ssh_openkeystore_keyid" const val SSH_SEE_KEY = "ssh_see_key" + const val SYNC_ON_LAUNCH = "sync_on_launch" @Deprecated("To be used only in Migrations.kt") const val USE_GENERATED_KEY = "use_generated_key" diff --git a/app/src/main/res/layout/activity_launch.xml b/app/src/main/res/layout/activity_launch.xml new file mode 100644 index 00000000..df6e4f7b --- /dev/null +++ b/app/src/main/res/layout/activity_launch.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc9f1f24..f8be3859 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -407,6 +407,9 @@ Potentially incorrect URL It appears that your URL contains a custom port, but does not specify the ssh:// scheme.\nThis can cause the port to be considered a part of your path. Press OK here to fix the URL. + Sync on launch + Sync passwords when application is launched + Syncing… Proxy hostname diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 58a173c5..6631133f 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -138,6 +138,12 @@ app:key="biometric_auth" app:summary="@string/biometric_auth_summary" app:title="@string/biometric_auth_title" /> +