Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 16 → Rev 17

/trunk/src/de/pointedears/converter/app/CurrenciesActivity.java
1,11 → 1,17
package de.pointedears.converter.app;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
 
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.Menu;
23,9 → 29,10
import android.widget.TableRow;
import android.widget.TextView;
import de.pointedears.converter.R;
import de.pointedears.converter.db.ConversionData;
import de.pointedears.converter.db.CurrenciesDatabase;
import de.pointedears.converter.helpers.ConverterThread;
import de.pointedears.converter.net.RatesUpdater;
import de.pointedears.converter.helpers.Notifier;
import de.pointedears.converter.helpers.UpdateService;
 
/**
* Activity that implements currency conversion
34,6 → 41,16
*/
public class CurrenciesActivity extends Activity
{
/**
* String to use to indicate that an exchange rate has never been updated
*/
private static final String TEXT_NEVER = "never";
 
/**
* Serialization version id
*/
private static final long serialVersionUID = 1L;
 
/*
* Constants for mapping value strings
*
58,16 → 75,44
/* Unit spinners (dropdowns) */
private Spinner spinnerUnit1;
private Spinner spinnerUnit2;
private CurrenciesDatabase db;
private CurrenciesDatabase database;
 
private HashMap<String, Double> conversionRates;
private ConverterThread updateThread;
private Handler handler;
private HashMap<String, ConversionData> conversionRates;
 
private RatesUpdater updateRates;
/**
* Receiver for intent broadcasts, registered in
* {@link CurrenciesActivity#onCreate(Bundle)}
*/
public class UpdateBroadcastReceiver extends BroadcastReceiver
{
/**
* Notification message template
*/
private static final String EXCHANGE_RATES_UPDATED_TO =
" exchange rates updated to ";
 
/*
* (non-Javadoc)
*
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(UpdateService.ACTION_UPDATE))
{
Bundle extras = intent.getExtras();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
Notifier.sendMessage(CurrenciesActivity.this,
extras.get(UpdateService.EXTRA_NUM_RATES)
+ UpdateBroadcastReceiver.EXCHANGE_RATES_UPDATED_TO
+ df.format(extras.get(UpdateService.EXTRA_DATE)));
}
}
}
 
/** Called when the activity is first created. */
 
@Override
public void onCreate(Bundle savedInstanceState)
{
74,9 → 119,12
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_currencies);
 
UpdateBroadcastReceiver br = new UpdateBroadcastReceiver();
this.registerReceiver(br, new IntentFilter(UpdateService.ACTION_UPDATE));
 
/* Set up currency database, and retrieve conversion rates */
this.db = new CurrenciesDatabase(this);
this.conversionRates = this.db.getConversionRates();
this.database = new CurrenciesDatabase(this);
this.setConversionRates(this.getDatabase().getConversionRates());
this.fillTableRates();
 
final EditText editValue1 =
192,25 → 240,25
editValue2.setText("");
}
});
 
if (this.handler == null)
{
this.handler = new Handler();
}
 
this.updateThread = null;
}
 
/**
* Fills the table with currency conversion rates
*/
private void fillTableRates()
public void fillTableRates()
{
TableLayout tableRates =
(TableLayout) this.findViewById(R.id.currencies_table_rates);
 
for (Entry<String, Double> factorEntry : this.conversionRates.entrySet())
/* Remove any pre-existing currency rows */
while (tableRates.getChildCount() > 3)
{
tableRates.removeViewAt(3);
}
 
for (Entry<String, ConversionData> factorEntry : this.getConversionRates()
.entrySet())
{
TableRow row = new TableRow(this);
 
TextView columnCurrency1 = new TextView(this);
218,9 → 266,25
row.addView(columnCurrency1);
 
TextView columnRate = new TextView(this);
columnRate.setText(factorEntry.getValue().toString());
final ConversionData conversionData = factorEntry.getValue();
columnRate.setText(conversionData.getRate().toString());
row.addView(columnRate);
 
TextView columnUpdated = new TextView(this);
Date updated = conversionData.getUpdated();
if (updated.getTime() > 0)
{
DateFormat df =
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
columnUpdated.setText(df.format(updated));
}
else
{
columnUpdated.setText(CurrenciesActivity.TEXT_NEVER);
}
 
row.addView(columnUpdated);
 
tableRates.addView(row);
}
}
251,28 → 315,28
* NOTE: Had to do it the complicated way because somehow the Android SDK
* won't get it another way
*/
Double factorToEuro = null;
ConversionData conversionData1 = null;
Double factorToEuro = 1.0;
if (selectedItemValue1 != null)
{
factorToEuro = this.conversionRates.get(selectedItemValue1);
conversionData1 = this.getConversionRates().get(selectedItemValue1);
if (conversionData1 != null)
{
factorToEuro = conversionData1.getRate();
}
}
 
if (factorToEuro == null)
{
factorToEuro = 1.0;
}
 
Double factorFromEuro = null;
ConversionData conversionData2 = null;
Double factorFromEuro = 1.0;
if (selectedItemValue2 != null)
{
factorFromEuro = this.conversionRates.get(selectedItemValue2);
conversionData2 = this.getConversionRates().get(selectedItemValue2);
if (conversionData2 != null)
{
factorFromEuro = conversionData2.getRate();
}
}
 
if (factorFromEuro == null)
{
factorFromEuro = 1.0;
}
 
newValue = newValue / factorToEuro * factorFromEuro;
 
return newValue.toString();
304,47 → 368,24
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
/* Handle item selection */
switch (item.getItemId())
{
case R.id.item_options_update:
if (this.updateThread == null)
{
this.updateRates = new RatesUpdater(this);
this.updateThread =
new ConverterThread(this.updateRates, this.handler);
this.updateRates.setUpdateThread(this.updateThread);
}
/*
* Request the update service to run a thread to fetch the rates from
* the Web (project requirement)
*/
Intent intent = new Intent(this, UpdateService.class);
intent.setAction(UpdateService.ACTION_UPDATE);
 
try
{
this.updateThread.start();
// this.editValue1.setText("Gestartet!");
}
catch (IllegalThreadStateException e)
{
// this.editValue1.setText("Bereits gestartet!");
}
return true;
/*
* FIXME: Not thread-safe!
* Get the activity context from the intent directly instead
*/
UpdateService.setActivityContext(this);
 
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");
}
this.startService(intent);
return true;
 
default:
351,4 → 392,29
return super.onOptionsItemSelected(item);
}
}
 
/**
* @return the conversionRates
*/
public HashMap<String, ConversionData> getConversionRates()
{
return this.conversionRates;
}
 
/**
* @param conversionRates
* the conversionRates to set
*/
public void setConversionRates(HashMap<String, ConversionData> conversionRates)
{
this.conversionRates = conversionRates;
}
 
/**
* @return the database
*/
public CurrenciesDatabase getDatabase()
{
return this.database;
}
}