Rev 5 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5 | PointedEar | 1 | package ch.ffhs.converter.app; |
2 | |||
7 | PointedEar | 3 | import java.util.HashMap; |
4 | |||
5 | PointedEar | 5 | import android.app.Activity; |
6 | import android.os.Bundle; |
||
7 | PointedEar | 7 | import android.text.Editable; |
8 | import android.view.KeyEvent; |
||
9 | import android.view.View; |
||
10 | import android.view.View.OnKeyListener; |
||
11 | import android.widget.AdapterView; |
||
12 | import android.widget.AdapterView.OnItemSelectedListener; |
||
13 | import android.widget.EditText; |
||
14 | import android.widget.Spinner; |
||
5 | PointedEar | 15 | import ch.ffhs.converter.R; |
16 | |||
17 | /** |
||
7 | PointedEar | 18 | * Activity that implements currency conversion |
5 | PointedEar | 19 | * |
7 | PointedEar | 20 | * @author pelinux |
5 | PointedEar | 21 | */ |
22 | public class CurrenciesActivity extends Activity |
||
23 | { |
||
7 | PointedEar | 24 | /* |
25 | * Constants for mapping value strings to internal IDs |
||
26 | */ |
||
27 | private static final String VALUE_CHF = "CHF"; //$NON-NLS-1$ |
||
28 | private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$ |
||
29 | 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 | |||
5 | PointedEar | 34 | /** |
7 | PointedEar | 35 | * Maps value strings to internal IDs |
5 | PointedEar | 36 | */ |
7 | PointedEar | 37 | private final static HashMap<String, Integer> valueToId = |
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 | |||
49 | /* Unit spinners (dropdowns) */ |
||
50 | private Spinner spinnerUnit1; |
||
51 | private Spinner spinnerUnit2; |
||
52 | |||
53 | /** Called when the activity is first created. */ |
||
54 | |||
5 | PointedEar | 55 | @Override |
7 | PointedEar | 56 | public void onCreate(Bundle savedInstanceState) |
5 | PointedEar | 57 | { |
58 | super.onCreate(savedInstanceState); |
||
7 | PointedEar | 59 | this.setContentView(R.layout.activity_currencies); |
5 | PointedEar | 60 | |
7 | PointedEar | 61 | final EditText editValue1 = |
62 | (EditText) this.findViewById(R.id.currencies_edit_value1); |
||
63 | final EditText editValue2 = |
||
64 | (EditText) this.findViewById(R.id.currencies_edit_value2); |
||
65 | |||
66 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
||
67 | @Override |
||
68 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
69 | { |
||
70 | Editable editable1 = ((EditText) v).getText(); |
||
71 | |||
72 | Double value1; |
||
73 | try |
||
74 | { |
||
75 | value1 = Double.parseDouble(editable1.toString()); |
||
76 | } |
||
77 | catch (NumberFormatException e) |
||
78 | { |
||
79 | value1 = null; |
||
80 | } |
||
81 | |||
82 | String string2 = ""; //$NON-NLS-1$ |
||
83 | if (value1 != null) |
||
84 | { |
||
85 | string2 = CurrenciesActivity.this.getConvertedValue(value1, false); |
||
86 | } |
||
87 | |||
88 | editValue2.setText(string2); |
||
89 | |||
90 | return false; |
||
91 | } |
||
92 | }; |
||
93 | editValue1.setOnKeyListener(editValue1OnKey); |
||
94 | |||
95 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
||
96 | @Override |
||
97 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
98 | { |
||
99 | Editable editable2 = ((EditText) v).getText(); |
||
100 | |||
101 | Double value2; |
||
102 | try |
||
103 | { |
||
104 | value2 = Double.parseDouble(editable2.toString()); |
||
105 | } |
||
106 | catch (NumberFormatException e) |
||
107 | { |
||
108 | value2 = null; |
||
109 | } |
||
110 | |||
111 | String string1 = ""; //$NON-NLS-1$ |
||
112 | if (value2 != null) |
||
113 | { |
||
114 | string1 = CurrenciesActivity.this.getConvertedValue(value2, true); |
||
115 | } |
||
116 | |||
117 | editValue1.setText(string1); |
||
118 | |||
119 | return false; |
||
120 | } |
||
121 | }; |
||
122 | editValue2.setOnKeyListener(editValue2OnKey); |
||
123 | |||
124 | this.spinnerUnit1 = |
||
125 | (Spinner) this.findViewById(R.id.currencies_spinner_unit1); |
||
126 | this.spinnerUnit2 = |
||
127 | (Spinner) this.findViewById(R.id.currencies_spinner_unit2); |
||
128 | |||
129 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
130 | @Override |
||
131 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
132 | long arg3) |
||
133 | { |
||
134 | /* Simulate input in second EditText so that first EditText is updated */ |
||
135 | editValue2OnKey.onKey(editValue2, 0, null); |
||
136 | } |
||
137 | |||
138 | @Override |
||
139 | public void onNothingSelected(AdapterView<?> arg0) |
||
140 | { |
||
141 | /* no-op */ |
||
142 | } |
||
143 | }); |
||
144 | |||
145 | this.spinnerUnit2.setSelection(1); |
||
146 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
147 | @Override |
||
148 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
149 | long arg3) |
||
150 | { |
||
151 | /* Simulate input in first EditText so that second EditText is updated */ |
||
152 | editValue1OnKey.onKey(editValue1, 0, null); |
||
153 | } |
||
154 | |||
155 | @Override |
||
156 | public void onNothingSelected(AdapterView<?> arg0) |
||
157 | { |
||
158 | /* no-op */ |
||
159 | } |
||
160 | }); |
||
5 | PointedEar | 161 | } |
7 | PointedEar | 162 | |
163 | /** |
||
164 | * @param value |
||
165 | * @return |
||
166 | */ |
||
167 | private String getConvertedValue(double value, boolean reverse) |
||
168 | { |
||
169 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
||
170 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
||
171 | String[] itemArray = |
||
172 | this.getResources().getStringArray(R.array.currency_units_values); |
||
173 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
||
174 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
||
175 | |||
176 | if (reverse) |
||
177 | { |
||
178 | String tmp = selectedItemValue1; |
||
179 | selectedItemValue1 = selectedItemValue2; |
||
180 | selectedItemValue2 = tmp; |
||
181 | } |
||
182 | |||
183 | int itemId1 = CurrenciesActivity.valueToId.get(selectedItemValue1); |
||
184 | int itemId2 = CurrenciesActivity.valueToId.get(selectedItemValue2); |
||
185 | |||
186 | Double newValue = value; |
||
187 | |||
188 | switch (itemId1) |
||
189 | { |
||
190 | case ITEM_CHF: |
||
191 | switch (itemId2) |
||
192 | { |
||
193 | case ITEM_EUR: |
||
194 | newValue = new Double(value * 0.767842293); |
||
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) |
||
205 | { |
||
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); |
||
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 | } |
||
227 | break; |
||
228 | } |
||
229 | |||
230 | return newValue.toString(); |
||
231 | } |
||
5 | PointedEar | 232 | } |