forked from plaid/plaid-link-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivityJava.java
133 lines (117 loc) · 4.04 KB
/
MainActivityJava.java
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
/*
* Copyright (c) 2020 Plaid Technologies, Inc. <[email protected]>
*/
package com.plaid.linksample;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.plaid.link.OpenPlaidLink;
import com.plaid.link.Plaid;
import com.plaid.link.configuration.LinkTokenConfiguration;
import com.plaid.link.result.LinkExit;
import com.plaid.link.result.LinkSuccess;
import com.plaid.linksample.network.LinkTokenRequester;
import kotlin.Unit;
public class MainActivityJava extends AppCompatActivity {
private TextView result;
private TextView tokenResult;
private ActivityResultLauncher<LinkTokenConfiguration> linkAccountToPlaid = registerForActivityResult(
new OpenPlaidLink(),
result -> {
if (result instanceof LinkSuccess) {
showSuccess((LinkSuccess) result);
} else {
showFailure((LinkExit) result);
}
});
private void showSuccess(LinkSuccess success) {
tokenResult.setText(getString(R.string.public_token_result, success.getPublicToken()));
result.setText(getString(R.string.content_success));
}
private void showFailure(LinkExit exit) {
tokenResult.setText("");
if (exit.getError() != null) {
result.setText(getString(
R.string.content_exit,
exit.getError().getDisplayMessage(),
exit.getError().getErrorCode()));
} else {
result.setText(getString(
R.string.content_cancel,
exit.getMetadata().getStatus() != null ? exit.getMetadata().getStatus().getJsonValue() : "unknown"));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
tokenResult = findViewById(R.id.public_token_result);
View button = findViewById(R.id.open_link);
button.setOnClickListener(view -> {
setOptionalEventListener();
openLink();
});
}
/**
* Optional, set an <a href="https://plaid.com/docs/link/android/#handling-onevent">event listener</a>.
*/
private void setOptionalEventListener() {
Plaid.setLinkEventListener(linkEvent -> {
Log.i("Event", linkEvent.toString());
return Unit.INSTANCE;
});
}
/**
* For all Link configuration options, have a look at the
* <a href="https://plaid.com/docs/link/android/#parameter-reference">parameter reference</>
*/
private void openLink() {
LinkTokenRequester.INSTANCE.getToken()
.subscribe(this::onLinkTokenSuccess, this::onLinkTokenError);
}
private void onLinkTokenSuccess(String token) {
LinkTokenConfiguration configuration = new LinkTokenConfiguration.Builder()
.token(token)
.build();
linkAccountToPlaid.launch(configuration);
}
private void onLinkTokenError(Throwable error) {
if (error instanceof 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.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_java, menu);
return true;
}
@SuppressWarnings("SwitchStatementWithTooFewBranches")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.show_kotlin:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}