aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/crypto/GetKeyIdsActivity.kt
blob: b6a3e9552deadfa5d5244a9c976b00416e2155e6 (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
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package com.zeapo.pwdstore.crypto

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts.StartIntentSenderForResult
import androidx.lifecycle.lifecycleScope
import com.github.ajalt.timberkt.e
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.runCatching
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.msfjarvis.openpgpktx.util.OpenPgpApi
import me.msfjarvis.openpgpktx.util.OpenPgpUtils
import org.openintents.openpgp.IOpenPgpService2

class GetKeyIdsActivity : BasePgpActivity() {

    private val userInteractionRequiredResult = registerForActivityResult(StartIntentSenderForResult()) { result ->
        if (result.data == null || result.resultCode == RESULT_CANCELED) {
            setResult(RESULT_CANCELED, result.data)
            finish()
            return@registerForActivityResult
        }
        getKeyIds(result.data!!)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindToOpenKeychain(this)
    }

    override fun onBound(service: IOpenPgpService2) {
        super.onBound(service)
        getKeyIds()
    }

    override fun onError(e: Exception) {
        e(e)
    }

    /**
     * Get the Key ids from OpenKeychain
     */
    private fun getKeyIds(data: Intent = Intent()) {
        data.action = OpenPgpApi.ACTION_GET_KEY_IDS
        lifecycleScope.launch(Dispatchers.IO) {
            api?.executeApiAsync(data, null, null) { result ->
                when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                    OpenPgpApi.RESULT_CODE_SUCCESS -> {
                        runCatching {
                            val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map {
                                OpenPgpUtils.convertKeyIdToHex(it)
                            } ?: emptyList()
                            val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray())
                            setResult(RESULT_OK, keyResult)
                            finish()
                        }.onFailure { e ->
                            e(e)
                        }
                    }
                    OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
                        val sender = getUserInteractionRequestIntent(result)
                        userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
                    }
                    OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
                }
            }
        }
    }
}