OSDN Git Service

7e8b5b31082d07cc57800c09119e6917190864c6
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / service / NoteUpdatorService.java
1 package com.yuji.ef.service;
2
3 import java.util.Calendar;
4
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;
13
14 import com.evernote.client.oauth.android.EvernoteSession;
15 import com.yuji.ef.Initialize;
16 import com.yuji.ef.R;
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.exception.EfException;
21 import com.yuji.ef.pref.EditPrefUtil;
22 import com.yuji.ef.task.NoteUpdateTask;
23 import com.yuji.ef.utility.DateUtil;
24 import com.yuji.ef.utility.Debug;
25 import com.yuji.ef.utility.EvernoteUtil;
26 import com.yuji.ef.utility.NetworkUtil;
27 import com.yuji.ef.utility.OAuthUtil;
28
29 public class NoteUpdatorService extends Service {
30         private static final boolean DEBUG = false;
31
32         public class KitchenTimerBinder extends Binder {
33                 public NoteUpdatorService getService() {
34                         return NoteUpdatorService.this;
35                 }
36         }
37
38         public static final String ACTION = "NoteUpdatorService";
39         public static final String ACTION_START = "START";
40         private EditPrefUtil pref = new EditPrefUtil(this);
41         private KitchenTimerBinder binder = new KitchenTimerBinder();
42         private String action = null;
43
44         @Override
45         public void onCreate() {
46                 super.onCreate();
47
48                 if (DEBUG) {
49                         Toast toast = Toast.makeText(getApplicationContext(), "onCreate()",
50                                         Toast.LENGTH_SHORT);
51                         toast.show();
52                 }
53         }
54
55         private Runnable task = new Runnable() {
56                 public void run() {
57                         try {
58                                 //cancelAlermTime(NoteUpdatorService.this);
59                                 
60                                 if (action.equals(ACTION_START)) {
61                                         // 初期起動
62                                 } else {
63                                         // タイマー起動
64                                         synchronized (binder) {
65                                                 LockDao dao = (LockDao) LockDao.getInstance();
66                                                 try {
67                                                         if (!NetworkUtil
68                                                                         .isConnected(NoteUpdatorService.this)) {
69                                                                 updateDate(NoteUpdatorService.this
70                                                                                 .getString(R.string.serviceUpdateDisableNetworkMsg));
71                                                                 return;
72                                                         }
73                                                         OAuthUtil authUtil = OAuthUtil.getInstance();
74                                                         EvernoteSession session = authUtil
75                                                                         .setupSession(NoteUpdatorService.this);
76                                                         EvernoteUtil util = EvernoteUtil.getInstance();
77                                                         util.setSession(session);
78                                                         if (!util.isLoggedIn()) {
79                                                                 updateDate(NoteUpdatorService.this
80                                                                                 .getString(R.string.serviceUpdateNotLoggedkMsg));
81                                                                 return;
82                                                         }
83                                                         boolean b = dao.lock(NoteUpdatorService.this,
84                                                                         Constant.LOCK_UPDATE_NOTE);
85                                                         if (b) {
86                                                                 execute();
87                                                         } else {
88                                                                 updateDate(NoteUpdatorService.this
89                                                                                 .getString(R.string.serviceUpdateReferenceDataMsg));
90                                                         }
91                                                 } catch (Exception e) {
92                                                         Debug.d(this, null, e);
93                                                         updateDate(NoteUpdatorService.this
94                                                                         .getString(R.string.serviceUpdateUnexpectedErrorkMsg));
95                                                 } finally {
96                                                         // ロック未取得でも解除する(ゴミ掃除)
97                                                         dao.unlock(NoteUpdatorService.this,
98                                                                         Constant.LOCK_UPDATE_NOTE);
99
100                                                         Intent intent = new Intent(Constant.ACTION_UPDATE);
101                                                         sendBroadcast(intent);
102                                                 }
103                                         }
104                                 }
105                         } finally {
106                                 setNextTime(NoteUpdatorService.this);
107                                 NoteUpdatorService.this.stopSelf();
108                         }
109                 }
110
111                 private void execute() throws EfException {
112                         pref.put(Constant.PREF_UPDATE_DATA, Constant.ON);
113                         pref.update();
114
115                         Initialize.initialize(NoteUpdatorService.this, null);
116
117                         NoteUpdateTask task = new NoteUpdateTask(null, true, true, true);
118                         task.doExecute();
119
120                         updateDate(null);
121                 }
122
123         };
124
125         private void updateDate(String msg) {
126                 Calendar cal = Calendar.getInstance();
127                 String date = DateUtil.toDateString(this, cal.getTimeInMillis());
128
129                 if (msg != null) {
130                         date = date + " (" + msg + ")";
131                 }
132
133                 pref.put(Constant.PREF_UPDATE_DATETIME, date);
134                 pref.update();
135         }
136
137         @Override
138         public void onStart(Intent intent, int startId) {
139                 super.onStart(intent, startId);
140
141                 action = intent.getAction();
142                 action = (action == null) ? "" : action;
143
144                 if (DEBUG) {
145                         Toast toast = Toast.makeText(getApplicationContext(), "onStart() "
146                                         + action + " " + startId, Toast.LENGTH_SHORT);
147                         toast.show();
148                 }
149
150                 Thread thread = new Thread(null, task, "AlarmService_Service");
151                 thread.start();
152         }
153
154         @Override
155         public void onDestroy() {
156                 super.onDestroy();
157
158                 if (DEBUG) {
159                         Toast toast = Toast.makeText(getApplicationContext(),
160                                         "onDestroy()", Toast.LENGTH_SHORT);
161                         toast.show();
162                 }
163         }
164
165         @Override
166         public IBinder onBind(Intent intent) {
167                 if (DEBUG) {
168                         Toast toast = Toast.makeText(getApplicationContext(), "onBind()",
169                                         Toast.LENGTH_SHORT);
170                         toast.show();
171                 }
172
173                 return binder;
174         }
175
176         @Override
177         public void onRebind(Intent intent) {
178                 if (DEBUG) {
179                         Toast toast = Toast.makeText(getApplicationContext(), "onRebind()",
180                                         Toast.LENGTH_SHORT);
181                         toast.show();
182                 }
183         }
184
185         @Override
186         public boolean onUnbind(Intent intent) {
187                 if (DEBUG) {
188                         Toast toast = Toast.makeText(getApplicationContext(), "onUnbind()",
189                                         Toast.LENGTH_SHORT);
190                         toast.show();
191                 }
192                 return true;
193         }
194
195         public static void setNextTime(Context context) {
196                 EditPrefUtil pref = new EditPrefUtil(context);
197
198                 long updateTime = pref.getLong(Constant.PREF_UPDATE_TIME);
199                 if (updateTime <= 0) {
200                         return;
201                 }
202
203                 Calendar cal = Calendar.getInstance();
204                 // cal.add(Calendar.HOUR, (int) updateTime);
205                 cal.add(Calendar.SECOND, (int) 15); // TODO
206
207                 long t = cal.getTimeInMillis();
208                 PendingIntent alarmSender = PendingIntent.getService(context, 0,
209                                 new Intent(context, NoteUpdatorService.class), 0);
210                 AlarmManager am = (AlarmManager) context
211                                 .getSystemService(Context.ALARM_SERVICE);
212                 am.set(AlarmManager.RTC, t, alarmSender);
213                 
214                 pref.put(Constant.PREF_NEXT_TIME, t);
215                 pref.update();
216         }
217
218         private static void cancelAlermTime(Context context){
219                 Calendar cal = Calendar.getInstance();
220
221                 long t = cal.getTimeInMillis();
222                 PendingIntent alarmSender = PendingIntent.getService(context, 0,
223                                 new Intent(context, NoteUpdatorService.class), 0);
224                 AlarmManager am = (AlarmManager) context
225                                 .getSystemService(Context.ALARM_SERVICE);
226                 am.cancel(alarmSender);
227         }
228         
229         public static void stopService(Context context) {
230                 PendingIntent alarmSender = PendingIntent.getService(context, 0,
231                                 new Intent(context, NoteUpdatorService.class),
232                                 PendingIntent.FLAG_CANCEL_CURRENT);
233         }
234
235         public static boolean isSetAlarmSender(Context context) {
236                 PendingIntent alarmSender = PendingIntent.getService(context, 0,
237                                 new Intent(context, NoteUpdatorService.class),
238                                 PendingIntent.FLAG_NO_CREATE);
239                 return alarmSender != null;
240         }
241
242         public static void init(Context context) {
243                 EditPrefUtil pref = new EditPrefUtil(context);
244
245                 boolean isSet = NoteUpdatorService.isSetAlarmSender(context);
246                 int updateTime = pref.getInt(Constant.PREF_UPDATE_TIME);
247
248                 // TODO
249                 // サービス起動中でなく、ロックが残っている場合、削除(DB排他で、ロックされないこと)
250                 // サービスのロック削除
251                 long t = pref.getLong(Constant.PREF_NEXT_TIME);
252                 Calendar cal = Calendar.getInstance();
253                 long now = cal.getTimeInMillis();
254                 if (t < now){
255                         isSet = false;
256                         
257                         // サービス起動中の場合は、DBロックがかかり
258                         // 更新後まで、ロックは解除されない。
259                         LockDao lockDao = (LockDao) LockDao.getInstance();
260                         lockDao.unlock(NoteUpdatorService.class,
261                                         Constant.LOCK_UPDATE_NOTE);
262                         lockDao.unlock(SettingActivity.class, Constant.LOCK_UPDATE_NOTE);
263                         lockDao = null;
264                 }
265                 
266                 if (updateTime > 0) {
267                         if (!isSet) {
268                                 // 再起動時
269                                 setNextTime(context);
270                         }
271                 } else {
272                         if (isSet) {
273                                 // データ削除時
274                                 // isSetはクリアされないが、問題ないので毎回よぶ
275                                 stopService(context);
276                         }
277                 }
278         }
279 }