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 android.widget.TextView; |
15 | import android.widget.TextView; |
16 | import ch.ffhs.converter.R; |
16 | import de.pointedears.converter.R; |
17 | 17 | ||
18 | /**
|
18 | /**
|
19 | * Activity that implements length conversion
|
19 | * Activity that implements length conversion
|
20 | *
|
20 | *
|
21 | * @author pelinux
|
21 | * @author pelinux
|
22 | */
|
22 | */
|
23 | public class TemperaturesActivity extends Activity |
23 | public class TemperaturesActivity extends Activity |
24 | {
|
24 | {
|
25 | /**
|
- | |
26 | *
|
- | |
27 | */
|
- | |
28 | private static final String MSG_BELOW_ABS_ZERO = "K must be >= 0"; |
- | |
29 | /*
|
25 | /*
|
30 | * Constants for mapping value strings to internal IDs
|
26 | * Constants for mapping value strings to internal IDs
|
31 | */
|
27 | */
|
32 | private static final String VALUE_CELSIUS = "C"; //$NON-NLS-1$ |
28 | private static final String VALUE_CELSIUS = "C"; //$NON-NLS-1$ |
33 | private static final String VALUE_FAHRENHEIT = "F"; //$NON-NLS-1$ |
29 | private static final String VALUE_FAHRENHEIT = "F"; //$NON-NLS-1$ |
34 | private static final String VALUE_KELVIN = "K"; //$NON-NLS-1$ |
30 | private static final String VALUE_KELVIN = "K"; //$NON-NLS-1$ |
35 | private static final int ITEM_CELSIUS = 0; |
31 | private static final int ITEM_CELSIUS = 0; |
36 | private static final int ITEM_FAHRENHEIT = 1; |
32 | private static final int ITEM_FAHRENHEIT = 1; |
37 | private static final int ITEM_KELVIN = 2; |
33 | private static final int ITEM_KELVIN = 2; |
38 | 34 | ||
39 | /**
|
35 | /**
|
40 | * Maps value strings to internal IDs
|
36 | * Maps value strings to internal IDs
|
41 | */
|
37 | */
|
42 | private final static HashMap<String, Integer> valueToId = |
38 | private final static HashMap<String, Integer> valueToId = |
43 | new HashMap<String, Integer>(); |
39 | new HashMap<String, Integer>(); |
44 | 40 | ||
45 | /* Unit spinners (dropdowns) */
|
41 | /* Unit spinners (dropdowns) */
|
46 | private Spinner spinnerUnit1; |
42 | private Spinner spinnerUnit1; |
47 | private Spinner spinnerUnit2; |
43 | private Spinner spinnerUnit2; |
48 | 44 | ||
49 | /* Hint that value is off scale */
|
45 | /* Hint that value is off scale */
|
50 | private TextView textOffScale; |
46 | private TextView textOffScale; |
51 | 47 | ||
52 | static
|
48 | static
|
53 | {
|
49 | {
|
54 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_CELSIUS, |
50 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_CELSIUS, |
55 | TemperaturesActivity.ITEM_CELSIUS); |
51 | TemperaturesActivity.ITEM_CELSIUS); |
56 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_FAHRENHEIT, |
52 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_FAHRENHEIT, |
57 | TemperaturesActivity.ITEM_FAHRENHEIT); |
53 | TemperaturesActivity.ITEM_FAHRENHEIT); |
58 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_KELVIN, |
54 | TemperaturesActivity.valueToId.put(TemperaturesActivity.VALUE_KELVIN, |
59 | TemperaturesActivity.ITEM_KELVIN); |
55 | TemperaturesActivity.ITEM_KELVIN); |
60 | }
|
56 | }
|
61 | 57 | ||
62 | /** Called when the activity is first created. */
|
58 | /** Called when the activity is first created. */
|
63 | 59 | ||
64 | @Override |
60 | @Override |
65 | public void onCreate(Bundle savedInstanceState) |
61 | public void onCreate(Bundle savedInstanceState) |
66 | {
|
62 | {
|
67 | super.onCreate(savedInstanceState); |
63 | super.onCreate(savedInstanceState); |
68 | this.setContentView(R.layout.activity_temperatures); |
64 | this.setContentView(R.layout.activity_temperatures); |
69 | 65 | ||
70 | this.textOffScale = |
66 | this.textOffScale = |
71 | (TextView) this.findViewById(R.id.temperatures_text_off_scale); |
67 | (TextView) this.findViewById(R.id.temperatures_text_off_scale); |
72 | 68 | ||
73 | final EditText editValue1 =
|
69 | final EditText editValue1 =
|
74 | (EditText) this.findViewById(R.id.temperatures_edit_value1); |
70 | (EditText) this.findViewById(R.id.temperatures_edit_value1); |
75 | final EditText editValue2 =
|
71 | final EditText editValue2 =
|
76 | (EditText) this.findViewById(R.id.temperatures_edit_value2); |
72 | (EditText) this.findViewById(R.id.temperatures_edit_value2); |
77 | 73 | ||
78 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
74 | final OnKeyListener editValue1OnKey = new OnKeyListener() { |
79 | @Override |
75 | @Override |
80 | public boolean onKey(View v, int keyCode, KeyEvent event) |
76 | public boolean onKey(View v, int keyCode, KeyEvent event) |
81 | {
|
77 | {
|
82 | Editable editable1 = ((EditText) v).getText(); |
78 | Editable editable1 = ((EditText) v).getText(); |
83 | 79 | ||
84 | Double value1; |
80 | Double value1; |
85 | try
|
81 | try
|
86 | {
|
82 | {
|
87 | value1 = Double.parseDouble(editable1.toString()); |
83 | value1 = Double.parseDouble(editable1.toString()); |
88 | }
|
84 | }
|
89 | catch (NumberFormatException e) |
85 | catch (NumberFormatException e) |
90 | {
|
86 | {
|
91 | value1 = null; |
87 | value1 = null; |
92 | }
|
88 | }
|
93 | 89 | ||
94 | String string2 = ""; //$NON-NLS-1$ |
90 | String string2 = ""; //$NON-NLS-1$ |
95 | if (value1 != null) |
91 | if (value1 != null) |
96 | {
|
92 | {
|
97 | string2 = TemperaturesActivity.this.getConvertedValue(value1, false); |
93 | string2 = TemperaturesActivity.this.getConvertedValue(value1, false); |
98 | }
|
94 | }
|
99 | 95 | ||
100 | editValue2.setText(string2); |
96 | editValue2.setText(string2); |
101 | 97 | ||
102 | return false; |
98 | return false; |
103 | }
|
99 | }
|
104 | }; |
100 | }; |
105 | editValue1.setOnKeyListener(editValue1OnKey); |
101 | editValue1.setOnKeyListener(editValue1OnKey); |
106 | 102 | ||
107 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
103 | final OnKeyListener editValue2OnKey = new OnKeyListener() { |
108 | @Override |
104 | @Override |
109 | public boolean onKey(View v, int keyCode, KeyEvent event) |
105 | public boolean onKey(View v, int keyCode, KeyEvent event) |
110 | {
|
106 | {
|
111 | Editable editable2 = ((EditText) v).getText(); |
107 | Editable editable2 = ((EditText) v).getText(); |
112 | 108 | ||
113 | Double value2; |
109 | Double value2; |
114 | try
|
110 | try
|
115 | {
|
111 | {
|
116 | value2 = Double.parseDouble(editable2.toString()); |
112 | value2 = Double.parseDouble(editable2.toString()); |
117 | }
|
113 | }
|
118 | catch (NumberFormatException e) |
114 | catch (NumberFormatException e) |
119 | {
|
115 | {
|
120 | value2 = null; |
116 | value2 = null; |
121 | }
|
117 | }
|
122 | 118 | ||
123 | String string1 = ""; //$NON-NLS-1$ |
119 | String string1 = ""; //$NON-NLS-1$ |
124 | if (value2 != null) |
120 | if (value2 != null) |
125 | {
|
121 | {
|
126 | string1 = TemperaturesActivity.this.getConvertedValue(value2, true); |
122 | string1 = TemperaturesActivity.this.getConvertedValue(value2, true); |
127 | }
|
123 | }
|
128 | 124 | ||
129 | editValue1.setText(string1); |
125 | editValue1.setText(string1); |
130 | 126 | ||
131 | return false; |
127 | return false; |
132 | }
|
128 | }
|
133 | }; |
129 | }; |
134 | editValue2.setOnKeyListener(editValue2OnKey); |
130 | editValue2.setOnKeyListener(editValue2OnKey); |
135 | 131 | ||
136 | this.spinnerUnit1 = |
132 | this.spinnerUnit1 = |
137 | (Spinner) this.findViewById(R.id.temperatures_spinner_unit1); |
133 | (Spinner) this.findViewById(R.id.temperatures_spinner_unit1); |
138 | this.spinnerUnit2 = |
134 | this.spinnerUnit2 = |
139 | (Spinner) this.findViewById(R.id.temperatures_spinner_unit2); |
135 | (Spinner) this.findViewById(R.id.temperatures_spinner_unit2); |
140 | 136 | ||
141 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
137 | this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() { |
142 | @Override |
138 | @Override |
143 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
139 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
144 | long arg3) |
140 | long arg3) |
145 | {
|
141 | {
|
146 | /* Simulate input in second EditText so that first EditText is updated */
|
142 | /* Simulate input in second EditText so that first EditText is updated */
|
147 | editValue2OnKey.onKey(editValue2, 0, null); |
143 | editValue2OnKey.onKey(editValue2, 0, null); |
148 | }
|
144 | }
|
149 | 145 | ||
150 | @Override |
146 | @Override |
151 | public void onNothingSelected(AdapterView<?> arg0) |
147 | public void onNothingSelected(AdapterView<?> arg0) |
152 | {
|
148 | {
|
153 | /* no-op */
|
149 | /* no-op */
|
154 | }
|
150 | }
|
155 | }); |
151 | }); |
156 | 152 | ||
157 | this.spinnerUnit2.setSelection(1); |
153 | this.spinnerUnit2.setSelection(1); |
158 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
154 | this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() { |
159 | @Override |
155 | @Override |
160 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
156 | public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
161 | long arg3) |
157 | long arg3) |
162 | {
|
158 | {
|
163 | /* Simulate input in first EditText so that second EditText is updated */
|
159 | /* Simulate input in first EditText so that second EditText is updated */
|
164 | editValue1OnKey.onKey(editValue1, 0, null); |
160 | editValue1OnKey.onKey(editValue1, 0, null); |
165 | }
|
161 | }
|
166 | 162 | ||
167 | @Override |
163 | @Override |
168 | public void onNothingSelected(AdapterView<?> arg0) |
164 | public void onNothingSelected(AdapterView<?> arg0) |
169 | {
|
165 | {
|
170 | /* no-op */
|
166 | /* no-op */
|
171 | }
|
167 | }
|
172 | }); |
168 | }); |
173 | }
|
169 | }
|
174 | 170 | ||
175 | /**
|
171 | /**
|
176 | * @param value
|
172 | * @param value
|
177 | * @return
|
173 | * @return
|
178 | */
|
174 | */
|
179 | private String getConvertedValue(double value, boolean reverse) |
175 | private String getConvertedValue(double value, boolean reverse) |
180 | {
|
176 | {
|
181 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
177 | int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition(); |
182 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
178 | int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition(); |
183 | String[] itemArray = |
179 | String[] itemArray = |
184 | this.getResources().getStringArray(R.array.temperature_units_values); |
180 | this.getResources().getStringArray(R.array.temperature_units_values); |
185 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
181 | String selectedItemValue1 = itemArray[selectedItemPosition1]; |
186 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
182 | String selectedItemValue2 = itemArray[selectedItemPosition2]; |
187 | 183 | ||
188 | if (reverse) |
184 | if (reverse) |
189 | {
|
185 | {
|
190 | String tmp = selectedItemValue1; |
186 | String tmp = selectedItemValue1; |
191 | selectedItemValue1 = selectedItemValue2;
|
187 | selectedItemValue1 = selectedItemValue2;
|
192 | selectedItemValue2 = tmp;
|
188 | selectedItemValue2 = tmp;
|
193 | }
|
189 | }
|
194 | 190 | ||
195 | int itemId1 = TemperaturesActivity.valueToId.get(selectedItemValue1); |
191 | int itemId1 = TemperaturesActivity.valueToId.get(selectedItemValue1); |
196 | int itemId2 = TemperaturesActivity.valueToId.get(selectedItemValue2); |
192 | int itemId2 = TemperaturesActivity.valueToId.get(selectedItemValue2); |
197 | 193 | ||
198 | Double newValue = value; |
194 | Double newValue = value; |
199 | 195 | ||
200 | this.textOffScale.setVisibility(View.INVISIBLE); |
196 | this.textOffScale.setVisibility(View.INVISIBLE); |
201 | 197 | ||
202 | switch (itemId1) |
198 | switch (itemId1) |
203 | {
|
199 | {
|
204 | case ITEM_CELSIUS:
|
200 | case ITEM_CELSIUS:
|
205 | switch (itemId2) |
201 | switch (itemId2) |
206 | {
|
202 | {
|
207 | case ITEM_FAHRENHEIT:
|
203 | case ITEM_FAHRENHEIT:
|
208 | newValue = new Double(value * 9.0 / 5 + 32); |
204 | newValue = new Double(value * 9.0 / 5 + 32); |
209 | break; |
205 | break; |
210 | 206 | ||
211 | case ITEM_KELVIN:
|
207 | case ITEM_KELVIN:
|
212 | newValue = new Double(value + 273.15); |
208 | newValue = new Double(value + 273.15); |
213 | 209 | ||
214 | if (newValue < 0.0) |
210 | if (newValue < 0.0) |
215 | {
|
211 | {
|
216 | this.textOffScale.setVisibility(View.VISIBLE); |
212 | this.textOffScale.setVisibility(View.VISIBLE); |
217 | return "*" + newValue.toString(); |
213 | return "*" + newValue.toString(); |
218 | }
|
214 | }
|
219 | break; |
215 | break; |
220 | }
|
216 | }
|
221 | break; |
217 | break; |
222 | 218 | ||
223 | case ITEM_FAHRENHEIT:
|
219 | case ITEM_FAHRENHEIT:
|
224 | switch (itemId2) |
220 | switch (itemId2) |
225 | {
|
221 | {
|
226 | case ITEM_CELSIUS:
|
222 | case ITEM_CELSIUS:
|
227 | newValue = new Double((value - 32) * 5.0 / 9); |
223 | newValue = new Double((value - 32) * 5.0 / 9); |
228 | break; |
224 | break; |
229 | 225 | ||
230 | case ITEM_KELVIN:
|
226 | case ITEM_KELVIN:
|
231 | newValue = new Double((value + 459.67) * 5.0 / 9); |
227 | newValue = new Double((value + 459.67) * 5.0 / 9); |
232 | 228 | ||
233 | if (newValue < 0.0) |
229 | if (newValue < 0.0) |
234 | {
|
230 | {
|
235 | this.textOffScale.setVisibility(View.VISIBLE); |
231 | this.textOffScale.setVisibility(View.VISIBLE); |
236 | return "*" + newValue.toString(); |
232 | return "*" + newValue.toString(); |
237 | }
|
233 | }
|
238 | break; |
234 | break; |
239 | }
|
235 | }
|
240 | break; |
236 | break; |
241 | 237 | ||
242 | case ITEM_KELVIN:
|
238 | case ITEM_KELVIN:
|
243 | if (value < 0.0) |
- | |
244 | {
|
- | |
245 | return TemperaturesActivity.MSG_BELOW_ABS_ZERO; |
- | |
246 | }
|
- | |
247 | - | ||
248 | switch (itemId2) |
239 | switch (itemId2) |
249 | {
|
240 | {
|
250 | case ITEM_CELSIUS:
|
241 | case ITEM_CELSIUS:
|
251 | newValue = new Double(value - 273.15); |
242 | newValue = new Double(value - 273.15); |
252 | break; |
243 | break; |
253 | 244 | ||
254 | case ITEM_FAHRENHEIT:
|
245 | case ITEM_FAHRENHEIT:
|
255 | newValue = new Double(value * 9.0 / 5 - 459.67); |
246 | newValue = new Double(value * 9.0 / 5 - 459.67); |
256 | break; |
247 | break; |
257 | }
|
248 | }
|
- | 249 | ||
- | 250 | if (value < 0.0) |
|
- | 251 | {
|
|
- | 252 | this.textOffScale.setVisibility(View.VISIBLE); |
|
- | 253 | return "*" + newValue.toString(); |
|
- | 254 | }
|
|
258 | break; |
255 | break; |
259 | }
|
256 | }
|
260 | 257 | ||
261 | return newValue.toString(); |
258 | return newValue.toString(); |
262 | }
|
259 | }
|
263 | }
|
260 | }
|
264 | 261 |