Rev 5 | Rev 7 | 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 | |||
6 | PointedEar | 3 | import java.util.HashMap; |
4 | |||
5 | PointedEar | 5 | import android.app.Activity; |
6 | import android.os.Bundle; |
||
6 | 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; |
||
15 | import ch.ffhs.converter.R; |
||
5 | PointedEar | 16 | |
6 | PointedEar | 17 | /** |
18 | * Activity that implements length conversion |
||
19 | * |
||
20 | * @author pelinux |
||
21 | */ |
||
5 | PointedEar | 22 | public class LengthsActivity extends Activity |
23 | { |
||
6 | PointedEar | 24 | /* |
25 | * Constants for mapping value strings to internal IDs |
||
26 | */ |
||
27 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
||
28 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
||
29 | private static final String VALUE_MILES = "mi"; //$NON-NLS-1$ |
||
30 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
||
31 | private static final int ITEM_INCHES = 0; |
||
32 | private static final int ITEM_METERS = 1; |
||
33 | private static final int ITEM_MILES = 2; |
||
34 | private static final int ITEM_KILOMETERS = 3; |
||
35 | |||
36 | /** |
||
37 | * Maps value strings to internal IDs |
||
38 | */ |
||
39 | private final static HashMap<String, Integer> valueToId = |
||
40 | new HashMap<String, Integer>(); |
||
41 | |||
42 | /* Unit spinners (dropdowns) */ |
||
43 | private Spinner spinnerUnit1; |
||
44 | private Spinner spinnerUnit2; |
||
45 | |||
46 | static |
||
47 | { |
||
48 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
||
49 | LengthsActivity.ITEM_INCHES); |
||
50 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
||
51 | LengthsActivity.ITEM_METERS); |
||
52 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
||
53 | LengthsActivity.ITEM_MILES); |
||
54 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
||
55 | LengthsActivity.ITEM_KILOMETERS); |
||
56 | } |
||
57 | |||
5 | PointedEar | 58 | /** Called when the activity is first created. */ |
59 | |||
60 | @Override |
||
61 | public void onCreate(Bundle savedInstanceState) |
||
62 | { |
||
63 | super.onCreate(savedInstanceState); |
||
6 | PointedEar | 64 | this.setContentView(R.layout.activity_lengths); |
65 | |||
66 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
||
67 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
||
68 | |||
69 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
||
70 | @Override |
||
71 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
72 | { |
||
73 | Editable editable1 = ((EditText) v).getText(); |
||
74 | |||
75 | double value1; |
||
76 | try |
||
77 | { |
||
78 | value1 = Double.parseDouble(editable1.toString()); |
||
79 | } |
||
80 | catch (NumberFormatException e) |
||
81 | { |
||
82 | value1 = -1.0; |
||
83 | } |
||
84 | |||
85 | String string2 = ""; //$NON-NLS-1$ |
||
86 | if (value1 >= 0.0) |
||
87 | { |
||
88 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
||
89 | } |
||
90 | |||
91 | editValue2.setText(string2); |
||
92 | |||
93 | return false; |
||
94 | } |
||
95 | }; |
||
96 | editValue1.setOnKeyListener(editValue1OnKey); |
||
97 | |||
98 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
||
99 | @Override |
||
100 | public boolean onKey(View v, int keyCode, KeyEvent event) |
||
101 | { |
||
102 | Editable editable2 = ((EditText) v).getText(); |
||
103 | |||
104 | double value2; |
||
105 | try |
||
106 | { |
||
107 | value2 = Double.parseDouble(editable2.toString()); |
||
108 | } |
||
109 | catch (NumberFormatException e) |
||
110 | { |
||
111 | value2 = -1.0; |
||
112 | } |
||
113 | |||
114 | String string1 = ""; //$NON-NLS-1$ |
||
115 | if (value2 >= 0.0) |
||
116 | { |
||
117 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
||
118 | } |
||
119 | |||
120 | editValue1.setText(string1); |
||
121 | |||
122 | return false; |
||
123 | } |
||
124 | }; |
||
125 | editValue2.setOnKeyListener(editValue2OnKey); |
||
126 | |||
127 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
||
128 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
||
129 | |||
130 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
||
131 | @Override |
||
132 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
||
133 | long arg3) |
||
134 | { |
||
135 | /* Simulate input in second EditText so that first EditText is updated */ |
||
136 | editValue2OnKey.onKey(editValue2, 0, null); |
||
137 | } |
||
138 | |||
139 | @Override |
||
140 | public void onNothingSelected(AdapterView<?> arg0) |
||
141 | { |
||
142 | /* no-op */ |
||
143 | } |
||
144 | }); |
||
145 | |||
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 | } |
162 | |||
6 | PointedEar | 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.length_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 = LengthsActivity.valueToId.get(selectedItemValue1); |
||
184 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
||
185 | |||
186 | Double newValue = value; |
||
187 | |||
188 | switch (itemId1) |
||
189 | { |
||
190 | case ITEM_INCHES: |
||
191 | switch (itemId2) |
||
192 | { |
||
193 | case ITEM_METERS: |
||
194 | newValue = new Double(value * 0.0254); |
||
195 | break; |
||
196 | |||
197 | case ITEM_MILES: |
||
198 | /* 12 in/ft and 5280 ft/mi */ |
||
199 | newValue = new Double(value / 12 / 5280); |
||
200 | break; |
||
201 | |||
202 | case ITEM_KILOMETERS: |
||
203 | /* see ITEM_METERS */ |
||
204 | newValue = new Double((value * 0.0254) / 1000); |
||
205 | break; |
||
206 | } |
||
207 | break; |
||
208 | |||
209 | case ITEM_METERS: |
||
210 | switch (itemId2) |
||
211 | { |
||
212 | case ITEM_KILOMETERS: |
||
213 | newValue = new Double(value / 1000); |
||
214 | break; |
||
215 | |||
216 | case ITEM_INCHES: |
||
217 | /* 1 m = 39.370 in */ |
||
218 | newValue = new Double(value * 39.370); |
||
219 | break; |
||
220 | |||
221 | case ITEM_MILES: |
||
222 | /* 1 mi = 1609.344 m */ |
||
223 | newValue = new Double(value / 1609.344); |
||
224 | break; |
||
225 | } |
||
226 | break; |
||
227 | |||
228 | case ITEM_MILES: |
||
229 | switch (itemId2) |
||
230 | { |
||
231 | case ITEM_INCHES: |
||
232 | /* 1 mi = 5280 ft, 1 ft = 12 in */ |
||
233 | newValue = new Double(value * 5280 * 12); |
||
234 | break; |
||
235 | |||
236 | case ITEM_METERS: |
||
237 | newValue = new Double(value * 1609.344); |
||
238 | break; |
||
239 | |||
240 | case ITEM_KILOMETERS: |
||
241 | newValue = new Double(value * 1.609344); |
||
242 | break; |
||
243 | } |
||
244 | break; |
||
245 | |||
246 | case ITEM_KILOMETERS: |
||
247 | switch (itemId2) |
||
248 | { |
||
249 | case ITEM_INCHES: |
||
250 | /* 1 m = 39.370 in */ |
||
251 | newValue = new Double(value * 1000 * 39.370); |
||
252 | break; |
||
253 | |||
254 | case ITEM_METERS: |
||
255 | newValue = new Double(value * 1000); |
||
256 | break; |
||
257 | |||
258 | case ITEM_MILES: |
||
259 | newValue = new Double(value / 1.609344); |
||
260 | break; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return newValue.toString(); |
||
265 | } |
||
5 | PointedEar | 266 | } |