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