Rev 8 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8 | Rev 13 | ||
---|---|---|---|
1 | package de.pointedears.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.OnClickListener; |
|
10 | import android.view.View.OnKeyListener; |
11 | import android.view.View.OnKeyListener; |
11 | import android.widget.AdapterView; |
12 | import android.widget.AdapterView; |
12 | import android.widget.AdapterView.OnItemSelectedListener; |
13 | import android.widget.AdapterView.OnItemSelectedListener; |
- | 14 | import android.widget.Button; |
|
13 | import android.widget.EditText; |
15 | import android.widget.EditText; |
14 | import android.widget.Spinner; |
16 | import android.widget.Spinner; |
15 | import de.pointedears.converter.R; |
17 | import de.pointedears.converter.R; |
16 | 18 | ||
17 | /**
|
19 | /**
|
18 | * Activity that implements length conversion
|
20 | * Activity that implements length conversion
|
19 | *
|
21 | *
|
20 | * @author pelinux
|
22 | * @author pelinux
|
21 | */
|
23 | */
|
22 | public class LengthsActivity extends Activity |
24 | public class LengthsActivity extends Activity |
23 | {
|
25 | {
|
24 | /*
|
26 | /*
|
25 | * Constants for mapping value strings to internal IDs
|
27 | * Constants for mapping value strings to internal IDs
|
26 | */
|
28 | */
|
27 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
29 | private static final String VALUE_INCHES = "inch"; //$NON-NLS-1$ |
28 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
30 | private static final String VALUE_KILOMETERS = "km"; //$NON-NLS-1$ |
29 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
31 | private static final String VALUE_METERS = "m"; //$NON-NLS-1$ |
30 | private static final String VALUE_MILES = "mi"; //$NON-NLS-1$ |
32 | private static final String VALUE_MILES = "mi"; //$NON-NLS-1$ |
31 | private static final int ITEM_INCHES = 0; |
33 | private static final int ITEM_INCHES = 0; |
32 | private static final int ITEM_KILOMETERS = 1; |
34 | private static final int ITEM_KILOMETERS = 1; |
33 | private static final int ITEM_METERS = 2; |
35 | private static final int ITEM_METERS = 2; |
34 | private static final int ITEM_MILES = 3; |
36 | private static final int ITEM_MILES = 3; |
35 | 37 | ||
36 | /**
|
38 | /**
|
37 | * Maps value strings to internal IDs
|
39 | * Maps value strings to internal IDs
|
38 | */
|
40 | */
|
39 | private final static HashMap<String, Integer> valueToId = |
41 | private final static HashMap<String, Integer> valueToId = |
40 | new HashMap<String, Integer>(); |
42 | new HashMap<String, Integer>(); |
41 | static
|
43 | static
|
42 | {
|
44 | {
|
43 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_INCHES, |
44 | LengthsActivity.ITEM_INCHES); |
46 | LengthsActivity.ITEM_INCHES); |
45 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_KILOMETERS, |
46 | LengthsActivity.ITEM_KILOMETERS); |
48 | LengthsActivity.ITEM_KILOMETERS); |
47 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_METERS, |
48 | LengthsActivity.ITEM_METERS); |
50 | LengthsActivity.ITEM_METERS); |
49 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
51 | LengthsActivity.valueToId.put(LengthsActivity.VALUE_MILES, |
50 | LengthsActivity.ITEM_MILES); |
52 | LengthsActivity.ITEM_MILES); |
51 | }
|
53 | }
|
52 | 54 | ||
53 | /* Unit spinners (dropdowns) */
|
55 | /* Unit spinners (dropdowns) */
|
54 | private Spinner spinnerUnit1; |
56 | private Spinner spinnerUnit1; |
55 | private Spinner spinnerUnit2; |
57 | private Spinner spinnerUnit2; |
56 | 58 | ||
57 | /** Called when the activity is first created. */
|
59 | /** Called when the activity is first created. */
|
58 | 60 | ||
59 | @Override |
61 | @Override |
60 | public void onCreate(Bundle savedInstanceState) |
62 | public void onCreate(Bundle savedInstanceState) |
61 | {
|
63 | {
|
62 | super.onCreate(savedInstanceState); |
64 | super.onCreate(savedInstanceState); |
63 | this.setContentView(R.layout.activity_lengths); |
65 | this.setContentView(R.layout.activity_lengths); |
64 | 66 | ||
65 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
67 | final EditText editValue1 = (EditText) this.findViewById(R.id.edit_value1); |
66 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
68 | final EditText editValue2 = (EditText) this.findViewById(R.id.edit_value2); |
67 | 69 | ||
68 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
70 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
69 | @Override |
71 | @Override |
70 | public boolean onKey(View v, int keyCode, KeyEvent event) |
72 | public boolean onKey(View v, int keyCode, KeyEvent event) |
71 | {
|
73 | {
|
72 | Editable editable1 = ((EditText) v).getText(); |
74 | Editable editable1 = ((EditText) v).getText(); |
73 | 75 | ||
74 | Double value1; |
76 | Double value1; |
75 | try
|
77 | try
|
76 | {
|
78 | {
|
77 | value1 = Double.parseDouble(editable1.toString()); |
79 | value1 = Double.parseDouble(editable1.toString()); |
78 | }
|
80 | }
|
79 | catch (NumberFormatException e) |
81 | catch (NumberFormatException e) |
80 | {
|
82 | {
|
81 | value1 = null; |
83 | value1 = null; |
82 | }
|
84 | }
|
83 | 85 | ||
84 | String string2 = ""; //$NON-NLS-1$ |
86 | String string2 = ""; //$NON-NLS-1$ |
85 | if (value1 != null) |
87 | if (value1 != null) |
86 | {
|
88 | {
|
87 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
89 | string2 = LengthsActivity.this.getConvertedValue(value1, false); |
88 | }
|
90 | }
|
89 | 91 | ||
90 | editValue2.setText(string2); |
92 | editValue2.setText(string2); |
91 | 93 | ||
92 | return false; |
94 | return false; |
93 | }
|
95 | }
|
94 | }; |
96 | }; |
95 | editValue1.setOnKeyListener(editValue1OnKey); |
97 | editValue1.setOnKeyListener(editValue1OnKey); |
96 | 98 | ||
97 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
99 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
98 | @Override |
100 | @Override |
99 | public boolean onKey(View v, int keyCode, KeyEvent event) |
101 | public boolean onKey(View v, int keyCode, KeyEvent event) |
100 | {
|
102 | {
|
101 | Editable editable2 = ((EditText) v).getText(); |
103 | Editable editable2 = ((EditText) v).getText(); |
102 | 104 | ||
103 | Double value2; |
105 | Double value2; |
104 | try
|
106 | try
|
105 | {
|
107 | {
|
106 | value2 = Double.parseDouble(editable2.toString()); |
108 | value2 = Double.parseDouble(editable2.toString()); |
107 | }
|
109 | }
|
108 | catch (NumberFormatException e) |
110 | catch (NumberFormatException e) |
109 | {
|
111 | {
|
110 | value2 = null; |
112 | value2 = null; |
111 | }
|
113 | }
|
112 | 114 | ||
113 | String string1 = ""; //$NON-NLS-1$ |
115 | String string1 = ""; //$NON-NLS-1$ |
114 | if (value2 != null) |
116 | if (value2 != null) |
115 | {
|
117 | {
|
116 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
118 | string1 = LengthsActivity.this.getConvertedValue(value2, true); |
117 | }
|
119 | }
|
118 | 120 | ||
119 | editValue1.setText(string1); |
121 | editValue1.setText(string1); |
120 | 122 | ||
121 | return false; |
123 | return false; |
122 | }
|
124 | }
|
123 | }; |
125 | }; |
124 | editValue2.setOnKeyListener(editValue2OnKey); |
126 | editValue2.setOnKeyListener(editValue2OnKey); |
125 | 127 | ||
126 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
128 | this.spinnerUnit1 = (Spinner) this.findViewById(R.id.spinner_unit1); |
127 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
129 | this.spinnerUnit2 = (Spinner) this.findViewById(R.id.spinner_unit2); |
128 | 130 | ||
129 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
131 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
130 | @Override |
132 | @Override |
131 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
133 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
132 | long arg3) |
134 | long arg3) |
133 | {
|
135 | {
|
134 | /* Simulate input in second EditText so that first EditText is updated */
|
136 | /* Simulate input in second EditText so that first EditText is updated */
|
135 | editValue2OnKey.onKey(editValue2, 0, null); |
137 | editValue2OnKey.onKey(editValue2, 0, null); |
136 | }
|
138 | }
|
137 | 139 | ||
138 | @Override |
140 | @Override |
139 | public void onNothingSelected(AdapterView<?> arg0) |
141 | public void onNothingSelected(AdapterView<?> arg0) |
140 | {
|
142 | {
|
141 | /* no-op */
|
143 | /* no-op */
|
142 | }
|
144 | }
|
143 | }); |
145 | }); |
144 | 146 | ||
145 | this.spinnerUnit2.setSelection(1); |
147 | this.spinnerUnit2.setSelection(1); |
146 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
148 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
147 | @Override |
149 | @Override |
148 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
150 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
149 | long arg3) |
151 | long arg3) |
150 | {
|
152 | {
|
151 | /* Simulate input in first EditText so that second EditText is updated */
|
153 | /* Simulate input in first EditText so that second EditText is updated */
|
152 | editValue1OnKey.onKey(editValue1, 0, null); |
154 | editValue1OnKey.onKey(editValue1, 0, null); |
153 | }
|
155 | }
|
154 | 156 | ||
155 | @Override |
157 | @Override |
156 | public void onNothingSelected(AdapterView<?> arg0) |
158 | public void onNothingSelected(AdapterView<?> arg0) |
157 | {
|
159 | {
|
158 | /* no-op */
|
160 | /* no-op */
|
159 | }
|
161 | }
|
- | 162 | }); |
|
- | 163 | ||
- | 164 | Button buttonClear = |
|
- | 165 | (Button) this.findViewById(R.id.lengths_button_clear); |
|
- | 166 | buttonClear.setOnClickListener(new OnClickListener() { |
|
- | 167 | ||
- | 168 | @SuppressWarnings("nls") |
|
- | 169 | @Override |
|
- | 170 | public void onClick(View v) |
|
- | 171 | {
|
|
- | 172 | editValue1.setText(""); |
|
- | 173 | editValue2.setText(""); |
|
- | 174 | }
|
|
160 | }); |
175 | }); |
161 | }
|
176 | }
|
162 | 177 | ||
163 | /**
|
178 | /**
|
164 | * @param value
|
179 | * @param value
|
165 | * @return
|
180 | * @return
|
166 | */
|
181 | */
|
167 | private String getConvertedValue(double value, boolean reverse) |
182 | private String getConvertedValue(double value, boolean reverse) |
168 | {
|
183 | {
|
169 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
184 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
170 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
185 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
171 | String[] itemArray = |
186 | String[] itemArray = |
172 | this.getResources().getStringArray(R.array.length_units_values); |
187 | this.getResources().getStringArray(R.array.length_units_values); |
173 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
188 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
174 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
189 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
175 | 190 | ||
176 | if (reverse) |
191 | if (reverse) |
177 | {
|
192 | {
|
178 | String tmp = selectedItemValue1; |
193 | String tmp = selectedItemValue1; |
179 | selectedItemValue1 = selectedItemValue2;
|
194 | selectedItemValue1 = selectedItemValue2;
|
180 | selectedItemValue2 = tmp;
|
195 | selectedItemValue2 = tmp;
|
181 | }
|
196 | }
|
182 | 197 | ||
183 | int itemId1 = LengthsActivity.valueToId.get(selectedItemValue1); |
198 | int itemId1 = LengthsActivity.valueToId.get(selectedItemValue1); |
184 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
199 | int itemId2 = LengthsActivity.valueToId.get(selectedItemValue2); |
185 | 200 | ||
186 | Double newValue = value; |
201 | Double newValue = value; |
187 | 202 | ||
188 | switch (itemId1) |
203 | switch (itemId1) |
189 | {
|
204 | {
|
190 | case ITEM_INCHES:
|
205 | case ITEM_INCHES:
|
191 | switch (itemId2) |
206 | switch (itemId2) |
192 | {
|
207 | {
|
193 | case ITEM_KILOMETERS:
|
208 | case ITEM_KILOMETERS:
|
194 | /* see ITEM_METERS */
|
209 | /* see ITEM_METERS */
|
195 | newValue = new Double((value * 0.0254) / 1000); |
210 | newValue = new Double((value * 0.0254) / 1000); |
196 | break; |
211 | break; |
197 | 212 | ||
198 | case ITEM_METERS:
|
213 | case ITEM_METERS:
|
199 | newValue = new Double(value * 0.0254); |
214 | newValue = new Double(value * 0.0254); |
200 | break; |
215 | break; |
201 | 216 | ||
202 | case ITEM_MILES:
|
217 | case ITEM_MILES:
|
203 | /* 12 in/ft and 5280 ft/mi */
|
218 | /* 12 in/ft and 5280 ft/mi */
|
204 | newValue = new Double(value / 12 / 5280); |
219 | newValue = new Double(value / 12 / 5280); |
205 | break; |
220 | break; |
206 | }
|
221 | }
|
207 | break; |
222 | break; |
208 | 223 | ||
209 | case ITEM_KILOMETERS:
|
224 | case ITEM_KILOMETERS:
|
210 | switch (itemId2) |
225 | switch (itemId2) |
211 | {
|
226 | {
|
212 | case ITEM_INCHES:
|
227 | case ITEM_INCHES:
|
213 | /* 1 m = 39.370 in */
|
228 | /* 1 m = 39.370 in */
|
214 | newValue = new Double(value * 1000 * 39.370); |
229 | newValue = new Double(value * 1000 * 39.370); |
215 | break; |
230 | break; |
216 | 231 | ||
217 | case ITEM_METERS:
|
232 | case ITEM_METERS:
|
218 | newValue = new Double(value * 1000); |
233 | newValue = new Double(value * 1000); |
219 | break; |
234 | break; |
220 | 235 | ||
221 | case ITEM_MILES:
|
236 | case ITEM_MILES:
|
222 | newValue = new Double(value / 1.609344); |
237 | newValue = new Double(value / 1.609344); |
223 | break; |
238 | break; |
224 | }
|
239 | }
|
225 | break; |
240 | break; |
226 | 241 | ||
227 | case ITEM_METERS:
|
242 | case ITEM_METERS:
|
228 | switch (itemId2) |
243 | switch (itemId2) |
229 | {
|
244 | {
|
230 | case ITEM_INCHES:
|
245 | case ITEM_INCHES:
|
231 | /* 1 m = 39.370 in */
|
246 | /* 1 m = 39.370 in */
|
232 | newValue = new Double(value * 39.370); |
247 | newValue = new Double(value * 39.370); |
233 | break; |
248 | break; |
234 | 249 | ||
235 | case ITEM_KILOMETERS:
|
250 | case ITEM_KILOMETERS:
|
236 | newValue = new Double(value / 1000); |
251 | newValue = new Double(value / 1000); |
237 | break; |
252 | break; |
238 | 253 | ||
239 | case ITEM_MILES:
|
254 | case ITEM_MILES:
|
240 | /* 1 mi = 1609.344 m */
|
255 | /* 1 mi = 1609.344 m */
|
241 | newValue = new Double(value / 1609.344); |
256 | newValue = new Double(value / 1609.344); |
242 | break; |
257 | break; |
243 | }
|
258 | }
|
244 | break; |
259 | break; |
245 | 260 | ||
246 | case ITEM_MILES:
|
261 | case ITEM_MILES:
|
247 | switch (itemId2) |
262 | switch (itemId2) |
248 | {
|
263 | {
|
249 | case ITEM_INCHES:
|
264 | case ITEM_INCHES:
|
250 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
265 | /* 1 mi = 5280 ft, 1 ft = 12 in */
|
251 | newValue = new Double(value * 5280 * 12); |
266 | newValue = new Double(value * 5280 * 12); |
252 | break; |
267 | break; |
253 | 268 | ||
254 | case ITEM_KILOMETERS:
|
269 | case ITEM_KILOMETERS:
|
255 | newValue = new Double(value * 1.609344); |
270 | newValue = new Double(value * 1.609344); |
256 | break; |
271 | break; |
257 | 272 | ||
258 | case ITEM_METERS:
|
273 | case ITEM_METERS:
|
259 | newValue = new Double(value * 1609.344); |
274 | newValue = new Double(value * 1609.344); |
260 | break; |
275 | break; |
261 | }
|
276 | }
|
262 | break; |
277 | break; |
263 | }
|
278 | }
|
264 | 279 | ||
265 | return newValue.toString(); |
280 | return newValue.toString(); |
266 | }
|
281 | }
|
267 | }
|
282 | }
|
268 | 283 |