Subversion Repositories ES

Rev

Rev 18 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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