Subversion Repositories ES

Rev

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

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