-
Notifications
You must be signed in to change notification settings - Fork 497
/
BackupTask.java
307 lines (269 loc) · 12.2 KB
/
BackupTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.zegoggles.smssync.service;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import android.util.Log;
import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException;
import com.squareup.otto.Subscribe;
import com.zegoggles.smssync.App;
import com.zegoggles.smssync.R;
import com.zegoggles.smssync.auth.OAuth2Client;
import com.zegoggles.smssync.auth.TokenRefreshException;
import com.zegoggles.smssync.auth.TokenRefresher;
import com.zegoggles.smssync.calendar.CalendarAccessor;
import com.zegoggles.smssync.contacts.ContactAccessor;
import com.zegoggles.smssync.contacts.ContactGroupIds;
import com.zegoggles.smssync.mail.BackupImapStore;
import com.zegoggles.smssync.mail.CallFormatter;
import com.zegoggles.smssync.mail.ConversionResult;
import com.zegoggles.smssync.mail.DataType;
import com.zegoggles.smssync.mail.MessageConverter;
import com.zegoggles.smssync.mail.PersonLookup;
import com.zegoggles.smssync.preferences.AuthPreferences;
import com.zegoggles.smssync.preferences.Preferences;
import com.zegoggles.smssync.service.state.BackupState;
import com.zegoggles.smssync.service.state.SmsSyncState;
import java.util.List;
import java.util.Locale;
import static com.zegoggles.smssync.App.LOCAL_LOGV;
import static com.zegoggles.smssync.App.TAG;
import static com.zegoggles.smssync.mail.DataType.CALLLOG;
import static com.zegoggles.smssync.mail.DataType.Defaults.MAX_SYNCED_DATE;
import static com.zegoggles.smssync.mail.DataType.MMS;
import static com.zegoggles.smssync.mail.DataType.SMS;
import static com.zegoggles.smssync.service.BackupType.MANUAL;
import static com.zegoggles.smssync.service.BackupType.SKIP;
import static com.zegoggles.smssync.service.state.SmsSyncState.BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.CALC;
import static com.zegoggles.smssync.service.state.SmsSyncState.CANCELED_BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.ERROR;
import static com.zegoggles.smssync.service.state.SmsSyncState.FINISHED_BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.LOGIN;
class BackupTask extends AsyncTask<BackupConfig, BackupState, BackupState> {
@SuppressLint("StaticFieldLeak")
private final SmsBackupService service;
private final BackupItemsFetcher fetcher;
private final MessageConverter converter;
private final CalendarSyncer calendarSyncer;
private final AuthPreferences authPreferences;
private final Preferences preferences;
private final ContactAccessor contactAccessor;
private final TokenRefresher tokenRefresher;
BackupTask(@NonNull SmsBackupService service) {
final Context context = service.getApplicationContext();
this.service = service;
this.authPreferences = service.getAuthPreferences();
this.preferences = service.getPreferences();
this.fetcher = new BackupItemsFetcher(
context.getContentResolver(),
new BackupQueryBuilder(preferences.getDataTypePreferences()));
PersonLookup personLookup = new PersonLookup(service.getContentResolver());
this.contactAccessor = new ContactAccessor();
this.converter = new MessageConverter(context, service.getPreferences(), authPreferences.getUserEmail(), personLookup, contactAccessor);
if (preferences.isCallLogCalendarSyncEnabled()) {
calendarSyncer = new CalendarSyncer(
CalendarAccessor.Get.instance(service.getContentResolver()),
preferences.getCallLogCalendarId(),
personLookup,
new CallFormatter(context.getResources())
);
} else {
calendarSyncer = null;
}
this.tokenRefresher = new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences);
}
BackupTask(SmsBackupService service,
BackupItemsFetcher fetcher,
MessageConverter messageConverter,
CalendarSyncer syncer,
AuthPreferences authPreferences,
Preferences preferences,
ContactAccessor accessor,
TokenRefresher refresher) {
this.service = service;
this.fetcher = fetcher;
this.converter = messageConverter;
this.calendarSyncer = syncer;
this.authPreferences = authPreferences;
this.preferences = preferences;
this.contactAccessor = accessor;
this.tokenRefresher = refresher;
}
@Override
protected void onPreExecute() {
App.register(this);
}
@Subscribe public void canceled(CancelEvent cancelEvent) {
if (LOCAL_LOGV) {
Log.v(TAG, "canceled("+cancelEvent+")");
}
cancel(cancelEvent.mayInterruptIfRunning());
}
@Override protected BackupState doInBackground(BackupConfig... params) {
if (params == null || params.length == 0) {
throw new IllegalArgumentException("No config passed");
}
final BackupConfig config = params[0];
if (config.backupType == SKIP) {
return skip(config.typesToBackup);
} else {
return acquireLocksAndBackup(config);
}
}
private BackupState acquireLocksAndBackup(BackupConfig config) {
try {
service.acquireLocks();
return fetchAndBackupItems(config);
} finally {
service.releaseLocks();
}
}
private BackupState fetchAndBackupItems(BackupConfig config) {
BackupCursors cursors = null;
try {
final ContactGroupIds groupIds = contactAccessor.getGroupContactIds(service.getContentResolver(), config.groupToBackup);
cursors = new BulkFetcher(fetcher).fetch(config.typesToBackup, groupIds, config.maxItemsPerSync);
final int itemsToSync = cursors.count();
if (itemsToSync > 0) {
appLog(R.string.app_log_backup_messages, cursors.count(SMS), cursors.count(MMS), cursors.count(CALLLOG));
if (config.debug) {
appLog(R.string.app_log_backup_messages_with_config, config);
}
return backupCursors(cursors, config.imapStore, config.backupType, itemsToSync);
} else {
appLog(R.string.app_log_skip_backup_no_items);
if (preferences.isFirstBackup()) {
// If this is the first backup we need to write something to MAX_SYNCED_DATE
// such that we know that we've performed a backup before.
preferences.getDataTypePreferences().setMaxSyncedDate(SMS, MAX_SYNCED_DATE);
preferences.getDataTypePreferences().setMaxSyncedDate(MMS, MAX_SYNCED_DATE);
}
Log.i(TAG, "Nothing to do.");
return transition(FINISHED_BACKUP, null);
}
} catch (XOAuth2AuthenticationFailedException e) {
return handleAuthError(config, e);
} catch (AuthenticationFailedException e) {
return transition(ERROR, e);
} catch (MessagingException e) {
return transition(ERROR, e);
} catch (SecurityException e) {
return transition(ERROR, e);
} finally {
if (cursors != null) {
cursors.close();
}
}
}
private BackupState handleAuthError(BackupConfig config, XOAuth2AuthenticationFailedException e) {
if (e.getStatus() == 400) {
appLogDebug("need to perform xoauth2 token refresh");
if (config.currentTry < 1) {
try {
tokenRefresher.refreshOAuth2Token();
// we got a new token, let's handleAuthError one more time - we need to pass in a new store object
// since the auth params on it are immutable
appLogDebug("token refreshed, retrying");
return fetchAndBackupItems(config.retryWithStore(service.getBackupImapStore()));
} catch (MessagingException ignored) {
Log.w(TAG, ignored);
} catch (TokenRefreshException refreshException) {
appLogDebug("error refreshing token: "+refreshException+", cause="+refreshException.getCause());
}
} else {
appLogDebug("no new token obtained, giving up");
}
} else {
appLogDebug("unexpected xoauth status code " + e.getStatus());
}
return transition(ERROR, e);
}
private BackupState skip(Iterable<DataType> types) {
appLog(R.string.app_log_skip_backup_skip_messages);
for (DataType type : types) {
try {
preferences.getDataTypePreferences().setMaxSyncedDate(type, fetcher.getMostRecentTimestamp(type));
} catch (SecurityException e ) {
return new BackupState(ERROR, 0, 0, MANUAL, type, e);
}
}
Log.i(TAG, "All messages skipped.");
return new BackupState(FINISHED_BACKUP, 0, 0, MANUAL, null, null);
}
private void appLog(int id, Object... args) {
service.appLog(id, args);
}
private void appLogDebug(String message, Object... args) {
service.appLogDebug(message, args);
}
private BackupState transition(SmsSyncState smsSyncState, Exception exception) {
return service.transition(smsSyncState, exception);
}
@Override
protected void onProgressUpdate(BackupState... progress) {
if (progress != null && progress.length > 0 && !isCancelled()) {
post(progress[0]);
}
}
@Override
protected void onPostExecute(BackupState result) {
if (result != null) {
post(result);
}
App.unregister(this);
}
@Override
protected void onCancelled() {
post(transition(CANCELED_BACKUP, null));
App.unregister(this);
}
private void post(BackupState state) {
if (state == null) return;
App.post(state);
}
private BackupState backupCursors(BackupCursors cursors, BackupImapStore store, BackupType backupType, int itemsToSync)
throws MessagingException {
Log.i(TAG, String.format(Locale.ENGLISH, "Starting backup (%d messages)", itemsToSync));
publish(LOGIN);
store.checkSettings();
try {
publish(CALC);
int backedUpItems = 0;
while (!isCancelled() && cursors.hasNext()) {
BackupCursors.CursorAndType cursor = cursors.next();
if (LOCAL_LOGV) Log.v(TAG, "backing up: " + cursor);
ConversionResult result = converter.convertMessages(cursor.cursor, cursor.type);
if (!result.isEmpty()) {
List<Message> messages = result.getMessages();
if (LOCAL_LOGV) {
Log.v(TAG, String.format(Locale.ENGLISH, "sending %d %s message(s) to server.",
messages.size(), cursor.type));
}
store.getFolder(cursor.type, preferences.getDataTypePreferences()).appendMessages(messages);
if (cursor.type == CALLLOG && calendarSyncer != null) {
calendarSyncer.syncCalendar(result);
}
preferences.getDataTypePreferences().setMaxSyncedDate(cursor.type, result.getMaxDate());
backedUpItems += messages.size();
} else {
Log.w(TAG, "no messages converted");
itemsToSync -= 1;
}
publishProgress(new BackupState(BACKUP, backedUpItems, itemsToSync, backupType, cursor.type, null));
}
return new BackupState(FINISHED_BACKUP,
backedUpItems,
itemsToSync,
backupType, null, null);
} finally {
store.closeFolders();
}
}
private void publish(SmsSyncState state) {
publishProgress(service.transition(state, null));
}
}