Subversion Repositories ES

Rev

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