use of com.battlelancer.seriesguide.items.SearchResult in project SeriesGuide by UweTrottmann.
the class AddShowTask method doInBackground.
@Override
protected Void doInBackground(Void... params) {
Timber.d("Starting to add shows...");
// don't even get started
if (addQueue.isEmpty()) {
Timber.d("Finished. Queue was empty.");
return null;
}
// set values required for progress update
SearchResult nextShow = addQueue.peek();
currentShowName = nextShow.title;
currentShowTvdbId = nextShow.tvdbid;
if (!AndroidUtils.isNetworkConnected(app)) {
Timber.d("Finished. No internet connection.");
publishProgress(RESULT_OFFLINE);
return null;
}
if (isCancelled()) {
Timber.d("Finished. Cancelled.");
return null;
}
// if not connected to Hexagon, get episodes from trakt
HashMap<Integer, BaseShow> traktCollection = null;
HashMap<Integer, BaseShow> traktWatched = null;
if (!HexagonSettings.isEnabled(app) && TraktCredentials.get(app).hasCredentials()) {
Timber.d("Getting watched and collected episodes from trakt.");
// get collection
HashMap<Integer, BaseShow> traktShows = getTraktShows("get collection", true);
if (traktShows == null) {
// can not get collected state from trakt, give up.
return null;
}
traktCollection = traktShows;
// get watched
traktShows = getTraktShows("get watched", false);
if (traktShows == null) {
// can not get watched state from trakt, give up.
return null;
}
traktWatched = traktShows;
}
int result;
boolean addedAtLeastOneShow = false;
boolean failedMergingShows = false;
while (!addQueue.isEmpty()) {
Timber.d("Starting to add next show...");
if (isCancelled()) {
Timber.d("Finished. Cancelled.");
// table yet
return null;
}
nextShow = addQueue.removeFirst();
// set values required for progress update
currentShowName = nextShow.title;
currentShowTvdbId = nextShow.tvdbid;
if (!AndroidUtils.isNetworkConnected(app)) {
Timber.d("Finished. No connection.");
publishProgress(RESULT_OFFLINE);
failedMergingShows = true;
break;
}
try {
boolean addedShow = TvdbTools.getInstance(app).addShow(nextShow.tvdbid, nextShow.language, traktCollection, traktWatched);
result = addedShow ? PROGRESS_SUCCESS : PROGRESS_EXISTS;
addedAtLeastOneShow = addedShow || // do not overwrite previous success
addedAtLeastOneShow;
} catch (TvdbException e) {
// because it does not exist (any longer)
if (!(isMergingShows && e.itemDoesNotExist())) {
failedMergingShows = true;
}
if (e.service() == TvdbException.Service.TVDB) {
if (e.itemDoesNotExist()) {
result = PROGRESS_ERROR_TVDB_NOT_EXISTS;
} else {
result = PROGRESS_ERROR_TVDB;
}
} else if (e.service() == TvdbException.Service.HEXAGON) {
result = PROGRESS_ERROR_HEXAGON;
} else if (e.service() == TvdbException.Service.DATA) {
result = PROGRESS_ERROR_DATA;
} else {
result = PROGRESS_ERROR;
}
Timber.e(e, "Adding show failed");
}
publishProgress(result);
Timber.d("Finished adding show. (Result code: %s)", result);
}
isFinishedAddingShows = true;
// when merging shows down from Hexagon, set success flag
if (isMergingShows && !failedMergingShows) {
HexagonSettings.setHasMergedShows(app, true);
}
if (addedAtLeastOneShow) {
// make sure the next sync will download all ratings
PreferenceManager.getDefaultSharedPreferences(app).edit().putLong(TraktSettings.KEY_LAST_SHOWS_RATED_AT, 0).putLong(TraktSettings.KEY_LAST_EPISODES_RATED_AT, 0).apply();
// renew FTS3 table
Timber.d("Renewing search table.");
DBUtils.rebuildFtsTable(app);
}
Timber.d("Finished adding shows.");
return null;
}
Aggregations