OSDN Git Service

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