Subversion Repositories ES

Rev

Rev 11 | Rev 13 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11 Rev 12
Line 1... Line 1...
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.database.Cursor;
-
 
7
import android.database.sqlite.SQLiteDatabase;
-
 
8
import android.database.sqlite.SQLiteException;
-
 
9
import android.os.Bundle;
6
import android.os.Bundle;
10
import android.text.Editable;
7
import android.text.Editable;
11
import android.view.KeyEvent;
8
import android.view.KeyEvent;
12
import android.view.Menu;
9
import android.view.Menu;
13
import android.view.MenuInflater;
10
import android.view.MenuInflater;
Line 28... Line 25...
28
 */
25
 */
29
public class CurrenciesActivity extends Activity
26
public class CurrenciesActivity extends Activity
30
{
27
{
31
  /*
28
  /*
32
   * Constants for mapping value strings
29
   * Constants for mapping value strings
-
 
30
   *
-
 
31
   * @todo: Use resource IDs
-
 
32
   */
-
 
33
  /**
-
 
34
   * Database field/spinner value for Swiss Francs
-
 
35
   */
-
 
36
  public static final String VALUE_CHF = "CHF"; //$NON-NLS-1$
-
 
37
-
 
38
  /**
-
 
39
   * Database field/spinner value for Euros
33
   */
40
   */
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
41
-
 
42
  public static final String VALUE_EUR = "EUR"; //$NON-NLS-1$
-
 
43
-
 
44
  /**
-
 
45
   * Database field/spinner value for US Dollars
-
 
46
   */
38
  protected static HashMap<String, HashMap<String, Double>> currencyConversions;
47
  public static final String VALUE_USD = "USD"; //$NON-NLS-1$
39
48
40
  /* Unit spinners (dropdowns) */
49
  /* Unit spinners (dropdowns) */
41
  private Spinner spinnerUnit1;
50
  private Spinner spinnerUnit1;
42
  private Spinner spinnerUnit2;
51
  private Spinner spinnerUnit2;
-
 
52
  private CurrenciesDatabase db;
-
 
53
-
 
54
  private HashMap<String, HashMap<String, Double>> conversionRates;
43
55
44
  /** Called when the activity is first created. */
56
  /** Called when the activity is first created. */
45
57
46
  @Override
58
  @Override
47
  public void onCreate(Bundle savedInstanceState)
59
  public void onCreate(Bundle savedInstanceState)
48
  {
60
  {
49
    super.onCreate(savedInstanceState);
61
    super.onCreate(savedInstanceState);
50
    this.setContentView(R.layout.activity_currencies);
62
    this.setContentView(R.layout.activity_currencies);
51
63
52
    /* Set up currency database */
64
    /* Set up currency database, and retrieve conversion rates */
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);
-
 
64
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
-
 
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
-
 
74
    /* Create database with values above if it does not exist */
-
 
75
    CurrenciesDatabase db = new CurrenciesDatabase(this);
65
    this.db = new CurrenciesDatabase(this);
76
    try
-
 
77
    {
-
 
78
      SQLiteDatabase dbConn = db.getReadableDatabase();
66
    this.conversionRates = this.db.getConversionRates();
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
67
132
    final EditText editValue1 =
68
    final EditText editValue1 =
133
      (EditText) this.findViewById(R.id.currencies_edit_value1);
69
      (EditText) this.findViewById(R.id.currencies_edit_value1);
134
    final EditText editValue2 =
70
    final EditText editValue2 =
135
      (EditText) this.findViewById(R.id.currencies_edit_value2);
71
      (EditText) this.findViewById(R.id.currencies_edit_value2);
Line 252... Line 188...
252
    }
188
    }
253
189
254
    Double newValue = value;
190
    Double newValue = value;
255
191
256
    HashMap<String, Double> mapForCurrency =
192
    HashMap<String, Double> mapForCurrency =
257
      CurrenciesActivity.currencyConversions.get(selectedItemValue1);
193
      this.conversionRates.get(selectedItemValue1);
258
    if (mapForCurrency != null)
194
    if (mapForCurrency != null)
259
    {
195
    {
260
      Double conversionFactor = mapForCurrency.get(selectedItemValue2);
196
      Double conversionFactor = mapForCurrency.get(selectedItemValue2);
261
      if (conversionFactor != null)
197
      if (conversionFactor != null)
262
      {
198
      {
Line 270... Line 206...
270
  /*
206
  /*
271
   * (non-Javadoc)
207
   * (non-Javadoc)
272
   *
208
   *
273
   * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
209
   * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
274
   */
210
   */
-
 
211
  /*
-
 
212
   * (non-Javadoc)
-
 
213
   *
-
 
214
   * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
-
 
215
   */
275
  @Override
216
  @Override
276
  public boolean onCreateOptionsMenu(Menu menu)
217
  public boolean onCreateOptionsMenu(Menu menu)
277
  {
218
  {
278
    MenuInflater inflater = this.getMenuInflater();
219
    MenuInflater inflater = this.getMenuInflater();
279
    inflater.inflate(R.menu.options, menu);
220
    inflater.inflate(R.menu.options, menu);
Line 283... Line 224...
283
  /*
224
  /*
284
   * (non-Javadoc)
225
   * (non-Javadoc)
285
   *
226
   *
286
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
227
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
287
   */
228
   */
-
 
229
  /*
-
 
230
   * (non-Javadoc)
-
 
231
   *
-
 
232
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
-
 
233
   */
288
  @Override
234
  @Override
289
  public boolean onOptionsItemSelected(MenuItem item)
235
  public boolean onOptionsItemSelected(MenuItem item)
290
  {
236
  {
291
    /* update here */
237
    /* update here */
292
    return super.onOptionsItemSelected(item);
238
    return super.onOptionsItemSelected(item);
293
  }
239
  }
294
-
 
295
  /**
-
 
296
   * @return
-
 
297
   */
-
 
298
  public HashMap<String, HashMap<String, Double>> getCurrencyConversions()
-
 
299
  {
-
 
300
    return CurrenciesActivity.currencyConversions;
-
 
301
  }
-
 
302
}
240
}