Subversion Repositories ES

Rev

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