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