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