Subversion Repositories ES

Rev

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