Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 11 → Rev 12

/trunk/src/de/pointedears/converter/ConverterApplication.java
1,17 → 1,5
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright (C) 2010 Thomas Lahn
*/
 
package de.pointedears.converter;
19,17 → 7,10
import android.app.Application;
 
/**
* This is an example of a {@link android.app.Application} class. Ordinarily you
* would use
* a class like this as a central repository for information that might be
* shared between multiple
* activities.
* Converter application class. Holds the intent category for building
* the list menu. Might later initialize prefs.
*
* In this case, we have not defined any specific work for this Application.
*
* See samples/ApiDemos/tests/src/de.pointedears.converter/ApiDemosApplicationTests for
* an example
* of how to perform unit tests on an Application object.
* @author Thomas 'PointedEars' Lahn
*/
public class ConverterApplication extends Application
{
37,8 → 18,13
* Activity category for automatically filtering converter Activities
*/
public final static String CATEGORY_CONVERTER =
"android.intent.category.CONVERTER";
"android.intent.category.CONVERTER"; //$NON-NLS-1$
 
/*
* (non-Javadoc)
*
* @see android.app.Application#onCreate()
*/
@Override
public void onCreate()
{
49,6 → 35,11
// PreferenceManager.setDefaultValues(this, R.xml.default_values, false);
}
 
/*
* (non-Javadoc)
*
* @see android.app.Application#onTerminate()
*/
@Override
public void onTerminate()
{
/trunk/src/de/pointedears/converter/app/CurrenciesActivity.java
3,9 → 3,6
import java.util.HashMap;
 
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
30,17 → 27,32
{
/*
* Constants for mapping value strings
*
* @todo: Use resource IDs
*/
private static final String VALUE_CHF = "CHF"; //$NON-NLS-1$
private static final String VALUE_EUR = "EUR"; //$NON-NLS-1$
private static final String VALUE_USD = "USD"; //$NON-NLS-1$
/**
* Database field/spinner value for Swiss Francs
*/
public static final String VALUE_CHF = "CHF"; //$NON-NLS-1$
 
protected static HashMap<String, HashMap<String, Double>> currencyConversions;
/**
* Database field/spinner value for Euros
*/
 
public static final String VALUE_EUR = "EUR"; //$NON-NLS-1$
 
/**
* Database field/spinner value for US Dollars
*/
public static final String VALUE_USD = "USD"; //$NON-NLS-1$
 
/* Unit spinners (dropdowns) */
private Spinner spinnerUnit1;
private Spinner spinnerUnit2;
private CurrenciesDatabase db;
 
private HashMap<String, HashMap<String, Double>> conversionRates;
 
/** Called when the activity is first created. */
 
@Override
49,86 → 61,10
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_currencies);
 
/* Set up currency database */
CurrenciesActivity.currencyConversions =
new HashMap<String, HashMap<String, Double>>();
/* Set up currency database, and retrieve conversion rates */
this.db = new CurrenciesDatabase(this);
this.conversionRates = this.db.getConversionRates();
 
HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_CHF,
conversionFactors);
 
conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_EUR,
conversionFactors);
 
conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412);
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743);
CurrenciesActivity.currencyConversions.put(CurrenciesActivity.VALUE_USD,
conversionFactors);
 
/* Create database with values above if it does not exist */
CurrenciesDatabase db = new CurrenciesDatabase(this);
try
{
SQLiteDatabase dbConn = db.getReadableDatabase();
 
@SuppressWarnings("nls")
Cursor myCursor =
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
+ CurrenciesDatabase.COLUMN_CURRENCY2, null);
 
@SuppressWarnings({ "unused", "nls" })
String queryResult = "";
if (myCursor != null)
{
try
{
int currency1Id =
myCursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
int currency2Id =
myCursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
int factorId =
myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
 
if (myCursor.moveToFirst())
{
do
{
String currency1Str = myCursor.getString(currency1Id);
String currency2Str = myCursor.getString(currency2Id);
Double factor = myCursor.getDouble(factorId);
 
/* DEBUG */
queryResult +=
currency1Str + " --> " + currency2Str + ": " + factor + "\n";
}
while (myCursor.moveToNext());
}
}
catch (IllegalArgumentException e)
{
/* Could not retrieve column index */
e.printStackTrace();
}
}
 
/* TODO: Close only on exit */
dbConn.close();
}
catch (SQLiteException e1)
{
/* Could not open database */
e1.printStackTrace();
}
 
final EditText editValue1 =
(EditText) this.findViewById(R.id.currencies_edit_value1);
final EditText editValue2 =
254,7 → 190,7
Double newValue = value;
 
HashMap<String, Double> mapForCurrency =
CurrenciesActivity.currencyConversions.get(selectedItemValue1);
this.conversionRates.get(selectedItemValue1);
if (mapForCurrency != null)
{
Double conversionFactor = mapForCurrency.get(selectedItemValue2);
272,6 → 208,11
*
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
285,6 → 226,11
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
/*
* (non-Javadoc)
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
291,12 → 237,4
/* update here */
return super.onOptionsItemSelected(item);
}
 
/**
* @return
*/
public HashMap<String, HashMap<String, Double>> getCurrencyConversions()
{
return CurrenciesActivity.currencyConversions;
}
}
/trunk/src/de/pointedears/converter/db/CurrenciesDatabase.java
7,7 → 7,9
import java.util.Map.Entry;
 
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import de.pointedears.converter.app.CurrenciesActivity;
 
18,13 → 20,35
public class CurrenciesDatabase extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
private static final int DATABASE_VERSION = 1;
private static final int DATABASE_VERSION = 2;
 
public static final String TABLE = "currency"; //$NON-NLS-1$
public static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
public static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
public static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
private static final String TABLE = "currency"; //$NON-NLS-1$
private static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
private static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
 
private static HashMap<String, HashMap<String, Double>> currencyConversions =
new HashMap<String, HashMap<String, Double>>();
static
{
HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_CHF,
conversionFactors);
 
conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_EUR,
conversionFactors);
 
conversionFactors = new HashMap<String, Double>();
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412);
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743);
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_USD,
conversionFactors);
}
private final CurrenciesActivity context;
 
/**
36,6 → 60,7
super(context, CurrenciesDatabase.DATABASE_NAME, null,
CurrenciesDatabase.DATABASE_VERSION);
this.context = context;
this.readConversionsFromDatabase();
}
 
/*
50,13 → 75,16
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
+ "(" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
+ " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
+ CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
+ CurrenciesDatabase.COLUMN_FACTOR
+ " NUMERIC)");
+ " NUMERIC"
+ ", CONSTRAINT unique_currency_pair UNIQUE ("
+ CurrenciesDatabase.COLUMN_CURRENCY1 + ", "
+ CurrenciesDatabase.COLUMN_CURRENCY2 + "))");
 
HashMap<String, HashMap<String, Double>> currencyConversions =
this.context.getCurrencyConversions();
this.getConversionRates();
for (String key : currencyConversions.keySet())
{
for (Entry<String, Double> factorEntry : currencyConversions.get(key)
87,4 → 115,177
db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
this.onCreate(db);
}
 
/**
* @return
*/
public HashMap<String, HashMap<String, Double>> getConversionRates()
{
return CurrenciesDatabase.currencyConversions;
}
 
/**
* Reads currency conversions and updates the static currencyConversions field
* of this class
*/
public void readConversionsFromDatabase()
{
try
{
/* Get database connection, but upgrade database first if necessary! */
SQLiteDatabase dbConn = this.getWritableDatabase();
 
@SuppressWarnings("nls")
Cursor cursor =
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
+ CurrenciesDatabase.COLUMN_CURRENCY2, null);
 
if (cursor != null)
{
try
{
int currency1Id =
cursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
int currency2Id =
cursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
int factorId =
cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
 
/* NOTE: Don't change the default values if the table is empty */
if (cursor.moveToFirst())
{
HashMap<String, HashMap<String, Double>> newCurrencyConversions =
new HashMap<String, HashMap<String, Double>>();
HashMap<String, Double> mapForCurrency = null;
String lastCurrency1Str = null;
String currency1Str;
 
do
{
currency1Str = cursor.getString(currency1Id);
String currency2Str = cursor.getString(currency2Id);
Double factor = cursor.getDouble(factorId);
 
if (lastCurrency1Str == null
|| !lastCurrency1Str.equals(currency1Str))
{
/*
* NOTE: Update outer map when we see a new currency;
* ORDER BY ensures we don't see a currency1 twice except
* consecutively
*/
if (mapForCurrency != null)
{
newCurrencyConversions.put(lastCurrency1Str, mapForCurrency);
}
 
lastCurrency1Str = new String(currency1Str);
 
/* NOTE: New currency1: Reset inner map */
mapForCurrency = newCurrencyConversions.get(currency1Str);
}
 
/* If we did not see this currency1 before */
if (mapForCurrency == null)
{
mapForCurrency = new HashMap<String, Double>();
}
 
/*
* NOTE: Update inner map after each table row; assignment to
* mapForCurrency above ensures we are putting the factor
* into the correct map.
*/
mapForCurrency.put(currency2Str, factor);
}
while (cursor.moveToNext());
 
/*
* NOTE: Update from last table row; cursor not empty, so we can
* skip the test for null
*/
newCurrencyConversions.put(currency1Str, mapForCurrency);
 
CurrenciesDatabase.currencyConversions = newCurrencyConversions;
}
}
catch (IllegalArgumentException e)
{
/* Could not retrieve column index */
e.printStackTrace();
}
}
 
dbConn.close();
}
catch (SQLiteException e1)
{
/* Could not open database */
e1.printStackTrace();
}
}
 
/**
* Tests the database access
*/
public void testAccess()
{
try
{
SQLiteDatabase dbConn = this.getReadableDatabase();
 
@SuppressWarnings("nls")
Cursor myCursor =
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
+ CurrenciesDatabase.COLUMN_CURRENCY2, null);
 
@SuppressWarnings({ "unused", "nls" })
String queryResult = "";
if (myCursor != null)
{
try
{
int currency1Id =
myCursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
int currency2Id =
myCursor
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
int factorId =
myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
 
if (myCursor.moveToFirst())
{
do
{
String currency1Str = myCursor.getString(currency1Id);
String currency2Str = myCursor.getString(currency2Id);
Double factor = myCursor.getDouble(factorId);
 
/* DEBUG */
queryResult +=
currency1Str + " --> " + currency2Str + ": " + factor + "\n";
}
while (myCursor.moveToNext());
}
}
catch (IllegalArgumentException e)
{
/* Could not retrieve column index */
e.printStackTrace();
}
}
 
dbConn.close();
}
catch (SQLiteException e1)
{
/* Could not open database */
e1.printStackTrace();
}
}
}
/trunk/bin/classes.dex
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/trunk/bin/Converter.apk
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream