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