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.web; |
24 | |
25 | import java.net.UnknownHostException; |
26 | import java.util.ArrayList; |
27 | |
28 | import org.json.JSONArray; |
29 | import org.json.JSONException; |
30 | import org.json.JSONObject; |
31 | import org.tomdroid.Note; |
32 | import org.tomdroid.sync.ServiceAuth; |
33 | import org.tomdroid.sync.SyncService; |
34 | import org.tomdroid.ui.Tomdroid; |
35 | import org.tomdroid.util.ErrorList; |
36 | import org.tomdroid.util.Preferences; |
37 | |
38 | import android.app.Activity; |
39 | import android.net.Uri; |
40 | import android.os.Handler; |
41 | import android.os.Message; |
42 | import android.util.Log; |
43 | |
44 | public class SnowySyncService extends SyncService implements ServiceAuth { |
45 | |
46 | private static final String TAG = "SnowySyncService"; |
47 | |
48 | public SnowySyncService(Activity activity, Handler handler) { |
49 | super(activity, handler); |
50 | } |
51 | |
52 | @Override |
53 | public String getDescription() { |
54 | return "Tomboy Web"; |
55 | } |
56 | |
57 | @Override |
58 | public String getName() { |
59 | return "tomboy-web"; |
60 | } |
61 | |
62 | public boolean isConfigured() { |
63 | OAuthConnection auth = getAuthConnection(); |
64 | return auth.isAuthenticated(); |
65 | } |
66 | |
67 | @Override |
68 | public boolean needsServer() { |
69 | return true; |
70 | } |
71 | |
72 | @Override |
73 | public boolean needsAuth() { |
74 | return true; |
75 | } |
76 | |
77 | public void getAuthUri(final String server, final Handler handler) { |
78 | |
79 | execInThread(new Runnable() { |
80 | |
81 | public void run() { |
82 | |
83 | // Reset the authentication credentials |
84 | OAuthConnection auth = new OAuthConnection(); |
85 | Uri authUri = null; |
86 | |
87 | try { |
88 | authUri = auth.getAuthorizationUrl(server); |
89 | |
90 | } catch (UnknownHostException e) { |
91 | Log.e(TAG, "Internet connection not available"); |
92 | sendMessage(NO_INTERNET); |
93 | } |
94 | |
95 | Message message = new Message(); |
96 | message.obj = authUri; |
97 | handler.sendMessage(message); |
98 | } |
99 | |
100 | }); |
101 | } |
102 | |
103 | public void remoteAuthComplete(final Uri uri, final Handler handler) { |
104 | |
105 | execInThread(new Runnable() { |
106 | |
107 | public void run() { |
108 | |
109 | try { |
110 | // TODO: might be intelligent to show something like a progress dialog |
111 | // else the user might try to sync before the authorization process |
112 | // is complete |
113 | OAuthConnection auth = getAuthConnection(); |
114 | boolean result = auth.getAccess(uri.getQueryParameter("oauth_verifier")); |
115 | |
116 | if (result) { |
117 | if (Tomdroid.LOGGING_ENABLED) Log.i(TAG, "The authorization process is complete."); |
118 | } else { |
119 | Log.e(TAG, "Something went wrong during the authorization process."); |
120 | } |
121 | } catch (UnknownHostException e) { |
122 | Log.e(TAG, "Internet connection not available"); |
123 | sendMessage(NO_INTERNET); |
124 | } |
125 | |
126 | // We don't care what we send, just remove the dialog |
127 | handler.sendEmptyMessage(0); |
128 | } |
129 | }); |
130 | } |
131 | |
132 | @Override |
133 | public boolean isSyncable(){ |
134 | return super.isSyncable() && isConfigured(); |
135 | } |
136 | |
137 | |
138 | @Override |
139 | protected void sync() { |
140 | |
141 | // start loading snowy notes |
142 | setSyncProgress(0); |
143 | if (Tomdroid.LOGGING_ENABLED) Log.v(TAG, "Loading Snowy notes"); |
144 | |
145 | final String userRef = Preferences.getString(Preferences.Key.SYNC_SERVER_USER_API); |
146 | |
147 | syncInThread(new Runnable() { |
148 | |
149 | public void run() { |
150 | |
151 | OAuthConnection auth = getAuthConnection(); |
152 | |
153 | try { |
154 | String rawResponse = auth.get(userRef); |
155 | setSyncProgress(30); |
156 | |
157 | try { |
158 | JSONObject response = new JSONObject(rawResponse); |
159 | String notesUrl = response.getJSONObject("notes-ref").getString("api-ref"); |
160 | |
161 | response = new JSONObject(auth.get(notesUrl)); |
162 | |
163 | long latestSyncRevision = (Long)Preferences.getLong(Preferences.Key.LATEST_SYNC_REVISION); |
164 | setSyncProgress(35); |
165 | |
166 | if (response.getLong("latest-sync-revision") < latestSyncRevision) { |
167 | setSyncProgress(100); |
168 | return; |
169 | } |
170 | |
171 | rawResponse = auth.get(notesUrl + "?include_notes=true"); |
172 | |
173 | response = new JSONObject(rawResponse); |
174 | JSONArray notes = response.getJSONArray("notes"); |
175 | setSyncProgress(60); |
176 | |
177 | // Delete the notes that are not in the database |
178 | ArrayList<String> remoteGuids = new ArrayList<String>(); |
179 | |
180 | for (int i = 0; i < notes.length(); i++) { |
181 | remoteGuids.add(notes.getJSONObject(i).getString("guid")); |
182 | } |
183 | |
184 | deleteNotes(remoteGuids); |
185 | setSyncProgress(70); |
186 | |
187 | // Insert or update the rest of the notes |
188 | for (int i = 0; i < notes.length() - 1; i++) { |
189 | |
190 | JSONObject jsonNote = null; |
191 | |
192 | try { |
193 | jsonNote = notes.getJSONObject(i); |
194 | insertNote(new Note(jsonNote)); |
195 | } catch (JSONException e) { |
196 | Log.e(TAG, "Problem parsing the server response", e); |
197 | String json = (jsonNote != null) ? jsonNote.toString(2) : rawResponse; |
198 | sendMessage(PARSING_FAILED, ErrorList.createErrorWithContents("JSON parsing", "json", e, json)); |
199 | } |
200 | } |
201 | setSyncProgress(90); |
202 | |
203 | JSONObject jsonNote = notes.getJSONObject(notes.length() - 1); |
204 | insertNote(new Note(jsonNote)); |
205 | |
206 | Preferences.putLong(Preferences.Key.LATEST_SYNC_REVISION, response |
207 | .getLong("latest-sync-revision")); |
208 | |
209 | } catch (JSONException e) { |
210 | Log.e(TAG, "Problem parsing the server response", e); |
211 | sendMessage(PARSING_FAILED, ErrorList.createErrorWithContents("JSON parsing", "json", e, rawResponse)); |
212 | setSyncProgress(100); |
213 | return; |
214 | } |
215 | |
216 | setSyncProgress(100); |
217 | |
218 | } catch (java.net.UnknownHostException e) { |
219 | Log.e(TAG, "Internet connection not available"); |
220 | sendMessage(NO_INTERNET); |
221 | setSyncProgress(100); |
222 | return; |
223 | } |
224 | |
225 | sendMessage(PARSING_COMPLETE); |
226 | } |
227 | }); |
228 | } |
229 | |
230 | private OAuthConnection getAuthConnection() { |
231 | |
232 | OAuthConnection auth = new OAuthConnection(); |
233 | |
234 | auth.accessToken = Preferences.getString(Preferences.Key.ACCESS_TOKEN); |
235 | auth.accessTokenSecret = Preferences.getString(Preferences.Key.ACCESS_TOKEN_SECRET); |
236 | auth.requestToken = Preferences.getString(Preferences.Key.REQUEST_TOKEN); |
237 | auth.requestTokenSecret = Preferences.getString(Preferences.Key.REQUEST_TOKEN_SECRET); |
238 | auth.oauth10a = Preferences.getBoolean(Preferences.Key.OAUTH_10A); |
239 | auth.authorizeUrl = Preferences.getString(Preferences.Key.AUTHORIZE_URL); |
240 | auth.accessTokenUrl = Preferences.getString(Preferences.Key.ACCESS_TOKEN_URL); |
241 | auth.requestTokenUrl = Preferences.getString(Preferences.Key.REQUEST_TOKEN_URL); |
242 | auth.rootApi = Preferences.getString(Preferences.Key.SYNC_SERVER_ROOT_API); |
243 | auth.userApi = Preferences.getString(Preferences.Key.SYNC_SERVER_USER_API); |
244 | |
245 | return auth; |
246 | } |
247 | } |