/
DataLoaderScreen.java
253 lines (231 loc) · 10.3 KB
/
DataLoaderScreen.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package javinator9889.bitcoinpools;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.firebase.crash.FirebaseCrash;
import com.google.firebase.perf.metrics.AddTrace;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import javax.net.ssl.HttpsURLConnection;
import javinator9889.bitcoinpools.JSONTools.JSONTools;
import static javinator9889.bitcoinpools.MainActivity.round;
/**
* Created by Javinator9889 on 04/03/2018.
* Based on: https://stackoverflow.com/questions/10115403/progressdialog-while-load-activity
*/
public class DataLoaderScreen extends AppCompatActivity {
public static AppCompatActivity dataLoaderScreenActivity;
private float mpu;
private HashMap<String, Float> retrievedData;
private HashMap<String, Float> cardsData;
private HashMap<Date, Float> btcPrice;
@Override
@AddTrace(name = "getApplicationValues")
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataLoaderScreenActivity = this;
setContentView(R.layout.activity_loading);
if (BitCoinApp.isOnline()) {
Log.d(Constants.LOG.MATAG, Constants.LOG.CREATING_MAINVIEW);
new DataLoader().execute();
} else {
new MaterialDialog.Builder(this)
.title(R.string.noConnectionTitle)
.content(R.string.noConnectionDesc)
.cancelable(false)
.positiveText(R.string.accept)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog,
@NonNull DialogAction which) {
onBackPressed();
}
})
.build()
.show();
}
}
@SuppressLint("StaticFieldLeak")
class DataLoader extends AsyncTask<Void, Void, Boolean> {
private Thread marketPriceThread;
private Thread poolsDataThread;
private Thread cardsDataThread;
private Thread btcPriceThread;
private Thread.UncaughtExceptionHandler threadExceptions = new Thread
.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
isAnyExceptionThrown = true;
Log.e("DataLoaderScreen", "Exception on thread: " + t.getName()
+ " | Message: " + e.getMessage());
FirebaseCrash.log("DataLoaderScreen. Exception on thread: " + t.getName()
+ " | Message: " + e.getMessage());
}
};
private boolean isAnyExceptionThrown = false;
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Boolean result) {
if (result && !isAnyExceptionThrown) {
Intent activityMainIntent = new Intent(DataLoaderScreen.this,
MainActivity.class);
activityMainIntent.putExtra("MPU", mpu);
activityMainIntent.putExtra("RD", retrievedData);
activityMainIntent.putExtra("CARDS", cardsData);
activityMainIntent.putExtra("BTCPRICE", btcPrice);
startActivity(activityMainIntent);
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
} else {
if (BitCoinApp.isOnline()) {
Log.d(Constants.LOG.MATAG, Constants.LOG.CREATING_MAINVIEW);
new DataLoader().execute();
} else {
new MaterialDialog.Builder(DataLoaderScreen.this)
.title(R.string.noConnectionTitle)
.content(R.string.noConnectionDesc)
.cancelable(false)
.positiveText(R.string.accept)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog,
@NonNull DialogAction which) {
onBackPressed();
}
})
.build()
.show();
}
Log.e("DataLoaderScreen", "An exception was thrown. Trying to obtain " +
"data again");
}
}
@Override
protected Boolean doInBackground(@Nullable Void... params) {
try {
getBitCoinMarketPrice();
getPoolsData();
getCardsData();
getBitCoinPriceHistory();
marketPriceThread.join();
poolsDataThread.join();
cardsDataThread.join();
btcPriceThread.join();
return true;
} catch (InterruptedException | DataLoaderException e) {
Log.e(Constants.LOG.MATAG, Constants.LOG.JOIN_ERROR + e.getMessage());
return false;
}
}
private JSONObject getHTTPSRequest(String requestUrl) throws Exception {
StringBuilder response = new StringBuilder();
URL urlObject = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) urlObject.openConnection();
connection.setRequestMethod("GET");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
response.append(line);
}
bufferedReader.close();
return new JSONObject(response.toString());
}
private void getBitCoinMarketPrice() throws DataLoaderException {
marketPriceThread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(Constants.LOG.MATAG, Constants.LOG.LOADING_MPU);
try {
mpu = round((float) getHTTPSRequest(Constants.STATS_URL)
.getDouble(Constants.MARKET_NAME), 2);
} catch (Exception e) {
Log.e(Constants.LOG.MATAG,
Constants.LOG.MARKET_PRICE_ERROR + e.getMessage());
mpu = -1;
throw new DataLoaderException(
"Failed to get data from: " + Constants.STATS_URL);
}
}
});
marketPriceThread.setUncaughtExceptionHandler(threadExceptions);
marketPriceThread.setName("MarketPriceThread");
marketPriceThread.start();
}
private void getPoolsData() {
poolsDataThread = new Thread(new Runnable() {
@Override
public void run() {
int days = BitCoinApp.getSharedPreferences()
.getInt(Constants.SHARED_PREFERENCES.DAYS_TO_CHECK, 1);
Log.d(Constants.LOG.MATAG, Constants.LOG.LOADING_RD);
String url = Constants.POOLS_URL + days + "days";
try {
retrievedData = JSONTools.convert2HashMap(getHTTPSRequest(url));
} catch (Exception e) {
retrievedData = null;
Log.e(Constants.LOG.MATAG, Constants.LOG.DATA_ERROR + e.getMessage());
throw new DataLoaderException("Unable to get data from: " + url);
}
}
});
poolsDataThread.setUncaughtExceptionHandler(threadExceptions);
poolsDataThread.setName("PoolsDataThread");
poolsDataThread.start();
}
private void getCardsData() {
cardsDataThread = new Thread(new Runnable() {
@Override
public void run() {
try {
cardsData = JSONTools.convert2HashMap(getHTTPSRequest(Constants.STATS_URL));
} catch (Exception e) {
cardsData = null;
Log.e(Constants.LOG.MATAG, Constants.LOG.DATA_ERROR + e.getMessage());
throw new DataLoaderException(
"Unable to get data from: " + Constants.STATS_URL);
}
}
});
cardsDataThread.setUncaughtExceptionHandler(threadExceptions);
cardsDataThread.setName("CardsDataThread");
cardsDataThread.start();
}
private void getBitCoinPriceHistory() {
btcPriceThread = new Thread(new Runnable() {
@Override
public void run() {
try {
btcPrice = JSONTools.convert2DateHashMap(getHTTPSRequest(Constants.API_URL)
.getJSONObject("bpi"));
} catch (Exception e) {
btcPrice = null;
Log.e(Constants.LOG.MATAG, Constants.LOG.DATA_ERROR + e.getMessage());
throw new DataLoaderException(
"Unable to get data from: " + Constants.API_URL);
}
}
});
btcPriceThread.setUncaughtExceptionHandler(threadExceptions);
btcPriceThread.setName("BtcPriceThread");
btcPriceThread.start();
}
}
class DataLoaderException extends RuntimeException {
DataLoaderException(@NonNull String message) {
super(message);
}
}
}