1 package com.td.service;
\r
3 import java.util.Calendar;
\r
4 import java.util.List;
\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
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
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
36 private final IBinder binder = new Binder();
\r
37 private static Scheduler scheduler = null;
\r
39 private EditPrefUtil pref = new EditPrefUtil(this);
\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
46 private static long id = 0;
\r
49 public IBinder onBind(Intent intent) {
\r
53 private void schedule(long delay) {
\r
55 Debug.d(this, "run() " + id);
\r
57 if (scheduler == null) {
\r
60 List<Train> list = scheduler.getTrains();
\r
65 Calendar cal = Calendar.getInstance();
\r
66 listener.setCalender(cal);
\r
70 for (Train train : list) {
\r
71 String text = train.getSearchText();
\r
73 query = new Query();
\r
74 query.setRpp(100); // TODO
\r
75 query.setQuery(text);
\r
76 twitter.search(query);
\r
79 // notificate("id = " + id);
\r
81 id = (id + 1) % 1000000;
\r
82 } catch (Exception e) {
\r
83 Debug.d(this, null, e);
\r
87 private void notificate(String msg) {
\r
89 String ns = Context.NOTIFICATION_SERVICE;
\r
90 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
\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
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
108 mNotificationManager.notify(HELLO_ID, notification);
\r
109 } catch (Exception e) {
\r
110 Debug.d(this, null, e);
\r
115 public void onCreate() {
\r
118 Debug.d(this, Debug.ON_CREATE);
\r
119 twitter.addListener(listener);
\r
122 private Runnable task = new Runnable() {
\r
123 public void run() {
\r
124 boolean flag = false;
\r
127 long now = System.currentTimeMillis();
\r
129 Debug.d(this, "id=" + id);
\r
130 synchronized (binder) {
\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
139 if (scheduler == null) {
\r
140 scheduler = new Scheduler();
\r
141 DatabaseHelper.init(getApplicationContext());
\r
145 } catch (Exception e) {
\r
147 Debug.t(TrainDelayedServiceImpl.this,
\r
149 Debug.d(this, null, e);
\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
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
167 if (action.compareTo(START_ACTION) == 0) {
\r
168 pref.put(PREF_SVS_STATUS, 2);
\r
171 Intent intent = new Intent(DelayReceiver.START_SERVICE);
\r
172 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
174 Debug.t(TrainDelayedServiceImpl.this, START_ACTION);
\r
177 //
\83T
\81[
\83r
\83X
\8fI
\97¹
\r
178 TrainDelayedServiceImpl.this.stopSelf();
\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
187 pref.put(PREF_SVS_STATUS, 0);
\r
190 Intent intent = new Intent(DelayReceiver.STOP_SERVICE);
\r
191 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
198 public void onStart(Intent intent, int startId) {
\r
199 super.onStart(intent, startId);
\r
201 Debug.d(this, Debug.ON_START);
\r
203 action = intent.getAction();
\r
204 action = (action == null) ? "" : action;
\r
206 Thread thread = new Thread(null, task,
\r
207 TrainDelayedServiceImpl.class.getSimpleName());
\r
212 public void onDestroy() {
\r
213 Debug.d(this, Debug.ON_DESTROY);
\r
219 public void onRebind(Intent intent) {
\r
220 super.onRebind(intent);
\r
222 Debug.d(this, Debug.ON_REBIND);
\r
226 public boolean onUnbind(Intent intent) {
\r
227 Debug.d(this, Debug.ON_UNBIND);
\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
234 public List<Keyword> getKeywords() {
\r
235 if (scheduler == null) {
\r
238 return scheduler.getKeywords();
\r