|
| 1 | +/* |
| 2 | + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: GPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package com.zeapo.pwdstore.crypto |
| 7 | + |
| 8 | +import android.content.Intent |
| 9 | +import android.os.Bundle |
| 10 | +import androidx.activity.result.IntentSenderRequest |
| 11 | +import androidx.activity.result.contract.ActivityResultContracts.StartIntentSenderForResult |
| 12 | +import androidx.lifecycle.lifecycleScope |
| 13 | +import com.github.ajalt.timberkt.e |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.launch |
| 16 | +import me.msfjarvis.openpgpktx.util.OpenPgpApi |
| 17 | +import me.msfjarvis.openpgpktx.util.OpenPgpUtils |
| 18 | +import org.openintents.openpgp.IOpenPgpService2 |
| 19 | + |
| 20 | +class GetKeyIdsActivity : BasePgpActivity() { |
| 21 | + |
| 22 | + private val userInteractionRequiredResult = registerForActivityResult(StartIntentSenderForResult()) { result -> |
| 23 | + if (result.data == null || result.resultCode == RESULT_CANCELED) { |
| 24 | + setResult(RESULT_CANCELED, result.data) |
| 25 | + finish() |
| 26 | + return@registerForActivityResult |
| 27 | + } |
| 28 | + getKeyIds(result.data!!) |
| 29 | + } |
| 30 | + |
| 31 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 32 | + super.onCreate(savedInstanceState) |
| 33 | + bindToOpenKeychain(this) |
| 34 | + } |
| 35 | + |
| 36 | + override fun onBound(service: IOpenPgpService2) { |
| 37 | + super.onBound(service) |
| 38 | + getKeyIds() |
| 39 | + } |
| 40 | + |
| 41 | + override fun onError(e: Exception) { |
| 42 | + e(e) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the Key ids from OpenKeychain |
| 47 | + */ |
| 48 | + private fun getKeyIds(data: Intent = Intent()) { |
| 49 | + data.action = OpenPgpApi.ACTION_GET_KEY_IDS |
| 50 | + lifecycleScope.launch(Dispatchers.IO) { |
| 51 | + api?.executeApiAsync(data, null, null) { result -> |
| 52 | + when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { |
| 53 | + OpenPgpApi.RESULT_CODE_SUCCESS -> { |
| 54 | + try { |
| 55 | + val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map { |
| 56 | + OpenPgpUtils.convertKeyIdToHex(it) |
| 57 | + } ?: emptyList() |
| 58 | + val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray()) |
| 59 | + setResult(RESULT_OK, keyResult) |
| 60 | + finish() |
| 61 | + } catch (e: Exception) { |
| 62 | + e(e) |
| 63 | + } |
| 64 | + } |
| 65 | + OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> { |
| 66 | + val sender = getUserInteractionRequestIntent(result) |
| 67 | + userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build()) |
| 68 | + } |
| 69 | + OpenPgpApi.RESULT_CODE_ERROR -> handleError(result) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments