5,6 → 5,7 |
|
import android.app.Activity; |
import android.os.Bundle; |
import android.os.Handler; |
import android.text.Editable; |
import android.view.KeyEvent; |
import android.view.Menu; |
23,7 → 24,8 |
import android.widget.TextView; |
import de.pointedears.converter.R; |
import de.pointedears.converter.db.CurrenciesDatabase; |
import de.pointedears.converter.helpers.CurrenciesUpdateThread; |
import de.pointedears.converter.helpers.ConverterThread; |
import de.pointedears.converter.net.RatesUpdater; |
|
/** |
* Activity that implements currency conversion |
58,8 → 60,12 |
private Spinner spinnerUnit2; |
private CurrenciesDatabase db; |
|
private HashMap<String, HashMap<String, Double>> conversionRates; |
private HashMap<String, Double> conversionRates; |
private ConverterThread updateThread; |
private Handler handler; |
|
private RatesUpdater updateRates; |
|
/** Called when the activity is first created. */ |
|
@Override |
186,8 → 192,15 |
editValue2.setText(""); |
} |
}); |
|
if (this.handler == null) |
{ |
this.handler = new Handler(); |
} |
|
this.updateThread = null; |
} |
|
/** |
* Fills the table with currency conversion rates |
*/ |
196,21 → 209,14 |
TableLayout tableRates = |
(TableLayout) this.findViewById(R.id.currencies_table_rates); |
|
for (String key : this.conversionRates.keySet()) |
for (Entry<String, Double> factorEntry : this.conversionRates.entrySet()) |
{ |
for (Entry<String, Double> factorEntry : this.conversionRates.get(key) |
.entrySet()) |
{ |
TableRow row = new TableRow(this); |
|
TextView columnCurrency1 = new TextView(this); |
columnCurrency1.setText(key); |
columnCurrency1.setText(factorEntry.getKey()); |
row.addView(columnCurrency1); |
|
TextView columnCurrency2 = new TextView(this); |
columnCurrency2.setText(factorEntry.getKey()); |
row.addView(columnCurrency2); |
|
TextView columnRate = new TextView(this); |
columnRate.setText(factorEntry.getValue().toString()); |
row.addView(columnRate); |
218,7 → 224,6 |
tableRates.addView(row); |
} |
} |
} |
|
/** |
* @param value |
242,17 → 247,34 |
|
Double newValue = value; |
|
HashMap<String, Double> mapForCurrency = |
this.conversionRates.get(selectedItemValue1); |
if (mapForCurrency != null) |
/* |
* NOTE: Had to do it the complicated way because somehow the Android SDK |
* won't get it another way |
*/ |
Double factorToEuro = null; |
if (selectedItemValue1 != null) |
{ |
Double conversionFactor = mapForCurrency.get(selectedItemValue2); |
if (conversionFactor != null) |
factorToEuro = this.conversionRates.get(selectedItemValue1); |
} |
|
if (factorToEuro == null) |
{ |
newValue *= conversionFactor; |
factorToEuro = 1.0; |
} |
|
Double factorFromEuro = null; |
if (selectedItemValue2 != null) |
{ |
factorFromEuro = this.conversionRates.get(selectedItemValue2); |
} |
|
if (factorFromEuro == null) |
{ |
factorFromEuro = 1.0; |
} |
|
newValue = newValue / factorToEuro * factorFromEuro; |
|
return newValue.toString(); |
} |
|
286,10 → 308,45 |
switch (item.getItemId()) |
{ |
case R.id.item_options_update: |
Thread updateThread = new CurrenciesUpdateThread(); |
updateThread.start(); |
if (this.updateThread == null) |
{ |
this.updateRates = new RatesUpdater(this); |
this.updateThread = |
new ConverterThread(this.updateRates, this.handler); |
this.updateRates.setUpdateThread(this.updateThread); |
} |
|
try |
{ |
this.updateThread.start(); |
// this.editValue1.setText("Gestartet!"); |
} |
catch (IllegalThreadStateException e) |
{ |
// this.editValue1.setText("Bereits gestartet!"); |
} |
return true; |
|
case R.id.item_options_quit: |
if (this.updateThread != null) |
{ |
try |
{ |
this.updateThread.join(); |
} |
catch (InterruptedException e) |
{ |
// TODO Auto-generated catch block |
} |
|
// this.editValue1.setText("Gestoppt -> Warten auf Start"); |
} |
else |
{ |
// this.editValue1.setText("Bereits gestoppt -> Warten auf Start"); |
} |
return true; |
|
default: |
return super.onOptionsItemSelected(item); |
} |