First cut to download timezones data from a timezone service.
[acal.git] / src / com / morphoss / acal / service / aCalService.java
blob64fd4abb82cb32677a8c607a0c13d0cbfa837792
1 /*
2 * Copyright (C) 2011 Morphoss Ltd
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com.morphoss.acal.service;
21 import android.app.AlarmManager;
22 import android.app.PendingIntent;
23 import android.app.Service;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.SharedPreferences;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.os.IBinder;
29 import android.os.Process;
30 import android.os.RemoteException;
31 import android.preference.PreferenceManager;
32 import android.util.Log;
34 import com.morphoss.acal.Constants;
35 import com.morphoss.acal.R;
36 import com.morphoss.acal.database.alarmmanager.AlarmQueueManager;
37 import com.morphoss.acal.database.cachemanager.CacheManager;
38 import com.morphoss.acal.database.resourcesmanager.ResourceManager;
40 public class aCalService extends Service {
43 private ServiceRequest.Stub serviceRequest = new ServiceRequestHandler();
44 private WorkerClass worker;
45 public static final String TAG = "aCalService";
46 public static String aCalVersion = "aCal/1.0"; // Updated at start of program.
47 //public static final DatabaseEventDispatcher databaseDispatcher = new DatabaseEventDispatcher();
49 private final static long serviceStartedAt = System.currentTimeMillis();
50 private ResourceManager rm;
51 private CacheManager cm;
52 private AlarmQueueManager am;
54 private static SharedPreferences prefs = null;
56 //TODO remove this line
57 public static Context context;
59 public void onCreate() {
60 super.onCreate();
61 aCalService.context = this;
63 aCalVersion = getString(R.string.appName) + "/";
64 try {
65 aCalVersion += getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
67 catch (NameNotFoundException e) {
68 Log.e(TAG,"Can't find our good self in the PackageManager!");
69 Log.e(TAG,Log.getStackTraceString(e));
72 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
73 startService();
77 private synchronized void startService() {
79 rm = ResourceManager.getInstance(this);
80 cm = CacheManager.getInstance(this);
81 am = AlarmQueueManager.getInstance(this);
83 worker = WorkerClass.getInstance(this);
85 // Schedule immediate sync of any changes to the server
86 worker.addJobAndWake(new SyncChangesToServer());
88 // Start sync running for all active collections
89 SynchronisationJobs.startCollectionSync(worker, this, 35000L);
91 // Start periodic syncing of timezone data
92 worker.addJobAndWake(new UpdateTimezones(15000L));
96 // This is the old onStart method that will be called on the pre-2.0
97 // platform.  On 2.0 or later we override onStartCommand() so this
98 // method will not be called.
99 @Override
100 public void onStart(Intent intent, int startId) {
101 handleCommand(intent);
104 @Override
105 public int onStartCommand(Intent intent, int flags, int startId) {
106 handleCommand(intent);
107 // We want this service to continue running until it is explicitly
108 // stopped, so return sticky.
109 return Service.START_STICKY;
112 // The actual start command, regardless of whether we're running under
113 // 1.x or 2.x
114 private void handleCommand( Intent inRequest ) {
115 if ( inRequest == null ) return;
116 if ( inRequest.hasExtra("UISTARTED") ) {
117 // The UI is currently starting, so we might schedule some stuff
118 // to happen soon.
119 long uiStarted = inRequest.getLongExtra("UISTARTED", System.currentTimeMillis());
120 if ( serviceStartedAt > uiStarted ) return; // Not if everything just started!
122 // Tell the dataService to rebuild it's caches, just to be sure.
123 if ( Constants.LOG_DEBUG )
124 Log.i(TAG,"UI Started, requesting internal cache revalidation.");
126 ServiceJob job = new SynchronisationJobs(SynchronisationJobs.CACHE_RESYNC);
127 job.TIME_TO_EXECUTE = 5000L;
128 worker.addJobAndWake(job);
130 // Start sync running for all active collections
131 SynchronisationJobs.startCollectionSync(worker, this, 25000L);
137 @Override
138 public void onDestroy() {
139 super.onDestroy();
140 if (Constants.LOG_DEBUG) Log.println(Constants.LOGD,TAG, "On destroy called. Killing worker thread.");
141 //Ensure database is closed properly and worker is terminated.
142 if ( worker != null ) worker.killWorker();
143 worker = null;
144 am.close();
145 rm.close();
146 cm.close();
147 cm = null;
148 rm = null;
149 if (Constants.LOG_DEBUG) Log.println(Constants.LOGD,TAG, "Worker killed.");
154 private synchronized void scheduleServiceRestart() {
155 long restartTime = System.currentTimeMillis() + 60000;
157 Intent serviceIntent = new Intent(this, aCalService.class);
158 serviceIntent.putExtra("RESTARTED", System.currentTimeMillis());
160 PendingIntent ourFutureSelf = PendingIntent.getService(getApplicationContext(), 0, serviceIntent, 0);
161 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
162 alarmManager.set(AlarmManager.RTC_WAKEUP, restartTime, ourFutureSelf);
163 Log.e(TAG, "Scheduling aCalService restart in 60 seconds.");
164 this.stopSelf();
168 //@Override
169 public IBinder onBind(Intent arg0) {
170 return serviceRequest;
173 public void addWorkerJob(ServiceJob s) {
174 Runtime r = Runtime.getRuntime();
175 if ( ((r.totalMemory() * 100) / r.maxMemory()) > 115 ) {
176 scheduleServiceRestart();
178 else {
179 if ( worker == null ) startService();
180 this.worker.addJobAndWake(s);
184 public String getPreferenceString(String key, String defValue) {
185 if ( prefs == null )
186 prefs = PreferenceManager.getDefaultSharedPreferences(this);
187 return prefs.getString(key, defValue);
191 private class ServiceRequestHandler extends ServiceRequest.Stub {
193 @Override
194 public void discoverHomeSets() throws RemoteException {
195 ServiceJob job = new SynchronisationJobs(SynchronisationJobs.HOME_SET_DISCOVERY);
196 job.TIME_TO_EXECUTE = System.currentTimeMillis();
197 worker.addJobAndWake(job);
200 @Override
201 public void updateCollectionsFromHomeSets() throws RemoteException {
202 ServiceJob job = new SynchronisationJobs(SynchronisationJobs.HOME_SETS_UPDATE);
203 job.TIME_TO_EXECUTE = System.currentTimeMillis();
204 worker.addJobAndWake(job);
207 @Override
208 public void fullResync() throws RemoteException {
209 ServiceJob[] jobs = new ServiceJob[2];
210 jobs[0] = new SynchronisationJobs(SynchronisationJobs.HOME_SET_DISCOVERY);
211 jobs[1] = new SynchronisationJobs(SynchronisationJobs.HOME_SETS_UPDATE);
212 worker.addJobsAndWake(jobs);
213 SynchronisationJobs.startCollectionSync(worker, aCalService.this, 15000L);
216 @Override
217 public void revertDatabase() throws RemoteException {
218 worker.addJobAndWake(new DebugDatabase(DebugDatabase.REVERT));
221 public void saveDatabase() throws RemoteException {
222 worker.addJobAndWake(new DebugDatabase(DebugDatabase.SAVE));
225 @Override
226 public void homeSetDiscovery(int server) throws RemoteException {
227 HomeSetDiscovery job = new HomeSetDiscovery(server);
228 worker.addJobAndWake(job);
231 @Override
232 public void syncCollectionNow(long collectionId) throws RemoteException {
233 SyncCollectionContents job = new SyncCollectionContents(collectionId, true);
234 worker.addJobAndWake(job);
237 @Override
238 public void fullCollectionResync(long collectionId) throws RemoteException {
239 InitialCollectionSync job = new InitialCollectionSync(collectionId);
240 worker.addJobAndWake(job);