Rev 17 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 17 | Rev 18 | ||
---|---|---|---|
1 | package de.pointedears.converter.app; |
1 | package de.pointedears.converter.app; |
2 | 2 | ||
3 | import java.text.DateFormat; |
3 | import java.text.DateFormat; |
4 | import java.text.SimpleDateFormat; |
4 | import java.text.SimpleDateFormat; |
5 | import java.util.Date; |
5 | import java.util.Date; |
6 | import java.util.HashMap; |
6 | import java.util.HashMap; |
7 | import java.util.Map.Entry; |
7 | import java.util.Map.Entry; |
8 | 8 | ||
9 | import android.app.Activity; |
9 | import android.app.Activity; |
10 | import android.content.BroadcastReceiver; |
10 | import android.content.BroadcastReceiver; |
11 | import android.content.Context; |
11 | import android.content.Context; |
12 | import android.content.Intent; |
12 | import android.content.Intent; |
13 | import android.content.IntentFilter; |
13 | import android.content.IntentFilter; |
14 | import android.os.Bundle; |
14 | import android.os.Bundle; |
15 | import android.text.Editable; |
15 | import android.text.Editable; |
16 | import android.view.KeyEvent; |
16 | import android.view.KeyEvent; |
17 | import android.view.Menu; |
17 | import android.view.Menu; |
18 | import android.view.MenuInflater; |
18 | import android.view.MenuInflater; |
19 | import android.view.MenuItem; |
19 | import android.view.MenuItem; |
20 | import android.view.View; |
20 | import android.view.View; |
21 | import android.view.View.OnClickListener; |
21 | import android.view.View.OnClickListener; |
22 | import android.view.View.OnKeyListener; |
22 | import android.view.View.OnKeyListener; |
23 | import android.widget.AdapterView; |
23 | import android.widget.AdapterView; |
24 | import android.widget.AdapterView.OnItemSelectedListener; |
24 | import android.widget.AdapterView.OnItemSelectedListener; |
25 | import android.widget.Button; |
25 | import android.widget.Button; |
26 | import android.widget.EditText; |
26 | import android.widget.EditText; |
27 | import android.widget.Spinner; |
27 | import android.widget.Spinner; |
28 | import android.widget.TableLayout; |
28 | import android.widget.TableLayout; |
29 | import android.widget.TableRow; |
29 | import android.widget.TableRow; |
30 | import android.widget.TextView; |
30 | import android.widget.TextView; |
31 | import de.pointedears.converter.R; |
31 | import de.pointedears.converter.R; |
32 | import de.pointedears.converter.db.ConversionData; |
32 | import de.pointedears.converter.db.ConversionData; |
33 | import de.pointedears.converter.db.CurrenciesDatabase; |
33 | import de.pointedears.converter.db.CurrenciesDatabase; |
34 | import de.pointedears.converter.helpers.Notifier; |
34 | import de.pointedears.converter.helpers.Notifier; |
35 | import de.pointedears.converter.helpers.UpdateService; |
35 | import de.pointedears.converter.helpers.UpdateService; |
36 | 36 | ||
37 | /**
|
37 | /**
|
38 | * Activity that implements currency conversion
|
38 | * Activity that implements currency conversion
|
39 | *
|
39 | *
|
40 | * @author pelinux
|
40 | * @author pelinux
|
41 | */
|
41 | */
|
42 | public class CurrenciesActivity extends Activity |
42 | public class CurrenciesActivity extends Activity |
43 | {
|
43 | {
|
44 | /**
|
44 | /**
|
45 | * String to use to indicate that an exchange rate has never been updated
|
45 | * String to use to indicate that an exchange rate has never been updated
|
46 | */
|
46 | */
|
47 | private static final String TEXT_NEVER = "never"; |
47 | private static final String TEXT_NEVER = "never"; |
48 | 48 | ||
49 | /**
|
49 | /**
|
50 | * Serialization version id
|
50 | * Serialization version id
|
51 | */
|
51 | */
|
52 | private static final long serialVersionUID = 1L; |
52 | private static final long serialVersionUID = 1L; |
53 | 53 | ||
54 | /*
|
54 | /*
|
55 | * Constants for mapping value strings
|
55 | * Constants for mapping value strings
|
56 | *
|
56 | *
|
57 | * @todo: Use resource IDs
|
57 | * @todo: Use resource IDs
|
58 | */
|
58 | */
|
59 | /**
|
59 | /**
|
60 | * Database field/spinner value for Swiss Francs
|
60 | * Database field/spinner value for Swiss Francs
|
61 | */
|
61 | */
|
62 | public static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
62 | public static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
63 | 63 | ||
64 | /**
|
64 | /**
|
65 | * Database field/spinner value for Euros
|
65 | * Database field/spinner value for Euros
|
66 | */
|
66 | */
|
67 | 67 | ||
68 | public static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
68 | public static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
69 | 69 | ||
70 | /**
|
70 | /**
|
71 | * Database field/spinner value for US Dollars
|
71 | * Database field/spinner value for US Dollars
|
72 | */
|
72 | */
|
73 | public static final String VALUE_USD = "USD"; //$NON-NLS-1$ |
73 | public static final String VALUE_USD = "USD"; //$NON-NLS-1$ |
74 | 74 | ||
75 | /* Unit spinners (dropdowns) */
|
75 | /* Unit spinners (dropdowns) */
|
76 | private Spinner spinnerUnit1; |
76 | private Spinner spinnerUnit1; |
77 | private Spinner spinnerUnit2; |
77 | private Spinner spinnerUnit2; |
78 | private CurrenciesDatabase database; |
78 | private CurrenciesDatabase database; |
79 | 79 | ||
80 | private HashMap<String, ConversionData> conversionRates; |
80 | private HashMap<String, ConversionData> conversionRates; |
81 | 81 | ||
82 | /**
|
82 | /**
|
83 | * Receiver for intent broadcasts, registered in
|
83 | * Receiver for intent broadcasts, registered in
|
84 | * {@link CurrenciesActivity#onCreate(Bundle)}
|
84 | * {@link CurrenciesActivity#onCreate(Bundle)}
|
85 | */
|
85 | */
|
86 | public class UpdateBroadcastReceiver extends BroadcastReceiver |
86 | public class UpdateBroadcastReceiver extends BroadcastReceiver |
87 | {
|
87 | {
|
88 | /**
|
88 | /**
|
89 | * Notification message template
|
89 | * Notification message template
|
90 | */
|
90 | */
|
91 | private static final String EXCHANGE_RATES_UPDATED_TO = |
91 | private static final String EXCHANGE_RATES_UPDATED_TO = |
92 | " exchange rates updated to "; |
92 | " exchange rates updated to "; |
93 | 93 | ||
94 | /*
|
94 | /*
|
95 | * (non-Javadoc)
|
95 | * (non-Javadoc)
|
96 | *
|
96 | *
|
97 | * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
|
97 | * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
|
98 | * android.content.Intent)
|
98 | * android.content.Intent)
|
99 | */
|
99 | */
|
100 | @Override |
100 | @Override |
101 | public void onReceive(Context context, Intent intent) |
101 | public void onReceive(Context context, Intent intent) |
102 | {
|
102 | {
|
103 | if (intent.getAction().equals(UpdateService.ACTION_UPDATE)) |
103 | if (intent.getAction().equals(UpdateService.ACTION_UPDATE)) |
104 | {
|
104 | {
|
- | 105 | CurrenciesActivity.this.fillTableRates(); |
|
- | 106 | ||
105 | Bundle extras = intent.getExtras(); |
107 | Bundle extras = intent.getExtras(); |
106 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$ |
108 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$ |
107 | Notifier.sendMessage(CurrenciesActivity.this, |
109 | Notifier.sendMessage(CurrenciesActivity.this, |
108 | extras.get(UpdateService.EXTRA_NUM_RATES) |
110 | extras.get(UpdateService.EXTRA_NUM_RATES) |
109 | + UpdateBroadcastReceiver.EXCHANGE_RATES_UPDATED_TO
|
111 | + UpdateBroadcastReceiver.EXCHANGE_RATES_UPDATED_TO
|
110 | + df.format(extras.get(UpdateService.EXTRA_DATE))); |
112 | + df.format(extras.get(UpdateService.EXTRA_DATE))); |
111 | }
|
113 | }
|
112 | }
|
114 | }
|
113 | }
|
115 | }
|
114 | 116 | ||
115 | /** Called when the activity is first created. */
|
117 | /** Called when the activity is first created. */
|
116 | @Override |
118 | @Override |
117 | public void onCreate(Bundle savedInstanceState) |
119 | public void onCreate(Bundle savedInstanceState) |
118 | {
|
120 | {
|
119 | super.onCreate(savedInstanceState); |
121 | super.onCreate(savedInstanceState); |
120 | this.setContentView(R.layout.activity_currencies); |
122 | this.setContentView(R.layout.activity_currencies); |
121 | 123 | ||
122 | UpdateBroadcastReceiver br = new UpdateBroadcastReceiver(); |
124 | UpdateBroadcastReceiver br = new UpdateBroadcastReceiver(); |
123 | this.registerReceiver(br, new IntentFilter(UpdateService.ACTION_UPDATE)); |
125 | this.registerReceiver(br, new IntentFilter(UpdateService.ACTION_UPDATE)); |
124 | 126 | ||
125 | /* Set up currency database, and retrieve conversion rates */
|
127 | /* Set up currency database, and retrieve conversion rates */
|
126 | this.database = new CurrenciesDatabase(this); |
128 | this.database = new CurrenciesDatabase(this); |
127 | this.setConversionRates(this.getDatabase().getConversionRates()); |
129 | this.setConversionRates(this.getDatabase().getConversionRates()); |
128 | this.fillTableRates(); |
130 | this.fillTableRates(); |
129 | 131 | ||
130 | final EditText editValue1 =
|
132 | final EditText editValue1 =
|
131 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
133 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
132 | final EditText editValue2 =
|
134 | final EditText editValue2 =
|
133 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
135 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
134 | 136 | ||
135 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
137 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
136 | @Override |
138 | @Override |
137 | public boolean onKey(View v, int keyCode, KeyEvent event) |
139 | public boolean onKey(View v, int keyCode, KeyEvent event) |
138 | {
|
140 | {
|
139 | Editable editable1 = ((EditText) v).getText(); |
141 | Editable editable1 = ((EditText) v).getText(); |
140 | 142 | ||
141 | Double value1; |
143 | Double value1; |
142 | try
|
144 | try
|
143 | {
|
145 | {
|
144 | value1 = Double.parseDouble(editable1.toString()); |
146 | value1 = Double.parseDouble(editable1.toString()); |
145 | }
|
147 | }
|
146 | catch (NumberFormatException e) |
148 | catch (NumberFormatException e) |
147 | {
|
149 | {
|
148 | value1 = null; |
150 | value1 = null; |
149 | }
|
151 | }
|
150 | 152 | ||
151 | String string2 = ""; //$NON-NLS-1$ |
153 | String string2 = ""; //$NON-NLS-1$ |
152 | if (value1 != null) |
154 | if (value1 != null) |
153 | {
|
155 | {
|
154 | string2 = CurrenciesActivity.this.getConvertedValue(value1, false); |
156 | string2 = CurrenciesActivity.this.getConvertedValue(value1, false); |
155 | }
|
157 | }
|
156 | 158 | ||
157 | editValue2.setText(string2); |
159 | editValue2.setText(string2); |
158 | 160 | ||
159 | return false; |
161 | return false; |
160 | }
|
162 | }
|
161 | }; |
163 | }; |
162 | editValue1.setOnKeyListener(editValue1OnKey); |
164 | editValue1.setOnKeyListener(editValue1OnKey); |
163 | 165 | ||
164 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
166 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
165 | @Override |
167 | @Override |
166 | public boolean onKey(View v, int keyCode, KeyEvent event) |
168 | public boolean onKey(View v, int keyCode, KeyEvent event) |
167 | {
|
169 | {
|
168 | Editable editable2 = ((EditText) v).getText(); |
170 | Editable editable2 = ((EditText) v).getText(); |
169 | 171 | ||
170 | Double value2; |
172 | Double value2; |
171 | try
|
173 | try
|
172 | {
|
174 | {
|
173 | value2 = Double.parseDouble(editable2.toString()); |
175 | value2 = Double.parseDouble(editable2.toString()); |
174 | }
|
176 | }
|
175 | catch (NumberFormatException e) |
177 | catch (NumberFormatException e) |
176 | {
|
178 | {
|
177 | value2 = null; |
179 | value2 = null; |
178 | }
|
180 | }
|
179 | 181 | ||
180 | String string1 = ""; //$NON-NLS-1$ |
182 | String string1 = ""; //$NON-NLS-1$ |
181 | if (value2 != null) |
183 | if (value2 != null) |
182 | {
|
184 | {
|
183 | string1 = CurrenciesActivity.this.getConvertedValue(value2, true); |
185 | string1 = CurrenciesActivity.this.getConvertedValue(value2, true); |
184 | }
|
186 | }
|
185 | 187 | ||
186 | editValue1.setText(string1); |
188 | editValue1.setText(string1); |
187 | 189 | ||
188 | return false; |
190 | return false; |
189 | }
|
191 | }
|
190 | }; |
192 | }; |
191 | editValue2.setOnKeyListener(editValue2OnKey); |
193 | editValue2.setOnKeyListener(editValue2OnKey); |
192 | 194 | ||
193 | this.spinnerUnit1 = |
195 | this.spinnerUnit1 = |
194 | (Spinner) this.findViewById(R.id.currencies_spinner_unit1); |
196 | (Spinner) this.findViewById(R.id.currencies_spinner_unit1); |
195 | this.spinnerUnit2 = |
197 | this.spinnerUnit2 = |
196 | (Spinner) this.findViewById(R.id.currencies_spinner_unit2); |
198 | (Spinner) this.findViewById(R.id.currencies_spinner_unit2); |
197 | 199 | ||
198 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
200 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
199 | @Override |
201 | @Override |
200 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
202 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
201 | long arg3) |
203 | long arg3) |
202 | {
|
204 | {
|
203 | /* Simulate input in second EditText so that first EditText is updated */
|
205 | /* Simulate input in second EditText so that first EditText is updated */
|
204 | editValue2OnKey.onKey(editValue2, 0, null); |
206 | editValue2OnKey.onKey(editValue2, 0, null); |
205 | }
|
207 | }
|
206 | 208 | ||
207 | @Override |
209 | @Override |
208 | public void onNothingSelected(AdapterView<?> arg0) |
210 | public void onNothingSelected(AdapterView<?> arg0) |
209 | {
|
211 | {
|
210 | /* no-op */
|
212 | /* no-op */
|
211 | }
|
213 | }
|
212 | }); |
214 | }); |
213 | 215 | ||
214 | this.spinnerUnit2.setSelection(1); |
216 | this.spinnerUnit2.setSelection(1); |
215 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
217 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
216 | @Override |
218 | @Override |
217 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
219 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
218 | long arg3) |
220 | long arg3) |
219 | {
|
221 | {
|
220 | /* Simulate input in first EditText so that second EditText is updated */
|
222 | /* Simulate input in first EditText so that second EditText is updated */
|
221 | editValue1OnKey.onKey(editValue1, 0, null); |
223 | editValue1OnKey.onKey(editValue1, 0, null); |
222 | }
|
224 | }
|
223 | 225 | ||
224 | @Override |
226 | @Override |
225 | public void onNothingSelected(AdapterView<?> arg0) |
227 | public void onNothingSelected(AdapterView<?> arg0) |
226 | {
|
228 | {
|
227 | /* no-op */
|
229 | /* no-op */
|
228 | }
|
230 | }
|
229 | }); |
231 | }); |
230 | 232 | ||
231 | Button buttonClear = |
233 | Button buttonClear = |
232 | (Button) this.findViewById(R.id.currencies_button_clear); |
234 | (Button) this.findViewById(R.id.currencies_button_clear); |
233 | buttonClear.setOnClickListener(new OnClickListener() { |
235 | buttonClear.setOnClickListener(new OnClickListener() { |
234 | 236 | ||
235 | @SuppressWarnings("nls") |
237 | @SuppressWarnings("nls") |
236 | @Override |
238 | @Override |
237 | public void onClick(View v) |
239 | public void onClick(View v) |
238 | {
|
240 | {
|
239 | editValue1.setText(""); |
241 | editValue1.setText(""); |
240 | editValue2.setText(""); |
242 | editValue2.setText(""); |
241 | }
|
243 | }
|
242 | }); |
244 | }); |
243 | }
|
245 | }
|
244 | 246 | ||
245 | /**
|
247 | /**
|
246 | * Fills the table with currency conversion rates
|
248 | * Fills the table with currency conversion rates
|
247 | */
|
249 | */
|
248 | public void fillTableRates() |
250 | public void fillTableRates() |
249 | {
|
251 | {
|
250 | TableLayout tableRates = |
252 | TableLayout tableRates = |
251 | (TableLayout) this.findViewById(R.id.currencies_table_rates); |
253 | (TableLayout) this.findViewById(R.id.currencies_table_rates); |
252 | 254 | ||
253 | /* Remove any pre-existing currency rows */
|
255 | /* Remove any pre-existing currency rows */
|
254 | while (tableRates.getChildCount() > 3) |
256 | while (tableRates.getChildCount() > 3) |
255 | {
|
257 | {
|
256 | tableRates.removeViewAt(3); |
258 | tableRates.removeViewAt(3); |
257 | }
|
259 | }
|
258 | 260 | ||
259 | for (Entry<String, ConversionData> factorEntry : this.getConversionRates() |
261 | for (Entry<String, ConversionData> factorEntry : this.getConversionRates() |
260 | .entrySet()) |
262 | .entrySet()) |
261 | {
|
263 | {
|
262 | TableRow row = new TableRow(this); |
264 | TableRow row = new TableRow(this); |
263 | 265 | ||
264 | TextView columnCurrency1 = new TextView(this); |
266 | TextView columnCurrency1 = new TextView(this); |
265 | columnCurrency1.setText(factorEntry.getKey()); |
267 | columnCurrency1.setText(factorEntry.getKey()); |
266 | row.addView(columnCurrency1); |
268 | row.addView(columnCurrency1); |
267 | 269 | ||
268 | TextView columnRate = new TextView(this); |
270 | TextView columnRate = new TextView(this); |
269 | final ConversionData conversionData = factorEntry.getValue(); |
271 | final ConversionData conversionData = factorEntry.getValue(); |
270 | columnRate.setText(conversionData.getRate().toString()); |
272 | columnRate.setText(conversionData.getRate().toString()); |
271 | row.addView(columnRate); |
273 | row.addView(columnRate); |
272 | 274 | ||
273 | TextView columnUpdated = new TextView(this); |
275 | TextView columnUpdated = new TextView(this); |
274 | Date updated = conversionData.getUpdated(); |
276 | Date updated = conversionData.getUpdated(); |
275 | if (updated.getTime() > 0) |
277 | if (updated.getTime() > 0) |
276 | {
|
278 | {
|
277 | DateFormat df = |
279 | DateFormat df = |
278 | DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); |
280 | DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); |
279 | columnUpdated.setText(df.format(updated)); |
281 | columnUpdated.setText(df.format(updated)); |
280 | }
|
282 | }
|
281 | else
|
283 | else
|
282 | {
|
284 | {
|
283 | columnUpdated.setText(CurrenciesActivity.TEXT_NEVER); |
285 | columnUpdated.setText(CurrenciesActivity.TEXT_NEVER); |
284 | }
|
286 | }
|
285 | 287 | ||
286 | row.addView(columnUpdated); |
288 | row.addView(columnUpdated); |
287 | 289 | ||
288 | tableRates.addView(row); |
290 | tableRates.addView(row); |
289 | }
|
291 | }
|
290 | }
|
292 | }
|
291 | 293 | ||
292 | /**
|
294 | /**
|
293 | * @param value
|
295 | * @param value
|
294 | * @return
|
296 | * @return
|
295 | */
|
297 | */
|
296 | private String getConvertedValue(double value, boolean reverse) |
298 | private String getConvertedValue(double value, boolean reverse) |
297 | {
|
299 | {
|
298 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
300 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
299 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
301 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
300 | String[] items = |
302 | String[] items = |
301 | this.getResources().getStringArray(R.array.currency_units_values); |
303 | this.getResources().getStringArray(R.array.currency_units_values); |
302 | String selectedItemValue1 = items[selectedItemPosition1]; |
304 | String selectedItemValue1 = items[selectedItemPosition1]; |
303 | String selectedItemValue2 = items[selectedItemPosition2]; |
305 | String selectedItemValue2 = items[selectedItemPosition2]; |
304 | 306 | ||
305 | if (reverse) |
307 | if (reverse) |
306 | {
|
308 | {
|
307 | String tmp = selectedItemValue1; |
309 | String tmp = selectedItemValue1; |
308 | selectedItemValue1 = selectedItemValue2;
|
310 | selectedItemValue1 = selectedItemValue2;
|
309 | selectedItemValue2 = tmp;
|
311 | selectedItemValue2 = tmp;
|
310 | }
|
312 | }
|
311 | 313 | ||
312 | Double newValue = value; |
314 | Double newValue = value; |
313 | 315 | ||
314 | /*
|
316 | /*
|
315 | * NOTE: Had to do it the complicated way because somehow the Android SDK
|
317 | * NOTE: Had to do it the complicated way because somehow the Android SDK
|
316 | * won't get it another way
|
318 | * won't get it another way
|
317 | */
|
319 | */
|
318 | ConversionData conversionData1 = null; |
320 | ConversionData conversionData1 = null; |
319 | Double factorToEuro = 1.0; |
321 | Double factorToEuro = 1.0; |
320 | if (selectedItemValue1 != null) |
322 | if (selectedItemValue1 != null) |
321 | {
|
323 | {
|
322 | conversionData1 = this.getConversionRates().get(selectedItemValue1); |
324 | conversionData1 = this.getConversionRates().get(selectedItemValue1); |
323 | if (conversionData1 != null) |
325 | if (conversionData1 != null) |
324 | {
|
326 | {
|
325 | factorToEuro = conversionData1.getRate(); |
327 | factorToEuro = conversionData1.getRate(); |
326 | }
|
328 | }
|
327 | }
|
329 | }
|
328 | 330 | ||
329 | ConversionData conversionData2 = null; |
331 | ConversionData conversionData2 = null; |
330 | Double factorFromEuro = 1.0; |
332 | Double factorFromEuro = 1.0; |
331 | if (selectedItemValue2 != null) |
333 | if (selectedItemValue2 != null) |
332 | {
|
334 | {
|
333 | conversionData2 = this.getConversionRates().get(selectedItemValue2); |
335 | conversionData2 = this.getConversionRates().get(selectedItemValue2); |
334 | if (conversionData2 != null) |
336 | if (conversionData2 != null) |
335 | {
|
337 | {
|
336 | factorFromEuro = conversionData2.getRate(); |
338 | factorFromEuro = conversionData2.getRate(); |
337 | }
|
339 | }
|
338 | }
|
340 | }
|
339 | 341 | ||
340 | newValue = newValue / factorToEuro * factorFromEuro; |
342 | newValue = newValue / factorToEuro * factorFromEuro; |
341 | 343 | ||
342 | return newValue.toString(); |
344 | return newValue.toString(); |
343 | }
|
345 | }
|
344 | 346 | ||
345 | /*
|
347 | /*
|
346 | * (non-Javadoc)
|
348 | * (non-Javadoc)
|
347 | *
|
349 | *
|
348 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
|
350 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
|
349 | */
|
351 | */
|
350 | /*
|
352 | /*
|
351 | * (non-Javadoc)
|
353 | * (non-Javadoc)
|
352 | *
|
354 | *
|
353 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
|
355 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
|
354 | */
|
356 | */
|
355 | @Override |
357 | @Override |
356 | public boolean onCreateOptionsMenu(Menu menu) |
358 | public boolean onCreateOptionsMenu(Menu menu) |
357 | {
|
359 | {
|
358 | MenuInflater inflater = this.getMenuInflater(); |
360 | MenuInflater inflater = this.getMenuInflater(); |
359 | inflater.inflate(R.menu.options, menu); |
361 | inflater.inflate(R.menu.options, menu); |
360 | return true; |
362 | return true; |
361 | }
|
363 | }
|
362 | 364 | ||
363 | /*
|
365 | /*
|
364 | * (non-Javadoc)
|
366 | * (non-Javadoc)
|
365 | *
|
367 | *
|
366 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
|
368 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
|
367 | */
|
369 | */
|
368 | @Override |
370 | @Override |
369 | public boolean onOptionsItemSelected(MenuItem item) |
371 | public boolean onOptionsItemSelected(MenuItem item) |
370 | {
|
372 | {
|
371 | /* Handle item selection */
|
373 | /* Handle item selection */
|
372 | switch (item.getItemId()) |
374 | switch (item.getItemId()) |
373 | {
|
375 | {
|
374 | case R.id.item_options_update: |
376 | case R.id.item_options_update: |
375 | /*
|
377 | /*
|
376 | * Request the update service to run a thread to fetch the rates from
|
378 | * Request the update service to run a thread to fetch the rates from
|
377 | * the Web (project requirement)
|
379 | * the Web (project requirement)
|
378 | */
|
380 | */
|
379 | Intent intent = new Intent(this, UpdateService.class); |
381 | Intent intent = new Intent(this, UpdateService.class); |
380 | intent.setAction(UpdateService.ACTION_UPDATE); |
382 | intent.setAction(UpdateService.ACTION_UPDATE); |
381 | 383 | ||
382 | /*
|
384 | /*
|
383 | * FIXME: Not thread-safe!
|
385 | * FIXME: Not thread-safe!
|
384 | * Get the activity context from the intent directly instead
|
386 | * Get the activity context from the intent directly instead
|
385 | */
|
387 | */
|
386 | UpdateService.setActivityContext(this); |
388 | UpdateService.setActivityContext(this); |
387 | 389 | ||
388 | this.startService(intent); |
390 | this.startService(intent); |
389 | return true; |
391 | return true; |
390 | 392 | ||
391 | default:
|
393 | default:
|
392 | return super.onOptionsItemSelected(item); |
394 | return super.onOptionsItemSelected(item); |
393 | }
|
395 | }
|
394 | }
|
396 | }
|
395 | 397 | ||
396 | /**
|
398 | /**
|
397 | * @return the conversionRates
|
399 | * @return the conversionRates
|
398 | */
|
400 | */
|
399 | public HashMap<String, ConversionData> getConversionRates() |
401 | public HashMap<String, ConversionData> getConversionRates() |
400 | {
|
402 | {
|
401 | return this.conversionRates; |
403 | return this.conversionRates; |
402 | }
|
404 | }
|
403 | 405 | ||
404 | /**
|
406 | /**
|
405 | * @param conversionRates
|
407 | * @param conversionRates
|
406 | * the conversionRates to set
|
408 | * the conversionRates to set
|
407 | */
|
409 | */
|
408 | public void setConversionRates(HashMap<String, ConversionData> conversionRates) |
410 | public void setConversionRates(HashMap<String, ConversionData> conversionRates) |
409 | {
|
411 | {
|
410 | this.conversionRates = conversionRates; |
412 | this.conversionRates = conversionRates; |
411 | }
|
413 | }
|
412 | 414 | ||
413 | /**
|
415 | /**
|
414 | * @return the database
|
416 | * @return the database
|
415 | */
|
417 | */
|
416 | public CurrenciesDatabase getDatabase() |
418 | public CurrenciesDatabase getDatabase() |
417 | {
|
419 | {
|
418 | return this.database; |
420 | return this.database; |
419 | }
|
421 | }
|
420 | }
|
422 | }
|
421 | 423 |