Subversion Repositories ES

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

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