Subversion Repositories ES

Rev

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