EMMA Coverage Report (generated Tue Jun 26 14:54:12 CEST 2012)
[all classes][org.tomdroid.sync.web]

COVERAGE SUMMARY FOR SOURCE FILE [SnowySyncService.java]

nameclass, %method, %block, %line, %
SnowySyncService.java25%  (1/4)14%  (5/36)3%   (13/505)6%   (6/99)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SnowySyncService$10%   (0/1)0%   (0/2)0%   (0/46)0%   (0/12)
SnowySyncService$1 (SnowySyncService, String, Handler): void 0%   (0/1)0%   (0/12)0%   (0/1)
run (): void 0%   (0/1)0%   (0/34)0%   (0/11)
     
class SnowySyncService$20%   (0/1)0%   (0/2)0%   (0/46)0%   (0/11)
SnowySyncService$2 (SnowySyncService, Uri, Handler): void 0%   (0/1)0%   (0/12)0%   (0/1)
run (): void 0%   (0/1)0%   (0/34)0%   (0/10)
     
class SnowySyncService$30%   (0/1)0%   (0/2)0%   (0/229)0%   (0/49)
SnowySyncService$3 (SnowySyncService, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/220)0%   (0/48)
     
class SnowySyncService100% (1/1)17%  (5/30)7%   (13/184)20%  (6/30)
access$000 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$100 (SnowySyncService): OAuthConnection 0%   (0/1)0%   (0/3)0%   (0/1)
access$1000 (SnowySyncService, int, HashMap): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
access$1100 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1200 (SnowySyncService, Note): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1300 (SnowySyncService, int, HashMap): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
access$1400 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1500 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1600 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1700 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$1800 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$200 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$300 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$400 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$500 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$600 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$700 (SnowySyncService, ArrayList): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$800 (SnowySyncService, int): void 0%   (0/1)0%   (0/4)0%   (0/1)
access$900 (SnowySyncService, Note): void 0%   (0/1)0%   (0/4)0%   (0/1)
getAuthConnection (): OAuthConnection 0%   (0/1)0%   (0/46)0%   (0/12)
getAuthUri (String, Handler): void 0%   (0/1)0%   (0/9)0%   (0/2)
isConfigured (): boolean 0%   (0/1)0%   (0/6)0%   (0/2)
isSyncable (): boolean 0%   (0/1)0%   (0/10)0%   (0/1)
remoteAuthComplete (Uri, Handler): void 0%   (0/1)0%   (0/9)0%   (0/2)
sync (): void 0%   (0/1)0%   (0/14)0%   (0/4)
SnowySyncService (Activity, Handler): void 100% (1/1)100% (5/5)100% (2/2)
getDescription (): String 100% (1/1)100% (2/2)100% (1/1)
getName (): String 100% (1/1)100% (2/2)100% (1/1)
needsAuth (): boolean 100% (1/1)100% (2/2)100% (1/1)
needsServer (): boolean 100% (1/1)100% (2/2)100% (1/1)

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 */
23package org.tomdroid.sync.web;
24 
25import java.net.UnknownHostException;
26import java.util.ArrayList;
27 
28import org.json.JSONArray;
29import org.json.JSONException;
30import org.json.JSONObject;
31import org.tomdroid.Note;
32import org.tomdroid.sync.ServiceAuth;
33import org.tomdroid.sync.SyncService;
34import org.tomdroid.ui.Tomdroid;
35import org.tomdroid.util.ErrorList;
36import org.tomdroid.util.Preferences;
37 
38import android.app.Activity;
39import android.net.Uri;
40import android.os.Handler;
41import android.os.Message;
42import android.util.Log;
43 
44public 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}

[all classes][org.tomdroid.sync.web]
EMMA 0.0.0 (unsupported private build) (C) Vladimir Roubtsov