package de.pointedears.converter.app;
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;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.Spinner;
import de.pointedears.converter.R;
import de.pointedears.converter.db.CurrenciesDatabase;
/**
* Activity that implements currency conversion
*
* @author pelinux
*/
public class CurrenciesActivity
extends Activity
{
/*
* Constants for mapping value strings
*/
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$
protected static HashMap<String,
HashMap<String,
Double>> currencyConversions
;
/* Unit spinners (dropdowns) */
private Spinner spinnerUnit1
;
private Spinner spinnerUnit2
;
/** Called when the activity is first created. */
@
Override
public void onCreate
(Bundle savedInstanceState
)
{
super.
onCreate(savedInstanceState
);
this.
setContentView(R.
layout.
activity_currencies);
/* Set up currency database */
CurrenciesActivity.
currencyConversions =
new HashMap<String,
HashMap<String,
Double>>();
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 =
(EditText
) this.
findViewById(R.
id.
currencies_edit_value2);
final OnKeyListener editValue1OnKey =
new OnKeyListener
() {
@
Override
public boolean onKey
(View v,
int keyCode,
KeyEvent event
)
{
Editable editable1 =
((EditText
) v
).
getText();
Double value1
;
try
{
value1 =
Double.
parseDouble(editable1.
toString());
}
catch (NumberFormatException e
)
{
value1 =
null;
}
String string2 =
""; //$NON-NLS-1$
if (value1
!=
null)
{
string2 = CurrenciesActivity.
this.
getConvertedValue(value1,
false);
}
editValue2.
setText(string2
);
return false;
}
};
editValue1.
setOnKeyListener(editValue1OnKey
);
final OnKeyListener editValue2OnKey =
new OnKeyListener
() {
@
Override
public boolean onKey
(View v,
int keyCode,
KeyEvent event
)
{
Editable editable2 =
((EditText
) v
).
getText();
Double value2
;
try
{
value2 =
Double.
parseDouble(editable2.
toString());
}
catch (NumberFormatException e
)
{
value2 =
null;
}
String string1 =
""; //$NON-NLS-1$
if (value2
!=
null)
{
string1 = CurrenciesActivity.
this.
getConvertedValue(value2,
true);
}
editValue1.
setText(string1
);
return false;
}
};
editValue2.
setOnKeyListener(editValue2OnKey
);
this.
spinnerUnit1 =
(Spinner
) this.
findViewById(R.
id.
currencies_spinner_unit1);
this.
spinnerUnit2 =
(Spinner
) this.
findViewById(R.
id.
currencies_spinner_unit2);
this.
spinnerUnit1.
setOnItemSelectedListener(new OnItemSelectedListener
() {
@
Override
public void onItemSelected
(AdapterView
<?> arg0,
View arg1,
int arg2,
long arg3
)
{
/* Simulate input in second EditText so that first EditText is updated */
editValue2OnKey.
onKey(editValue2,
0,
null);
}
@
Override
public void onNothingSelected
(AdapterView
<?> arg0
)
{
/* no-op */
}
});
this.
spinnerUnit2.
setSelection(1);
this.
spinnerUnit2.
setOnItemSelectedListener(new OnItemSelectedListener
() {
@
Override
public void onItemSelected
(AdapterView
<?> arg0,
View arg1,
int arg2,
long arg3
)
{
/* Simulate input in first EditText so that second EditText is updated */
editValue1OnKey.
onKey(editValue1,
0,
null);
}
@
Override
public void onNothingSelected
(AdapterView
<?> arg0
)
{
/* no-op */
}
});
}
/**
* @param value
* @return
*/
private String getConvertedValue
(double value,
boolean reverse
)
{
int selectedItemPosition1 =
this.
spinnerUnit1.
getSelectedItemPosition();
int selectedItemPosition2 =
this.
spinnerUnit2.
getSelectedItemPosition();
String[] items =
this.
getResources().
getStringArray(R.
array.
currency_units_values);
String selectedItemValue1 = items
[selectedItemPosition1
];
String selectedItemValue2 = items
[selectedItemPosition2
];
if (reverse
)
{
String tmp = selectedItemValue1
;
selectedItemValue1 = selectedItemValue2
;
selectedItemValue2 = tmp
;
}
Double newValue = value
;
HashMap<String,
Double> mapForCurrency =
CurrenciesActivity.
currencyConversions.
get(selectedItemValue1
);
if (mapForCurrency
!=
null)
{
Double conversionFactor = mapForCurrency.
get(selectedItemValue2
);
if (conversionFactor
!=
null)
{
newValue
*= conversionFactor
;
}
}
return newValue.
toString();
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@
Override
public boolean onCreateOptionsMenu
(Menu menu
)
{
MenuInflater inflater =
this.
getMenuInflater();
inflater.
inflate(R.
menu.
options, menu
);
return true;
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@
Override
public boolean onOptionsItemSelected
(MenuItem item
)
{
/* update here */
return super.
onOptionsItemSelected(item
);
}
/**
* @return
*/
public HashMap<String,
HashMap<String,
Double>> getCurrencyConversions
()
{
return CurrenciesActivity.
currencyConversions;
}
}