aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt
blob: f6f0b9a8484f051787f6f77f2a61f65424a68074 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
package com.zeapo.pwdstore.crypto

import android.app.PendingIntent
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.IntentSender
import android.content.SharedPreferences
import android.graphics.Typeface
import android.os.AsyncTask
import android.os.Bundle
import android.os.ConditionVariable
import android.os.Handler
import android.preference.PreferenceManager
import android.text.TextUtils
import android.text.format.DateUtils
import android.text.method.PasswordTransformationMethod
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.zeapo.pwdstore.PasswordEntry
import com.zeapo.pwdstore.PasswordGeneratorDialogFragment
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.UserPreference
import com.zeapo.pwdstore.utils.Otp
import kotlinx.android.synthetic.main.decrypt_layout.*
import kotlinx.android.synthetic.main.encrypt_layout.*
import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
import org.openintents.openpgp.IOpenPgpService2
import org.openintents.openpgp.OpenPgpError
import org.openintents.openpgp.util.OpenPgpApi
import org.openintents.openpgp.util.OpenPgpApi.ACTION_DECRYPT_VERIFY
import org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE
import org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_ERROR
import org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_SUCCESS
import org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED
import org.openintents.openpgp.util.OpenPgpApi.RESULT_ERROR
import org.openintents.openpgp.util.OpenPgpApi.RESULT_INTENT
import org.openintents.openpgp.util.OpenPgpServiceConnection
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.nio.charset.Charset
import java.util.Date

class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
    private val clipboard: ClipboardManager by lazy {
        getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    }
    private var passwordEntry: PasswordEntry? = null
    private var api: OpenPgpApi? = null

    private var editName: String? = null
    private var editPass: String? = null
    private var editExtra: String? = null

    private val operation: String by lazy { intent.getStringExtra("OPERATION") }
    private val repoPath: String by lazy { intent.getStringExtra("REPO_PATH") }

    private val fullPath: String by lazy { intent.getStringExtra("FILE_PATH") }
    private val name: String by lazy { getName(fullPath) }
    private val lastChangedString: CharSequence by lazy {
        getLastChangedString(
            intent.getIntExtra(
                "LAST_CHANGED_TIMESTAMP",
                -1
            )
        )
    }
    private val relativeParentPath: String by lazy { getParentPath(fullPath, repoPath) }

    private val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
    private val keyIDs: MutableSet<String> by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) }
    private var mServiceConnection: OpenPgpServiceConnection? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)

        // some persistence
        val providerPackageName = settings.getString("openpgp_provider_list", "")

        if (TextUtils.isEmpty(providerPackageName)) {
            Toast.makeText(this, this.resources.getString(R.string.provider_toast_text), Toast.LENGTH_LONG).show()
            val intent = Intent(this, UserPreference::class.java)
            startActivityForResult(intent, OPEN_PGP_BOUND)
        } else {
            // bind to service
            mServiceConnection = OpenPgpServiceConnection(this, providerPackageName, this)
            mServiceConnection?.bindToService()
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }

        when (operation) {
            "DECRYPT", "EDIT" -> {
                setContentView(R.layout.decrypt_layout)
                crypto_password_category_decrypt.text = relativeParentPath
                crypto_password_file.text = name

                crypto_password_last_changed.text = try {
                    this.resources.getString(R.string.last_changed, lastChangedString)
                } catch (e: RuntimeException) {
                    showToast(getString(R.string.get_last_changed_failed))
                    ""
                }
            }
            "ENCRYPT" -> {
                setContentView(R.layout.encrypt_layout)

                generate_password?.setOnClickListener {
                    PasswordGeneratorDialogFragment().show(supportFragmentManager, "generator")
                }

                title = getString(R.string.new_password_title)
                crypto_password_category.text = getRelativePath(fullPath, repoPath)
            }
        }
    }

    override fun onDestroy() {
        checkAndIncrementHotp()
        super.onDestroy()
        mServiceConnection?.unbindFromService()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        // Do not use the value `operation` in this case as it is not valid when editing
        val menuId = when (intent.getStringExtra("OPERATION")) {
            "ENCRYPT", "EDIT" -> R.menu.pgp_handler_new_password
            "DECRYPT" -> R.menu.pgp_handler
            else -> R.menu.pgp_handler
        }

        menuInflater.inflate(menuId, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> {
                if (passwordEntry?.hotpIsIncremented() == false) {
                    setResult(RESULT_CANCELED)
                }
                finish()
            }
            R.id.copy_password -> copyPasswordToClipBoard()
            R.id.share_password_as_plaintext -> shareAsPlaintext()
            R.id.edit_password -> editPassword()
            R.id.crypto_confirm_add -> encrypt()
            R.id.crypto_cancel_add -> {
                if (passwordEntry?.hotpIsIncremented() == false) {
                    setResult(RESULT_CANCELED)
                }
                finish()
            }
            else -> return super.onOptionsItemSelected(item)
        }
        return true
    }

    /**
     * Shows a simple toast message
     */
    private fun showToast(message: String) {
        runOnUiThread { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() }
    }

    /**
     * Handle the case where OpenKeychain returns that it needs to interact with the user
     *
     * @param result The intent returned by OpenKeychain
     * @param requestCode The code we'd like to use to identify the behaviour
     */
    private fun handleUserInteractionRequest(result: Intent, requestCode: Int) {
        Log.i(TAG, "RESULT_CODE_USER_INTERACTION_REQUIRED")

        val pi: PendingIntent = result.getParcelableExtra(RESULT_INTENT)
        try {
            this@PgpActivity.startIntentSenderFromChild(
                this@PgpActivity, pi.intentSender, requestCode,
                null, 0, 0, 0
            )
        } catch (e: IntentSender.SendIntentException) {
            Log.e(TAG, "SendIntentException", e)
        }
    }

    /**
     * Handle the error returned by OpenKeychain
     *
     * @param result The intent returned by OpenKeychain
     */
    private fun handleError(result: Intent) {
        // TODO show what kind of error it is
        /* For example:
         * No suitable key found -> no key in OpenKeyChain
         *
         * Check in open-pgp-lib how their definitions and error code
         */
        val error: OpenPgpError = result.getParcelableExtra(RESULT_ERROR)
        showToast("Error from OpenKeyChain : " + error.message)
        Log.e(TAG, "onError getErrorId:" + error.errorId)
        Log.e(TAG, "onError getMessage:" + error.message)
    }

    private fun initOpenPgpApi() {
        api = api ?: OpenPgpApi(this, mServiceConnection?.service)
    }

    private fun decryptAndVerify(receivedIntent: Intent? = null) {
        val data = receivedIntent ?: Intent()
        data.action = ACTION_DECRYPT_VERIFY

        val iStream = FileUtils.openInputStream(File(fullPath))
        val oStream = ByteArrayOutputStream()

        api?.executeApiAsync(data, iStream, oStream) { result: Intent? ->
            when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) {
                RESULT_CODE_SUCCESS -> {
                    try {
                        val showPassword = settings.getBoolean("show_password", true)
                        val showExtraContent = settings.getBoolean("show_extra_content", true)

                        crypto_container_decrypt.visibility = View.VISIBLE

                        val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf")
                        val entry = PasswordEntry(oStream)

                        passwordEntry = entry

                        if (intent.getStringExtra("OPERATION") == "EDIT") {
                            editPassword()
                            return@executeApiAsync
                        }

                        if (entry.password.isEmpty()) {
                            crypto_password_show.visibility = View.GONE
                            crypto_password_show_label.visibility = View.GONE
                        } else {
                            crypto_password_show.visibility = View.VISIBLE
                            crypto_password_show_label.visibility = View.VISIBLE
                            crypto_password_show.typeface = monoTypeface
                            crypto_password_show.text = entry.password
                        }
                        crypto_password_show.typeface = monoTypeface
                        crypto_password_show.text = entry.password

                        crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE
                        crypto_password_show.transformationMethod = if (showPassword) {
                            null
                        } else {
                            HoldToShowPasswordTransformation(
                                crypto_password_toggle_show,
                                Runnable { crypto_password_show.text = entry.password }
                            )
                        }

                        if (entry.hasExtraContent()) {
                            crypto_extra_show.typeface = monoTypeface
                            crypto_extra_show.text = entry.extraContent

                            if (showExtraContent) {
                                crypto_extra_show_layout.visibility = View.VISIBLE
                                crypto_extra_toggle_show.visibility = View.GONE
                                crypto_extra_show.transformationMethod = null
                            } else {
                                crypto_extra_show_layout.visibility = View.GONE
                                crypto_extra_toggle_show.visibility = View.VISIBLE
                                crypto_extra_toggle_show.setOnCheckedChangeListener { _, _ ->
                                    crypto_extra_show.text = entry.extraContent
                                }

                                crypto_extra_show.transformationMethod = object : PasswordTransformationMethod() {
                                    override fun getTransformation(source: CharSequence, view: View): CharSequence {
                                        return if (crypto_extra_toggle_show.isChecked) source else super.getTransformation(source, view)
                                    }
                                }
                            }

                            if (entry.hasUsername()) {
                                crypto_username_show.visibility = View.VISIBLE
                                crypto_username_show_label.visibility = View.VISIBLE
                                crypto_copy_username.visibility = View.VISIBLE

                                crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username!!) }
                                crypto_username_show.typeface = monoTypeface
                                crypto_username_show.text = entry.username
                            } else {
                                crypto_username_show.visibility = View.GONE
                                crypto_username_show_label.visibility = View.GONE
                                crypto_copy_username.visibility = View.GONE
                            }
                        }

                        if (entry.hasTotp() || entry.hasHotp()) {
                            crypto_extra_show_layout.visibility = View.VISIBLE
                            crypto_extra_show.typeface = monoTypeface
                            crypto_extra_show.text = entry.extraContent

                            crypto_otp_show.visibility = View.VISIBLE
                            crypto_otp_show_label.visibility = View.VISIBLE
                            crypto_copy_otp.visibility = View.VISIBLE

                            if (entry.hasTotp()) {
                                crypto_copy_otp.setOnClickListener {
                                    copyOtpToClipBoard(
                                        Otp.calculateCode(
                                            entry.totpSecret,
                                            Date().time / (1000 * entry.totpPeriod),
                                                entry.totpAlgorithm,
                                                entry.digits)
                                    )
                                }
                                crypto_otp_show.text =
                                    Otp.calculateCode(
                                            entry.totpSecret,
                                            Date().time / (1000 * entry.totpPeriod),
                                            entry.totpAlgorithm,
                                            entry.digits)
                            } else {
                                // we only want to calculate and show HOTP if the user requests it
                                crypto_copy_otp.setOnClickListener {
                                    if (settings.getBoolean("hotp_remember_check", false)) {
                                        if (settings.getBoolean("hotp_remember_choice", false)) {
                                            calculateAndCommitHotp(entry)
                                        } else {
                                            calculateHotp(entry)
                                        }
                                    } else {
                                        // show a dialog asking permission to update the HOTP counter in the entry
                                        val checkInflater = LayoutInflater.from(this)
                                        val checkLayout = checkInflater.inflate(R.layout.otp_confirm_layout, null)
                                        val rememberCheck: CheckBox =
                                            checkLayout.findViewById(R.id.hotp_remember_checkbox)
                                        val dialogBuilder = AlertDialog.Builder(this)
                                        dialogBuilder.setView(checkLayout)
                                        dialogBuilder.setMessage(R.string.dialog_update_body)
                                            .setCancelable(false)
                                            .setPositiveButton(R.string.dialog_update_positive) { _, _ ->
                                                run {
                                                    calculateAndCommitHotp(entry)
                                                    if (rememberCheck.isChecked) {
                                                        val editor = settings.edit()
                                                        editor.putBoolean("hotp_remember_check", true)
                                                        editor.putBoolean("hotp_remember_choice", true)
                                                        editor.apply()
                                                    }
                                                }
                                            }
                                            .setNegativeButton(R.string.dialog_update_negative) { _, _ ->
                                                run {
                                                    calculateHotp(entry)
                                                    val editor = settings.edit()
                                                    editor.putBoolean("hotp_remember_check", true)
                                                    editor.putBoolean("hotp_remember_choice", false)
                                                    editor.apply()
                                                }
                                            }
                                        val updateDialog = dialogBuilder.create()
                                        updateDialog.setTitle(R.string.dialog_update_title)
                                        updateDialog.show()
                                    }
                                }
                                crypto_otp_show.setText(R.string.hotp_pending)
                            }
                            crypto_otp_show.typeface = monoTypeface
                        } else {
                            crypto_otp_show.visibility = View.GONE
                            crypto_otp_show_label.visibility = View.GONE
                            crypto_copy_otp.visibility = View.GONE
                        }

                        if (settings.getBoolean("copy_on_decrypt", true)) {
                            copyPasswordToClipBoard()
                        }
                    } catch (e: Exception) {
                        Log.e(TAG, "An Exception occurred", e)
                    }
                }
                RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_DECRYPT)
                RESULT_CODE_ERROR -> handleError(result)
            }

        }
    }

    /**
     * Encrypts the password and the extra content
     */
    private fun encrypt() {
        // if HOTP was incremented, we leave fields as is; they have already been set
        if (intent.getStringExtra("OPERATION") != "INCREMENT") {
            editName = crypto_password_file_edit.text.toString().trim()
            editPass = crypto_password_edit.text.toString()
            editExtra = crypto_extra_edit.text.toString()
        }

        if (editName?.isEmpty() == true) {
            showToast(resources.getString(R.string.file_toast_text))
            return
        }

        if (editPass?.isEmpty() == true && editExtra?.isEmpty() == true) {
            showToast(resources.getString(R.string.empty_toast_text))
            return
        }

        val data = Intent()
        data.action = OpenPgpApi.ACTION_ENCRYPT

        // EXTRA_KEY_IDS requires long[]
        val longKeys = keyIDs.map { it.toLong() }
        data.putExtra(OpenPgpApi.EXTRA_KEY_IDS, longKeys.toLongArray())
        data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true)

        // TODO Check if we could use PasswordEntry to generate the file
        val iStream = ByteArrayInputStream("$editPass\n$editExtra".toByteArray(Charset.forName("UTF-8")))
        val oStream = ByteArrayOutputStream()

        val path = if (intent.getBooleanExtra("fromDecrypt", false)) fullPath else "$fullPath/$editName.gpg"

        api?.executeApiAsync(data, iStream, oStream) { result: Intent? ->
            when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                OpenPgpApi.RESULT_CODE_SUCCESS -> {
                    try {
                        // TODO This might fail, we should check that the write is successful
                        val outputStream = FileUtils.openOutputStream(File(path))
                        outputStream.write(oStream.toByteArray())
                        outputStream.close()

                        val returnIntent = Intent()
                        returnIntent.putExtra("CREATED_FILE", path)
                        returnIntent.putExtra("NAME", editName)
                        returnIntent.putExtra("LONG_NAME", getLongName(fullPath, repoPath, this.editName!!))

                        // if coming from decrypt screen->edit button
                        if (intent.getBooleanExtra("fromDecrypt", false)) {
                            returnIntent.putExtra("OPERATION", "EDIT")
                            returnIntent.putExtra("needCommit", true)
                        }
                        setResult(RESULT_OK, returnIntent)
                        finish()
                    } catch (e: Exception) {
                        Log.e(TAG, "An Exception occurred", e)
                    }
                }
                OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
            }

        }
    }

    /**
     * Opens EncryptActivity with the information for this file to be edited
     */
    private fun editPassword() {
        setContentView(R.layout.encrypt_layout)
        generate_password?.setOnClickListener {
            PasswordGeneratorDialogFragment().show(supportFragmentManager, "generator")
        }

        title = getString(R.string.edit_password_title)

        val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf")
        crypto_password_edit.setText(passwordEntry?.password)
        crypto_password_edit.typeface = monoTypeface
        crypto_extra_edit.setText(passwordEntry?.extraContent)
        crypto_extra_edit.typeface = monoTypeface

        crypto_password_category.text = relativeParentPath
        crypto_password_file_edit.setText(name)
        crypto_password_file_edit.isEnabled = false

        delayTask?.cancelAndSignal(true)

        val data = Intent(this, PgpActivity::class.java)
        data.putExtra("OPERATION", "EDIT")
        data.putExtra("fromDecrypt", true)
        intent = data
        invalidateOptionsMenu()
    }

    /**
     * Writes updated HOTP counter to edit fields and encrypts
     */
    private fun checkAndIncrementHotp() {
        // we do not want to increment the HOTP counter if the user has edited the entry or has not
        // generated an HOTP code
        if (intent.getStringExtra("OPERATION") != "EDIT" && passwordEntry?.hotpIsIncremented() == true) {
            editName = name.trim()
            editPass = passwordEntry?.password
            editExtra = passwordEntry?.extraContent

            val data = Intent(this, PgpActivity::class.java)
            data.putExtra("OPERATION", "INCREMENT")
            data.putExtra("fromDecrypt", true)
            intent = data
            encrypt()
        }
    }

    private fun calculateHotp(entry: PasswordEntry) {
        copyOtpToClipBoard(Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1, "sha1", entry.digits))
        crypto_otp_show.text = Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1, "sha1", entry.digits)
        crypto_extra_show.text = entry.extraContent
    }

    private fun calculateAndCommitHotp(entry: PasswordEntry) {
        calculateHotp(entry)
        entry.incrementHotp()
        // we must set the result before encrypt() is called, since in
        // some cases it is called during the finish() sequence
        val returnIntent = Intent()
        returnIntent.putExtra("NAME", name.trim())
        returnIntent.putExtra("OPERATION", "INCREMENT")
        returnIntent.putExtra("needCommit", true)
        setResult(RESULT_OK, returnIntent)
    }

    /**
     * Get the Key ids from OpenKeychain
     */
    private fun getKeyIds(receivedIntent: Intent? = null) {
        val data = receivedIntent ?: Intent()
        data.action = OpenPgpApi.ACTION_GET_KEY_IDS
        api?.executeApiAsync(data, null, null) { result: Intent? ->
            when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                OpenPgpApi.RESULT_CODE_SUCCESS -> {
                    try {
                        val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)
                        val keys = ids.map { it.toString() }.toSet()

                        // use Long
                        settings.edit().putStringSet("openpgp_key_ids_set", keys).apply()

                        showToast("PGP keys selected")

                        setResult(RESULT_OK)
                        finish()
                    } catch (e: Exception) {
                        Log.e(TAG, "An Exception occurred", e)
                    }
                }
                RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_KEY_ID)
                OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
            }
        }
    }

    override fun onError(e: Exception?) {}

    /**
     * The action to take when the PGP service is bound
     */
    override fun onBound(service: IOpenPgpService2?) {
        initOpenPgpApi()
        when (operation) {
            "EDIT", "DECRYPT" -> decryptAndVerify()
            "GET_KEY_ID" -> getKeyIds()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(TAG, "onActivityResult resultCode: $resultCode")

        if (data == null) {
            setResult(AppCompatActivity.RESULT_CANCELED, null)
            finish()
            return
        }

        // try again after user interaction
        if (resultCode == AppCompatActivity.RESULT_OK) {
            when (requestCode) {
                REQUEST_DECRYPT -> decryptAndVerify(data)
                REQUEST_KEY_ID -> getKeyIds(data)
                else -> {
                    setResult(AppCompatActivity.RESULT_OK)
                    finish()
                }
            }
        } else if (resultCode == AppCompatActivity.RESULT_CANCELED) {
            setResult(AppCompatActivity.RESULT_CANCELED, data)
            finish()
        }
    }

    private inner class HoldToShowPasswordTransformation constructor(button: Button, private val onToggle: Runnable) :
        PasswordTransformationMethod(), View.OnTouchListener {
        private var shown = false

        init {
            button.setOnTouchListener(this)
        }

        override fun getTransformation(charSequence: CharSequence, view: View): CharSequence {
            return if (shown) charSequence else super.getTransformation("12345", view)
        }

        @Suppress("ClickableViewAccessibility")
        override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
            when (motionEvent.action) {
                MotionEvent.ACTION_DOWN -> {
                    shown = true
                    onToggle.run()
                }
                MotionEvent.ACTION_UP -> {
                    shown = false
                    onToggle.run()
                }
            }
            return false
        }
    }

    private fun copyPasswordToClipBoard() {
        if (findViewById<TextView>(R.id.crypto_password_show) == null)
            return

        val clip = ClipData.newPlainText("pgp_handler_result_pm", passwordEntry?.password)
        clipboard.primaryClip = clip

        var clearAfter = 45
        try {
            clearAfter = Integer.parseInt(settings.getString("general_show_time", "45") as String)
        } catch (e: NumberFormatException) {
            // ignore and keep default
        }

        if (settings.getBoolean("clear_after_copy", true) && clearAfter != 0) {
            setTimer()
            showToast(this.resources.getString(R.string.clipboard_password_toast_text, clearAfter))
        } else {
            showToast(this.resources.getString(R.string.clipboard_password_no_clear_toast_text))
        }

    }

    private fun copyUsernameToClipBoard(username: String) {
        val clip = ClipData.newPlainText("pgp_handler_result_pm", username)
        clipboard.primaryClip = clip
        showToast(resources.getString(R.string.clipboard_username_toast_text))
    }

    private fun copyOtpToClipBoard(code: String) {
        val clip = ClipData.newPlainText("pgp_handler_result_pm", code)
        clipboard.primaryClip = clip
        showToast(resources.getString(R.string.clipboard_otp_toast_text))
    }

    private fun shareAsPlaintext() {
        if (findViewById<View>(R.id.share_password_as_plaintext) == null)
            return

        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.putExtra(Intent.EXTRA_TEXT, passwordEntry?.password)
        sendIntent.type = "text/plain"
        startActivity(
            Intent.createChooser(
                sendIntent,
                resources.getText(R.string.send_plaintext_password_to)
            )
        )//Always show a picker to give the user a chance to cancel
    }

    private fun setTimer() {

        // make sure to cancel any running tasks as soon as possible
        // if the previous task is still running, do not ask it to clear the password
        delayTask?.cancelAndSignal(true)

        // launch a new one
        delayTask = DelayShow(this)
        delayTask?.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
    }

    /**
     * Gets a relative string describing when this shape was last changed
     * (e.g. "one hour ago")
     */
    private fun getLastChangedString(timeStamp: Int): CharSequence {
        if (timeStamp < 0) {
            throw RuntimeException()
        }

        return DateUtils.getRelativeTimeSpanString(this, timeStamp.toLong() * 1000, true)
    }

    @Suppress("StaticFieldLeak")
    inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() {
        private val pb: ProgressBar by lazy { pbLoading }
        private var skip = false
        private var cancelNotify = ConditionVariable()

        private var showTime: Int = 0

        // Custom cancellation that can be triggered from another thread.
        //
        // This signals the DelayShow task to stop and avoids it having
        // to poll the AsyncTask.isCancelled() excessively. If skipClearing
        // is true, the cancelled task won't clear the clipboard.
        fun cancelAndSignal(skipClearing: Boolean) {
            skip = skipClearing
            cancelNotify.open()
        }

        val settings: SharedPreferences by lazy {
            PreferenceManager.getDefaultSharedPreferences(activity)
        }

        override fun onPreExecute() {
            showTime = try {
                Integer.parseInt(settings.getString("general_show_time", "45") as String)
            } catch (e: NumberFormatException) {
                45
            }

            val container = findViewById<LinearLayout>(R.id.crypto_container_decrypt)
            container.visibility = View.VISIBLE

            val extraText = findViewById<TextView>(R.id.crypto_extra_show)

            if (extraText.text.isNotEmpty())
                findViewById<View>(R.id.crypto_extra_show_layout).visibility = View.VISIBLE

            if (showTime == 0) {
                // treat 0 as forever, and the user must exit and/or clear clipboard on their own
                cancel(true)
            } else {
                this.pb.max = showTime
            }
        }

        override fun doInBackground(vararg params: Void): Boolean? {
            var current = 0
            while (current < showTime) {

                // Block for 1s or until cancel is signalled
                if (cancelNotify.block(1000)) {
                    Log.d("DELAY_SHOW", "Cancelled")
                    return true
                }

                current++
                publishProgress(current)
            }
            return true
        }

        override fun onPostExecute(b: Boolean?) {
            if (skip) return
            checkAndIncrementHotp()

            // No need to validate clear_after_copy. It was validated in copyPasswordToClipBoard()
            Log.d("DELAY_SHOW", "Clearing the clipboard")
            val clip = ClipData.newPlainText("pgp_handler_result_pm", "")
            clipboard.primaryClip = clip
            if (settings.getBoolean("clear_clipboard_20x", false)) {
                val handler = Handler()
                for (i in 0..19) {
                    val count = i.toString()
                    handler.postDelayed(
                        { clipboard.primaryClip = ClipData.newPlainText(count, count) },
                        (i * 500).toLong()
                    )
                }
            }

            if (crypto_password_show != null) {
                // clear password; if decrypt changed to encrypt layout via edit button, no need
                if (passwordEntry?.hotpIsIncremented() == false) {
                    setResult(AppCompatActivity.RESULT_CANCELED)
                }
                passwordEntry = null
                crypto_password_show.text = ""
                crypto_extra_show.text = ""
                crypto_extra_show_layout.visibility = View.INVISIBLE
                crypto_container_decrypt.visibility = View.INVISIBLE
                finish()
            }
        }

        override fun onProgressUpdate(vararg values: Int?) {
            this.pb.progress = values[0] ?: 0
        }
    }

    companion object {
        const val OPEN_PGP_BOUND = 101
        const val REQUEST_DECRYPT = 202
        const val REQUEST_KEY_ID = 203

        const val TAG = "PgpActivity"

        private var delayTask: DelayShow? = null

        /**
         * Gets the relative path to the repository
         */
        fun getRelativePath(fullPath: String, repositoryPath: String): String =
            fullPath.replace(repositoryPath, "").replace("/+".toRegex(), "/")

        /**
         * Gets the Parent path, relative to the repository
         */
        fun getParentPath(fullPath: String, repositoryPath: String): String {
            val relativePath = getRelativePath(fullPath, repositoryPath)
            val index = relativePath.lastIndexOf("/")
            return "/${relativePath.substring(startIndex = 0, endIndex = index + 1)}/".replace("/+".toRegex(), "/")
        }

        /**
         * Gets the name of the password (excluding .gpg)
         */
        fun getName(fullPath: String): String {
            return FilenameUtils.getBaseName(fullPath)
        }

        /**
         * /path/to/store/social/facebook.gpg -> social/facebook
         */
        @JvmStatic
        fun getLongName(fullPath: String, repositoryPath: String, basename: String): String {
            var relativePath = getRelativePath(fullPath, repositoryPath)
            return if (relativePath.isNotEmpty() && relativePath != "/") {
                // remove preceding '/'
                relativePath = relativePath.substring(1)
                if (relativePath.endsWith('/')) {
                    relativePath + basename
                } else {
                    "$relativePath/$basename"
                }
            } else {
                basename
            }
        }
    }
}