aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.kt
blob: a64cdcedbfd5aafa6b95582d794d46e3b44d0ff7 (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
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */
@file:Suppress("Deprecation")
package com.zeapo.pwdstore.autofill

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentSender
import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.e
import com.zeapo.pwdstore.PasswordStore
import com.zeapo.pwdstore.utils.splitLines
import org.eclipse.jgit.util.StringUtils

// blank activity started by service for calling startIntentSenderForResult
class AutofillActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val extras = intent.extras

        if (extras != null && extras.containsKey("pending_intent")) {
            try {
                val pi = extras.getParcelable<PendingIntent>("pending_intent") ?: return
                startIntentSenderForResult(pi.intentSender, REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0)
            } catch (e: IntentSender.SendIntentException) {
                tag(AutofillService.Constants.TAG).e(e) { "SendIntentException" }
            }
        } else if (extras != null && extras.containsKey("pick")) {
            val intent = Intent(applicationContext, PasswordStore::class.java)
            intent.putExtra("matchWith", true)
            startActivityForResult(intent, REQUEST_CODE_PICK)
        } else if (extras != null && extras.containsKey("pickMatchWith")) {
            val intent = Intent(applicationContext, PasswordStore::class.java)
            intent.putExtra("matchWith", true)
            startActivityForResult(intent, REQUEST_CODE_PICK_MATCH_WITH)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        finish() // go back to the password field app
        when (requestCode) {
            REQUEST_CODE_DECRYPT_AND_VERIFY -> if (resultCode == RESULT_OK) {
                require(data != null)
                AutofillService.instance?.setResultData(data) // report the result to service
            }
            REQUEST_CODE_PICK -> if (resultCode == RESULT_OK) {
                require(data != null)
                AutofillService.instance?.setPickedPassword(data.getStringExtra("path")!!)
            }
            REQUEST_CODE_PICK_MATCH_WITH -> if (resultCode == RESULT_OK) {
                require(data != null)
                // need to not only decrypt the picked password, but also
                // update the "match with" preference
                val extras = intent.extras ?: return
                val packageName = extras.getString("packageName")
                val isWeb = extras.getBoolean("isWeb")

                val path = data.getStringExtra("path")
                AutofillService.instance?.setPickedPassword(data.getStringExtra("path")!!)

                val prefs: SharedPreferences
                prefs = if (!isWeb) {
                    applicationContext.getSharedPreferences("autofill", Context.MODE_PRIVATE)
                } else {
                    applicationContext.getSharedPreferences("autofill_web", Context.MODE_PRIVATE)
                }
                prefs.edit {
                    when (val preference = prefs.getString(packageName, "")) {
                        "", "/first", "/never" -> putString(packageName, path)
                        else -> {
                            val matches = arrayListOf(*preference!!.trim { it <= ' ' }.splitLines())
                            matches.add(path)
                            val paths = StringUtils.join(matches, "\n")
                            putString(packageName, paths)
                        }
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    companion object {
        const val REQUEST_CODE_DECRYPT_AND_VERIFY = 9913
        const val REQUEST_CODE_PICK = 777
        const val REQUEST_CODE_PICK_MATCH_WITH = 778
    }
}