Rev 7 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7 | Rev 8 | ||
---|---|---|---|
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.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 de.pointedears.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_KILOMETERS = "km"; //$NON-NLS-1$ |
29 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
29 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
30 | private static final String VALUE_MILES = "mi"; //$NON-NLS-1$ |
30 | private static final String VALUE_MILES = "mi"; //$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_KILOMETERS = 1; |
32 | private static final int ITEM_KILOMETERS = 1; |
33 | private static final int ITEM_METERS = 2; |
33 | private static final int ITEM_METERS = 2; |
34 | private static final int ITEM_MILES = 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 | static
|
41 | static
|
42 | {
|
42 | {
|
43 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
43 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
44 | LengthsActivity.ITEM_INCHES); |
44 | LengthsActivity.ITEM_INCHES); |
45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
46 | LengthsActivity.ITEM_KILOMETERS); |
46 | LengthsActivity.ITEM_KILOMETERS); |
47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
48 | LengthsActivity.ITEM_METERS); |
48 | LengthsActivity.ITEM_METERS); |
49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
50 | LengthsActivity.ITEM_MILES); |
50 | LengthsActivity.ITEM_MILES); |
51 | }
|
51 | }
|
52 | 52 | ||
53 | /* Unit spinners (dropdowns) */
|
53 | /* Unit spinners (dropdowns) */
|
54 | private Spinner spinnerUnit1; |
54 | private Spinner spinnerUnit1; |
55 | private Spinner spinnerUnit2; |
55 | private Spinner spinnerUnit2; |
56 | 56 | ||
57 | /** Called when the activity is first created. */
|
57 | /** Called when the activity is first created. */
|
58 | 58 | ||
59 | @Override |
59 | @Override |
60 | public void onCreate(Bundle savedInstanceState) |
60 | public void onCreate(Bundle savedInstanceState) |
61 | {
|
61 | {
|
62 | super.onCreate(savedInstanceState); |
62 | super.onCreate(savedInstanceState); |
63 | this.setContentView(R.layout.activity_lengths); |
63 | this.setContentView(R.layout.activity_lengths); |
64 | 64 | ||
65 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
65 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
66 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
66 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
67 | 67 | ||
68 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
68 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
69 | @Override |
69 | @Override |
70 | public boolean onKey(View v, int keyCode, KeyEvent event) |
70 | public boolean onKey(View v, int keyCode, KeyEvent event) |
71 | {
|
71 | {
|
72 | Editable editable1 = ((EditText) v).getText(); |
72 | Editable editable1 = ((EditText) v).getText(); |
73 | 73 | ||
74 | Double value1; |
74 | Double value1; |
75 | try
|
75 | try
|
76 | {
|
76 | {
|
77 | value1 = Double.parseDouble(editable1.toString()); |
77 | value1 = Double.parseDouble(editable1.toString()); |
78 | }
|
78 | }
|
79 | catch (NumberFormatException e) |
79 | catch (NumberFormatException e) |
80 | {
|
80 | {
|
81 | value1 = null; |
81 | value1 = null; |
82 | }
|
82 | }
|
83 | 83 | ||
84 | String string2 = ""; //$NON-NLS-1$ |
84 | String string2 = ""; //$NON-NLS-1$ |
85 | if (value1 != null) |
85 | if (value1 != null) |
86 | {
|
86 | {
|
87 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
87 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
88 | }
|
88 | }
|
89 | 89 | ||
90 | editValue2.setText(string2); |
90 | editValue2.setText(string2); |
91 | 91 | ||
92 | return false; |
92 | return false; |
93 | }
|
93 | }
|
94 | }; |
94 | }; |
95 | editValue1.setOnKeyListener(editValue1OnKey); |
95 | editValue1.setOnKeyListener(editValue1OnKey); |
96 | 96 | ||
97 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
97 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
98 | @Override |
98 | @Override |
99 | public boolean onKey(View v, int keyCode, KeyEvent event) |
99 | public boolean onKey(View v, int keyCode, KeyEvent event) |
100 | {
|
100 | {
|
101 | Editable editable2 = ((EditText) v).getText(); |
101 | Editable editable2 = ((EditText) v).getText(); |
102 | 102 | ||
103 | Double value2; |
103 | Double value2; |
104 | try
|
104 | try
|
105 | {
|
105 | {
|
106 | value2 = Double.parseDouble(editable2.toString()); |
106 | value2 = Double.parseDouble(editable2.toString()); |
107 | }
|
107 | }
|
108 | catch (NumberFormatException e) |
108 | catch (NumberFormatException e) |
109 | {
|
109 | {
|
110 | value2 = null; |
110 | value2 = null; |
111 | }
|
111 | }
|
112 | 112 | ||
113 | String string1 = ""; //$NON-NLS-1$ |
113 | String string1 = ""; //$NON-NLS-1$ |
114 | if (value2 != null) |
114 | if (value2 != null) |
115 | {
|
115 | {
|
116 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
116 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
117 | }
|
117 | }
|
118 | 118 | ||
119 | editValue1.setText(string1); |
119 | editValue1.setText(string1); |
120 | 120 | ||
121 | return false; |
121 | return false; |
122 | }
|
122 | }
|
123 | }; |
123 | }; |
124 | editValue2.setOnKeyListener(editValue2OnKey); |
124 | editValue2.setOnKeyListener(editValue2OnKey); |
125 | 125 | ||
126 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
126 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
127 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
127 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
128 | 128 | ||
129 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
129 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
130 | @Override |
130 | @Override |
131 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
131 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
132 | long arg3) |
132 | long arg3) |
133 | {
|
133 | {
|
134 | /* Simulate input in second EditText so that first EditText is updated */
|
134 | /* Simulate input in second EditText so that first EditText is updated */
|
135 | editValue2OnKey.onKey(editValue2, 0, null); |
135 | editValue2OnKey.onKey(editValue2, 0, null); |
136 | }
|
136 | }
|
137 | 137 | ||
138 | @Override |
138 | @Override |
139 | public void onNothingSelected(AdapterView<?> arg0) |
139 | public void onNothingSelected(AdapterView<?> arg0) |
140 | {
|
140 | {
|
141 | /* no-op */
|
141 | /* no-op */
|
142 | }
|
142 | }
|
143 | }); |
143 | }); |
144 | 144 | ||
145 | this.spinnerUnit2.setSelection(1); |
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:
|
193 | case ITEM_KILOMETERS:
|
194 | /* see ITEM_METERS */
|
194 | /* see ITEM_METERS */
|
195 | newValue = new Double((value * 0.0254) / 1000); |
195 | newValue = new Double((value * 0.0254) / 1000); |
196 | break; |
196 | break; |
197 | 197 | ||
198 | case ITEM_METERS:
|
198 | case ITEM_METERS:
|
199 | newValue = new Double(value * 0.0254); |
199 | newValue = new Double(value * 0.0254); |
200 | break; |
200 | break; |
201 | 201 | ||
202 | case ITEM_MILES:
|
202 | case ITEM_MILES:
|
203 | /* 12 in/ft and 5280 ft/mi */
|
203 | /* 12 in/ft and 5280 ft/mi */
|
204 | newValue = new Double(value / 12 / 5280); |
204 | newValue = new Double(value / 12 / 5280); |
205 | break; |
205 | break; |
206 | }
|
206 | }
|
207 | break; |
207 | break; |
208 | 208 | ||
209 | case ITEM_KILOMETERS:
|
209 | case ITEM_KILOMETERS:
|
210 | switch (itemId2) |
210 | switch (itemId2) |
211 | {
|
211 | {
|
212 | case ITEM_INCHES:
|
212 | case ITEM_INCHES:
|
213 | /* 1 m = 39.370 in */
|
213 | /* 1 m = 39.370 in */
|
214 | newValue = new Double(value * 1000 * 39.370); |
214 | newValue = new Double(value * 1000 * 39.370); |
215 | break; |
215 | break; |
216 | 216 | ||
217 | case ITEM_METERS:
|
217 | case ITEM_METERS:
|
218 | newValue = new Double(value * 1000); |
218 | newValue = new Double(value * 1000); |
219 | break; |
219 | break; |
220 | 220 | ||
221 | case ITEM_MILES:
|
221 | case ITEM_MILES:
|
222 | newValue = new Double(value / 1.609344); |
222 | newValue = new Double(value / 1.609344); |
223 | break; |
223 | break; |
224 | }
|
224 | }
|
225 | break; |
225 | break; |
226 | 226 | ||
227 | case ITEM_METERS:
|
227 | case ITEM_METERS:
|
228 | switch (itemId2) |
228 | switch (itemId2) |
229 | {
|
229 | {
|
230 | case ITEM_INCHES:
|
230 | case ITEM_INCHES:
|
231 | /* 1 m = 39.370 in */
|
231 | /* 1 m = 39.370 in */
|
232 | newValue = new Double(value * 39.370); |
232 | newValue = new Double(value * 39.370); |
233 | break; |
233 | break; |
234 | 234 | ||
235 | case ITEM_KILOMETERS:
|
235 | case ITEM_KILOMETERS:
|
236 | newValue = new Double(value / 1000); |
236 | newValue = new Double(value / 1000); |
237 | break; |
237 | break; |
238 | 238 | ||
239 | case ITEM_MILES:
|
239 | case ITEM_MILES:
|
240 | /* 1 mi = 1609.344 m */
|
240 | /* 1 mi = 1609.344 m */
|
241 | newValue = new Double(value / 1609.344); |
241 | newValue = new Double(value / 1609.344); |
242 | break; |
242 | break; |
243 | }
|
243 | }
|
244 | break; |
244 | break; |
245 | 245 | ||
246 | case ITEM_MILES:
|
246 | case ITEM_MILES:
|
247 | switch (itemId2) |
247 | switch (itemId2) |
248 | {
|
248 | {
|
249 | case ITEM_INCHES:
|
249 | case ITEM_INCHES:
|
250 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
250 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
251 | newValue = new Double(value * 5280 * 12); |
251 | newValue = new Double(value * 5280 * 12); |
252 | break; |
252 | break; |
253 | 253 | ||
254 | case ITEM_KILOMETERS:
|
254 | case ITEM_KILOMETERS:
|
255 | newValue = new Double(value * 1.609344); |
255 | newValue = new Double(value * 1.609344); |
256 | break; |
256 | break; |
257 | 257 | ||
258 | case ITEM_METERS:
|
258 | case ITEM_METERS:
|
259 | newValue = new Double(value * 1609.344); |
259 | newValue = new Double(value * 1609.344); |
260 | break; |
260 | break; |
261 | }
|
261 | }
|
262 | break; |
262 | break; |
263 | }
|
263 | }
|
264 | 264 | ||
265 | return newValue.toString(); |
265 | return newValue.toString(); |
266 | }
|
266 | }
|
267 | }
|
267 | }
|
268 | 268 |