Rev 6 | Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6 | Rev 7 | ||
---|---|---|---|
Line 23... | Line 23... | ||
23 | {
|
23 | {
|
24 | /*
|
24 | /*
|
25 | * Constants for mapping value strings to internal IDs
|
25 | * Constants for mapping value strings to internal IDs
|
26 | */
|
26 | */
|
27 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
27 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
- | 28 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
|
28 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
29 | 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_MILES = "mi"; //$NON-NLS-1$ |
30 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
- | |
31 | private static final int ITEM_INCHES = 0; |
31 | private static final int ITEM_INCHES = 0; |
32 | private static final int ITEM_METERS = 1; |
32 | private static final int ITEM_KILOMETERS = 1; |
33 | private static final int ITEM_MILES = 2; |
33 | private static final int ITEM_METERS = 2; |
34 | private static final int ITEM_KILOMETERS = 3; |
34 | private static final int ITEM_MILES = 3; |
35 | 35 | ||
36 | /**
|
36 | /**
|
37 | * Maps value strings to internal IDs
|
37 | * Maps value strings to internal IDs
|
38 | */
|
38 | */
|
39 | private final static HashMap<String, Integer> valueToId = |
39 | private final static HashMap<String, Integer> valueToId = |
40 | new HashMap<String, Integer>(); |
40 | new HashMap<String, Integer>(); |
41 | - | ||
42 | /* Unit spinners (dropdowns) */
|
- | |
43 | private Spinner spinnerUnit1; |
- | |
44 | private Spinner spinnerUnit2; |
- | |
45 | - | ||
46 | static
|
41 | static
|
47 | {
|
42 | {
|
48 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
43 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
49 | LengthsActivity.ITEM_INCHES); |
44 | LengthsActivity.ITEM_INCHES); |
- | 45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
|
- | 46 | LengthsActivity.ITEM_KILOMETERS); |
|
50 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
51 | LengthsActivity.ITEM_METERS); |
48 | LengthsActivity.ITEM_METERS); |
52 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
53 | LengthsActivity.ITEM_MILES); |
50 | LengthsActivity.ITEM_MILES); |
54 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
- | |
55 | LengthsActivity.ITEM_KILOMETERS); |
- | |
56 | }
|
51 | }
|
57 | 52 | ||
- | 53 | /* Unit spinners (dropdowns) */
|
|
- | 54 | private Spinner spinnerUnit1; |
|
- | 55 | private Spinner spinnerUnit2; |
|
- | 56 | ||
58 | /** Called when the activity is first created. */
|
57 | /** Called when the activity is first created. */
|
59 | 58 | ||
60 | @Override |
59 | @Override |
61 | public void onCreate(Bundle savedInstanceState) |
60 | public void onCreate(Bundle savedInstanceState) |
62 | {
|
61 | {
|
Line 70... | Line 69... | ||
70 | @Override |
69 | @Override |
71 | public boolean onKey(View v, int keyCode, KeyEvent event) |
70 | public boolean onKey(View v, int keyCode, KeyEvent event) |
72 | {
|
71 | {
|
73 | Editable editable1 = ((EditText) v).getText(); |
72 | Editable editable1 = ((EditText) v).getText(); |
74 | 73 | ||
75 | double value1; |
74 | Double value1; |
76 | try
|
75 | try
|
77 | {
|
76 | {
|
78 | value1 = Double.parseDouble(editable1.toString()); |
77 | value1 = Double.parseDouble(editable1.toString()); |
79 | }
|
78 | }
|
80 | catch (NumberFormatException e) |
79 | catch (NumberFormatException e) |
81 | {
|
80 | {
|
82 | value1 = -1.0; |
81 | value1 = null; |
83 | }
|
82 | }
|
84 | 83 | ||
85 | String string2 = ""; //$NON-NLS-1$ |
84 | String string2 = ""; //$NON-NLS-1$ |
86 | if (value1 >= 0.0) |
85 | if (value1 != null) |
87 | {
|
86 | {
|
88 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
87 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
89 | }
|
88 | }
|
90 | 89 | ||
91 | editValue2.setText(string2); |
90 | editValue2.setText(string2); |
Line 99... | Line 98... | ||
99 | @Override |
98 | @Override |
100 | public boolean onKey(View v, int keyCode, KeyEvent event) |
99 | public boolean onKey(View v, int keyCode, KeyEvent event) |
101 | {
|
100 | {
|
102 | Editable editable2 = ((EditText) v).getText(); |
101 | Editable editable2 = ((EditText) v).getText(); |
103 | 102 | ||
104 | double value2; |
103 | Double value2; |
105 | try
|
104 | try
|
106 | {
|
105 | {
|
107 | value2 = Double.parseDouble(editable2.toString()); |
106 | value2 = Double.parseDouble(editable2.toString()); |
108 | }
|
107 | }
|
109 | catch (NumberFormatException e) |
108 | catch (NumberFormatException e) |
110 | {
|
109 | {
|
111 | value2 = -1.0; |
110 | value2 = null; |
112 | }
|
111 | }
|
113 | 112 | ||
114 | String string1 = ""; //$NON-NLS-1$ |
113 | String string1 = ""; //$NON-NLS-1$ |
115 | if (value2 >= 0.0) |
114 | if (value2 != null) |
116 | {
|
115 | {
|
117 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
116 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
118 | }
|
117 | }
|
119 | 118 | ||
120 | editValue1.setText(string1); |
119 | editValue1.setText(string1); |
Line 141... | Line 140... | ||
141 | {
|
140 | {
|
142 | /* no-op */
|
141 | /* no-op */
|
143 | }
|
142 | }
|
144 | }); |
143 | }); |
145 | 144 | ||
- | 145 | this.spinnerUnit2.setSelection(1); |
|
146 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
146 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
147 | @Override |
147 | @Override |
148 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
148 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
149 | long arg3) |
149 | long arg3) |
150 | {
|
150 | {
|
Line 188... | Line 188... | ||
188 | switch (itemId1) |
188 | switch (itemId1) |
189 | {
|
189 | {
|
190 | case ITEM_INCHES:
|
190 | case ITEM_INCHES:
|
191 | switch (itemId2) |
191 | switch (itemId2) |
192 | {
|
192 | {
|
- | 193 | case ITEM_KILOMETERS:
|
|
- | 194 | /* see ITEM_METERS */
|
|
- | 195 | newValue = new Double((value * 0.0254) / 1000); |
|
- | 196 | break; |
|
- | 197 | ||
193 | case ITEM_METERS:
|
198 | case ITEM_METERS:
|
194 | newValue = new Double(value * 0.0254); |
199 | newValue = new Double(value * 0.0254); |
195 | break; |
200 | break; |
196 | 201 | ||
197 | case ITEM_MILES:
|
202 | case ITEM_MILES:
|
198 | /* 12 in/ft and 5280 ft/mi */
|
203 | /* 12 in/ft and 5280 ft/mi */
|
199 | newValue = new Double(value / 12 / 5280); |
204 | newValue = new Double(value / 12 / 5280); |
200 | break; |
205 | break; |
- | 206 | }
|
|
- | 207 | break; |
|
201 | 208 | ||
202 | case ITEM_KILOMETERS:
|
209 | case ITEM_KILOMETERS:
|
- | 210 | switch (itemId2) |
|
- | 211 | {
|
|
- | 212 | case ITEM_INCHES:
|
|
- | 213 | /* 1 m = 39.370 in */
|
|
- | 214 | newValue = new Double(value * 1000 * 39.370); |
|
- | 215 | break; |
|
- | 216 | ||
203 | /* see ITEM_METERS */
|
217 | case ITEM_METERS:
|
204 | newValue = new Double((value * 0.0254) / 1000); |
218 | newValue = new Double(value * 1000); |
- | 219 | break; |
|
- | 220 | ||
- | 221 | case ITEM_MILES:
|
|
- | 222 | newValue = new Double(value / 1.609344); |
|
205 | break; |
223 | break; |
206 | }
|
224 | }
|
207 | break; |
225 | break; |
208 | 226 | ||
209 | case ITEM_METERS:
|
227 | case ITEM_METERS:
|
210 | switch (itemId2) |
228 | switch (itemId2) |
211 | {
|
229 | {
|
212 | case ITEM_KILOMETERS:
|
- | |
213 | newValue = new Double(value / 1000); |
- | |
214 | break; |
- | |
215 | - | ||
216 | case ITEM_INCHES:
|
230 | case ITEM_INCHES:
|
217 | /* 1 m = 39.370 in */
|
231 | /* 1 m = 39.370 in */
|
218 | newValue = new Double(value * 39.370); |
232 | newValue = new Double(value * 39.370); |
219 | break; |
233 | break; |
220 | 234 | ||
- | 235 | case ITEM_KILOMETERS:
|
|
- | 236 | newValue = new Double(value / 1000); |
|
- | 237 | break; |
|
- | 238 | ||
221 | case ITEM_MILES:
|
239 | case ITEM_MILES:
|
222 | /* 1 mi = 1609.344 m */
|
240 | /* 1 mi = 1609.344 m */
|
223 | newValue = new Double(value / 1609.344); |
241 | newValue = new Double(value / 1609.344); |
224 | break; |
242 | break; |
225 | }
|
243 | }
|
Line 231... | Line 249... | ||
231 | case ITEM_INCHES:
|
249 | case ITEM_INCHES:
|
232 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
250 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
233 | newValue = new Double(value * 5280 * 12); |
251 | newValue = new Double(value * 5280 * 12); |
234 | break; |
252 | break; |
235 | 253 | ||
236 | case ITEM_METERS:
|
- | |
237 | newValue = new Double(value * 1609.344); |
- | |
238 | break; |
- | |
239 | - | ||
240 | case ITEM_KILOMETERS:
|
254 | case ITEM_KILOMETERS:
|
241 | newValue = new Double(value * 1.609344); |
255 | newValue = new Double(value * 1.609344); |
242 | break; |
256 | 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 | 257 | ||
254 | case ITEM_METERS:
|
258 | case ITEM_METERS:
|
255 | newValue = new Double(value * 1000); |
- | |
256 | break; |
- | |
257 | - | ||
258 | case ITEM_MILES:
|
- | |
259 | newValue = new Double(value / 1.609344); |
259 | newValue = new Double(value * 1609.344); |
260 | break; |
260 | break; |
261 | }
|
261 | }
|
- | 262 | break; |
|
262 | }
|
263 | }
|
263 | 264 | ||
264 | return newValue.toString(); |
265 | return newValue.toString(); |
265 | }
|
266 | }
|
266 | }
|
267 | }
|