Subversion Repositories ES

Rev

Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 PointedEar 1
package de.pointedears.converter.helpers;
2
 
3
import android.app.Service;
4
import android.content.Intent;
5
import android.os.Handler;
6
import android.os.IBinder;
7
import de.pointedears.converter.app.CurrenciesActivity;
8
import de.pointedears.converter.net.RatesUpdater;
9
 
10
/**
11
 * Update service to run a thread to update currency rates
12
 *
13
 * @author Thomas 'PointedEars' Lahn
14
 */
15
public class UpdateService extends Service
16
{
17
  // private Timer myTimer = null;
18
  // private final BroadcastTimerTask sendTime = null;
19
  private static CurrenciesActivity activityContext;
20
  private ConverterThread updateThread;
21
  private Handler handler;
22
  private RatesUpdater ratesUpdater;
23
 
24
  public static final String ACTION_UPDATE =
25
    "de.pointedears.converter.ACTION_UPDATE"; //$NON-NLS-1$
26
  public static final String EXTRA_ACTIVITY =
27
    "de.pointedears.converter.extra_activity"; //$NON-NLS-1$
28
  public static final String EXTRA_NUM_RATES =
29
    "de.pointedears.converter.extra_num_rates"; //$NON-NLS-1$
30
  public static final String EXTRA_DATE = "de.pointedears.converter.extra_date"; //$NON-NLS-1$
31
 
18 PointedEar 32
  /* NOTE: Don't remove; may be used later for automated updates */
17 PointedEar 33
  // private sendTimerTask sendTime = null;
34
 
35
  // /** inner class implements the broadcast timer */
36
  // private class BroadcastTimerTask extends TimerTask
37
  // {
38
  // int counter = 0;
39
  //
40
  // @Override
41
  // public void run()
42
  // {
43
  // Intent intent = new Intent(UpdateService.ACTION_UPDATE);
44
  // String theTime =
45
  // "Time: " + System.currentTimeMillis() + ",  Counter = "
46
  // + Integer.toString(this.counter);
47
  // this.counter++;
48
  // intent.putExtra("TIME", theTime);
49
  // UpdateService.this.sendBroadcast(intent);
50
  // }
51
  // };
52
 
53
  @Override
54
  public IBinder onBind(Intent intent)
55
  {
56
    /* NOTE: Clients cannot bind to this service */
57
    return null;
58
  }
59
 
60
  @Override
61
  public void onCreate()
62
  {
63
    super.onCreate();
64
    // this.myTimer = new Timer("myTimer");
65
 
66
    if (this.handler == null)
67
    {
68
      this.handler = new Handler();
69
    }
70
 
71
    this.updateThread = null;
72
  }
73
 
74
  // /*
75
  // * (non-Javadoc)
76
  // *
77
  // * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
78
  // */
79
  // @Override
80
  // public int onStartCommand(Intent intent, int flags, int startId)
81
  // {
82
  // // TODO Auto-generated method stub
83
  // return super.onStartCommand(intent, flags, startId);
84
  // }
85
 
86
  @Override
87
  /**
88
   * @deprecated since SDK 2.0
89
   */
90
  public void onStart(Intent intent, int startId)
91
  {
92
    super.onStart(intent, startId);
93
    // this.myTimer.cancel();
94
    // this.myTimer = new Timer("myTimer");
95
    // this.sendTime = new BroadcastTimerTask();
96
    // this.myTimer.scheduleAtFixedRate(this.sendTime, 0, 1000 * 5);
97
 
98
    String action = intent.getAction();
99
    if (UpdateService.ACTION_UPDATE.equals(action))
100
    {
101
      if (this.updateThread == null)
102
      {
103
        this.ratesUpdater =
18 PointedEar 104
          new RatesUpdater(UpdateService.activityContext, this);
17 PointedEar 105
        this.updateThread =
106
          new ConverterThread(this.ratesUpdater, this.handler);
107
        this.ratesUpdater.setUpdateThread(this.updateThread);
108
      }
109
 
110
      try
111
      {
112
        this.updateThread.start();
113
        // this.editValue1.setText("Gestartet!");
114
      }
115
      catch (IllegalThreadStateException e)
116
      {
117
        // this.editValue1.setText("Bereits gestartet!");
118
      }
119
 
120
      // case R.id.item_options_quit:
121
      // if (this.updateThread != null)
122
      // {
123
      // try
124
      // {
125
      // this.updateThread.join();
126
      // }
127
      // catch (InterruptedException e)
128
      // {
129
      // // TODO Auto-generated catch block
130
      // }
131
      //
132
      // // this.editValue1.setText("Gestoppt -> Warten auf Start");
133
      // }
134
      // else
135
      // {
136
      // // this.editValue1.setText("Bereits gestoppt -> Warten auf Start");
137
      // }
138
      // return true;
139
    }
140
  }
141
 
142
  /**
143
   * @param activityContext
144
   *          the activityContext to set
145
   */
146
  public static void setActivityContext(CurrenciesActivity activityContext)
147
  {
148
    UpdateService.activityContext = activityContext;
149
  }
150
}