Rev 18 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 18 | Rev 20 | ||
---|---|---|---|
Line 1... | Line 1... | ||
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; |
Line 42... | Line 41... | ||
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,
|
Line 103... | Line 99... | ||
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. */
|
Line 119... | Line 116... | ||
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 =
|
Line 256... | Line 264... | ||
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); |
Line 274... | Line 283... | ||
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); |
Line 319... | Line 326... | ||
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 | }
|
Line 378... | Line 387... | ||
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); |
Line 396... | Line 398... | ||
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 | }
|