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