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.QueryResult;
\r
8 import twitter4j.Tweet;
\r
9 import twitter4j.TwitterAdapter;
\r
10 import twitter4j.TwitterException;
\r
11 import twitter4j.TwitterMethod;
\r
12 import android.content.ContentValues;
\r
13 import android.content.Context;
\r
14 import android.content.Intent;
\r
15 import android.net.Uri;
\r
17 import com.td.HistoryContentProvider;
\r
18 import com.td.broadcast.DelayReceiver;
\r
19 import com.td.db.Keyword;
\r
20 import com.td.db.Train;
\r
21 import com.td.utility.ConfUtil;
\r
22 import com.td.utility.DateUtil;
\r
23 import com.td.utility.Debug;
\r
24 import com.td.utility.Delay;
\r
25 import com.td.utility.EditPrefUtil;
\r
26 import com.td.utility.ExceptionUtil;
\r
28 public class TrainTwitterAdapter extends TwitterAdapter {
\r
29 public static final long START_ID = Long.MAX_VALUE;
\r
30 public static final long ERROR_ID = START_ID - 1;
\r
31 public static final long DELAY_ID = START_ID - 2;
\r
33 private TrainDelayedService impl;
\r
34 private EditPrefUtil pref;
\r
36 private Calendar cal;
\r
38 public TrainTwitterAdapter(TrainDelayedService impl) {
\r
40 pref = new EditPrefUtil(impl.getContext());
\r
43 public void setCalender(Calendar cal) {
\r
48 public void searched(QueryResult result) {
\r
49 super.searched(result);
\r
52 Debug.l("TrainTwitterAdapter.searched()");
\r
54 ExceptionUtil.getInstance().throwException(
\r
55 ExceptionUtil.TAG_E42);
\r
57 int len = ConfUtil.getLen(pref);
\r
59 long min = Long.MAX_VALUE;
\r
62 Date minDate = null;
\r
63 Date maxDate = null;
\r
64 Date curDate = cal.getTime();
\r
66 long cur = curDate.getTime();
\r
67 long from = cur - len * 60 * 1000;
\r
71 Debug.d(this, "
\8c\9f\8dõ
\8aJ
\8en
\8e\9e\8d\8f:" + DateUtil.formatted(cal));
\r
73 List<Tweet> tweets = result.getTweets();
\r
74 for (Tweet tweet : tweets) {
\r
75 String text = tweet.getText();
\r
76 Date at = tweet.getCreatedAt();
\r
77 long l = at.getTime();
\r
82 if (!filter(text)) {
\r
83 //
\92x
\89\84\88È
\8aO
\82Ì
\8f\91\82«
\8d\9e\82Ý
\r
84 // Debug.d(this, "NO MATCH:" + text);
\r
99 if (minDate != null && maxDate != null) {
\r
103 String query = result.getQuery();
\r
105 List<Train> trains = impl.getTrains();
\r
106 if (trains != null) {
\r
107 Train train = null;
\r
108 for (Train t : trains) {
\r
109 if (query.equals(t.getSearchText())) {
\r
111 routeId = t.getId();
\r
115 if (train == null) {
\r
119 if (minDate != null && maxDate != null) {
\r
120 Debug.d(this, minDate.toString(), "-", maxDate.toString());
\r
122 if (ExceptionUtil.getInstance().isDebug(ExceptionUtil.TAG_E90)) {
\r
123 count += 10 + (int)(Math.random() * 10);
\r
126 Debug.d(this, "routeId=" + routeId);
\r
127 Debug.d(this, "diff=" + diff);
\r
128 Debug.d(this, "count=" + count);
\r
130 checkDelay(train, cal, count, len);
\r
132 insert(routeId, len, count, 0);
\r
134 } catch (Exception e) {
\r
135 Debug.d(this, null, e);
\r
139 public boolean checkDelay(Train train, Calendar curCal, int count, int len) {
\r
140 boolean isDelay = true;
\r
142 int cntpmi = count / len; // twit
\90\94/
\95ª
\r
143 int h = ConfUtil.getHeight(pref);
\r
146 // Twit
\90\94\82ªH
\82ð
\92´
\82¦
\82Ä
\82¢
\82È
\82¢
\r
150 String data = ConfUtil.getDelay(pref);
\r
151 Delay[] delays = Delay.getInstances(data);
\r
152 Delay newDelay = null;
\r
154 Delay delay = Delay.search(delays, train.getId());
\r
155 if (delay != null) {
\r
156 Calendar dCal = DateUtil.getCalendar(delay.getYmd(), delay.getHm());
\r
157 long t = dCal.getTimeInMillis();
\r
158 int w = ConfUtil.getWidth(pref);
\r
159 long cur = curCal.getTimeInMillis();
\r
160 if (cur - t < w * 60 * 1000) {
\r
161 //
\8dÅ
\8cã
\82Ì
\92x
\89\84\82©
\82ç
\8e\9e\8aÔ
\82ª
\8co
\82Á
\82Ä
\82¢
\82È
\82¢(
\92x
\89\84\82Ì
\8e\9e\8aÔ
\82Í
\8dX
\90V
\82·
\82é)
\r
162 if (Debug.isDebug()){
\r
163 Debug.t(impl.getContext(),
\r
165 + "
\8dÅ
\8cã
\82Ì
\92x
\89\84\82©
\82ç
\8e\9e\8aÔ
\96¢
\8co
\89ß "
\r
166 + DateUtil.formattedHm(DateUtil
\r
167 .getHm(curCal.getTime()))
\r
169 + DateUtil.formattedHm(DateUtil.getHm(dCal
\r
170 .getTime())) + " " + w);
\r
174 //
\91O
\89ñ
\82Ì
\92x
\89\84\82©
\82ç
\81A
\8dÄ
\93x
\92x
\89\84\82ª
\94
\90¶
\r
175 Debug.t(impl.getContext(), train.getName() + "
\91O
\89ñ
\82Ì
\92x
\89\84\82©
\82ç
\81A
\8dÄ
\93x
\92x
\89\84\82ª
\94
\90¶");
\r
177 Date d = curCal.getTime();
\r
178 delay.setYmd(DateUtil.getYmd(d));
\r
179 delay.setHm(DateUtil.getHm(d));
\r
181 //
\90V
\8bK
\93o
\98^(
\92x
\89\84\94
\90¶)
\r
182 Date d = curCal.getTime();
\r
183 int ymd = DateUtil.getYmd(d);
\r
184 int hm = DateUtil.getHm(d);
\r
185 newDelay = new Delay(train.getId(), ymd, hm, 0, true);
\r
187 Debug.t(impl.getContext(), train.getName() + "
\90V
\8bK
\93o
\98^(
\92x
\89\84\94
\90¶)");
\r
189 TrainTwitterAdapter.insert(impl.getContext(), curCal,
\r
190 TrainTwitterAdapter.DELAY_ID, -1, (int) train.getId(), -1);
\r
193 StringBuffer sb = Delay.getData(delays);
\r
194 if (newDelay != null) {
\r
195 Delay.setData(sb, newDelay);
\r
197 ConfUtil.setDelay(pref, sb.toString());
\r
200 //
\92x
\89\84\82ð
\92Ê
\92m
\r
201 if (impl != null) {
\r
202 impl.notificate(train.getName());
\r
204 Intent intent = new Intent(DelayReceiver.DELAY);
\r
205 intent.putExtra("ID", train.getId());
\r
206 impl.getContext().sendBroadcast(intent);
\r
213 private boolean filter(String text) {
\r
214 List<Keyword> keywords = impl.getKeywords();
\r
215 if (keywords == null) {
\r
219 for (Keyword keyword : keywords) {
\r
220 if (text.indexOf(keyword.getText()) >= 0) {
\r
227 private void insert(long routeId, long diff, int count, int status) {
\r
228 insert(impl.getContext(), cal, routeId, diff, count, status);
\r
231 public static void insert(Context context, Calendar cal, long routeId,
\r
232 long diff, long count, int status) {
\r
233 Date date = cal.getTime();
\r
235 int ymd = DateUtil.getYmd(date);
\r
236 int hm = DateUtil.getHm(date);
\r
237 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
\r
239 Debug.d(context, "insert " + ymd, "/" + hm, "/" + w);
\r
241 ContentValues values = new ContentValues();
\r
242 values.put(HistoryContentProvider.Fields.ROUTE_ID, routeId);
\r
243 values.put(HistoryContentProvider.Fields.YMD, ymd);
\r
244 values.put(HistoryContentProvider.Fields.HM, hm);
\r
245 values.put(HistoryContentProvider.Fields.WEEK, w);
\r
246 values.put(HistoryContentProvider.Fields.PERIOD, diff);
\r
247 values.put(HistoryContentProvider.Fields.NUM, count);
\r
248 values.put(HistoryContentProvider.Fields.STATUS, status);
\r
250 Uri uri = context.getContentResolver().insert(
\r
251 HistoryContentProvider.CONTENT_URI, values);
\r
252 Debug.d(context, uri.toString());
\r
256 public void onException(TwitterException e, TwitterMethod method) {
\r
257 Debug.d(this, null, e);
\r
258 if (Debug.isDebug()) {
\r
259 insert(ERROR_ID, -1, -1, -1);
\r