Search in sources :

Example 1 with ShowResult

use of com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult 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;
}
Also used : BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) HexagonEpisodeSync(com.battlelancer.seriesguide.sync.HexagonEpisodeSync) ShowResult(com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult) SuppressLint(android.annotation.SuppressLint)

Example 2 with ShowResult

use of com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult in project SeriesGuide by UweTrottmann.

the class ShowSync method sync.

/**
 * Update shows based on the sync type.
 */
@SuppressLint("TimberExceptionLogging")
@Nullable
public SgSyncAdapter.UpdateResult sync(Context context, ShowTools showTools, long currentTime, SyncProgress progress) {
    hasUpdatedShows = false;
    List<Long> showsToUpdate = getShowsToUpdate(context, currentTime);
    if (showsToUpdate == null) {
        return null;
    }
    Timber.d("Updating %d show(s)...", showsToUpdate.size());
    // from here on we need more sophisticated abort handling, so keep track of errors
    SgSyncAdapter.UpdateResult resultCode = SgSyncAdapter.UpdateResult.SUCCESS;
    // loop through shows and download latest data from TVDb
    int consecutiveTimeouts = 0;
    for (Long showId : showsToUpdate) {
        // stop sync if connectivity is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            resultCode = SgSyncAdapter.UpdateResult.INCOMPLETE;
            break;
        }
        ShowResult result = showTools.updateShow(showId);
        if (result == ShowResult.SUCCESS) {
            hasUpdatedShows = true;
        } else {
            // failed, continue with other shows
            resultCode = SgSyncAdapter.UpdateResult.INCOMPLETE;
            SgShow2Helper helper = SgRoomDatabase.getInstance(context).sgShow2Helper();
            String showTitle = helper.getShowTitle(showId);
            Integer showTmdbId = helper.getShowTmdbId(showId);
            String message = String.format("Failed to update show ('%s', TMDB id %s).", showTitle, showTmdbId);
            if (result == ShowResult.DOES_NOT_EXIST) {
                message += " It no longer exists.";
            }
            progress.setImportantErrorIfNone(message);
            Timber.e(message);
            // Stop updating after multiple consecutive timeouts (around 3 * 15/20 seconds)
            if (result == ShowResult.TIMEOUT_ERROR) {
                consecutiveTimeouts++;
            } else if (consecutiveTimeouts > 0) {
                consecutiveTimeouts--;
            }
            if (consecutiveTimeouts == 3) {
                Timber.e("Connection unstable, give up.");
                return resultCode;
            }
        }
    }
    return resultCode;
}
Also used : SgShow2Helper(com.battlelancer.seriesguide.provider.SgShow2Helper) ShowResult(com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint) Nullable(androidx.annotation.Nullable)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 ShowResult (com.battlelancer.seriesguide.ui.shows.ShowTools2.ShowResult)2 Nullable (androidx.annotation.Nullable)1 SgShow2Helper (com.battlelancer.seriesguide.provider.SgShow2Helper)1 HexagonEpisodeSync (com.battlelancer.seriesguide.sync.HexagonEpisodeSync)1 BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)1