Rev 6 | Rev 8 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6 | Rev 7 | ||
---|---|---|---|
1 | package ch.ffhs.converter.app; |
1 | package ch.ffhs.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.View; |
9 | import android.view.View; |
10 | import android.view.View.OnKeyListener; |
10 | import android.view.View.OnKeyListener; |
11 | import android.widget.AdapterView; |
11 | import android.widget.AdapterView; |
12 | import android.widget.AdapterView.OnItemSelectedListener; |
12 | import android.widget.AdapterView.OnItemSelectedListener; |
13 | import android.widget.EditText; |
13 | import android.widget.EditText; |
14 | import android.widget.Spinner; |
14 | import android.widget.Spinner; |
15 | import ch.ffhs.converter.R; |
15 | import ch.ffhs.converter.R; |
16 | 16 | ||
17 | /**
|
17 | /**
|
18 | * Activity that implements length conversion
|
18 | * Activity that implements length conversion
|
19 | *
|
19 | *
|
20 | * @author pelinux
|
20 | * @author pelinux
|
21 | */
|
21 | */
|
22 | public class LengthsActivity extends Activity |
22 | public class LengthsActivity extends Activity |
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 | {
|
63 | super.onCreate(savedInstanceState); |
62 | super.onCreate(savedInstanceState); |
64 | this.setContentView(R.layout.activity_lengths); |
63 | this.setContentView(R.layout.activity_lengths); |
65 | 64 | ||
66 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
65 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
67 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
66 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
68 | 67 | ||
69 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
68 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
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); |
92 | 91 | ||
93 | return false; |
92 | return false; |
94 | }
|
93 | }
|
95 | }; |
94 | }; |
96 | editValue1.setOnKeyListener(editValue1OnKey); |
95 | editValue1.setOnKeyListener(editValue1OnKey); |
97 | 96 | ||
98 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
97 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
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); |
121 | 120 | ||
122 | return false; |
121 | return false; |
123 | }
|
122 | }
|
124 | }; |
123 | }; |
125 | editValue2.setOnKeyListener(editValue2OnKey); |
124 | editValue2.setOnKeyListener(editValue2OnKey); |
126 | 125 | ||
127 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
126 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
128 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
127 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
129 | 128 | ||
130 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
129 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
131 | @Override |
130 | @Override |
132 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
131 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
133 | long arg3) |
132 | long arg3) |
134 | {
|
133 | {
|
135 | /* Simulate input in second EditText so that first EditText is updated */
|
134 | /* Simulate input in second EditText so that first EditText is updated */
|
136 | editValue2OnKey.onKey(editValue2, 0, null); |
135 | editValue2OnKey.onKey(editValue2, 0, null); |
137 | }
|
136 | }
|
138 | 137 | ||
139 | @Override |
138 | @Override |
140 | public void onNothingSelected(AdapterView<?> arg0) |
139 | public void onNothingSelected(AdapterView<?> arg0) |
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 | {
|
151 | /* Simulate input in first EditText so that second EditText is updated */
|
151 | /* Simulate input in first EditText so that second EditText is updated */
|
152 | editValue1OnKey.onKey(editValue1, 0, null); |
152 | editValue1OnKey.onKey(editValue1, 0, null); |
153 | }
|
153 | }
|
154 | 154 | ||
155 | @Override |
155 | @Override |
156 | public void onNothingSelected(AdapterView<?> arg0) |
156 | public void onNothingSelected(AdapterView<?> arg0) |
157 | {
|
157 | {
|
158 | /* no-op */
|
158 | /* no-op */
|
159 | }
|
159 | }
|
160 | }); |
160 | }); |
161 | }
|
161 | }
|
162 | 162 | ||
163 | /**
|
163 | /**
|
164 | * @param value
|
164 | * @param value
|
165 | * @return
|
165 | * @return
|
166 | */
|
166 | */
|
167 | private String getConvertedValue(double value, boolean reverse) |
167 | private String getConvertedValue(double value, boolean reverse) |
168 | {
|
168 | {
|
169 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
169 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
170 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
170 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
171 | String[] itemArray = |
171 | String[] itemArray = |
172 | this.getResources().getStringArray(R.array.length_units_values); |
172 | this.getResources().getStringArray(R.array.length_units_values); |
173 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
173 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
174 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
174 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
175 | 175 | ||
176 | if (reverse) |
176 | if (reverse) |
177 | {
|
177 | {
|
178 | String tmp = selectedItemValue1; |
178 | String tmp = selectedItemValue1; |
179 | selectedItemValue1 = selectedItemValue2;
|
179 | selectedItemValue1 = selectedItemValue2;
|
180 | selectedItemValue2 = tmp;
|
180 | selectedItemValue2 = tmp;
|
181 | }
|
181 | }
|
182 | 182 | ||
183 | int itemId1 = LengthsActivity.valueToId.get(selectedItemValue1); |
183 | int itemId1 = LengthsActivity.valueToId.get(selectedItemValue1); |
184 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
184 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
185 | 185 | ||
186 | Double newValue = value; |
186 | Double newValue = value; |
187 | 187 | ||
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 | }
|
226 | break; |
244 | break; |
227 | 245 | ||
228 | case ITEM_MILES:
|
246 | case ITEM_MILES:
|
229 | switch (itemId2) |
247 | switch (itemId2) |
230 | {
|
248 | {
|
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 | }
|
267 | 268 |