Subversion Repositories ES

Rev

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