Rev 15 | Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8 | PointedEar | 1 | package de.pointedears.converter.app; |
5 | PointedEar | 2 | |
7 | PointedEar | 3 | import java.util.HashMap; |
14 | PointedEar | 4 | import java.util.Map.Entry; |
7 | PointedEar | 5 | |
5 | PointedEar | 6 | import android.app.Activity; |
7 | import android.os.Bundle; |
||
16 | PointedEar | 8 | import android.os.Handler; |
7 | PointedEar | 9 | import android.text.Editable; |
10 | import android.view.KeyEvent; |
||
8 | PointedEar | 11 | import android.view.Menu; |
12 | import android.view.MenuInflater; |
||
13 | import android.view.MenuItem; |
||
7 | PointedEar | 14 | import android.view.View; |
13 | PointedEar | 15 | import android.view.View.OnClickListener; |
7 | PointedEar | 16 | import android.view.View.OnKeyListener; |
17 | import android.widget.AdapterView; |
||
18 | import android.widget.AdapterView.OnItemSelectedListener; |
||
13 | PointedEar | 19 | import android.widget.Button; |
7 | PointedEar | 20 | import android.widget.EditText; |
21 | import android.widget.Spinner; |
||
14 | PointedEar | 22 | import android.widget.TableLayout; |
23 | import android.widget.TableRow; |
||
24 | import android.widget.TextView; |
||
8 | PointedEar | 25 | import de.pointedears.converter.R; |
11 | PointedEar | 26 | import de.pointedears.converter.db.CurrenciesDatabase; |
16 | PointedEar | 27 | import de.pointedears.converter.helpers.ConverterThread; |
28 | import de.pointedears.converter.net.RatesUpdater; |
||
5 | PointedEar | 29 | |
30 | /** |
||
7 | PointedEar | 31 | * Activity that implements currency conversion |
5 | PointedEar | 32 | * |
7 | PointedEar | 33 | * @author pelinux |
5 | PointedEar | 34 | */ |
35 | public class CurrenciesActivity extends Activity |
||
36 | { |
||
7 | PointedEar | 37 | /* |
8 | PointedEar | 38 | * Constants for mapping value strings |
12 | PointedEar | 39 | * |
40 | * @todo: Use resource IDs |
||
7 | PointedEar | 41 | */ |
12 | PointedEar | 42 | /** |
43 | * Database field/spinner value for Swiss Francs |
||
44 | */ |
||
45 | public static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
||
7 | PointedEar | 46 | |
12 | PointedEar | 47 | /** |
48 | * Database field/spinner value for Euros |
||
49 | */ |
||
7 | PointedEar | 50 | |
12 | PointedEar | 51 | public static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
52 | |||
53 | /** |
||
54 | * Database field/spinner value for US Dollars |
||
55 | */ |
||
56 | public static final String VALUE_USD = "USD"; //$NON-NLS-1$ |
||
57 | |||
7 | PointedEar | 58 | /* Unit spinners (dropdowns) */ |
59 | private Spinner spinnerUnit1; |
||
60 | private Spinner spinnerUnit2; |
||
12 | PointedEar | 61 | private CurrenciesDatabase db; |
7 | PointedEar | 62 | |
16 | PointedEar | 63 | private HashMap<String, Double> conversionRates; |
64 | private ConverterThread updateThread; |
||
65 | private Handler handler; |
||
12 | PointedEar | 66 | |
16 | PointedEar | 67 | private RatesUpdater updateRates; |
68 | |||
7 | PointedEar | 69 | /** Called when the activity is first created. */ |
70 | |||
5 | PointedEar | 71 | @Override |
7 | PointedEar | 72 | public void onCreate(Bundle savedInstanceState) |
5 | PointedEar | 73 | { |
74 | super.onCreate(savedInstanceState); |
||
7 | PointedEar | 75 | this.setContentView(R.layout.activity_currencies); |
5 | PointedEar | 76 | |
12 | PointedEar | 77 | /* Set up currency database, and retrieve conversion rates */ |
78 | this.db = new CurrenciesDatabase(this); |
||
79 | this.conversionRates = this.db.getConversionRates(); |
||
14 | PointedEar | 80 | this.fillTableRates(); |
8 | PointedEar | 81 | |
7 | PointedEar | 82 | final EditText editValue1 = |
83 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
||
84 | final EditText editValue2 = |
||
85 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
||
86 | |||
87 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
||
88 | @Override |
||
89 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
90 | { |
||
91 | Editable editable1 = ((EditText) v).getText(); |
||
92 | |||
93 | Double value1; |
||
94 | try |
||
95 | { |
||
96 | value1 = Double.parseDouble(editable1.toString()); |
||
97 | } |
||
98 | catch (NumberFormatException e) |
||
99 | { |
||
100 | value1 = null; |
||
101 | } |
||
102 | |||
103 | String string2 = ""; //$NON-NLS-1$ |
||
104 | if (value1 != null) |
||
105 | { |
||
106 | string2 = CurrenciesActivity.this.getConvertedValue(value1, false); |
||
107 | } |
||
108 | |||
109 | editValue2.setText(string2); |
||
110 | |||
111 | return false; |
||
112 | } |
||
113 | }; |
||
114 | editValue1.setOnKeyListener(editValue1OnKey); |
||
115 | |||
116 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
||
117 | @Override |
||
118 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
119 | { |
||
120 | Editable editable2 = ((EditText) v).getText(); |
||
121 | |||
122 | Double value2; |
||
123 | try |
||
124 | { |
||
125 | value2 = Double.parseDouble(editable2.toString()); |
||
126 | } |
||
127 | catch (NumberFormatException e) |
||
128 | { |
||
129 | value2 = null; |
||
130 | } |
||
131 | |||
132 | String string1 = ""; //$NON-NLS-1$ |
||
133 | if (value2 != null) |
||
134 | { |
||
135 | string1 = CurrenciesActivity.this.getConvertedValue(value2, true); |
||
136 | } |
||
137 | |||
138 | editValue1.setText(string1); |
||
139 | |||
140 | return false; |
||
141 | } |
||
142 | }; |
||
143 | editValue2.setOnKeyListener(editValue2OnKey); |
||
144 | |||
145 | this.spinnerUnit1 = |
||
146 | (Spinner) this.findViewById(R.id.currencies_spinner_unit1); |
||
147 | this.spinnerUnit2 = |
||
148 | (Spinner) this.findViewById(R.id.currencies_spinner_unit2); |
||
149 | |||
150 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
151 | @Override |
||
152 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
153 | long arg3) |
||
154 | { |
||
155 | /* Simulate input in second EditText so that first EditText is updated */ |
||
156 | editValue2OnKey.onKey(editValue2, 0, null); |
||
157 | } |
||
158 | |||
159 | @Override |
||
160 | public void onNothingSelected(AdapterView<?> arg0) |
||
161 | { |
||
162 | /* no-op */ |
||
163 | } |
||
164 | }); |
||
165 | |||
166 | this.spinnerUnit2.setSelection(1); |
||
167 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
168 | @Override |
||
169 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
170 | long arg3) |
||
171 | { |
||
172 | /* Simulate input in first EditText so that second EditText is updated */ |
||
173 | editValue1OnKey.onKey(editValue1, 0, null); |
||
174 | } |
||
175 | |||
176 | @Override |
||
177 | public void onNothingSelected(AdapterView<?> arg0) |
||
178 | { |
||
179 | /* no-op */ |
||
180 | } |
||
181 | }); |
||
13 | PointedEar | 182 | |
183 | Button buttonClear = |
||
184 | (Button) this.findViewById(R.id.currencies_button_clear); |
||
185 | buttonClear.setOnClickListener(new OnClickListener() { |
||
186 | |||
187 | @SuppressWarnings("nls") |
||
188 | @Override |
||
189 | public void onClick(View v) |
||
190 | { |
||
191 | editValue1.setText(""); |
||
192 | editValue2.setText(""); |
||
193 | } |
||
194 | }); |
||
16 | PointedEar | 195 | |
196 | if (this.handler == null) |
||
197 | { |
||
198 | this.handler = new Handler(); |
||
199 | } |
||
200 | |||
201 | this.updateThread = null; |
||
5 | PointedEar | 202 | } |
7 | PointedEar | 203 | |
204 | /** |
||
14 | PointedEar | 205 | * Fills the table with currency conversion rates |
206 | */ |
||
207 | private void fillTableRates() |
||
208 | { |
||
209 | TableLayout tableRates = |
||
210 | (TableLayout) this.findViewById(R.id.currencies_table_rates); |
||
211 | |||
16 | PointedEar | 212 | for (Entry<String, Double> factorEntry : this.conversionRates.entrySet()) |
14 | PointedEar | 213 | { |
16 | PointedEar | 214 | TableRow row = new TableRow(this); |
14 | PointedEar | 215 | |
16 | PointedEar | 216 | TextView columnCurrency1 = new TextView(this); |
217 | columnCurrency1.setText(factorEntry.getKey()); |
||
218 | row.addView(columnCurrency1); |
||
14 | PointedEar | 219 | |
16 | PointedEar | 220 | TextView columnRate = new TextView(this); |
221 | columnRate.setText(factorEntry.getValue().toString()); |
||
222 | row.addView(columnRate); |
||
14 | PointedEar | 223 | |
16 | PointedEar | 224 | tableRates.addView(row); |
14 | PointedEar | 225 | } |
226 | } |
||
227 | |||
228 | /** |
||
7 | PointedEar | 229 | * @param value |
230 | * @return |
||
231 | */ |
||
232 | private String getConvertedValue(double value, boolean reverse) |
||
233 | { |
||
234 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
||
235 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
||
8 | PointedEar | 236 | String[] items = |
7 | PointedEar | 237 | this.getResources().getStringArray(R.array.currency_units_values); |
8 | PointedEar | 238 | String selectedItemValue1 = items[selectedItemPosition1]; |
239 | String selectedItemValue2 = items[selectedItemPosition2]; |
||
7 | PointedEar | 240 | |
241 | if (reverse) |
||
242 | { |
||
243 | String tmp = selectedItemValue1; |
||
244 | selectedItemValue1 = selectedItemValue2; |
||
245 | selectedItemValue2 = tmp; |
||
246 | } |
||
247 | |||
248 | Double newValue = value; |
||
249 | |||
16 | PointedEar | 250 | /* |
251 | * NOTE: Had to do it the complicated way because somehow the Android SDK |
||
252 | * won't get it another way |
||
253 | */ |
||
254 | Double factorToEuro = null; |
||
255 | if (selectedItemValue1 != null) |
||
7 | PointedEar | 256 | { |
16 | PointedEar | 257 | factorToEuro = this.conversionRates.get(selectedItemValue1); |
7 | PointedEar | 258 | } |
259 | |||
16 | PointedEar | 260 | if (factorToEuro == null) |
261 | { |
||
262 | factorToEuro = 1.0; |
||
263 | } |
||
264 | |||
265 | Double factorFromEuro = null; |
||
266 | if (selectedItemValue2 != null) |
||
267 | { |
||
268 | factorFromEuro = this.conversionRates.get(selectedItemValue2); |
||
269 | } |
||
270 | |||
271 | if (factorFromEuro == null) |
||
272 | { |
||
273 | factorFromEuro = 1.0; |
||
274 | } |
||
275 | |||
276 | newValue = newValue / factorToEuro * factorFromEuro; |
||
277 | |||
7 | PointedEar | 278 | return newValue.toString(); |
279 | } |
||
8 | PointedEar | 280 | |
281 | /* |
||
282 | * (non-Javadoc) |
||
283 | * |
||
284 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) |
||
285 | */ |
||
12 | PointedEar | 286 | /* |
287 | * (non-Javadoc) |
||
288 | * |
||
289 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) |
||
290 | */ |
||
8 | PointedEar | 291 | @Override |
292 | public boolean onCreateOptionsMenu(Menu menu) |
||
293 | { |
||
294 | MenuInflater inflater = this.getMenuInflater(); |
||
295 | inflater.inflate(R.menu.options, menu); |
||
296 | return true; |
||
297 | } |
||
298 | |||
299 | /* |
||
300 | * (non-Javadoc) |
||
301 | * |
||
302 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) |
||
303 | */ |
||
304 | @Override |
||
305 | public boolean onOptionsItemSelected(MenuItem item) |
||
306 | { |
||
15 | PointedEar | 307 | // Handle item selection |
308 | switch (item.getItemId()) |
||
309 | { |
||
310 | case R.id.item_options_update: |
||
16 | PointedEar | 311 | if (this.updateThread == null) |
312 | { |
||
313 | this.updateRates = new RatesUpdater(this); |
||
314 | this.updateThread = |
||
315 | new ConverterThread(this.updateRates, this.handler); |
||
316 | this.updateRates.setUpdateThread(this.updateThread); |
||
317 | } |
||
318 | |||
319 | try |
||
320 | { |
||
321 | this.updateThread.start(); |
||
322 | // this.editValue1.setText("Gestartet!"); |
||
323 | } |
||
324 | catch (IllegalThreadStateException e) |
||
325 | { |
||
326 | // this.editValue1.setText("Bereits gestartet!"); |
||
327 | } |
||
15 | PointedEar | 328 | return true; |
329 | |||
16 | PointedEar | 330 | case R.id.item_options_quit: |
331 | if (this.updateThread != null) |
||
332 | { |
||
333 | try |
||
334 | { |
||
335 | this.updateThread.join(); |
||
336 | } |
||
337 | catch (InterruptedException e) |
||
338 | { |
||
339 | // TODO Auto-generated catch block |
||
340 | } |
||
341 | |||
342 | // this.editValue1.setText("Gestoppt -> Warten auf Start"); |
||
343 | } |
||
344 | else |
||
345 | { |
||
346 | // this.editValue1.setText("Bereits gestoppt -> Warten auf Start"); |
||
347 | } |
||
348 | return true; |
||
349 | |||
15 | PointedEar | 350 | default: |
351 | return super.onOptionsItemSelected(item); |
||
352 | } |
||
8 | PointedEar | 353 | } |
5 | PointedEar | 354 | } |