forked from plaid/plaid-link-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.kt
136 lines (119 loc) · 4.43 KB
/
MainActivity.kt
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
/*
* Copyright (c) 2020 Plaid Technologies, Inc. <support@plaid.com>
*/
package com.plaid.linksample
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import com.plaid.link.FastOpenPlaidLink
import com.plaid.link.Plaid
import com.plaid.link.PlaidHandler
import com.plaid.link.configuration.LinkTokenConfiguration
import com.plaid.link.result.LinkExit
import com.plaid.link.result.LinkSuccess
import com.plaid.linksample.network.LinkTokenRequester
class MainActivity : AppCompatActivity() {
private lateinit var result: TextView
private lateinit var tokenResult: TextView
private lateinit var prepareButton: MaterialButton
private lateinit var openButton: MaterialButton
private var plaidHandler: PlaidHandler? = null
private val linkAccountToPlaid = registerForActivityResult(FastOpenPlaidLink()) { result ->
when (result) {
is LinkSuccess -> showSuccess(result)
is LinkExit -> showFailure(result)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
result = findViewById(R.id.result)
tokenResult = findViewById(R.id.public_token_result)
prepareButton = findViewById(R.id.prepare_link)
prepareButton.setOnClickListener {
setOptionalEventListener()
prepareLink()
}
openButton = findViewById(R.id.open_link)
openButton.setOnClickListener {
openLink()
}
}
private fun prepareLink() {
LinkTokenRequester.token.subscribe(::onLinkTokenSuccess, ::onLinkTokenError)
}
/**
* Optional, set an [event listener](https://plaid.com/docs/link/android/#handling-onevent).
*/
private fun setOptionalEventListener() = Plaid.setLinkEventListener { event ->
Log.i("Event", event.toString())
}
/**
* For all Link configuration options, have a look at the
* [parameter reference](https://plaid.com/docs/link/android/#parameter-reference).
*/
private fun openLink() {
prepareButton.isEnabled = true
openButton.isEnabled = false
plaidHandler?.let { linkAccountToPlaid.launch(it) }
}
private fun onLinkTokenSuccess(linkToken: String) {
prepareButton.isEnabled = false
openButton.isEnabled = true
val tokenConfiguration = LinkTokenConfiguration.Builder()
.token(linkToken)
.build()
plaidHandler = Plaid.create(this.application, tokenConfiguration)
}
private fun onLinkTokenError(error: Throwable) {
if (error is java.net.ConnectException) {
Toast.makeText(this, "Please run `sh start_server.sh <client_id> <sandbox_secret>`", Toast.LENGTH_LONG).show()
return
}
Toast.makeText(this, error.message, Toast.LENGTH_SHORT).show()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean =
when (item.itemId) {
R.id.show_java -> {
val intent = Intent(this@MainActivity, MainActivityJava::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
true
}
R.id.show_activity_result -> {
val intent = Intent(this@MainActivity, MainActivityStartActivityForResult::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
true
}
R.id.show_activity_result_java -> {
val intent = Intent(this@MainActivity, MainActivityStartActivityForResultJava::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
true
}
else -> super.onOptionsItemSelected(item)
}
private fun showSuccess(success: LinkSuccess) {
tokenResult.text = getString(R.string.public_token_result, success.publicToken)
result.text = getString(R.string.content_success)
}
private fun showFailure(exit: LinkExit) {
tokenResult.text = ""
if (exit.error != null) {
result.text = getString(R.string.content_exit, exit.error?.displayMessage, exit.error?.errorCode)
} else {
result.text = getString(R.string.content_cancel, exit.metadata.status?.jsonValue ?: "unknown")
}
}
}