예제 #1
0
  /**
   * Just fetch all series and episode details and overwrite, add.
   *
   * @param showId
   * @throws SAXException
   * @throws IOException
   */
  public static void updateShow(String showId, Context context) throws SAXException {
    String language = getTheTVDBLanguage(context);
    Series show = fetchShow(showId, language, context);

    final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();
    batch.add(DBUtils.buildShowOp(show, context, false));
    batch.addAll(importShowEpisodes(showId, show.getAirsTime(), language, context));

    try {
      context.getContentResolver().applyBatch(SeriesContract.CONTENT_AUTHORITY, batch);
    } catch (RemoteException e) {
      // Failed binder transactions aren't recoverable
      throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
      // Failures like constraint violation aren't recoverable
      throw new RuntimeException("Problem applying batch operation", e);
    }
  }
  @TargetApi(11)
  private void fillData() {
    TextView seriesname = (TextView) findViewById(R.id.title);
    TextView overview = (TextView) findViewById(R.id.TextViewShowInfoOverview);
    TextView airstime = (TextView) findViewById(R.id.TextViewShowInfoAirtime);
    TextView network = (TextView) findViewById(R.id.TextViewShowInfoNetwork);
    TextView status = (TextView) findViewById(R.id.TextViewShowInfoStatus);

    final Series show = DBUtils.getShow(this, String.valueOf(getShowId()));
    if (show == null) {
      finish();
      return;
    }

    // Name
    seriesname.setText(show.getSeriesName());

    // Overview
    if (show.getOverview().length() == 0) {
      overview.setText("");
    } else {
      overview.setText(show.getOverview());
    }

    // Airtimes
    if (show.getAirsDayOfWeek().length() == 0 || show.getAirsTime() == -1) {
      airstime.setText(getString(R.string.show_noairtime));
    } else {
      String[] values =
          Utils.parseMillisecondsToTime(
              show.getAirsTime(), show.getAirsDayOfWeek(), getApplicationContext());
      airstime.setText(getString(R.string.show_airs) + " " + values[1] + " " + values[0]);
    }

    // Network
    if (show.getNetwork().length() == 0) {
      network.setText("");
    } else {
      network.setText(getString(R.string.show_network) + " " + show.getNetwork());
    }

    // Running state
    if (show.getStatus() == 1) {
      status.setTextColor(Color.GREEN);
      status.setText(getString(R.string.show_isalive));
    } else if (show.getStatus() == 0) {
      status.setTextColor(Color.GRAY);
      status.setText(getString(R.string.show_isnotalive));
    }

    // first airdate
    long airtime = Utils.buildEpisodeAirtime(show.getFirstAired(), show.getAirsTime());
    Utils.setValueOrPlaceholder(
        findViewById(R.id.TextViewShowInfoFirstAirdate), Utils.formatToDate(airtime, this));

    // Others
    Utils.setValueOrPlaceholder(
        findViewById(R.id.TextViewShowInfoActors), Utils.splitAndKitTVDBStrings(show.getActors()));
    Utils.setValueOrPlaceholder(
        findViewById(R.id.TextViewShowInfoContentRating), show.getContentRating());
    Utils.setValueOrPlaceholder(
        findViewById(R.id.TextViewShowInfoGenres), Utils.splitAndKitTVDBStrings(show.getGenres()));
    Utils.setValueOrPlaceholder(
        findViewById(R.id.TextViewShowInfoRuntime),
        show.getRuntime() + " " + getString(R.string.show_airtimeunit));

    // TVDb rating
    String ratingText = show.getRating();
    if (ratingText != null && ratingText.length() != 0) {
      RatingBar ratingBar = (RatingBar) findViewById(R.id.bar);
      ratingBar.setProgress((int) (Double.valueOf(ratingText) / 0.1));
      TextView rating = (TextView) findViewById(R.id.value);
      rating.setText(ratingText + "/10");
    }

    // IMDb button
    View imdbButton = (View) findViewById(R.id.buttonShowInfoIMDB);
    final String imdbid = show.getImdbId();
    if (imdbButton != null) {
      imdbButton.setOnClickListener(
          new OnClickListener() {
            public void onClick(View v) {
              fireTrackerEvent("Show IMDb page");

              if (imdbid.length() != 0) {
                Intent myIntent =
                    new Intent(Intent.ACTION_VIEW, Uri.parse("imdb:///title/" + imdbid + "/"));
                try {
                  startActivity(myIntent);
                } catch (ActivityNotFoundException e) {
                  myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(IMDB_TITLE_URL + imdbid));
                  startActivity(myIntent);
                }
              } else {
                Toast.makeText(
                        getApplicationContext(),
                        getString(R.string.show_noimdbentry),
                        Toast.LENGTH_LONG)
                    .show();
              }
            }
          });
    }

    // TVDb button
    View tvdbButton = (View) findViewById(R.id.buttonTVDB);
    final String tvdbId = show.getId();
    if (tvdbButton != null) {
      tvdbButton.setOnClickListener(
          new OnClickListener() {
            public void onClick(View v) {
              fireTrackerEvent("Show TVDb page");
              Intent i =
                  new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.TVDB_SHOW_URL + tvdbId));
              startActivity(i);
            }
          });
    }

    // Shout button
    findViewById(R.id.buttonShouts)
        .setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(View v) {
                fireTrackerEvent("Show Trakt Shouts");
                TraktShoutsFragment newFragment =
                    TraktShoutsFragment.newInstance(show.getSeriesName(), Integer.valueOf(tvdbId));

                newFragment.show(getSupportFragmentManager(), "shouts-dialog");
              }
            });

    // Share intent
    mShareIntentBuilder =
        ShareCompat.IntentBuilder.from(this)
            .setChooserTitle(R.string.share)
            .setText(
                getString(R.string.share_checkout)
                    + " \""
                    + show.getSeriesName()
                    + "\" via @SeriesGuide "
                    + ShowInfoActivity.IMDB_TITLE_URL
                    + imdbid)
            .setType("text/plain");

    // Poster
    final ImageView poster = (ImageView) findViewById(R.id.ImageViewShowInfoPoster);
    ImageProvider.getInstance(this).loadImage(poster, show.getPoster(), false);

    // trakt ratings
    TraktSummaryTask task = new TraktSummaryTask(this, findViewById(R.id.ratingbar)).show(tvdbId);
    AndroidUtils.executeAsyncTask(task, new Void[] {null});
  }
예제 #3
0
  /**
   * Adds a show and its episodes. If it already exists updates them. This uses two consequent
   * connections. The first one downloads the base series record, to check if the show is already in
   * the database. The second downloads all episode information. This allows for a smaller download,
   * if a show already exists in your database.
   *
   * @param showId
   * @param seenShows
   * @return true if show and its episodes were added, false if it already exists
   * @throws IOException
   * @throws SAXException
   */
  public static boolean addShow(String showId, List<TvShow> seenShows, Context context)
      throws SAXException {
    String language = getTheTVDBLanguage(context);
    Series show = fetchShow(showId, language, context);

    boolean isShowExists = DBUtils.isShowExists(showId, context);

    final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();
    batch.add(DBUtils.buildShowOp(show, context, !isShowExists));
    batch.addAll(importShowEpisodes(showId, show.getAirsTime(), language, context));

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

    // try to find seen episodes from trakt
    for (TvShow tvShow : seenShows) {
      if (showId.equals(tvShow.tvdbId)) {
        batch.clear();

        // try to find matching seasons
        final List<TvShowSeason> seasons = tvShow.seasons;
        for (TvShowSeason season : seasons) {
          final Cursor seasonMatch =
              context
                  .getContentResolver()
                  .query(
                      Seasons.buildSeasonsOfShowUri(showId),
                      new String[] {Seasons._ID},
                      Seasons.COMBINED + "=?",
                      new String[] {season.season.toString()},
                      null);

          // add ops to mark episodes as watched
          if (seasonMatch.moveToFirst()) {
            final String seasonId = seasonMatch.getString(0);

            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();
        }

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

        break;
      }
    }

    DBUtils.updateLatestEpisode(context, showId);

    return !isShowExists;
  }