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