Subversion Repositories ES

Rev

Rev 7 | Rev 11 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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