From 14b51d580899138bcebc539f45b7560bb1b82308 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 1 Oct 2020 20:46:21 +0530 Subject: Add key selection step to onboarding flow Signed-off-by: Harsh Shandilya --- .../onboarding/fragments/KeySelectionFragment.kt | 66 ++++++++++++++++++ .../onboarding/fragments/RepoLocationFragment.kt | 15 ++-- app/src/main/res/layout/fragment_key_selection.xml | 81 ++++++++++++++++++++++ app/src/main/res/values/strings.xml | 3 + 4 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/KeySelectionFragment.kt create mode 100644 app/src/main/res/layout/fragment_key_selection.xml (limited to 'app/src') diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/KeySelectionFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/KeySelectionFragment.kt new file mode 100644 index 00000000..66017379 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/KeySelectionFragment.kt @@ -0,0 +1,66 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package com.zeapo.pwdstore.ui.onboarding.fragments + +import android.content.Intent +import android.os.Bundle +import android.view.View +import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.edit +import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.crypto.GetKeyIdsActivity +import com.zeapo.pwdstore.databinding.FragmentKeySelectionBinding +import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys +import com.zeapo.pwdstore.utils.commitChange +import com.zeapo.pwdstore.utils.finish +import com.zeapo.pwdstore.utils.sharedPrefs +import com.zeapo.pwdstore.utils.viewBinding +import java.io.File +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import me.msfjarvis.openpgpktx.util.OpenPgpApi + +class KeySelectionFragment : Fragment(R.layout.fragment_key_selection) { + + private val settings by lazy { requireActivity().applicationContext.sharedPrefs } + private val binding by viewBinding(FragmentKeySelectionBinding::bind) + + private val gpgKeySelectAction = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == AppCompatActivity.RESULT_OK) { + result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> + lifecycleScope.launch { + withContext(Dispatchers.IO) { + val gpgIdentifierFile = File(PasswordRepository.getRepositoryDirectory(), ".gpg-id") + gpgIdentifierFile.writeText(keyIds.joinToString("\n")) + } + settings.edit { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } + requireActivity().commitChange(getString( + R.string.git_commit_gpg_id, + getString(R.string.app_name) + )) + } + } + } else { + throw IllegalStateException("Failed to initialize repository state.") + } + finish() + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.selectKey.setOnClickListener { gpgKeySelectAction.launch(Intent(requireContext(), GetKeyIdsActivity::class.java)) } + } + + companion object { + + fun newInstance() = KeySelectionFragment() + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt index 9e8a8f9d..2253dea8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt @@ -14,6 +14,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.edit import androidx.fragment.app.Fragment import com.github.ajalt.timberkt.d +import com.github.ajalt.timberkt.e import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.runCatching import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -26,14 +27,14 @@ import com.zeapo.pwdstore.utils.finish import com.zeapo.pwdstore.utils.getString import com.zeapo.pwdstore.utils.isPermissionGranted import com.zeapo.pwdstore.utils.listFilesRecursively +import com.zeapo.pwdstore.utils.performTransactionWithBackStack import com.zeapo.pwdstore.utils.sharedPrefs import com.zeapo.pwdstore.utils.viewBinding import java.io.File class RepoLocationFragment : Fragment(R.layout.fragment_repo_location) { - private val firstRunActivity by lazy { requireActivity() } - private val settings by lazy { firstRunActivity.applicationContext.sharedPrefs } + private val settings by lazy { requireActivity().applicationContext.sharedPrefs } private val binding by viewBinding(FragmentRepoLocationBinding::bind) private val sortOrder: PasswordRepository.PasswordSortOrder get() = PasswordRepository.PasswordSortOrder.getSortOrder(settings) @@ -151,18 +152,14 @@ class RepoLocationFragment : Fragment(R.layout.fragment_repo_location) { if (!PasswordRepository.isInitialized) { PasswordRepository.initialize() } - if (File(localDir.absolutePath + "/.gpg-id").createNewFile()) { - settings.edit { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } - } else { - throw IllegalStateException("Failed to initialize repository state.") - } + parentFragmentManager.performTransactionWithBackStack(KeySelectionFragment.newInstance()) }.onFailure { e -> - e.printStackTrace() + e(e) if (!localDir.delete()) { d { "Failed to delete local repository: $localDir" } } + finish() } - finish() } private fun initializeRepositoryInfo() { diff --git a/app/src/main/res/layout/fragment_key_selection.xml b/app/src/main/res/layout/fragment_key_selection.xml new file mode 100644 index 00000000..9a391276 --- /dev/null +++ b/app/src/main/res/layout/fragment_key_selection.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 57c32126..4ed3ee52 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -400,6 +400,9 @@ Select if you want to create a local repo or clone a remote repo. Clone Remote Repo Create Local Repo + Select\nGPG\nKey + Select a GPG key to initialize your store with + Select key Potentially incorrect URL -- cgit v1.2.3