Subversion Repositories ES

Rev

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