1 package com.yuji.ef.service;
3 import java.util.Calendar;
5 import android.app.AlarmManager;
6 import android.app.PendingIntent;
7 import android.app.Service;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.os.Binder;
11 import android.os.IBinder;
12 import android.widget.Toast;
14 import com.evernote.client.oauth.android.EvernoteSession;
15 import com.yuji.ef.Initialize;
17 import com.yuji.ef.SettingActivity;
18 import com.yuji.ef.common.Constant;
19 import com.yuji.ef.dao.LockDao;
20 import com.yuji.ef.dao.NodeCacheDao;
21 import com.yuji.ef.dao.NodeDao;
22 import com.yuji.ef.exception.EfError;
23 import com.yuji.ef.exception.EfException;
24 import com.yuji.ef.exception.RollbackException;
25 import com.yuji.ef.pref.EditPrefUtil;
26 import com.yuji.ef.task.NoteUpdateTask;
27 import com.yuji.ef.utility.DateUtil;
28 import com.yuji.ef.utility.Debug;
29 import com.yuji.ef.utility.EvernoteUtil;
30 import com.yuji.ef.utility.NetworkUtil;
31 import com.yuji.ef.utility.OAuthUtil;
33 public class NoteUpdatorService extends Service {
34 private static final boolean DEBUG = false;
36 public class KitchenTimerBinder extends Binder {
37 public NoteUpdatorService getService() {
38 return NoteUpdatorService.this;
42 public static final String ACTION = "NoteUpdatorService";
43 public static final String ACTION_START = "START";
44 private EditPrefUtil pref = new EditPrefUtil(this);
45 private KitchenTimerBinder binder = new KitchenTimerBinder();
46 private String action = null;
49 public void onCreate() {
53 Toast toast = Toast.makeText(getApplicationContext(), "onCreate()",
59 private Runnable task = new Runnable() {
62 // cancelAlermTime(NoteUpdatorService.this);
64 if (action.equals(ACTION_START)) {
68 synchronized (binder) {
69 LockDao dao = (LockDao) LockDao.getInstance();
72 .isConnected(NoteUpdatorService.this)) {
73 updateDate(NoteUpdatorService.this
74 .getString(R.string.serviceUpdateDisableNetworkMsg));
77 OAuthUtil authUtil = OAuthUtil.getInstance();
78 EvernoteSession session = authUtil
79 .setupSession(NoteUpdatorService.this);
80 EvernoteUtil util = EvernoteUtil.getInstance();
81 util.setSession(session);
82 if (!util.isLoggedIn()) {
83 updateDate(NoteUpdatorService.this
84 .getString(R.string.serviceUpdateNotLoggedkMsg));
87 boolean b = dao.lock(NoteUpdatorService.this,
88 Constant.LOCK_UPDATE_NOTE);
92 String info = dao.getLockInfo();
94 updateDate(NoteUpdatorService.this
95 .getString(R.string.serviceUpdateReferenceDataMsg));
97 } catch (RollbackException e) {
98 if (e.getError() == EfError.NETWORK) {
99 updateDate(NoteUpdatorService.this
100 .getString(R.string.serviceUpdateDisableNetworkMsg));
102 updateDate(NoteUpdatorService.this
103 .getString(R.string.serviceUpdateUnexpectedErrorkMsg));
105 } catch (Exception e) {
106 Debug.d(this, null, e);
107 updateDate(NoteUpdatorService.this
108 .getString(R.string.serviceUpdateUnexpectedErrorkMsg));
110 // ロック未取得でも解除する(ゴミ掃除)
111 dao.unlock(NoteUpdatorService.this,
112 Constant.LOCK_UPDATE_NOTE);
114 Intent intent = new Intent(Constant.ACTION_UPDATE);
115 sendBroadcast(intent);
120 setNextTime(NoteUpdatorService.this);
121 NoteUpdatorService.this.stopSelf();
125 private void execute() throws EfException {
126 pref.put(Constant.PREF_UPDATE_DATA, Constant.ON);
129 Initialize.initialize(NoteUpdatorService.this, null);
131 NoteUpdateTask task = new NoteUpdateTask(null, true, true, true,
140 private void updateDate(String msg) {
141 Calendar cal = Calendar.getInstance();
142 String date = DateUtil.toDateString(this, cal.getTimeInMillis());
145 date = date + " (" + msg + ")";
148 pref.put(Constant.PREF_UPDATE_DATETIME, date);
153 public void onStart(Intent intent, int startId) {
154 super.onStart(intent, startId);
156 action = intent.getAction();
157 action = (action == null) ? "" : action;
160 Toast toast = Toast.makeText(getApplicationContext(), "onStart() "
161 + action + " " + startId, Toast.LENGTH_SHORT);
165 Thread thread = new Thread(null, task, "AlarmService_Service");
170 public void onDestroy() {
174 Toast toast = Toast.makeText(getApplicationContext(),
175 "onDestroy()", Toast.LENGTH_SHORT);
181 public IBinder onBind(Intent intent) {
183 Toast toast = Toast.makeText(getApplicationContext(), "onBind()",
192 public void onRebind(Intent intent) {
194 Toast toast = Toast.makeText(getApplicationContext(), "onRebind()",
201 public boolean onUnbind(Intent intent) {
203 Toast toast = Toast.makeText(getApplicationContext(), "onUnbind()",
210 public static void setNextTime(Context context) {
211 EditPrefUtil pref = new EditPrefUtil(context);
213 long updateTime = pref.getLong(Constant.PREF_UPDATE_TIME);
214 if (updateTime <= 0) {
218 Calendar cal = Calendar.getInstance();
219 // cal.add(Calendar.HOUR, (int) updateTime);
220 cal.add(Calendar.SECOND, (int) 15); // TODO
222 long t = cal.getTimeInMillis();
223 PendingIntent alarmSender = PendingIntent.getService(context, 0,
224 new Intent(context, NoteUpdatorService.class), 0);
225 AlarmManager am = (AlarmManager) context
226 .getSystemService(Context.ALARM_SERVICE);
227 am.set(AlarmManager.RTC, t, alarmSender);
229 pref.put(Constant.PREF_NEXT_TIME, t);
233 private static void cancelAlermTime(Context context) {
234 Calendar cal = Calendar.getInstance();
236 long t = cal.getTimeInMillis();
237 PendingIntent alarmSender = PendingIntent.getService(context, 0,
238 new Intent(context, NoteUpdatorService.class), 0);
239 AlarmManager am = (AlarmManager) context
240 .getSystemService(Context.ALARM_SERVICE);
241 am.cancel(alarmSender);
244 public static void stopService(Context context) {
245 PendingIntent alarmSender = PendingIntent.getService(context, 0,
246 new Intent(context, NoteUpdatorService.class),
247 PendingIntent.FLAG_CANCEL_CURRENT);
250 public static boolean isSetAlarmSender(Context context) {
251 PendingIntent alarmSender = PendingIntent.getService(context, 0,
252 new Intent(context, NoteUpdatorService.class),
253 PendingIntent.FLAG_NO_CREATE);
254 return alarmSender != null;
257 public static void init(Context context) {
258 EditPrefUtil pref = new EditPrefUtil(context);
260 boolean isSet = NoteUpdatorService.isSetAlarmSender(context);
261 int updateTime = pref.getInt(Constant.PREF_UPDATE_TIME);
263 // サービス起動中でなく、ロックが残っている場合、削除(DB排他で、ロックされないこと)
265 long t = pref.getLong(Constant.PREF_NEXT_TIME);
266 Calendar cal = Calendar.getInstance();
267 long now = cal.getTimeInMillis();
271 // サービス起動中の場合は、DBロックがかかり
272 // 下記の処理でLockDaoがLOCK_UPDATE_NOTEを削除させないようにするため
273 // ダミーでisEmpty()を読んでいる
274 NodeDao nodeDao = (NodeDao) NodeCacheDao.getInstance();
277 // サービスが起動していない & ロックが残ってしまったので、強制的に削除
278 LockDao lockDao = (LockDao) LockDao.getInstance();
279 lockDao.unlock(NoteUpdatorService.class, Constant.LOCK_UPDATE_NOTE);
280 // サービスでのロック解除のため、下記は削除しない
281 // lockDao.unlock(NoteUpdateTask.LOCK_OBJ, Constant.LOCK_UPDATE_NOTE);
282 //lockDao.unlock(SettingActivity.class, Constant.LOCK_UPDATE_NOTE);
286 if (updateTime > 0) {
289 setNextTime(context);
294 // isSetはクリアされないが、問題ないので毎回よぶ
295 stopService(context);