Esempio n. 1
0
    public void run() {
      final FragmentActivity context = getActivity();
      if (context == null) {
        return;
      }

      if (mSeasonId != null) {
        // update one season
        DBUtils.updateUnwatchedCount(context, mSeasonId);
      } else {
        // update all seasons of this show, start with the most recent
        // one
        final Cursor seasons =
            context
                .getContentResolver()
                .query(
                    Seasons.buildSeasonsOfShowUri(mShowId),
                    new String[] {Seasons._ID},
                    null,
                    null,
                    Seasons.COMBINED + " DESC");
        if (seasons == null) {
          return;
        }
        while (seasons.moveToNext()) {
          String seasonId = seasons.getString(0);
          DBUtils.updateUnwatchedCount(context, seasonId);

          notifyContentProvider(context);
        }
        seasons.close();
      }

      notifyContentProvider(context);
    }
Esempio n. 2
0
 private void notifyContentProvider(final FragmentActivity context) {
   context.getContentResolver().notifyChange(Seasons.buildSeasonsOfShowUri(mShowId), null);
 }
Esempio n. 3
0
  private Integer syncToSeriesGuide(ServiceManager manager, String username) {
    mResult = "";

    List<TvShow> shows;
    try {
      // get watched episodes from trakt
      shows =
          manager.userService().libraryShowsWatched(username).extended(ExtendedParam.Min).fire();
    } catch (TraktException e) {
      fireTrackerEventToSeriesGuide(e.getMessage());
      Log.w(TAG, e);
      return FAILED_API;
    } catch (ApiException e) {
      fireTrackerEventToSeriesGuide(e.getMessage());
      Log.w(TAG, e);
      return FAILED_API;
    }

    // get show ids in local database
    Cursor showTvdbIds =
        mContext
            .getContentResolver()
            .query(Shows.CONTENT_URI, new String[] {Shows._ID}, null, null, null);

    // assume we have a local list of which shows to sync (later...)
    while (showTvdbIds.moveToNext()) {
      String tvdbId = showTvdbIds.getString(0);
      for (TvShow tvShow : shows) {
        if (tvdbId.equalsIgnoreCase(tvShow.tvdbId)) {
          if (mResult.length() != 0) {
            mResult += ", ";
          }

          if (mIsSyncingUnseen) {
            ContentValues values = new ContentValues();
            values.put(Episodes.WATCHED, false);
            mContext
                .getContentResolver()
                .update(Episodes.buildEpisodesOfShowUri(tvdbId), values, null, null);
          }

          final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

          // go through watched seasons, try to match them with local
          // season
          List<TvShowSeason> seasons = tvShow.seasons;
          for (TvShowSeason season : seasons) {
            Cursor seasonMatch =
                mContext
                    .getContentResolver()
                    .query(
                        Seasons.buildSeasonsOfShowUri(tvdbId),
                        new String[] {Seasons._ID},
                        Seasons.COMBINED + "=?",
                        new String[] {season.season.toString()},
                        null);

            // if we found a season, go on with its episodes
            if (seasonMatch.moveToFirst()) {
              String seasonId = seasonMatch.getString(0);

              // build episodes update query to mark seen episodes

              for (Integer episode : season.episodes.numbers) {
                batch.add(
                    ContentProviderOperation.newUpdate(Episodes.buildEpisodesOfSeasonUri(seasonId))
                        .withSelection(Episodes.NUMBER + "=?", new String[] {episode.toString()})
                        .withValue(Episodes.WATCHED, true)
                        .build());
              }
            }

            seasonMatch.close();
          }

          // last chance to abort before doing work
          if (isCancelled()) {
            showTvdbIds.close();
            return null;
          }

          try {
            mContext.getContentResolver().applyBatch(SeriesContract.CONTENT_AUTHORITY, batch);
          } catch (RemoteException e) {
            // Failed binder transactions aren't recoverable
            fireTrackerEventToSeriesGuide(e.getMessage());
            throw new RuntimeException("Problem applying batch operation", e);
          } catch (OperationApplicationException e) {
            // Failures like constraint violation aren't
            // recoverable
            fireTrackerEventToSeriesGuide(e.getMessage());
            throw new RuntimeException("Problem applying batch operation", e);
          }

          mResult += tvShow.title;

          // remove synced show
          shows.remove(tvShow);
          break;
        }
      }
    }

    showTvdbIds.close();
    if (mResult.length() != 0) {
      return SUCCESS_WORK;
    } else {
      return SUCCESS_NOWORK;
    }
  }
Esempio n. 4
0
  private Integer syncToTrakt(ServiceManager manager) {
    // get show ids in local database for which syncing is enabled
    Cursor showTvdbIds =
        mContext
            .getContentResolver()
            .query(
                Shows.CONTENT_URI, new String[] {Shows._ID}, Shows.SYNCENABLED + "=1", null, null);

    if (showTvdbIds.getCount() == 0) {
      return SUCCESS_NOWORK;
    }

    while (showTvdbIds.moveToNext()) {
      String tvdbId = showTvdbIds.getString(0);
      EpisodeSeenBuilder builder = manager.showService().episodeSeen(Integer.valueOf(tvdbId));

      // build seen episodes trakt post
      Cursor seenEpisodes =
          mContext
              .getContentResolver()
              .query(
                  Episodes.buildEpisodesOfShowUri(tvdbId),
                  new String[] {Episodes.SEASON, Episodes.NUMBER},
                  Episodes.WATCHED + "=?",
                  new String[] {"1"},
                  null);
      if (seenEpisodes.getCount() == 0) {
        builder = null;
      } else {
        while (seenEpisodes.moveToNext()) {
          int season = seenEpisodes.getInt(0);
          int episode = seenEpisodes.getInt(1);
          builder.episode(season, episode);
        }
      }
      seenEpisodes.close();

      // build unseen episodes trakt post
      EpisodeUnseenBuilder builderUnseen = null;
      if (mIsSyncingUnseen) {
        builderUnseen = manager.showService().episodeUnseen(Integer.valueOf(tvdbId));
        Cursor unseenEpisodes =
            mContext
                .getContentResolver()
                .query(
                    Episodes.buildEpisodesOfShowUri(tvdbId),
                    new String[] {Episodes.SEASON, Episodes.NUMBER},
                    Episodes.WATCHED + "=?",
                    new String[] {"0"},
                    null);
        if (unseenEpisodes.getCount() == 0) {
          builderUnseen = null;
        } else {
          while (unseenEpisodes.moveToNext()) {
            int season = unseenEpisodes.getInt(0);
            int episode = unseenEpisodes.getInt(1);
            builderUnseen.episode(season, episode);
          }
        }
        unseenEpisodes.close();
      }

      // last chance to abort
      if (isCancelled()) {
        showTvdbIds.close();
        return null;
      }

      try {
        // mark episodes of show
        if (builder != null) {
          builder.fire();
        }
        if (mIsSyncingUnseen && builderUnseen != null) {
          builderUnseen.fire();
        }
      } catch (TraktException e) {
        fireTrackerEventToTrakt(e.getMessage());
        Log.w(TAG, e);
        return FAILED_API;
      } catch (ApiException e) {
        fireTrackerEventToTrakt(e.getMessage());
        Log.w(TAG, e);
        return FAILED_API;
      }
    }

    showTvdbIds.close();
    return SUCCESS_WORK;
  }