Rev 7 | Rev 11 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 7 | Rev 8 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | package ch.ffhs.converter.app; |
1 | package de.pointedears.converter.app; |
| 2 | 2 | ||
| 3 | import java.util.HashMap; |
3 | import java.util.HashMap; |
| 4 | 4 | ||
| 5 | import android.app.Activity; |
5 | import android.app.Activity; |
| 6 | import android.os.Bundle; |
6 | import android.os.Bundle; |
| 7 | import android.text.Editable; |
7 | import android.text.Editable; |
| 8 | import android.view.KeyEvent; |
8 | import android.view.KeyEvent; |
| - | 9 | import android.view.Menu; |
|
| - | 10 | import android.view.MenuInflater; |
|
| - | 11 | import android.view.MenuItem; |
|
| 9 | import android.view.View; |
12 | import android.view.View; |
| 10 | import android.view.View.OnKeyListener; |
13 | import android.view.View.OnKeyListener; |
| 11 | import android.widget.AdapterView; |
14 | import android.widget.AdapterView; |
| 12 | import android.widget.AdapterView.OnItemSelectedListener; |
15 | import android.widget.AdapterView.OnItemSelectedListener; |
| 13 | import android.widget.EditText; |
16 | import android.widget.EditText; |
| 14 | import android.widget.Spinner; |
17 | import android.widget.Spinner; |
| 15 | import ch.ffhs.converter.R; |
18 | import de.pointedears.converter.R; |
| 16 | 19 | ||
| 17 | /**
|
20 | /**
|
| 18 | * Activity that implements currency conversion
|
21 | * Activity that implements currency conversion
|
| 19 | *
|
22 | *
|
| 20 | * @author pelinux
|
23 | * @author pelinux
|
| 21 | */
|
24 | */
|
| 22 | public class CurrenciesActivity extends Activity |
25 | public class CurrenciesActivity extends Activity |
| 23 | {
|
26 | {
|
| 24 | /*
|
27 | /*
|
| 25 | * Constants for mapping value strings to internal IDs
|
28 | * Constants for mapping value strings
|
| 26 | */
|
29 | */
|
| 27 | private static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
30 | private static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
| 28 | private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
31 | private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
| 29 | private static final String VALUE_USD = "USD"; //$NON-NLS-1$ |
32 | private static final String VALUE_USD = "USD"; //$NON-NLS-1$ |
| 30 | private static final int ITEM_CHF = 0; |
- | |
| 31 | private static final int ITEM_EUR = 1; |
- | |
| 32 | private static final int ITEM_USD = 2; |
- | |
| 33 | 33 | ||
| 34 | /**
|
- | |
| 35 | * Maps value strings to internal IDs
|
- | |
| 36 | */
|
- | |
| 37 | private final static HashMap<String, Integer> valueToId = |
34 | private static HashMap<String, HashMap<String, Double>> currencyConversions; |
| 38 | new HashMap<String, Integer>(); |
- | |
| 39 | static
|
- | |
| 40 | {
|
- | |
| 41 | CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_CHF, |
- | |
| 42 | CurrenciesActivity.ITEM_CHF); |
- | |
| 43 | CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_EUR, |
- | |
| 44 | CurrenciesActivity.ITEM_EUR); |
- | |
| 45 | CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_USD, |
- | |
| 46 | CurrenciesActivity.ITEM_USD); |
- | |
| 47 | }
|
- | |
| 48 | 35 | ||
| 49 | /* Unit spinners (dropdowns) */
|
36 | /* Unit spinners (dropdowns) */
|
| 50 | private Spinner spinnerUnit1; |
37 | private Spinner spinnerUnit1; |
| 51 | private Spinner spinnerUnit2; |
38 | private Spinner spinnerUnit2; |
| 52 | 39 | ||
| Line 56... | Line 43... | ||
| 56 | public void onCreate(Bundle savedInstanceState) |
43 | public void onCreate(Bundle savedInstanceState) |
| 57 | {
|
44 | {
|
| 58 | super.onCreate(savedInstanceState); |
45 | super.onCreate(savedInstanceState); |
| 59 | this.setContentView(R.layout.activity_currencies); |
46 | this.setContentView(R.layout.activity_currencies); |
| 60 | 47 | ||
| - | 48 | /* TODO: Set up currency database */
|
|
| - | 49 | CurrenciesActivity.currencyConversions =
|
|
| - | 50 | new HashMap<String, HashMap<String, Double>>(); |
|
| - | 51 | ||
| - | 52 | HashMap<String, Double> conversionFactors = new HashMap<String, Double>(); |
|
| - | 53 | conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293); |
|
| - | 54 | conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413); |
|
| - | 55 | CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_CHF, |
|
| - | 56 | conversionFactors); |
|
| - | 57 | ||
| - | 58 | conversionFactors = new HashMap<String, Double>(); |
|
| - | 59 | conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077); |
|
| - | 60 | conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.3468); |
|
| - | 61 | CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_EUR, |
|
| - | 62 | conversionFactors); |
|
| - | 63 | ||
| - | 64 | conversionFactors = new HashMap<String, Double>(); |
|
| - | 65 | conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412); |
|
| - | 66 | conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743); |
|
| - | 67 | CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_USD, |
|
| - | 68 | conversionFactors); |
|
| - | 69 | ||
| 61 | final EditText editValue1 =
|
70 | final EditText editValue1 =
|
| 62 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
71 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
| 63 | final EditText editValue2 =
|
72 | final EditText editValue2 =
|
| 64 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
73 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
| 65 | 74 | ||
| Line 166... | Line 175... | ||
| 166 | */
|
175 | */
|
| 167 | private String getConvertedValue(double value, boolean reverse) |
176 | private String getConvertedValue(double value, boolean reverse) |
| 168 | {
|
177 | {
|
| 169 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
178 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
| 170 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
179 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
| 171 | String[] itemArray = |
180 | String[] items = |
| 172 | this.getResources().getStringArray(R.array.currency_units_values); |
181 | this.getResources().getStringArray(R.array.currency_units_values); |
| 173 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
182 | String selectedItemValue1 = items[selectedItemPosition1]; |
| 174 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
183 | String selectedItemValue2 = items[selectedItemPosition2]; |
| 175 | 184 | ||
| 176 | if (reverse) |
185 | if (reverse) |
| 177 | {
|
186 | {
|
| 178 | String tmp = selectedItemValue1; |
187 | String tmp = selectedItemValue1; |
| 179 | selectedItemValue1 = selectedItemValue2;
|
188 | selectedItemValue1 = selectedItemValue2;
|
| 180 | selectedItemValue2 = tmp;
|
189 | selectedItemValue2 = tmp;
|
| 181 | }
|
190 | }
|
| 182 | 191 | ||
| 183 | int itemId1 = CurrenciesActivity.valueToId.get(selectedItemValue1); |
- | |
| 184 | int itemId2 = CurrenciesActivity.valueToId.get(selectedItemValue2); |
- | |
| 185 | - | ||
| 186 | Double newValue = value; |
192 | Double newValue = value; |
| 187 | 193 | ||
| - | 194 | HashMap<String, Double> mapForCurrency = |
|
| - | 195 | CurrenciesActivity.currencyConversions.get(selectedItemValue1); |
|
| 188 | switch (itemId1) |
196 | if (mapForCurrency != null) |
| 189 | {
|
197 | {
|
| 190 | case ITEM_CHF:
|
- | |
| 191 | switch (itemId2) |
- | |
| 192 | {
|
- | |
| 193 | case ITEM_EUR:
|
- | |
| 194 | newValue = new Double(value * 0.767842293); |
198 | Double conversionFactor = mapForCurrency.get(selectedItemValue2); |
| 195 | break; |
- | |
| 196 | - | ||
| 197 | case ITEM_USD:
|
- | |
| 198 | newValue = new Double(value * 1.03413); |
- | |
| 199 | break; |
- | |
| 200 | }
|
- | |
| 201 | break; |
- | |
| 202 | - | ||
| 203 | case ITEM_EUR:
|
- | |
| 204 | switch (itemId2) |
199 | if (conversionFactor != null) |
| 205 | {
|
200 | {
|
| 206 | case ITEM_CHF:
|
- | |
| 207 | newValue = new Double(value * 1.30235077); |
- | |
| 208 | break; |
- | |
| 209 | - | ||
| 210 | case ITEM_USD:
|
- | |
| 211 | newValue = new Double(value * 1.3468); |
201 | newValue *= conversionFactor; |
| 212 | break; |
- | |
| 213 | }
|
- | |
| 214 | break; |
- | |
| 215 | - | ||
| 216 | case ITEM_USD:
|
- | |
| 217 | switch (itemId2) |
- | |
| 218 | {
|
- | |
| 219 | case ITEM_CHF:
|
- | |
| 220 | newValue = new Double(value * 0.966996412); |
- | |
| 221 | break; |
- | |
| 222 | - | ||
| 223 | case ITEM_EUR:
|
- | |
| 224 | newValue = new Double(value * 0.742500743); |
- | |
| 225 | break; |
- | |
| 226 | }
|
202 | }
|
| 227 | break; |
- | |
| 228 | }
|
203 | }
|
| 229 | 204 | ||
| 230 | return newValue.toString(); |
205 | return newValue.toString(); |
| 231 | }
|
206 | }
|
| - | 207 | ||
| - | 208 | /*
|
|
| - | 209 | * (non-Javadoc)
|
|
| - | 210 | *
|
|
| - | 211 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
|
|
| - | 212 | */
|
|
| - | 213 | @Override |
|
| - | 214 | public boolean onCreateOptionsMenu(Menu menu) |
|
| - | 215 | {
|
|
| - | 216 | MenuInflater inflater = this.getMenuInflater(); |
|
| - | 217 | inflater.inflate(R.menu.options, menu); |
|
| - | 218 | return true; |
|
| - | 219 | }
|
|
| - | 220 | ||
| - | 221 | /*
|
|
| - | 222 | * (non-Javadoc)
|
|
| - | 223 | *
|
|
| - | 224 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
|
|
| - | 225 | */
|
|
| - | 226 | @Override |
|
| - | 227 | public boolean onOptionsItemSelected(MenuItem item) |
|
| - | 228 | {
|
|
| - | 229 | /* update here */
|
|
| - | 230 | return super.onOptionsItemSelected(item); |
|
| - | 231 | }
|
|
| 232 | }
|
232 | }
|