| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 7 | * |
| 8 | * This file is part of Tomdroid. |
| 9 | * |
| 10 | * Tomdroid is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * Tomdroid is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | package org.tomdroid.sync; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | import org.tomdroid.sync.sd.SdCardSyncService; |
| 28 | import org.tomdroid.sync.web.SnowySyncService; |
| 29 | import org.tomdroid.util.Preferences; |
| 30 | |
| 31 | import android.app.Activity; |
| 32 | import android.os.Handler; |
| 33 | |
| 34 | public class SyncManager { |
| 35 | |
| 36 | private static final String TAG = "SyncManager"; |
| 37 | |
| 38 | private ArrayList<SyncService> services = new ArrayList<SyncService>(); |
| 39 | |
| 40 | public SyncManager() { |
| 41 | createServices(); |
| 42 | } |
| 43 | |
| 44 | public ArrayList<SyncService> getServices() { |
| 45 | return services; |
| 46 | } |
| 47 | |
| 48 | public SyncService getService(String name) { |
| 49 | |
| 50 | for (int i = 0; i < services.size(); i++) { |
| 51 | SyncService service = services.get(i); |
| 52 | if (name.equals(service.getName())) |
| 53 | return service; |
| 54 | } |
| 55 | |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | public void startSynchronization() { |
| 60 | |
| 61 | SyncService service = getCurrentService(); |
| 62 | service.startSynchronization(); |
| 63 | } |
| 64 | |
| 65 | public SyncService getCurrentService() { |
| 66 | String serviceName = Preferences.getString(Preferences.Key.SYNC_SERVICE); |
| 67 | return getService(serviceName); |
| 68 | } |
| 69 | |
| 70 | private static SyncManager instance = null; |
| 71 | private static Activity activity; |
| 72 | private static Handler handler; |
| 73 | |
| 74 | public static SyncManager getInstance() { |
| 75 | |
| 76 | if (instance == null) |
| 77 | instance = new SyncManager(); |
| 78 | |
| 79 | return instance; |
| 80 | } |
| 81 | |
| 82 | public static void setActivity(Activity a) { |
| 83 | activity = a; |
| 84 | getInstance().createServices(); |
| 85 | } |
| 86 | |
| 87 | public static void setHandler(Handler h) { |
| 88 | handler = h; |
| 89 | getInstance().createServices(); |
| 90 | } |
| 91 | |
| 92 | private void createServices() { |
| 93 | services.clear(); |
| 94 | |
| 95 | services.add(new SnowySyncService(activity, handler)); |
| 96 | services.add(new SdCardSyncService(activity, handler)); |
| 97 | } |
| 98 | } |