summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt
blob: 1003167f97040e824e90cbee7a7a6040226a9c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */
package com.zeapo.pwdstore

import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.os.Handler
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 : 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) {
                when (it) {
                    is BiometricAuthenticator.Result.Success -> {
                        startTargetActivity(false)
                    }
                    is BiometricAuthenticator.Result.HardwareUnavailableOrDisabled -> {
                        prefs.edit { remove(PreferenceKeys.BIOMETRIC_AUTH) }
                        startTargetActivity(false)
                    }
                    is BiometricAuthenticator.Result.Failure, BiometricAuthenticator.Result.Cancelled -> {
                        finish()
                    }
                }
            }
        } else {
            startTargetActivity(true)
        }
    }

    private fun startTargetActivity(noAuth: Boolean) {
        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)
            }
        }
    }

    private fun startPasswordStoreActivity(intent: Intent, noAuth: Boolean) {
        Handler().postDelayed({
            startActivity(intent)
            finish()
        }, if (noAuth) 500L else 0L)
    }

    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 {

        const val ACTION_DECRYPT_PASS = "DECRYPT_PASS"
    }
}