Compare Revisions
Last modification
- Rev 17 2011-01-26 21:08:16
- Author: PointedEars
- Log message:
- Currencies:
+ Added/updated resource strings
+ Disabled "Quit" (options menu button)
- Added update notifications
- Added exchange rates update service (project requirement)
- Clean-up:
+ Outsourced conversion data
+ Layout: Replaced deprecated fill_parent with match_parent
/trunk/src/de/pointedears/converter/db/ConversionData.java |
File deleted |
\ No newline at end of file
|
Property changes: |
Deleted: svn:mime-type |
## -1 +0,0 ## |
-text/plain |
\ No newline at end of property |
Index: CurrenciesDatabase.java |
=================================================================== |
--- CurrenciesDatabase.java (revision 17) |
+++ CurrenciesDatabase.java (revision 16) |
@@ -3,20 +3,14 @@ |
*/ |
package de.pointedears.converter.db; |
-import java.text.DateFormat; |
-import java.text.ParseException; |
-import java.text.SimpleDateFormat; |
-import java.util.Date; |
import java.util.HashMap; |
import java.util.Map.Entry; |
|
import android.content.ContentValues; |
-import android.content.Context; |
import android.database.Cursor; |
import android.database.sqlite.SQLiteDatabase; |
import android.database.sqlite.SQLiteException; |
import android.database.sqlite.SQLiteOpenHelper; |
-import android.util.Log; |
import de.pointedears.converter.app.CurrenciesActivity; |
|
/** |
@@ -25,38 +19,29 @@ |
*/ |
public class CurrenciesDatabase extends SQLiteOpenHelper |
{ |
- |
private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ |
- private static final int DATABASE_VERSION = 8; |
+ private static final int DATABASE_VERSION = 3; |
|
private static final String TABLE = "currency"; //$NON-NLS-1$ |
private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$ |
private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
- private static final String COLUMN_UPDATED = "updated"; //$NON-NLS-1$ |
|
- private static HashMap<String, ConversionData> conversionRates = |
- new HashMap<String, ConversionData>(); |
+ private static HashMap<String, Double> conversionRates = |
+ new HashMap<String, Double>(); |
static |
{ |
/* Default conversion rates from Euro (EUR) to other currencies */ |
- Date epoch = new Date(0); |
CurrenciesDatabase.conversionRates |
- .put(CurrenciesActivity.VALUE_CHF, |
- new ConversionData(1.3013, epoch)); |
+ .put(CurrenciesActivity.VALUE_CHF, 1.3013); |
CurrenciesDatabase.conversionRates |
- .put(CurrenciesActivity.VALUE_USD, |
- new ConversionData(1.3521, epoch)); |
+ .put(CurrenciesActivity.VALUE_USD, 1.3521); |
} |
|
- private SQLiteDatabase database; |
- private final DateFormat iso8601format = new SimpleDateFormat( |
- "yyyy-MM-dd HH:mm:ss"); |
- |
/** |
* @param context |
* The Activity in which this wrapper is used |
*/ |
- public CurrenciesDatabase(Context context) |
+ public CurrenciesDatabase(CurrenciesActivity context) |
{ |
super(context, CurrenciesDatabase.DATABASE_NAME, null, |
CurrenciesDatabase.DATABASE_VERSION); |
@@ -75,62 +60,22 @@ |
public void onCreate(SQLiteDatabase db) |
{ |
db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
- + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT" |
- + ", " + CurrenciesDatabase.COLUMN_FACTOR + " NUMERIC" |
- + ", " + CurrenciesDatabase.COLUMN_UPDATED |
- + " TEXT" |
+ + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT, " |
+ + CurrenciesDatabase.COLUMN_FACTOR |
+ + " NUMERIC" |
+ ", CONSTRAINT unique_currency_pair UNIQUE (" |
- + CurrenciesDatabase.COLUMN_CURRENCY + ") ON CONFLICT REPLACE)"); |
+ + CurrenciesDatabase.COLUMN_CURRENCY + "))"); |
|
- this.writeConversionsToDatabase(db); |
- } |
- |
- /** |
- * @param db |
- * The database; <code>null</code> uses the default database |
- */ |
- public void writeConversionsToDatabase(SQLiteDatabase db) |
- { |
- HashMap<String, ConversionData> currencyConversions = |
+ HashMap<String, Double> currencyConversions = |
this.getConversionRates(); |
- |
- if (db == null) |
+ for (Entry<String, Double> factorEntry : currencyConversions.entrySet()) |
{ |
- db = this.database; |
+ ContentValues values = new ContentValues(); |
+ values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
+ values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue()); |
+ db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR, |
+ values); |
} |
- |
- if (!db.isOpen()) |
- { |
- try |
- { |
- db = this.getWritableDatabase(); |
- } |
- catch (SQLiteException e) |
- { |
- Log.e(this.getClass().toString(), "Could not open database", e); |
- throw e; |
- } |
- } |
- |
- if (db.isOpen()) |
- { |
- for (Entry<String, ConversionData> factorEntry : currencyConversions |
- .entrySet()) |
- { |
- ContentValues values = new ContentValues(); |
- values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
- values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue() |
- .getRate()); |
- values.put(CurrenciesDatabase.COLUMN_UPDATED, |
- this.iso8601format.format(factorEntry.getValue() |
- .getUpdated())); |
- |
- /* INSERT suffices here, thanks to ON CONFLICT REPLACE */ |
- db.insert(CurrenciesDatabase.TABLE, |
- CurrenciesDatabase.COLUMN_FACTOR, |
- values); |
- } |
- } |
} |
|
/* |
@@ -152,7 +97,7 @@ |
/** |
* @return |
*/ |
- public HashMap<String, ConversionData> getConversionRates() |
+ public HashMap<String, Double> getConversionRates() |
{ |
return CurrenciesDatabase.conversionRates; |
} |
@@ -166,11 +111,11 @@ |
try |
{ |
/* Get database connection, but upgrade database first if necessary! */ |
- this.database = this.getWritableDatabase(); |
+ SQLiteDatabase dbConn = this.getWritableDatabase(); |
|
Cursor cursor = |
- this.database.query(true, CurrenciesDatabase.TABLE, null, null, null, |
- null, null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
+ dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
+ null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
|
if (cursor != null) |
{ |
@@ -181,37 +126,18 @@ |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
int factorId = |
cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
- int updatedId = |
- cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_UPDATED); |
|
/* NOTE: Don't change the default values if the table is empty */ |
if (cursor.moveToFirst()) |
{ |
- HashMap<String, ConversionData> newCurrencyConversions = |
- new HashMap<String, ConversionData>(); |
+ HashMap<String, Double> newCurrencyConversions = |
+ new HashMap<String, Double>(); |
|
do |
{ |
String currencyStr = cursor.getString(currency1Id); |
Double factor = cursor.getDouble(factorId); |
- String updatedStr = cursor.getString(updatedId); |
- |
- Date updated = new Date(0); |
- try |
- { |
- if (updatedStr != null) |
- { |
- updated = this.iso8601format.parse(updatedStr); |
- } |
- } |
- catch (ParseException e) |
- { |
- Log.e(this.getClass().toString(), |
- "Parsing ISO8601 datetime failed: '" + updatedStr + "'", e); |
- } |
- |
- newCurrencyConversions.put(currencyStr, new ConversionData( |
- factor, updated)); |
+ newCurrencyConversions.put(currencyStr, factor); |
} |
while (cursor.moveToNext()); |
|
@@ -220,16 +146,72 @@ |
} |
catch (IllegalArgumentException e) |
{ |
- Log.e(this.getClass().toString(), "Could not retrieve column index", |
- e); |
+ /* Could not retrieve column index */ |
+ e.printStackTrace(); |
} |
} |
|
- this.database.close(); |
+ dbConn.close(); |
} |
catch (SQLiteException e1) |
{ |
- Log.e(this.getClass().toString(), "Could not open database", e1); |
+ /* Could not open database */ |
+ e1.printStackTrace(); |
} |
} |
+ |
+ /** |
+ * Tests the database access |
+ */ |
+ public void testAccess() |
+ { |
+ try |
+ { |
+ SQLiteDatabase dbConn = this.getReadableDatabase(); |
+ |
+ Cursor myCursor = |
+ dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
+ null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
+ |
+ @SuppressWarnings({ "unused", "nls" }) |
+ String queryResult = ""; |
+ if (myCursor != null) |
+ { |
+ try |
+ { |
+ int currency1Id = |
+ myCursor |
+ .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
+ int factorId = |
+ myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
+ |
+ if (myCursor.moveToFirst()) |
+ { |
+ do |
+ { |
+ String currencyStr = myCursor.getString(currency1Id); |
+ Double factor = myCursor.getDouble(factorId); |
+ |
+ /* DEBUG */ |
+ queryResult += |
+ "EUR --> " + currencyStr + ": " + factor + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
+ } |
+ while (myCursor.moveToNext()); |
+ } |
+ } |
+ catch (IllegalArgumentException e) |
+ { |
+ /* Could not retrieve column index */ |
+ e.printStackTrace(); |
+ } |
+ } |
+ |
+ dbConn.close(); |
+ } |
+ catch (SQLiteException e1) |
+ { |
+ /* Could not open database */ |
+ e1.printStackTrace(); |
+ } |
+ } |
} |