package com.td.service;\r
\r
import java.util.Calendar;\r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
+import java.util.List;\r
\r
import twitter4j.AsyncTwitter;\r
import twitter4j.AsyncTwitterFactory;\r
import twitter4j.Query;\r
-\r
+import android.app.AlarmManager;\r
+import android.app.Notification;\r
+import android.app.NotificationManager;\r
+import android.app.PendingIntent;\r
import android.app.Service;\r
+import android.content.Context;\r
import android.content.Intent;\r
import android.os.Binder;\r
import android.os.IBinder;\r
-import android.widget.Toast;\r
\r
-import com.td.TrainTwitterAdapter;\r
+import com.td.R;\r
+import com.td.TrainDelayedActivity;\r
+import com.td.broadcast.DelayReceiver;\r
+import com.td.db.DatabaseHelper;\r
+import com.td.db.Keyword;\r
+import com.td.db.Train;\r
+import com.td.utility.DateUtil;\r
import com.td.utility.Debug;\r
+import com.td.utility.EditPrefUtil;\r
+import com.td.utility.Util;\r
\r
public class TrainDelayedServiceImpl extends Service {\r
- public static final String ACTION = TrainDelayedServiceImpl.class\r
- .getSimpleName();\r
+ public static final String PREF_SVS_STATUS = "SVS_STATUS";\r
+ public static final String PREF_SVS_DBFLG = "SVS_DBFLG";\r
+ public static final String START_ACTION = "com.td.START";\r
\r
- public class TrainDelayedBinder extends Binder {\r
- public TrainDelayedServiceImpl getService() {\r
- return TrainDelayedServiceImpl.this;\r
- }\r
+ private final IBinder binder = new Binder();\r
+ private static Scheduler scheduler = null;\r
+\r
+ private EditPrefUtil pref = new EditPrefUtil(this);\r
+\r
+ private static AsyncTwitterFactory factory = new AsyncTwitterFactory();\r
+ private AsyncTwitter twitter = factory.getInstance();\r
+ private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);\r
+ private String action = null;\r
+\r
+ private static long id = 0;\r
+\r
+ @Override\r
+ public IBinder onBind(Intent intent) {\r
+ return binder;\r
}\r
\r
- // TODO\r
- private AsyncTwitterFactory factory = new AsyncTwitterFactory();\r
- private AsyncTwitter twitter = factory.getInstance();\r
- private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);\r
- \r
- private Timer timer = null;\r
- private int id = 0;\r
- \r
- // http://www.atmarkit.co.jp/fjava/javatips/078java008.html\r
- // \83N\83\89\83C\83A\83\93\83g\82©\82ç\8cÄ\82Ñ\8fo\82³\82ê\82é\83\81\83\\83b\83h\r
- public void schedule(long delay) {\r
- twitter.addListener(listener);\r
+ private void schedule(long delay) {\r
+ try {\r
+ Debug.d(this, "run() " + id);\r
\r
- if (timer != null) {\r
- timer.cancel();\r
- }\r
- timer = new Timer();\r
- TimerTask timerTask = new TimerTask() {\r
- \r
- public void run() {\r
- try {\r
-// Toast toast = Toast.makeText(getApplicationContext(), "run()", Toast.LENGTH_SHORT);\r
-// toast.show();\r
- Debug.d(this, "run() " + id);\r
- \r
- Calendar cal = Calendar.getInstance();\r
- listener.setCalender(cal);\r
- \r
- Query query;\r
- \r
- query = new Query();\r
- query.setRpp(100);\r
- query.setQuery("\8b\9e\95l\93\8c\96k");\r
- twitter.search(query);\r
-\r
- query = new Query();\r
- query.setRpp(100);\r
- query.setQuery("\93\8c\8aC\93¹");\r
- twitter.search(query);\r
-\r
- query = new Query();\r
- query.setRpp(100);\r
- query.setQuery("\8b\9e\8b}");\r
- twitter.search(query);\r
- \r
- id++;\r
- }\r
- catch (Exception e){\r
- Debug.d(this, null, e);\r
- }\r
+ if (scheduler == null) {\r
+ return;\r
}\r
- \r
- };\r
- timer.schedule(timerTask, 0, delay);\r
- }\r
- \r
- public void scheduleStop(){\r
- if (timer != null) {\r
- timer.cancel();\r
- } \r
+ List<Train> list = scheduler.getTrains();\r
+ if (list == null) {\r
+ return;\r
+ }\r
+\r
+ Calendar cal = Calendar.getInstance();\r
+ listener.setCalender(cal);\r
+\r
+ Query query;\r
+\r
+ for (Train train : list) {\r
+ String text = train.getSearchText();\r
+\r
+ query = new Query();\r
+ query.setRpp(100); // TODO\r
+ query.setQuery(text);\r
+ twitter.search(query);\r
+ }\r
+ // TODO\r
+ // notificate("id = " + id);\r
+\r
+ id = (id + 1) % 1000000;\r
+ } catch (Exception e) {\r
+ Debug.d(this, null, e);\r
+ }\r
}\r
- \r
- @Override\r
- public IBinder onBind(Intent intent) {\r
- return new TrainDelayedBinder();\r
+\r
+ private void notificate(String msg) {\r
+ try {\r
+ String ns = Context.NOTIFICATION_SERVICE;\r
+ NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);\r
+\r
+ int icon = R.drawable.icon;\r
+ CharSequence tickerText = "\93d\8eÔ\82ª\92x\82ê\82Ä\82é?";\r
+ long when = System.currentTimeMillis();\r
+ Notification notification = new Notification(icon, tickerText, when);\r
+\r
+ Context context = getApplicationContext();\r
+ CharSequence contentTitle = "My notification"; // TODO\r
+ CharSequence contentText = msg;\r
+ Intent notificationIntent = new Intent(this,\r
+ TrainDelayedActivity.class);\r
+ PendingIntent contentIntent = PendingIntent.getActivity(this, 0,\r
+ notificationIntent, 0);\r
+ notification.setLatestEventInfo(context, contentTitle, contentText,\r
+ contentIntent);\r
+\r
+ int HELLO_ID = 1;\r
+ mNotificationManager.notify(HELLO_ID, notification);\r
+ } catch (Exception e) {\r
+ Debug.d(this, null, e);\r
+ }\r
}\r
\r
@Override\r
public void onCreate() {\r
super.onCreate();\r
- \r
+\r
Debug.d(this, Debug.ON_CREATE);\r
+ twitter.addListener(listener);\r
}\r
- \r
+\r
+ private Runnable task = new Runnable() {\r
+ public void run() {\r
+ boolean flag = false;\r
+ try {\r
+ try {\r
+ long now = System.currentTimeMillis();\r
+\r
+ Debug.d(this, "id=" + id);\r
+ synchronized (binder) {\r
+ try {\r
+ int prefDBFlag = pref.getInt(PREF_SVS_DBFLG, 0);\r
+ if (!Util.toBoolean(prefDBFlag)) {\r
+ pref.put(PREF_SVS_DBFLG, 1);\r
+ pref.update();\r
+ scheduler = null;\r
+ }\r
+\r
+ if (scheduler == null) {\r
+ scheduler = new Scheduler();\r
+ DatabaseHelper.init(getApplicationContext());\r
+ }\r
+ scheduler.init();\r
+ schedule(-1);\r
+ } catch (Exception e) {\r
+ // TODO\r
+ Debug.t(TrainDelayedServiceImpl.this,\r
+ e.getMessage());\r
+ Debug.d(this, null, e);\r
+ }\r
+ }\r
+\r
+ long next = scheduler.calcSchedule(now);\r
+ String nextStr = DateUtil.formatted(next);\r
+ Debug.t(TrainDelayedServiceImpl.this, "\8e\9f\89ñ\8bN\93®\8e\9e\8d\8f " + nextStr);\r
+ Debug.d(this, "\8e\9f\89ñ\8bN\93®\8e\9e\8d\8f " + nextStr);\r
+\r
+ // \8e\9f\89ñ\8bN\93®\93o\98^\r
+ PendingIntent alarmSender = PendingIntent.getService(\r
+ TrainDelayedServiceImpl.this, 0, new Intent(\r
+ TrainDelayedServiceImpl.this,\r
+ TrainDelayedServiceImpl.class), 0);\r
+ AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\r
+ alarm.set(AlarmManager.RTC, next, alarmSender);\r
+ flag = true;\r
+\r
+ if (action.compareTo(START_ACTION) == 0) {\r
+ pref.put(PREF_SVS_STATUS, 2);\r
+ pref.update();\r
+ \r
+ Intent intent = new Intent(DelayReceiver.START_SERVICE);\r
+ TrainDelayedServiceImpl.this.sendBroadcast(intent); \r
+\r
+ Debug.t(TrainDelayedServiceImpl.this, START_ACTION);\r
+ }\r
+\r
+ // \83T\81[\83r\83X\8fI\97¹\r
+ TrainDelayedServiceImpl.this.stopSelf();\r
+\r
+ id = (id + 1) % 1000000;\r
+ } catch (Exception e) {\r
+ Debug.t(TrainDelayedServiceImpl.this, e.getMessage());\r
+ Debug.d(this, null, e);\r
+ }\r
+ } finally {\r
+ if (!flag){\r
+ pref.put(PREF_SVS_STATUS, 0);\r
+ pref.update(); \r
+\r
+ Intent intent = new Intent(DelayReceiver.STOP_SERVICE);\r
+ TrainDelayedServiceImpl.this.sendBroadcast(intent); \r
+ }\r
+ }\r
+ }\r
+ };\r
+\r
@Override\r
public void onStart(Intent intent, int startId) {\r
super.onStart(intent, startId);\r
\r
Debug.d(this, Debug.ON_START);\r
+\r
+ action = intent.getAction();\r
+ action = (action == null) ? "" : action;\r
+\r
+ Thread thread = new Thread(null, task,\r
+ TrainDelayedServiceImpl.class.getSimpleName());\r
+ thread.start();\r
}\r
- \r
+\r
@Override\r
public void onDestroy() {\r
Debug.d(this, Debug.ON_DESTROY);\r
- scheduleStop();\r
- \r
+\r
super.onDestroy();\r
}\r
\r
\r
Debug.d(this, Debug.ON_REBIND);\r
}\r
- \r
+\r
@Override\r
public boolean onUnbind(Intent intent) {\r
Debug.d(this, Debug.ON_UNBIND);\r
\r
super.onUnbind(intent);\r
// \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
- return true; \r
- } \r
+ return true;\r
+ }\r
+\r
+ public List<Keyword> getKeywords() {\r
+ if (scheduler == null) {\r
+ return null;\r
+ }\r
+ return scheduler.getKeywords();\r
+ }\r
+ \r
+ public List<Train> getTrains(){\r
+ if (scheduler == null) {\r
+ return null;\r
+ }\r
+ return scheduler.getTrains(); \r
+ }\r
+ \r
+ public static void clearSchedule(EditPrefUtil pref){\r
+ pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);\r
+ pref.update(); \r
+ }\r
}\r