1 package com.td.service;
\r
3 import java.util.Calendar;
\r
4 import java.util.Date;
\r
5 import java.util.List;
\r
7 import twitter4j.AsyncTwitter;
\r
8 import twitter4j.AsyncTwitterFactory;
\r
9 import twitter4j.Query;
\r
10 import android.app.AlarmManager;
\r
11 import android.app.Notification;
\r
12 import android.app.NotificationManager;
\r
13 import android.app.PendingIntent;
\r
14 import android.app.Service;
\r
15 import android.content.Context;
\r
16 import android.content.Intent;
\r
17 import android.os.Binder;
\r
18 import android.os.IBinder;
\r
21 import com.td.TrainDelayedActivity;
\r
22 import com.td.broadcast.DelayReceiver;
\r
23 import com.td.db.DatabaseHelper;
\r
24 import com.td.db.HistoryDao;
\r
25 import com.td.db.Keyword;
\r
26 import com.td.db.Train;
\r
27 import com.td.utility.ConfUtil;
\r
28 import com.td.utility.Const;
\r
29 import com.td.utility.DateUtil;
\r
30 import com.td.utility.Debug;
\r
31 import com.td.utility.EditPrefUtil;
\r
32 import com.td.utility.StatusFlag;
\r
33 import com.td.utility.StatusFlagFactory;
\r
34 import com.td.utility.Util;
\r
36 public class TrainDelayedServiceImpl extends Service implements TrainDelayedService {
\r
37 private static final int NOTIFICATE_ID = 1;
\r
39 public static final String PREF_SVS_DBFLG = "SVS_DBFLG";
\r
40 public static final String START_ACTION = "com.td.START";
\r
42 private final IBinder binder = new Binder();
\r
43 private static Scheduler scheduler = null;
\r
45 private EditPrefUtil pref = new EditPrefUtil(this);
\r
47 private StatusFlag tdSf = StatusFlagFactory.getInstance().getStatusFlag(
\r
48 this, StatusFlagFactory.Type.TRAIN_DELAY_SERVICE);
\r
50 private static AsyncTwitterFactory factory = new AsyncTwitterFactory();
\r
51 private AsyncTwitter twitter = factory.getInstance();
\r
52 private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);
\r
53 private String action = null;
\r
55 private static long id = 0;
\r
58 public IBinder onBind(Intent intent) {
\r
62 private void schedule(long delay) {
\r
64 Debug.d(this, "run() " + id);
\r
66 if (scheduler == null) {
\r
69 List<Train> list = scheduler.getTrains();
\r
74 Calendar cal = Calendar.getInstance();
\r
75 listener.setCalender(cal);
\r
79 for (Train train : list) {
\r
80 String text = train.getSearchText();
\r
82 query = new Query();
\r
83 query.setRpp(Const.TWITTER_QUERY_MAX);
\r
84 query.setQuery(text);
\r
85 twitter.search(query);
\r
87 } catch (Exception e) {
\r
88 Debug.d(this, null, e);
\r
92 public void notificate(String msg) {
\r
94 String ns = Context.NOTIFICATION_SERVICE;
\r
95 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
\r
97 int icon = R.drawable.icon;
\r
98 CharSequence tickerText = "
\93d
\8eÔ
\82ª
\92x
\82ê
\82Ä
\82é?";
\r
99 long when = System.currentTimeMillis();
\r
100 Notification notification = new Notification(icon, tickerText, when);
\r
102 Context context = getApplicationContext();
\r
103 CharSequence contentTitle = "
\93d
\8eÔ
\82ª
\92x
\82ê
\82Ä
\82é?"; // TODO
\r
104 CharSequence contentText = msg;
\r
105 Intent notificationIntent = new Intent(this,
\r
106 TrainDelayedActivity.class);
\r
107 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
\r
108 notificationIntent, 0);
\r
109 notification.setLatestEventInfo(context, contentTitle, contentText,
\r
112 mNotificationManager.notify(NOTIFICATE_ID, notification);
\r
113 } catch (Exception e) {
\r
114 Debug.d(this, null, e);
\r
119 public void onCreate() {
\r
122 Debug.d(this, Debug.ON_CREATE);
\r
123 twitter.addListener(listener);
\r
126 private Runnable task = new Runnable() {
\r
127 public void run() {
\r
128 boolean flag = false;
\r
131 long now = System.currentTimeMillis();
\r
133 Debug.d(this, "id=" + id);
\r
134 synchronized (binder) {
\r
136 int prefDBFlag = pref.getInt(PREF_SVS_DBFLG, 0);
\r
137 if (!Util.toBoolean(prefDBFlag)) {
\r
138 pref.put(PREF_SVS_DBFLG, 1);
\r
143 if (scheduler == null) {
\r
144 scheduler = new Scheduler();
\r
145 DatabaseHelper.init(getApplicationContext());
\r
149 } catch (Exception e) {
\r
150 Debug.t(TrainDelayedServiceImpl.this,
\r
152 Debug.d(TrainDelayedServiceImpl.this, null, e);
\r
156 Calendar cal = Calendar.getInstance();
\r
157 cal.setTimeInMillis(now);
\r
158 Date nowDate = cal.getTime();
\r
160 int len = ConfUtil.getLen(pref);
\r
161 long next = scheduler.calcSchedule(now, len);
\r
163 //
\8e\9f\89ñ
\8bN
\93®
\8e\9e\8d\8f\82Ì
\90Ý
\92è
\82ª
\82È
\82¢
\8fê
\8d\87\82Í
\81A
\8aÄ
\8e\8b\8aÔ
\8au
\82Å
\8bN
\93®
\r
164 next = now + len * 60 * 1000;
\r
166 String nextStr = DateUtil.formatted(next);
\r
167 Debug.t(TrainDelayedServiceImpl.this, "
\8e\9f\89ñ
\8bN
\93®
\8e\9e\8d\8f " + nextStr);
\r
168 Debug.d(this, "
\8e\9f\89ñ
\8bN
\93®
\8e\9e\8d\8f " + nextStr);
\r
170 //
\8e\9f\89ñ
\8bN
\93®
\93o
\98^
\r
171 PendingIntent alarmSender = PendingIntent.getService(
\r
172 TrainDelayedServiceImpl.this, 0, new Intent(
\r
173 TrainDelayedServiceImpl.this,
\r
174 TrainDelayedServiceImpl.class), 0);
\r
175 AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
\r
176 alarm.set(AlarmManager.RTC, next, alarmSender);
\r
179 int nowYmd = DateUtil.getYmd(nowDate);
\r
180 cal.setTimeInMillis(next);
\r
181 Date date = cal.getTime();
\r
182 int ymd = DateUtil.getYmd(date);
\r
183 if (nowYmd != ymd){
\r
184 HistoryDao dao = HistoryDao.getInstance();
\r
185 int ymd7 = DateUtil.getYmd(DateUtil.getTime(nowDate,
\r
186 -DateUtil.WEEK, 0).getTime());
\r
187 dao.deleteByYmd(TrainDelayedServiceImpl.this, ymd7);
\r
188 Debug.d(this, "
\97\9a\97ð
\8dí
\8f\9c ymd < " + ymd7);
\r
191 if (Debug.isDebug()){
\r
192 int d = DateUtil.getD(ymd);
\r
193 int hm = DateUtil.getHm(date);
\r
195 long val = id * 100 * 10000 + d * 10000 + hm;
\r
197 TrainTwitterAdapter.insert(
\r
198 TrainDelayedServiceImpl.this,
\r
199 Calendar.getInstance(),
\r
200 TrainTwitterAdapter.START_ID, -1, val,
\r
204 if (action.compareTo(START_ACTION) == 0) {
\r
206 //pref.put(PREF_SVS_STATUS, 2);
\r
210 Intent intent = new Intent(DelayReceiver.START_SERVICE);
\r
211 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
213 Debug.t(TrainDelayedServiceImpl.this, START_ACTION);
\r
216 //
\83T
\81[
\83r
\83X
\8fI
\97¹
\r
217 TrainDelayedServiceImpl.this.stopSelf();
\r
219 id = (id + 1) % 1000000;
\r
220 } catch (Exception e) {
\r
221 Debug.t(TrainDelayedServiceImpl.this, e.getMessage());
\r
222 Debug.d(this, null, e);
\r
227 //pref.put(PREF_SVS_STATUS, 0);
\r
231 Intent intent = new Intent(DelayReceiver.STOP_SERVICE);
\r
232 TrainDelayedServiceImpl.this.sendBroadcast(intent);
\r
239 public void onStart(Intent intent, int startId) {
\r
240 super.onStart(intent, startId);
\r
242 Debug.d(this, Debug.ON_START);
\r
244 action = intent.getAction();
\r
245 action = (action == null) ? "" : action;
\r
247 Thread thread = new Thread(null, task,
\r
248 TrainDelayedServiceImpl.class.getSimpleName());
\r
253 public void onDestroy() {
\r
254 Debug.d(this, Debug.ON_DESTROY);
\r
260 public void onRebind(Intent intent) {
\r
261 super.onRebind(intent);
\r
263 Debug.d(this, Debug.ON_REBIND);
\r
267 public boolean onUnbind(Intent intent) {
\r
268 Debug.d(this, Debug.ON_UNBIND);
\r
270 super.onUnbind(intent);
\r
271 //
\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
275 public List<Keyword> getKeywords() {
\r
276 if (scheduler == null) {
\r
279 return scheduler.getKeywords();
\r
282 public List<Train> getTrains(){
\r
283 if (scheduler == null) {
\r
286 return scheduler.getTrains();
\r
289 public static void clearSchedule(EditPrefUtil pref){
\r
290 pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);
\r
294 public TrainDelayedServiceImpl getContext() {
\r