forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.java
More file actions
211 lines (190 loc) · 7.33 KB
/
Utility.java
File metadata and controls
211 lines (190 loc) · 7.33 KB
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
package com.example.nitishkumar.currencyconverter;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
class Utility {
public static SharedPreferences sharedPref;
private static final String LOG_TAG = "Hello";
private Utility() {
}
static List<Country> getCountries(String requestUrl) {
URL url = createUrl(requestUrl);
String jsonResponse = "";
try {
jsonResponse = httpRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making http request");
}
SharedPreferences exchangeRates = new Utility().updateSharedPrefrence(jsonResponse);
ArrayList<Country> countries = fetchCountriesFromPref(exchangeRates);
return countries;
}
private static ArrayList<Country> fetchCountriesFromPref(SharedPreferences exchangeRates) {
Map<String, String> rateKeys = (Map<String, String>) exchangeRates.getAll();
ArrayList<Country> countries = new ArrayList<>();
for (Map.Entry<String, String> entry : rateKeys.entrySet()) {
// Log.e(LOG_TAG,entry.getKey()+":"+Double.parseDouble(entry.getValue())+"");
countries.add(new Country(MainActivity.countryCodes.get(entry.getKey()), Double.parseDouble(entry.getValue())));
}
setFlags(countries);
return countries;
}
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Malformed URL", e);
}
return url;
}
private static String httpRequest(URL url) throws IOException {
String jsonResult = "";
if (url == null)
return jsonResult;
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getInputStream();
jsonResult = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code:" + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "error fetching result", e);
} finally {
if (urlConnection != null)
urlConnection.disconnect();
if (inputStream != null) {
inputStream.close();
}
}
return jsonResult;
}
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader buffreader = new BufferedReader(reader);
String line = buffreader.readLine();
while (line != null) {
output.append(line);
line = buffreader.readLine();
}
}
return output.toString();
}
private static void setFlags(ArrayList<Country> flags) {
Country c = flags.get(0);
c.setmImageResId(R.drawable._croatia);
c = flags.get(1);
c.setmImageResId(R.drawable._poland);
c = flags.get(2);
c.setmImageResId(R.drawable._australia);
c = flags.get(3);
c.setmImageResId(R.drawable._india);
c = flags.get(4);
c.setmImageResId(R.drawable._japan);
c = flags.get(5);
c.setmImageResId(R.drawable._indonesia);
c = flags.get(6);
c.setmImageResId(R.drawable.euro);
c = flags.get(7);
c.setmImageResId(R.drawable._bulgaria);
c = flags.get(8);
c.setmImageResId(R.drawable._thisreal);
c = flags.get(9);
c.setmImageResId(R.drawable._unitedkingdom);
c = flags.get(10);
c.setmImageResId(R.drawable._singapore);
c = flags.get(11);
c.setmImageResId(R.drawable._philippines);
c = flags.get(12);
c.setmImageResId(R.drawable._newzealand);
c = flags.get(13);
c.setmImageResId(R.drawable._denmark);
c = flags.get(14);
c.setmImageResId(R.drawable._czechrepublic);
c = flags.get(15);
c.setmImageResId(R.drawable._china);
c = flags.get(16);
c.setmImageResId(R.drawable._hungary);
c = flags.get(17);
c.setmImageResId(R.drawable._this);
c = flags.get(18);
c.setmImageResId(R.drawable._turkey);
c = flags.get(19);
c.setmImageResId(R.drawable._russia);
c = flags.get(20);
c.setmImageResId(R.drawable._koreouth);
c = flags.get(21);
c.setmImageResId(R.drawable._norway);
c = flags.get(22);
c.setmImageResId(R.drawable._switzerland);
c = flags.get(23);
c.setmImageResId(R.drawable._brazil);
c = flags.get(24);
c.setmImageResId(R.drawable._mexico);
c = flags.get(25);
c.setmImageResId(R.drawable._hongkong);
c = flags.get(26);
c.setmImageResId(R.drawable._romania);
c = flags.get(27);
c.setmImageResId(R.drawable._canada);
c = flags.get(28);
c.setmImageResId(R.drawable._sweden);
c = flags.get(29);
c.setmImageResId(R.drawable._thisasia);
c = flags.get(30);
c.setmImageResId(R.drawable._thailand);
Country c1 = new Country("U.S Dollar", 1.0000);
c1.setmImageResId(R.drawable._unitedstatesofamerica);
flags.add(c1);
}
private SharedPreferences updateSharedPrefrence(String jsonResult) {
Context applicationContext = MainActivity.getContextofApp();
SharedPreferences exchangeRates = PreferenceManager.getDefaultSharedPreferences(applicationContext);
if (jsonResult == null) {
return exchangeRates;
}
SharedPreferences.Editor editor = exchangeRates.edit();
try {
JSONObject baseJson = new JSONObject(jsonResult);
JSONObject rates = baseJson.getJSONObject("rates");
Iterator<String> iterator = rates.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Double keyRate = rates.getDouble(key);
// Log.e(LOG_TAG,keyRate.toString());
editor.putString(key, keyRate.toString());
}
editor.apply();
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the json object", e);
}
return exchangeRates;
}
}