forked from plaid/plaid-link-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivityJava.java
141 lines (124 loc) · 4.19 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
/*
* 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.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.plaid.link.Plaid;
import com.plaid.link.configuration.LinkTokenConfiguration;
import com.plaid.link.result.LinkResultHandler;
import com.plaid.linksample.network.LinkTokenRequester;
import kotlin.Unit;
public class MainActivityJava extends AppCompatActivity {
private TextView result;
private TextView tokenResult;
private LinkResultHandler myPlaidResultHandler = new LinkResultHandler(
linkSuccess -> {
tokenResult.setText(getString(
R.string.public_token_result,
linkSuccess.getPublicToken()));
result.setText(getString(
R.string.content_success));
return Unit.INSTANCE;
},
linkExit -> {
tokenResult.setText("");
if (linkExit.getError() != null) {
result.setText(getString(
R.string.content_exit,
linkExit.getError().getDisplayMessage(),
linkExit.getError().getErrorCode()));
} else {
result.setText(getString(
R.string.content_cancel,
linkExit.getMetadata().getStatus() != null ? linkExit.getMetadata()
.getStatus()
.getJsonValue() : "unknown"));
}
return Unit.INSTANCE;
}
);
@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) {
Plaid.create(
getApplication(),
new LinkTokenConfiguration.Builder()
.token(token)
.build())
.open(this);
}
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
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (!myPlaidResultHandler.onActivityResult(requestCode, resultCode, data)) {
Log.i(MainActivityJava.class.getSimpleName(), "Not handled");
}
}
@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);
}
}
}