\r
import com.td.R;\r
import com.td.TrainDelayedActivity;\r
-import com.td.TrainTwitterAdapter;\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 PREF_SVS_STATUS = "SVS_STATUS";\r
public static final String PREF_SVS_DBFLG = "SVS_DBFLG";\r
- public static final String ACTION = TrainDelayedServiceImpl.class\r
- .getSimpleName();\r
- \r
+ public static final String START_ACTION = "com.td.START";\r
+\r
private final IBinder binder = new Binder();\r
private static Scheduler scheduler = null;\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 int id = 0;\r
+ private static long id = 0;\r
\r
@Override\r
public IBinder onBind(Intent intent) {\r
try {\r
Debug.d(this, "run() " + id);\r
\r
- if (scheduler == null){\r
+ if (scheduler == null) {\r
return;\r
}\r
List<Train> list = scheduler.getTrains();\r
- if (list == null){\r
+ if (list == null) {\r
return;\r
}\r
\r
twitter.search(query);\r
}\r
// TODO\r
- //notificate("id = " + id);\r
- \r
+ // notificate("id = " + id);\r
+\r
id = (id + 1) % 1000000;\r
} catch (Exception e) {\r
Debug.d(this, null, e);\r
\r
private Runnable task = new Runnable() {\r
public void run() {\r
+ boolean flag = false;\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
+ 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
- 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.d(this, null, e);\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
- long next = scheduler.calcSchedule(now);\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,\r
- alarmSender);\r
-\r
- // \83T\81[\83r\83X\8fI\97¹\r
- TrainDelayedServiceImpl.this.stopSelf();\r
- \r
- id = (id + 1) % 1000000;\r
- } catch (Exception e) {\r
- Debug.d(this, null, e);\r
}\r
}\r
-\r
};\r
\r
@Override\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
public List<Keyword> getKeywords() {\r
- if (scheduler == null){\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