use of com.battlelancer.seriesguide.sync.HexagonEpisodeSync in project SeriesGuide by UweTrottmann.
the class AddShowTask method doInBackground.
@Override
protected Void doInBackground(Void... params) {
Timber.d("Starting to add shows...");
SearchResult firstShow = addQueue.peek();
if (firstShow == null) {
Timber.d("Finished. Queue was empty.");
return null;
}
if (!AndroidUtils.isNetworkConnected(context)) {
Timber.d("Finished. No internet connection.");
publishProgress(RESULT_OFFLINE, firstShow.getTmdbId(), firstShow.getTitle());
return null;
}
if (isCancelled()) {
Timber.d("Finished. Cancelled.");
return null;
}
// if not connected to Hexagon, get episodes from trakt
Map<Integer, BaseShow> traktCollection = null;
Map<Integer, BaseShow> traktWatched = null;
if (!HexagonSettings.isEnabled(context) && TraktCredentials.get(context).hasCredentials()) {
Timber.d("Getting watched and collected episodes from trakt.");
// get collection
Map<Integer, BaseShow> traktShows = getTraktShows(true);
if (traktShows == null) {
// can not get collected state from trakt, give up.
return null;
}
traktCollection = traktShows;
// get watched
traktShows = getTraktShows(false);
if (traktShows == null) {
// can not get watched state from trakt, give up.
return null;
}
traktWatched = traktShows;
}
HexagonEpisodeSync hexagonEpisodeSync = new HexagonEpisodeSync(context, SgApp.getServicesComponent(context).hexagonTools());
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;
}
SearchResult nextShow = addQueue.removeFirst();
// set values required for progress update
String currentShowName = nextShow.getTitle();
int currentShowTmdbId = nextShow.getTmdbId();
if (currentShowTmdbId <= 0) {
// Invalid ID, should never have been passed, report.
// Background: Hexagon gets requests with ID 0.
IllegalStateException invalidIdException = new IllegalStateException("Show id invalid: " + currentShowTmdbId + ", silentMode=" + isSilentMode + ", merging=" + isMergingShows);
Errors.logAndReport("Add show", invalidIdException);
continue;
}
if (!AndroidUtils.isNetworkConnected(context)) {
Timber.d("Finished. No connection.");
publishProgress(RESULT_OFFLINE, currentShowTmdbId, currentShowName);
failedMergingShows = true;
break;
}
ShowResult addResult = SgApp.getServicesComponent(context).showTools().addShow(nextShow.getTmdbId(), nextShow.getLanguage(), traktCollection, traktWatched, hexagonEpisodeSync);
if (addResult == ShowResult.SUCCESS) {
result = PROGRESS_SUCCESS;
addedAtLeastOneShow = true;
} else if (addResult == ShowResult.IN_DATABASE) {
result = PROGRESS_EXISTS;
} else {
Timber.e("Adding show failed: %s", addResult);
// not because it does not (longer) exist.
if (isMergingShows && addResult != ShowResult.DOES_NOT_EXIST) {
failedMergingShows = true;
}
switch(addResult) {
case DOES_NOT_EXIST:
result = PROGRESS_ERROR_TVDB_NOT_EXISTS;
break;
case TMDB_ERROR:
result = PROGRESS_ERROR_TVDB;
break;
case TRAKT_ERROR:
result = PROGRESS_ERROR_TRAKT;
break;
case HEXAGON_ERROR:
result = PROGRESS_ERROR_HEXAGON;
break;
case DATABASE_ERROR:
result = PROGRESS_ERROR_DATA;
break;
default:
result = PROGRESS_ERROR;
break;
}
}
publishProgress(result, currentShowTmdbId, currentShowName);
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(context, true);
}
if (addedAtLeastOneShow) {
// make sure the next sync will download all ratings
PreferenceManager.getDefaultSharedPreferences(context).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.");
SeriesGuideDatabase.rebuildFtsTable(context);
}
Timber.d("Finished adding shows.");
return null;
}
Aggregations