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