Subversion Repositories ES

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 7
Line 1... Line -...
1
/*
-
 
2
 * Copyright (C) 2008 The Android Open Source Project
-
 
3
 *
-
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
-
 
5
 * you may not use this file except in compliance with the License.
-
 
6
 * You may obtain a copy of the License at
-
 
7
 *
-
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
-
 
9
 *
-
 
10
 * Unless required by applicable law or agreed to in writing, software
-
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
-
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
 
13
 * See the License for the specific language governing permissions and
-
 
14
 * limitations under the License.
-
 
15
 */
-
 
16
-
 
17
package ch.ffhs.converter.app;
1
package ch.ffhs.converter.app;
18
2
19
// Need the following import to get access to the app resources, since this
-
 
20
// class is in a sub-package.
3
import java.util.HashMap;
-
 
4
21
import android.app.Activity;
5
import android.app.Activity;
22
import android.os.Bundle;
6
import android.os.Bundle;
-
 
7
import android.text.Editable;
-
 
8
import android.view.KeyEvent;
23
import android.view.Window;
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;
24
import ch.ffhs.converter.R;
16
import ch.ffhs.converter.R;
25
17
26
/**
18
/**
27
 * <h3>Dialog Activity</h3>
19
 * Activity that implements length conversion
28
 *
20
 *
29
 * <p>
-
 
30
 * This demonstrates the how to write an activity that looks like a pop-up
-
 
31
 * dialog.
21
 * @author pelinux
32
 * </p>
-
 
33
 */
22
 */
34
public class TemperaturesActivity extends Activity
23
public class TemperaturesActivity extends Activity
35
{
24
{
36
  /**
25
  /**
-
 
26
   *
-
 
27
   */
37
   * Initialization of the Activity after it is first created. Must at least
28
  private static final String MSG_BELOW_ABS_ZERO = "K must be >= 0";
38
   * call {@link android.app.Activity#setContentView setContentView()} to
-
 
-
 
29
  /*
39
   * describe what is to be displayed in the screen.
30
   * Constants for mapping value strings to internal IDs
40
   */
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
41
  @Override
64
  @Override
42
  protected void onCreate(Bundle savedInstanceState)
65
  public void onCreate(Bundle savedInstanceState)
43
  {
66
  {
44
    // Be sure to call the super class.
-
 
45
    super.onCreate(savedInstanceState);
67
    super.onCreate(savedInstanceState);
-
 
68
    this.setContentView(R.layout.activity_temperatures);
-
 
69
-
 
70
    this.textOffScale =
-
 
71
      (TextView) this.findViewById(R.id.temperatures_text_off_scale);
-
 
72
-
 
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);
-
 
77
-
 
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
    });
-
 
173
  }
-
 
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
        }
46
247
-
 
248
        switch (itemId2)
-
 
249
        {
-
 
250
          case ITEM_CELSIUS:
47
    this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
251
            newValue = new Double(value - 273.15);
-
 
252
            break;
48
253
49
    // See assets/res/any/layout/dialog_activity.xml for this
254
          case ITEM_FAHRENHEIT:
50
    // view layout definition, which is being set here as
255
            newValue = new Double(value * 9.0 / 5 - 459.67);
51
    // the content of our screen.
256
            break;
-
 
257
        }
52
    this.setContentView(R.layout.dialog_activity);
258
        break;
-
 
259
    }
53
260
54
    this.getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
-
 
55
                android.R.drawable.ic_dialog_alert);
261
    return newValue.toString();
56
  }
262
  }
57
}
263
}