/
HexagonListsSync.java
331 lines (291 loc) · 12.7 KB
/
HexagonListsSync.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.battlelancer.seriesguide.sync;
import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.battlelancer.seriesguide.backend.HexagonTools;
import com.battlelancer.seriesguide.backend.settings.HexagonSettings;
import com.battlelancer.seriesguide.provider.SeriesGuideContract;
import com.battlelancer.seriesguide.util.DBUtils;
import com.battlelancer.seriesguide.ui.lists.ListsTools;
import com.google.api.client.util.DateTime;
import com.uwetrottmann.seriesguide.backend.lists.Lists;
import com.uwetrottmann.seriesguide.backend.lists.model.SgList;
import com.uwetrottmann.seriesguide.backend.lists.model.SgListIds;
import com.uwetrottmann.seriesguide.backend.lists.model.SgListItem;
import com.uwetrottmann.seriesguide.backend.lists.model.SgListList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import timber.log.Timber;
public class HexagonListsSync {
private static final int LISTS_MAX_BATCH_SIZE = 10;
private Context context;
private HexagonTools hexagonTools;
public HexagonListsSync(Context context, HexagonTools hexagonTools) {
this.context = context;
this.hexagonTools = hexagonTools;
}
public boolean download(boolean hasMergedLists) {
long currentTime = System.currentTimeMillis();
DateTime lastSyncTime = new DateTime(HexagonSettings.getLastListsSyncTime(context));
if (hasMergedLists) {
Timber.d("download: lists changed since %s.", lastSyncTime);
} else {
Timber.d("download: all lists.");
}
HashSet<String> localListIds = ListsTools.getListIds(context);
List<SgList> lists;
String cursor = null;
do {
try {
// get service each time to check if auth was removed
Lists listsService = hexagonTools.getListsService();
if (listsService == null) {
return false; // no longer signed in
}
Lists.Get request = listsService.get(); // use default server limit
if (hasMergedLists) {
request.setUpdatedSince(lastSyncTime);
}
if (!TextUtils.isEmpty(cursor)) {
request.setCursor(cursor);
}
SgListList response = request.execute();
if (response == null) {
Timber.d("download: failed, response is null.");
break;
}
cursor = response.getCursor();
lists = response.getLists();
} catch (IOException e) {
HexagonTools.trackFailedRequest(context, "get lists", e);
return false;
}
if (lists == null || lists.size() == 0) {
break; // empty response, assume we are done
}
if (!doListsDatabaseUpdate(lists, localListIds, hasMergedLists)) {
return false; // database update failed, abort
}
} while (!TextUtils.isEmpty(cursor)); // fetch next batch
// set new last sync time
if (hasMergedLists) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putLong(HexagonSettings.KEY_LAST_SYNC_LISTS, currentTime)
.apply();
}
return true;
}
private boolean doListsDatabaseUpdate(List<SgList> lists, HashSet<String> localListIds,
boolean hasMergedLists) {
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (SgList list : lists) {
// add or update the list
String listId = list.getListId();
ContentProviderOperation.Builder builder = null;
if (localListIds.contains(listId)) {
// update
if (hasMergedLists) {
// only overwrite name and order if data was already merged
// use case: user disconnected for a while, changed lists, then reconnects
builder = ContentProviderOperation
.newUpdate(SeriesGuideContract.Lists.buildListUri(listId));
}
} else {
// insert
builder = ContentProviderOperation
.newInsert(SeriesGuideContract.Lists.CONTENT_URI)
.withValue(SeriesGuideContract.Lists.LIST_ID, listId);
}
if (builder != null) {
builder.withValue(SeriesGuideContract.Lists.NAME, list.getName());
if (list.getOrder() != null) {
builder.withValue(SeriesGuideContract.Lists.ORDER, list.getOrder());
}
batch.add(builder.build());
}
// keep track of items not in the list on hexagon
HashSet<String> listItemsToRemove = null;
if (hasMergedLists) {
listItemsToRemove = ListsTools.getListItemIds(context, listId);
if (listItemsToRemove == null) {
return false; // list item query failed
}
}
// add or update items of the list
List<SgListItem> listItems = list.getListItems();
if (listItems != null) {
for (SgListItem listItem : listItems) {
String listItemId = listItem.getListItemId();
String[] brokenUpId = SeriesGuideContract.ListItems.splitListItemId(listItemId);
if (brokenUpId == null) {
continue; // could not break up list item id
}
int itemTvdbId = -1;
int itemType = -1;
try {
itemTvdbId = Integer.parseInt(brokenUpId[0]);
itemType = Integer.parseInt(brokenUpId[1]);
} catch (NumberFormatException ignored) {
}
if (itemTvdbId == -1 || !SeriesGuideContract.ListItems.isValidItemType(
itemType)) {
continue; // failed to extract item TVDB id or item type not known
}
// just insert the list item, if the id already exists it will be replaced
builder = ContentProviderOperation
.newInsert(SeriesGuideContract.ListItems.CONTENT_URI)
.withValue(SeriesGuideContract.ListItems.LIST_ITEM_ID, listItemId)
.withValue(SeriesGuideContract.ListItems.ITEM_REF_ID, itemTvdbId)
.withValue(SeriesGuideContract.ListItems.TYPE, itemType)
.withValue(SeriesGuideContract.Lists.LIST_ID, listId);
batch.add(builder.build());
if (hasMergedLists) {
// do not remove this list item
listItemsToRemove.remove(listItemId);
}
}
}
if (hasMergedLists) {
// remove items no longer in the list
for (String listItemId : listItemsToRemove) {
builder = ContentProviderOperation
.newDelete(SeriesGuideContract.ListItems.buildListItemUri(listItemId));
batch.add(builder.build());
}
}
}
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "doListsDatabaseUpdate: failed.");
return false;
}
return true;
}
public boolean pruneRemovedLists() {
Timber.d("pruneRemovedLists");
HashSet<String> localListIds = ListsTools.getListIds(context);
if (localListIds == null) {
return false; // query failed
}
if (localListIds.size() <= 1) {
return true; // one or no list, can not remove any list
}
// get list of ids of lists on hexagon
List<String> hexagonListIds = new ArrayList<>(localListIds.size());
String cursor = null;
do {
try {
// get service each time to check if auth was removed
Lists listsService = hexagonTools.getListsService();
if (listsService == null) {
return false; // no longer signed in
}
Lists.GetIds request = listsService.getIds();
if (!TextUtils.isEmpty(cursor)) {
request.setCursor(cursor);
}
SgListIds response = request.execute();
if (response == null) {
Timber.d("pruneRemovedLists: failed, response is null.");
return false;
}
List<String> listIds = response.getListIds();
if (listIds == null || listIds.size() == 0) {
break; // empty response, assume we got all ids
}
hexagonListIds.addAll(listIds);
cursor = response.getCursor();
} catch (IOException e) {
HexagonTools.trackFailedRequest(context, "get list ids", e);
return false;
}
} while (!TextUtils.isEmpty(cursor)); // fetch next batch
if (hexagonListIds.size() <= 1) {
return true; // one or no list on hexagon, can not remove any list
}
// exclude any lists that are on hexagon
for (String listId : hexagonListIds) {
localListIds.remove(listId);
}
// remove any list not on hexagon
if (localListIds.size() > 0) {
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (String listId : localListIds) {
batch.add(ContentProviderOperation
.newDelete(SeriesGuideContract.Lists.buildListUri(listId))
.build());
}
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "pruneRemovedLists: deleting lists failed.");
return false;
}
}
return true;
}
public boolean uploadAll() {
Timber.d("uploadAll");
SgListList listsWrapper = new SgListList();
List<SgList> lists = new ArrayList<>(LISTS_MAX_BATCH_SIZE);
listsWrapper.setLists(lists);
Cursor listsQuery = context.getContentResolver()
.query(SeriesGuideContract.Lists.CONTENT_URI,
ListsTools.Query.PROJECTION_LIST, null, null, null);
if (listsQuery == null) {
return false; // query failed
}
while (listsQuery.moveToNext()) {
SgList list = new SgList();
// add list properties
String listId = listsQuery.getString(ListsTools.Query.LIST_ID);
String listName = listsQuery.getString(ListsTools.Query.NAME);
if (TextUtils.isEmpty(listId)) {
continue; // skip, no list id
}
list.setListId(listId);
list.setName(listName);
int order = listsQuery.getInt(ListsTools.Query.ORDER);
if (order != 0) {
list.setOrder(order);
}
// add list items
List<SgListItem> listItems = ListsTools.getListItems(context, listId);
if (listItems != null) {
list.setListItems(listItems);
} else {
Timber.d("uploadAll: no items to upload for list %s.", listId);
}
lists.add(list);
if (lists.size() == LISTS_MAX_BATCH_SIZE || listsQuery.isLast()) {
if (doUploadSomeLists(listsWrapper)) {
lists.clear();
} else {
return false; // part upload failed, next sync will try again
}
}
}
listsQuery.close();
return true;
}
private boolean doUploadSomeLists(SgListList listsWrapper) {
Lists listsService = hexagonTools.getListsService();
if (listsService == null) {
return false; // no longer signed in
}
try {
listsService.save(listsWrapper).execute();
} catch (IOException e) {
HexagonTools.trackFailedRequest(context, "save lists", e);
return false;
}
return true;
}
}