-
Notifications
You must be signed in to change notification settings - Fork 51
/
MainActivityJava.java
151 lines (132 loc) · 4.59 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* 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.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
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;
import kotlin.Unit;
public class MainActivityJava extends AppCompatActivity {
private TextView result;
private TextView tokenResult;
private MaterialButton prepareButton;
private MaterialButton openButton;
private PlaidHandler plaidHandler = null;
private ActivityResultLauncher<PlaidHandler> linkAccountToPlaid = registerForActivityResult(
new FastOpenPlaidLink(),
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);
prepareButton = findViewById(R.id.prepare_link);
prepareButton.setOnClickListener(view -> {
setOptionalEventListener();
prepareLink();
});
openButton = findViewById(R.id.open_link);
openButton.setOnClickListener(view -> {
openLink();
});
}
private void prepareLink() {
LinkTokenRequester.INSTANCE.getToken()
.subscribe(this::onLinkTokenSuccess, this::onLinkTokenError);
}
/**
* 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() {
prepareButton.setEnabled(true);
openButton.setEnabled(false);
linkAccountToPlaid.launch(plaidHandler);
}
private void onLinkTokenSuccess(String token) {
prepareButton.setEnabled(false);
openButton.setEnabled(true);
LinkTokenConfiguration configuration = new LinkTokenConfiguration.Builder()
.token(token)
.build();
plaidHandler = Plaid.create(this.getApplication(), 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);
}
}
}