Subversion Repositories ES

Rev

Rev 16 | Rev 18 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 17
Line 1... Line 1...
1
package de.pointedears.converter.app;
1
package de.pointedears.converter.app;
2
2
-
 
3
import java.text.DateFormat;
-
 
4
import java.text.SimpleDateFormat;
-
 
5
import java.util.Date;
3
import java.util.HashMap;
6
import java.util.HashMap;
4
import java.util.Map.Entry;
7
import java.util.Map.Entry;
5
8
6
import android.app.Activity;
9
import android.app.Activity;
-
 
10
import android.content.BroadcastReceiver;
-
 
11
import android.content.Context;
-
 
12
import android.content.Intent;
-
 
13
import android.content.IntentFilter;
7
import android.os.Bundle;
14
import android.os.Bundle;
8
import android.os.Handler;
-
 
9
import android.text.Editable;
15
import android.text.Editable;
10
import android.view.KeyEvent;
16
import android.view.KeyEvent;
11
import android.view.Menu;
17
import android.view.Menu;
12
import android.view.MenuInflater;
18
import android.view.MenuInflater;
13
import android.view.MenuItem;
19
import android.view.MenuItem;
Line 21... Line 27...
21
import android.widget.Spinner;
27
import android.widget.Spinner;
22
import android.widget.TableLayout;
28
import android.widget.TableLayout;
23
import android.widget.TableRow;
29
import android.widget.TableRow;
24
import android.widget.TextView;
30
import android.widget.TextView;
25
import de.pointedears.converter.R;
31
import de.pointedears.converter.R;
-
 
32
import de.pointedears.converter.db.ConversionData;
26
import de.pointedears.converter.db.CurrenciesDatabase;
33
import de.pointedears.converter.db.CurrenciesDatabase;
27
import de.pointedears.converter.helpers.ConverterThread;
34
import de.pointedears.converter.helpers.Notifier;
28
import de.pointedears.converter.net.RatesUpdater;
35
import de.pointedears.converter.helpers.UpdateService;
29
36
30
/**
37
/**
31
 * Activity that implements currency conversion
38
 * Activity that implements currency conversion
32
 *
39
 *
33
 * @author pelinux
40
 * @author pelinux
34
 */
41
 */
35
public class CurrenciesActivity extends Activity
42
public class CurrenciesActivity extends Activity
36
{
43
{
-
 
44
  /**
-
 
45
   * String to use to indicate that an exchange rate has never been updated
-
 
46
   */
-
 
47
  private static final String TEXT_NEVER = "never";
-
 
48
-
 
49
  /**
-
 
50
   * Serialization version id
-
 
51
   */
-
 
52
  private static final long serialVersionUID = 1L;
-
 
53
37
  /*
54
  /*
38
   * Constants for mapping value strings
55
   * Constants for mapping value strings
39
   *
56
   *
40
   * @todo: Use resource IDs
57
   * @todo: Use resource IDs
41
   */
58
   */
Line 56... Line 73...
56
  public static final String VALUE_USD = "USD"; //$NON-NLS-1$
73
  public static final String VALUE_USD = "USD"; //$NON-NLS-1$
57
74
58
  /* Unit spinners (dropdowns) */
75
  /* Unit spinners (dropdowns) */
59
  private Spinner spinnerUnit1;
76
  private Spinner spinnerUnit1;
60
  private Spinner spinnerUnit2;
77
  private Spinner spinnerUnit2;
61
  private CurrenciesDatabase db;
78
  private CurrenciesDatabase database;
62
79
63
  private HashMap<String, Double> conversionRates;
80
  private HashMap<String, ConversionData> conversionRates;
64
  private ConverterThread updateThread;
-
 
65
  private Handler handler;
-
 
66
81
-
 
82
  /**
-
 
83
   * Receiver for intent broadcasts, registered in
-
 
84
   * {@link CurrenciesActivity#onCreate(Bundle)}
-
 
85
   */
-
 
86
  public class UpdateBroadcastReceiver extends BroadcastReceiver
-
 
87
  {
-
 
88
    /**
-
 
89
     * Notification message template
-
 
90
     */
-
 
91
    private static final String EXCHANGE_RATES_UPDATED_TO =
67
  private RatesUpdater updateRates;
92
      " exchange rates updated to ";
68
93
-
 
94
    /*
-
 
95
     * (non-Javadoc)
-
 
96
     *
-
 
97
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
-
 
98
     * android.content.Intent)
-
 
99
     */
-
 
100
    @Override
-
 
101
    public void onReceive(Context context, Intent intent)
-
 
102
    {
-
 
103
      if (intent.getAction().equals(UpdateService.ACTION_UPDATE))
-
 
104
      {
69
  /** Called when the activity is first created. */
105
        Bundle extras = intent.getExtras();
-
 
106
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
-
 
107
        Notifier.sendMessage(CurrenciesActivity.this,
-
 
108
          extras.get(UpdateService.EXTRA_NUM_RATES)
-
 
109
            + UpdateBroadcastReceiver.EXCHANGE_RATES_UPDATED_TO
-
 
110
            + df.format(extras.get(UpdateService.EXTRA_DATE)));
-
 
111
      }
-
 
112
    }
-
 
113
  }
70
114
-
 
115
  /** Called when the activity is first created. */
71
  @Override
116
  @Override
72
  public void onCreate(Bundle savedInstanceState)
117
  public void onCreate(Bundle savedInstanceState)
73
  {
118
  {
74
    super.onCreate(savedInstanceState);
119
    super.onCreate(savedInstanceState);
75
    this.setContentView(R.layout.activity_currencies);
120
    this.setContentView(R.layout.activity_currencies);
76
121
-
 
122
    UpdateBroadcastReceiver br = new UpdateBroadcastReceiver();
-
 
123
    this.registerReceiver(br, new IntentFilter(UpdateService.ACTION_UPDATE));
-
 
124
77
    /* Set up currency database, and retrieve conversion rates */
125
    /* Set up currency database, and retrieve conversion rates */
78
    this.db = new CurrenciesDatabase(this);
126
    this.database = new CurrenciesDatabase(this);
79
    this.conversionRates = this.db.getConversionRates();
127
    this.setConversionRates(this.getDatabase().getConversionRates());
80
    this.fillTableRates();
128
    this.fillTableRates();
81
129
82
    final EditText editValue1 =
130
    final EditText editValue1 =
83
      (EditText) this.findViewById(R.id.currencies_edit_value1);
131
      (EditText) this.findViewById(R.id.currencies_edit_value1);
84
    final EditText editValue2 =
132
    final EditText editValue2 =
Line 190... Line 238...
190
      {
238
      {
191
        editValue1.setText("");
239
        editValue1.setText("");
192
        editValue2.setText("");
240
        editValue2.setText("");
193
      }
241
      }
194
    });
242
    });
195
-
 
196
    if (this.handler == null)
-
 
197
    {
-
 
198
      this.handler = new Handler();
-
 
199
    }
-
 
200
-
 
201
    this.updateThread = null;
-
 
202
  }
243
  }
203
244
204
  /**
245
  /**
205
   * Fills the table with currency conversion rates
246
   * Fills the table with currency conversion rates
206
   */
247
   */
207
  private void fillTableRates()
248
  public void fillTableRates()
208
  {
249
  {
209
    TableLayout tableRates =
250
    TableLayout tableRates =
210
      (TableLayout) this.findViewById(R.id.currencies_table_rates);
251
      (TableLayout) this.findViewById(R.id.currencies_table_rates);
211
252
-
 
253
    /* Remove any pre-existing currency rows */
-
 
254
    while (tableRates.getChildCount() > 3)
-
 
255
    {
-
 
256
      tableRates.removeViewAt(3);
-
 
257
    }
-
 
258
212
    for (Entry<String, Double> factorEntry : this.conversionRates.entrySet())
259
    for (Entry<String, ConversionData> factorEntry : this.getConversionRates()
-
 
260
      .entrySet())
213
    {
261
    {
214
      TableRow row = new TableRow(this);
262
      TableRow row = new TableRow(this);
215
263
216
      TextView columnCurrency1 = new TextView(this);
264
      TextView columnCurrency1 = new TextView(this);
217
      columnCurrency1.setText(factorEntry.getKey());
265
      columnCurrency1.setText(factorEntry.getKey());
218
      row.addView(columnCurrency1);
266
      row.addView(columnCurrency1);
219
267
220
      TextView columnRate = new TextView(this);
268
      TextView columnRate = new TextView(this);
-
 
269
      final ConversionData conversionData = factorEntry.getValue();
221
      columnRate.setText(factorEntry.getValue().toString());
270
      columnRate.setText(conversionData.getRate().toString());
222
      row.addView(columnRate);
271
      row.addView(columnRate);
223
272
-
 
273
      TextView columnUpdated = new TextView(this);
-
 
274
      Date updated = conversionData.getUpdated();
-
 
275
      if (updated.getTime() > 0)
-
 
276
      {
-
 
277
        DateFormat df =
-
 
278
          DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
-
 
279
        columnUpdated.setText(df.format(updated));
-
 
280
      }
-
 
281
      else
-
 
282
      {
-
 
283
        columnUpdated.setText(CurrenciesActivity.TEXT_NEVER);
-
 
284
      }
-
 
285
-
 
286
      row.addView(columnUpdated);
-
 
287
224
      tableRates.addView(row);
288
      tableRates.addView(row);
225
    }
289
    }
226
  }
290
  }
227
291
228
  /**
292
  /**
Line 249... Line 313...
249
313
250
    /*
314
    /*
251
     * NOTE: Had to do it the complicated way because somehow the Android SDK
315
     * NOTE: Had to do it the complicated way because somehow the Android SDK
252
     * won't get it another way
316
     * won't get it another way
253
     */
317
     */
-
 
318
    ConversionData conversionData1 = null;
254
    Double factorToEuro = null;
319
    Double factorToEuro = 1.0;
255
    if (selectedItemValue1 != null)
320
    if (selectedItemValue1 != null)
256
    {
321
    {
257
      factorToEuro = this.conversionRates.get(selectedItemValue1);
322
      conversionData1 = this.getConversionRates().get(selectedItemValue1);
258
    }
-
 
259
-
 
260
    if (factorToEuro == null)
323
      if (conversionData1 != null)
261
    {
324
      {
262
      factorToEuro = 1.0;
325
        factorToEuro = conversionData1.getRate();
-
 
326
      }
263
    }
327
    }
264
328
-
 
329
    ConversionData conversionData2 = null;
265
    Double factorFromEuro = null;
330
    Double factorFromEuro = 1.0;
266
    if (selectedItemValue2 != null)
331
    if (selectedItemValue2 != null)
267
    {
332
    {
268
      factorFromEuro = this.conversionRates.get(selectedItemValue2);
333
      conversionData2 = this.getConversionRates().get(selectedItemValue2);
269
    }
-
 
270
-
 
271
    if (factorFromEuro == null)
334
      if (conversionData2 != null)
272
    {
335
      {
273
      factorFromEuro = 1.0;
336
        factorFromEuro = conversionData2.getRate();
-
 
337
      }
274
    }
338
    }
275
339
276
    newValue = newValue / factorToEuro * factorFromEuro;
340
    newValue = newValue / factorToEuro * factorFromEuro;
277
341
278
    return newValue.toString();
342
    return newValue.toString();
Line 302... Line 366...
302
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
366
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
303
   */
367
   */
304
  @Override
368
  @Override
305
  public boolean onOptionsItemSelected(MenuItem item)
369
  public boolean onOptionsItemSelected(MenuItem item)
306
  {
370
  {
307
    // Handle item selection
371
    /* Handle item selection */
308
    switch (item.getItemId())
372
    switch (item.getItemId())
309
    {
373
    {
310
      case R.id.item_options_update:
374
      case R.id.item_options_update:
311
        if (this.updateThread == null)
-
 
312
        {
375
        /*
313
          this.updateRates = new RatesUpdater(this);
376
         * Request the update service to run a thread to fetch the rates from
314
          this.updateThread =
377
         * the Web (project requirement)
315
            new ConverterThread(this.updateRates, this.handler);
-
 
316
          this.updateRates.setUpdateThread(this.updateThread);
-
 
317
        }
-
 
318
-
 
319
        try
378
         */
320
        {
-
 
321
          this.updateThread.start();
-
 
322
          // this.editValue1.setText("Gestartet!");
379
        Intent intent = new Intent(this, UpdateService.class);
323
        }
-
 
324
        catch (IllegalThreadStateException e)
-
 
325
        {
-
 
326
          // this.editValue1.setText("Bereits gestartet!");
380
        intent.setAction(UpdateService.ACTION_UPDATE);
327
        }
-
 
328
        return true;
-
 
329
381
330
      case R.id.item_options_quit:
-
 
331
        if (this.updateThread != null)
-
 
332
        {
382
        /*
333
          try
-
 
334
          {
-
 
335
            this.updateThread.join();
383
         * FIXME: Not thread-safe!
336
          }
-
 
337
          catch (InterruptedException e)
384
         * Get the activity context from the intent directly instead
338
          {
385
         */
339
            // TODO Auto-generated catch block
386
        UpdateService.setActivityContext(this);
340
          }
-
 
341
387
342
          // this.editValue1.setText("Gestoppt -> Warten auf Start");
388
        this.startService(intent);
343
        }
-
 
344
        else
-
 
345
        {
-
 
346
          // this.editValue1.setText("Bereits gestoppt -> Warten auf Start");
-
 
347
        }
-
 
348
        return true;
389
        return true;
349
390
350
      default:
391
      default:
351
        return super.onOptionsItemSelected(item);
392
        return super.onOptionsItemSelected(item);
352
    }
393
    }
353
  }
394
  }
-
 
395
-
 
396
  /**
-
 
397
   * @return the conversionRates
-
 
398
   */
-
 
399
  public HashMap<String, ConversionData> getConversionRates()
-
 
400
  {
-
 
401
    return this.conversionRates;
-
 
402
  }
-
 
403
-
 
404
  /**
-
 
405
   * @param conversionRates
-
 
406
   *          the conversionRates to set
-
 
407
   */
-
 
408
  public void setConversionRates(HashMap<String, ConversionData> conversionRates)
-
 
409
  {
-
 
410
    this.conversionRates = conversionRates;
-
 
411
  }
-
 
412
-
 
413
  /**
-
 
414
   * @return the database
-
 
415
   */
-
 
416
  public CurrenciesDatabase getDatabase()
-
 
417
  {
-
 
418
    return this.database;
-
 
419
  }
354
}
420
}