-
Notifications
You must be signed in to change notification settings - Fork 7
/
MainActivity.java
191 lines (138 loc) · 6.15 KB
/
MainActivity.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.criticalblue.currencyconverterdemo;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import com.criticalblue.currencyconverterdemo.approov.ActivityView;
import com.criticalblue.currencyconverterdemo.approov.ApproovTokenSingleton;
import com.criticalblue.currencyconverterdemo.approov.HttpRequest;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private EditText fromCurrency;
private EditText toCurrency;
private EditText currencyValueToConvert;
private TextView currencyConvertedValue;
private TextView errorMessage;
private static final String TAG = "CURRENCY_CONVERTER_DEMO";
// In a production app this url should not be stored in the code.
//private String apiBaseUrl = "https://free.currencyconverterapi.com/api/v6/convert?";
private String apiBaseUrl = "https://currency-converter-demo.pdm.approov.io/v2";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
private ApproovTokenSingleton approov;
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public void convertCurrency(View v) {
clearError();
clearCurrencyConvertedValue();
closeKeyboard();
getCurrencyConversion(
fromCurrency.getText().toString(),
toCurrency.getText().toString(),
currencyValueToConvert.getText().toString()
);
}
public void setCurrencyConvertedValue(String value) {
this.currencyConvertedValue.setText(value);
}
public void setErrorMessage(String message) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("APPROOV","MESSAGE: " + message);
errorMessage.setText(message);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
approov = ApproovTokenSingleton.getInstance(this);
setContentView(R.layout.activity_main);
this.fromCurrency = (AutoCompleteTextView) findViewById(R.id.from_currency_input);
this.fromCurrency.setSelection(this.fromCurrency.getText().length());
this.toCurrency = (AutoCompleteTextView) findViewById(R.id.to_currency_input);
this.toCurrency.setSelection(this.toCurrency.getText().length());
this.currencyValueToConvert = (AutoCompleteTextView) findViewById(R.id.currency_value_to_convert);
this.currencyValueToConvert.setSelection(this.currencyValueToConvert.getText().length());
this.currencyConvertedValue = (TextView) findViewById(R.id.currency_converted_value);
this.errorMessage = (TextView) findViewById(R.id.error_message);
}
private void clearError() {
this.errorMessage.setText("");
}
private void clearCurrencyConvertedValue() {
this.currencyConvertedValue.setText("");
}
private void closeKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(this.INPUT_METHOD_SERVICE);
View focusedView = getCurrentFocus();
if (focusedView != null) {
inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
private void getCurrencyConversion(String fromCurrency, String toCurrency, final String currencyValueToConvert) {
// Building the url with the api key retrieved from the native C++ code with stringFromJNI().
String url = this.apiBaseUrl + "/currency/convert/" + currencyValueToConvert + "/from/" + fromCurrency + "/to/" + toCurrency;
makeApiRequest(url);
}
private void makeApiRequest(String url) {
MainActivityView activityView = new MainActivityView();
HttpRequest httpRequest = new VolleyHttpRequest(activityView, stringFromJNI());
approov.fetchApproovTokenOnBackground(httpRequest, url, activityView);
//approov.fetchApproovTokenAndWait(httpRequest, url, activityView);
}
class MainActivityView implements ActivityView {
@Override
public Context getContext() {
return getApplicationContext();
}
@Override
public void handleErrorMessage(String message) {
setErrorMessage(message);
}
@Override
public void handleResponse(JSONObject response) {
if (response.length() <= 0) {
Log.e(TAG, "API SERVER EMPTY RESPONSE.");
setErrorMessage("Empty response from server... Please Check that the currency codes are correct !!!");
return;
}
try {
if (response.has("converted_value")) {
String currencyValueFormatted = response.getString("converted_value");
Log.i(TAG, "CURRENCY RATE: " + currencyValueFormatted);
setCurrencyConvertedValue(currencyValueFormatted);
return;
}
if (response.has("error")) {
String error = response.getString("error");
Log.e(TAG, error);
setErrorMessage(error);
return;
}
Log.e(TAG, "Unknown response from the API server.");
setErrorMessage("Unknown response from the API server !!!");
return;
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
// Don't return the exception message in a Production App, once it can leak sensitive
// information
setErrorMessage(e.getMessage());
return;
}
}
}
}