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