Subversion Repositories ES

Rev

Rev 17 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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