OSDN Git Service

2350e12c529bbafd824c671ee88a8c1d21cdad61
[train-delayed/source.git] / workspace / TrainDelayed / src / com / td / service / TrainDelayedServiceImpl.java
1 package com.td.service;\r
2 \r
3 import java.util.Calendar;\r
4 import java.util.List;\r
5 \r
6 import twitter4j.AsyncTwitter;\r
7 import twitter4j.AsyncTwitterFactory;\r
8 import twitter4j.Query;\r
9 import android.app.AlarmManager;\r
10 import android.app.Notification;\r
11 import android.app.NotificationManager;\r
12 import android.app.PendingIntent;\r
13 import android.app.Service;\r
14 import android.content.Context;\r
15 import android.content.Intent;\r
16 import android.os.Binder;\r
17 import android.os.IBinder;\r
18 \r
19 import com.td.R;\r
20 import com.td.TrainDelayedActivity;\r
21 import com.td.broadcast.DelayReceiver;\r
22 import com.td.db.DatabaseHelper;\r
23 import com.td.db.Keyword;\r
24 import com.td.db.Train;\r
25 import com.td.utility.DateUtil;\r
26 import com.td.utility.Debug;\r
27 import com.td.utility.EditPrefUtil;\r
28 import com.td.utility.Util;\r
29 \r
30 public class TrainDelayedServiceImpl extends Service {\r
31         public static final String PREF_SVS_STATUS = "SVS_STATUS";\r
32         public static final String PREF_SVS_DBFLG = "SVS_DBFLG";\r
33         public static final String START_ACTION = "com.td.START";\r
34 \r
35         private final IBinder binder = new Binder();\r
36         private static Scheduler scheduler = null;\r
37 \r
38         private EditPrefUtil pref = new EditPrefUtil(this);\r
39 \r
40         private static AsyncTwitterFactory factory = new AsyncTwitterFactory();\r
41         private AsyncTwitter twitter = factory.getInstance();\r
42         private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);\r
43         private String action = null;\r
44 \r
45         private static long id = 0;\r
46 \r
47         @Override\r
48         public IBinder onBind(Intent intent) {\r
49                 return binder;\r
50         }\r
51 \r
52         private void schedule(long delay) {\r
53                 try {\r
54                         Debug.d(this, "run() " + id);\r
55 \r
56                         if (scheduler == null) {\r
57                                 return;\r
58                         }\r
59                         List<Train> list = scheduler.getTrains();\r
60                         if (list == null) {\r
61                                 return;\r
62                         }\r
63 \r
64                         Calendar cal = Calendar.getInstance();\r
65                         listener.setCalender(cal);\r
66 \r
67                         Query query;\r
68 \r
69                         for (Train train : list) {\r
70                                 String text = train.getSearchText();\r
71 \r
72                                 query = new Query();\r
73                                 query.setRpp(100); // TODO\r
74                                 query.setQuery(text);\r
75                                 twitter.search(query);\r
76                         }\r
77                         // TODO\r
78                         // notificate("id = " + id);\r
79 \r
80                         id = (id + 1) % 1000000;\r
81                 } catch (Exception e) {\r
82                         Debug.d(this, null, e);\r
83                 }\r
84         }\r
85 \r
86         private void notificate(String msg) {\r
87                 try {\r
88                         String ns = Context.NOTIFICATION_SERVICE;\r
89                         NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);\r
90 \r
91                         int icon = R.drawable.icon;\r
92                         CharSequence tickerText = "\93d\8eÔ\82ª\92x\82ê\82Ä\82é?";\r
93                         long when = System.currentTimeMillis();\r
94                         Notification notification = new Notification(icon, tickerText, when);\r
95 \r
96                         Context context = getApplicationContext();\r
97                         CharSequence contentTitle = "My notification"; // TODO\r
98                         CharSequence contentText = msg;\r
99                         Intent notificationIntent = new Intent(this,\r
100                                         TrainDelayedActivity.class);\r
101                         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,\r
102                                         notificationIntent, 0);\r
103                         notification.setLatestEventInfo(context, contentTitle, contentText,\r
104                                         contentIntent);\r
105 \r
106                         int HELLO_ID = 1;\r
107                         mNotificationManager.notify(HELLO_ID, notification);\r
108                 } catch (Exception e) {\r
109                         Debug.d(this, null, e);\r
110                 }\r
111         }\r
112 \r
113         @Override\r
114         public void onCreate() {\r
115                 super.onCreate();\r
116 \r
117                 Debug.d(this, Debug.ON_CREATE);\r
118                 twitter.addListener(listener);\r
119         }\r
120 \r
121         private Runnable task = new Runnable() {\r
122                 public void run() {\r
123                         boolean flag = false;\r
124                         try {\r
125                                 try {\r
126                                         long now = System.currentTimeMillis();\r
127 \r
128                                         Debug.d(this, "id=" + id);\r
129                                         synchronized (binder) {\r
130                                                 try {\r
131                                                         int prefDBFlag = pref.getInt(PREF_SVS_DBFLG, 0);\r
132                                                         if (!Util.toBoolean(prefDBFlag)) {\r
133                                                                 pref.put(PREF_SVS_DBFLG, 1);\r
134                                                                 pref.update();\r
135                                                                 scheduler = null;\r
136                                                         }\r
137 \r
138                                                         if (scheduler == null) {\r
139                                                                 scheduler = new Scheduler();\r
140                                                                 DatabaseHelper.init(getApplicationContext());\r
141                                                         }\r
142                                                         scheduler.init();\r
143                                                         schedule(-1);\r
144                                                 } catch (Exception e) {\r
145                                                         // TODO\r
146                                                         Debug.t(TrainDelayedServiceImpl.this,\r
147                                                                         e.getMessage());\r
148                                                         Debug.d(this, null, e);\r
149                                                 }\r
150                                         }\r
151 \r
152                                         long next = scheduler.calcSchedule(now);\r
153                                         String nextStr = DateUtil.formatted(next);\r
154                                         Debug.t(TrainDelayedServiceImpl.this, "\8e\9f\89ñ\8bN\93®\8e\9e\8d\8f " + nextStr);\r
155                                         Debug.d(this, "\8e\9f\89ñ\8bN\93®\8e\9e\8d\8f " + nextStr);\r
156 \r
157                                         // \8e\9f\89ñ\8bN\93®\93o\98^\r
158                                         PendingIntent alarmSender = PendingIntent.getService(\r
159                                                         TrainDelayedServiceImpl.this, 0, new Intent(\r
160                                                                         TrainDelayedServiceImpl.this,\r
161                                                                         TrainDelayedServiceImpl.class), 0);\r
162                                         AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\r
163                                         alarm.set(AlarmManager.RTC, next, alarmSender);\r
164                                         flag = true;\r
165 \r
166                                         if (action.compareTo(START_ACTION) == 0) {\r
167                                                 pref.put(PREF_SVS_STATUS, 2);\r
168                                                 pref.update();\r
169                                                 \r
170                                                 Intent intent = new Intent(DelayReceiver.START_SERVICE);\r
171                                                 TrainDelayedServiceImpl.this.sendBroadcast(intent);                                             \r
172 \r
173                                                 Debug.t(TrainDelayedServiceImpl.this, START_ACTION);\r
174                                         }\r
175 \r
176                                         // \83T\81[\83r\83X\8fI\97¹\r
177                                         TrainDelayedServiceImpl.this.stopSelf();\r
178 \r
179                                         id = (id + 1) % 1000000;\r
180                                 } catch (Exception e) {\r
181                                         Debug.t(TrainDelayedServiceImpl.this, e.getMessage());\r
182                                         Debug.d(this, null, e);\r
183                                 }\r
184                         } finally {\r
185                                 if (!flag){\r
186                                         pref.put(PREF_SVS_STATUS, 0);\r
187                                         pref.update();                                  \r
188 \r
189                                         Intent intent = new Intent(DelayReceiver.STOP_SERVICE);\r
190                                         TrainDelayedServiceImpl.this.sendBroadcast(intent);                                             \r
191                                 }\r
192                         }\r
193                 }\r
194         };\r
195 \r
196         @Override\r
197         public void onStart(Intent intent, int startId) {\r
198                 super.onStart(intent, startId);\r
199 \r
200                 Debug.d(this, Debug.ON_START);\r
201 \r
202                 action = intent.getAction();\r
203                 action = (action == null) ? "" : action;\r
204 \r
205                 Thread thread = new Thread(null, task,\r
206                                 TrainDelayedServiceImpl.class.getSimpleName());\r
207                 thread.start();\r
208         }\r
209 \r
210         @Override\r
211         public void onDestroy() {\r
212                 Debug.d(this, Debug.ON_DESTROY);\r
213 \r
214                 super.onDestroy();\r
215         }\r
216 \r
217         @Override\r
218         public void onRebind(Intent intent) {\r
219                 super.onRebind(intent);\r
220 \r
221                 Debug.d(this, Debug.ON_REBIND);\r
222         }\r
223 \r
224         @Override\r
225         public boolean onUnbind(Intent intent) {\r
226                 Debug.d(this, Debug.ON_UNBIND);\r
227 \r
228                 super.onUnbind(intent);\r
229                 // \8dÄ\93x\83N\83\89\83C\83A\83\93\83g\82©\82ç\90Ú\91±\82³\82ê\82½\8dÛ\82É onRebind \82ð\8cÄ\82Ñ\8fo\82³\82¹\82é\8fê\8d\87\82Í true \82ð\95Ô\82·\r
230                 return true;\r
231         }\r
232 \r
233         public List<Keyword> getKeywords() {\r
234                 if (scheduler == null) {\r
235                         return null;\r
236                 }\r
237                 return scheduler.getKeywords();\r
238         }\r
239         \r
240         public List<Train> getTrains(){\r
241                 if (scheduler == null) {\r
242                         return null;\r
243                 }\r
244                 return scheduler.getTrains();           \r
245         }\r
246         \r
247         public static void clearSchedule(EditPrefUtil pref){\r
248                 pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);\r
249                 pref.update();          \r
250         }\r
251 }\r