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;
-
 
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;
23
import ch.ffhs.converter.R;
15
import ch.ffhs.converter.R;
24
16
25
/**
17
/**
26
 * <h3>Dialog Activity</h3>
18
 * Activity that implements currency conversion
27
 *
19
 *
28
 * <p>
-
 
29
 * This demonstrates the how to write an activity that looks like a pop-up
-
 
30
 * dialog with a custom theme using a different text color.
-
 
31
 * </p>
20
 * @author pelinux
32
 */
21
 */
33
public class CurrenciesActivity extends Activity
22
public class CurrenciesActivity extends Activity
34
{
23
{
-
 
24
  /*
-
 
25
   * Constants for mapping value strings to internal IDs
-
 
26
   */
-
 
27
  private static final String VALUE_CHF = "CHF"; //$NON-NLS-1$
-
 
28
  private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$
-
 
29
  private static final String VALUE_USD = "USD"; //$NON-NLS-1$
-
 
30
  private static final int ITEM_CHF = 0;
-
 
31
  private static final int ITEM_EUR = 1;
-
 
32
  private static final int ITEM_USD = 2;
-
 
33
35
  /**
34
  /**
36
   * Initialization of the Activity after it is first created. Must at least
-
 
37
   * call {@link android.app.Activity#setContentView setContentView()} to
-
 
38
   * describe what is to be displayed in the screen.
35
   * Maps value strings to internal IDs
39
   */
36
   */
-
 
37
  private final static HashMap<String, Integer> valueToId =
-
 
38
    new HashMap<String, Integer>();
-
 
39
  static
-
 
40
  {
-
 
41
    CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_CHF,
-
 
42
      CurrenciesActivity.ITEM_CHF);
-
 
43
    CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_EUR,
-
 
44
      CurrenciesActivity.ITEM_EUR);
-
 
45
    CurrenciesActivity.valueToId.put(CurrenciesActivity.VALUE_USD,
-
 
46
      CurrenciesActivity.ITEM_USD);
-
 
47
  }
-
 
48
-
 
49
  /* Unit spinners (dropdowns) */
-
 
50
  private Spinner spinnerUnit1;
-
 
51
  private Spinner spinnerUnit2;
-
 
52
-
 
53
  /** Called when the activity is first created. */
-
 
54
40
  @Override
55
  @Override
41
  protected void onCreate(Bundle savedInstanceState)
56
  public void onCreate(Bundle savedInstanceState)
42
  {
57
  {
43
    // Be sure to call the super class.
-
 
44
    super.onCreate(savedInstanceState);
58
    super.onCreate(savedInstanceState);
-
 
59
    this.setContentView(R.layout.activity_currencies);
-
 
60
-
 
61
    final EditText editValue1 =
-
 
62
      (EditText) this.findViewById(R.id.currencies_edit_value1);
-
 
63
    final EditText editValue2 =
-
 
64
      (EditText) this.findViewById(R.id.currencies_edit_value2);
-
 
65
-
 
66
    final OnKeyListener editValue1OnKey = new OnKeyListener() {
-
 
67
      @Override
-
 
68
      public boolean onKey(View v, int keyCode, KeyEvent event)
-
 
69
      {
-
 
70
        Editable editable1 = ((EditText) v).getText();
-
 
71
-
 
72
        Double value1;
-
 
73
        try
-
 
74
        {
-
 
75
          value1 = Double.parseDouble(editable1.toString());
-
 
76
        }
-
 
77
        catch (NumberFormatException e)
-
 
78
        {
-
 
79
          value1 = null;
-
 
80
        }
-
 
81
-
 
82
        String string2 = ""; //$NON-NLS-1$
-
 
83
        if (value1 != null)
-
 
84
        {
-
 
85
          string2 = CurrenciesActivity.this.getConvertedValue(value1, false);
-
 
86
        }
-
 
87
-
 
88
        editValue2.setText(string2);
-
 
89
-
 
90
        return false;
-
 
91
      }
-
 
92
    };
-
 
93
    editValue1.setOnKeyListener(editValue1OnKey);
-
 
94
-
 
95
    final OnKeyListener editValue2OnKey = new OnKeyListener() {
-
 
96
      @Override
-
 
97
      public boolean onKey(View v, int keyCode, KeyEvent event)
-
 
98
      {
-
 
99
        Editable editable2 = ((EditText) v).getText();
-
 
100
-
 
101
        Double value2;
-
 
102
        try
-
 
103
        {
-
 
104
          value2 = Double.parseDouble(editable2.toString());
-
 
105
        }
-
 
106
        catch (NumberFormatException e)
-
 
107
        {
-
 
108
          value2 = null;
-
 
109
        }
-
 
110
-
 
111
        String string1 = ""; //$NON-NLS-1$
-
 
112
        if (value2 != null)
-
 
113
        {
-
 
114
          string1 = CurrenciesActivity.this.getConvertedValue(value2, true);
-
 
115
        }
-
 
116
-
 
117
        editValue1.setText(string1);
-
 
118
-
 
119
        return false;
-
 
120
      }
-
 
121
    };
-
 
122
    editValue2.setOnKeyListener(editValue2OnKey);
-
 
123
-
 
124
    this.spinnerUnit1 =
-
 
125
      (Spinner) this.findViewById(R.id.currencies_spinner_unit1);
-
 
126
    this.spinnerUnit2 =
-
 
127
      (Spinner) this.findViewById(R.id.currencies_spinner_unit2);
-
 
128
-
 
129
    this.spinnerUnit1.setOnItemSelectedListener(new OnItemSelectedListener() {
-
 
130
      @Override
-
 
131
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
-
 
132
        long arg3)
-
 
133
      {
-
 
134
        /* Simulate input in second EditText so that first EditText is updated */
-
 
135
        editValue2OnKey.onKey(editValue2, 0, null);
-
 
136
      }
-
 
137
-
 
138
      @Override
-
 
139
      public void onNothingSelected(AdapterView<?> arg0)
-
 
140
      {
-
 
141
        /* no-op */
-
 
142
      }
-
 
143
    });
-
 
144
-
 
145
    this.spinnerUnit2.setSelection(1);
-
 
146
    this.spinnerUnit2.setOnItemSelectedListener(new OnItemSelectedListener() {
-
 
147
      @Override
-
 
148
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
-
 
149
        long arg3)
-
 
150
      {
-
 
151
        /* Simulate input in first EditText so that second EditText is updated */
-
 
152
        editValue1OnKey.onKey(editValue1, 0, null);
-
 
153
      }
-
 
154
-
 
155
      @Override
-
 
156
      public void onNothingSelected(AdapterView<?> arg0)
-
 
157
      {
-
 
158
        /* no-op */
-
 
159
      }
-
 
160
    });
-
 
161
  }
-
 
162
-
 
163
  /**
-
 
164
   * @param value
-
 
165
   * @return
-
 
166
   */
-
 
167
  private String getConvertedValue(double value, boolean reverse)
-
 
168
  {
-
 
169
    int selectedItemPosition1 = this.spinnerUnit1.getSelectedItemPosition();
-
 
170
    int selectedItemPosition2 = this.spinnerUnit2.getSelectedItemPosition();
-
 
171
    String[] itemArray =
-
 
172
      this.getResources().getStringArray(R.array.currency_units_values);
-
 
173
    String selectedItemValue1 = itemArray[selectedItemPosition1];
-
 
174
    String selectedItemValue2 = itemArray[selectedItemPosition2];
-
 
175
-
 
176
    if (reverse)
-
 
177
    {
-
 
178
      String tmp = selectedItemValue1;
-
 
179
      selectedItemValue1 = selectedItemValue2;
-
 
180
      selectedItemValue2 = tmp;
-
 
181
    }
-
 
182
-
 
183
    int itemId1 = CurrenciesActivity.valueToId.get(selectedItemValue1);
-
 
184
    int itemId2 = CurrenciesActivity.valueToId.get(selectedItemValue2);
-
 
185
-
 
186
    Double newValue = value;
-
 
187
-
 
188
    switch (itemId1)
-
 
189
    {
-
 
190
      case ITEM_CHF:
-
 
191
        switch (itemId2)
-
 
192
        {
-
 
193
          case ITEM_EUR:
-
 
194
            newValue = new Double(value * 0.767842293);
-
 
195
            break;
-
 
196
-
 
197
          case ITEM_USD:
-
 
198
            newValue = new Double(value * 1.03413);
-
 
199
            break;
-
 
200
        }
-
 
201
        break;
-
 
202
-
 
203
      case ITEM_EUR:
-
 
204
        switch (itemId2)
-
 
205
        {
-
 
206
          case ITEM_CHF:
-
 
207
            newValue = new Double(value * 1.30235077);
-
 
208
            break;
-
 
209
-
 
210
          case ITEM_USD:
-
 
211
            newValue = new Double(value * 1.3468);
-
 
212
            break;
-
 
213
        }
-
 
214
        break;
-
 
215
-
 
216
      case ITEM_USD:
-
 
217
        switch (itemId2)
-
 
218
        {
-
 
219
          case ITEM_CHF:
-
 
220
            newValue = new Double(value * 0.966996412);
-
 
221
            break;
-
 
222
-
 
223
          case ITEM_EUR:
-
 
224
            newValue = new Double(value * 0.742500743);
-
 
225
            break;
-
 
226
        }
-
 
227
        break;
-
 
228
    }
45
229
46
    // See assets/res/any/layout/dialog_activity.xml for this
-
 
47
    // view layout definition, which is being set here as
-
 
48
    // the content of our screen.
230
    return newValue.toString();
49
    this.setContentView(R.layout.custom_dialog_activity);
-
 
50
  }
231
  }
51
}
232
}