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