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