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.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
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
35 private final IBinder binder = new Binder();
\r
36 private static Scheduler scheduler = null;
\r
38 private EditPrefUtil pref = new EditPrefUtil(this);
\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
45 private static long id = 0;
\r
48 public IBinder onBind(Intent intent) {
\r
52 private void schedule(long delay) {
\r
54 Debug.d(this, "run() " + id);
\r
56 if (scheduler == null) {
\r
59 List<Train> list = scheduler.getTrains();
\r
64 Calendar cal = Calendar.getInstance();
\r
65 listener.setCalender(cal);
\r
69 for (Train train : list) {
\r
70 String text = train.getSearchText();
\r
72 query = new Query();
\r
73 query.setRpp(100); // TODO
\r
74 query.setQuery(text);
\r
75 twitter.search(query);
\r
78 // notificate("id = " + id);
\r
80 id = (id + 1) % 1000000;
\r
81 } catch (Exception e) {
\r
82 Debug.d(this, null, e);
\r
86 private void notificate(String msg) {
\r
88 String ns = Context.NOTIFICATION_SERVICE;
\r
89 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
\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
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
107 mNotificationManager.notify(HELLO_ID, notification);
\r
108 } catch (Exception e) {
\r
109 Debug.d(this, null, e);
\r
114 public void onCreate() {
\r
117 Debug.d(this, Debug.ON_CREATE);
\r
118 twitter.addListener(listener);
\r
121 private Runnable task = new Runnable() {
\r
122 public void run() {
\r
123 boolean flag = false;
\r
126 long now = System.currentTimeMillis();
\r
128 Debug.d(this, "id=" + id);
\r
129 synchronized (binder) {
\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
138 if (scheduler == null) {
\r
139 scheduler = new Scheduler();
\r
140 DatabaseHelper.init(getApplicationContext());
\r
144 } catch (Exception e) {
\r
146 Debug.t(TrainDelayedServiceImpl.this,
\r
148 Debug.d(this, null, e);
\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
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
166 if (action.compareTo(START_ACTION) == 0) {
\r
167 pref.put(PREF_SVS_STATUS, 2);
\r
170 Intent intent = new Intent(DelayReceiver.START_SERVICE);
\r
171 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
173 Debug.t(TrainDelayedServiceImpl.this, START_ACTION);
\r
176 //
\83T
\81[
\83r
\83X
\8fI
\97¹
\r
177 TrainDelayedServiceImpl.this.stopSelf();
\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
186 pref.put(PREF_SVS_STATUS, 0);
\r
189 Intent intent = new Intent(DelayReceiver.STOP_SERVICE);
\r
190 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
197 public void onStart(Intent intent, int startId) {
\r
198 super.onStart(intent, startId);
\r
200 Debug.d(this, Debug.ON_START);
\r
202 action = intent.getAction();
\r
203 action = (action == null) ? "" : action;
\r
205 Thread thread = new Thread(null, task,
\r
206 TrainDelayedServiceImpl.class.getSimpleName());
\r
211 public void onDestroy() {
\r
212 Debug.d(this, Debug.ON_DESTROY);
\r
218 public void onRebind(Intent intent) {
\r
219 super.onRebind(intent);
\r
221 Debug.d(this, Debug.ON_REBIND);
\r
225 public boolean onUnbind(Intent intent) {
\r
226 Debug.d(this, Debug.ON_UNBIND);
\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
233 public List<Keyword> getKeywords() {
\r
234 if (scheduler == null) {
\r
237 return scheduler.getKeywords();
\r
240 public List<Train> getTrains(){
\r
241 if (scheduler == null) {
\r
244 return scheduler.getTrains();
\r
247 public static void clearSchedule(EditPrefUtil pref){
\r
248 pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);
\r