Beispiel #1
0
  @Override
  protected void onPostExecute(Response r) {
    if (r != null) {
      if (TraktStatus.SUCCESS.equals(r.status)) {
        // all good

        switch (mAction) {
          case CHECKIN_EPISODE:
          case CHECKIN_MOVIE:
            Toast.makeText(mContext, r.message, Toast.LENGTH_SHORT).show();
            break;
          default:
            Toast.makeText(
                    mContext,
                    r.message + " " + mContext.getString(R.string.ontrakt),
                    Toast.LENGTH_SHORT)
                .show();
            break;
        }

        EventBus.getDefault().post(new TraktActionCompleteEvent(mArgs, true));
        if (mListener != null) {
          mListener.onTraktActionComplete(mArgs, true);
        }

      } else if (TraktStatus.FAILURE.equals(r.status)) {
        if (r.wait != 0) {
          // looks like a check in is in progress

          if (mListener != null) {
            mListener.onCheckinBlocked(mArgs, r.wait);
          }
        } else {
          // well, something went wrong

          Toast.makeText(mContext, r.error, Toast.LENGTH_LONG).show();

          if (mListener != null) {
            mListener.onTraktActionComplete(mArgs, false);
          }
        }
      }
    } else {
      // notify that our first run completed, however due to invalid
      // credentials we have not done anything
      if (mListener != null) {
        mListener.onTraktActionComplete(mArgs, false);
      }
    }
  }
Beispiel #2
0
  @Override
  protected Response doInBackground(Void... params) {
    // we need this value in onPostExecute, so get it already here
    mAction = TraktAction.values()[mArgs.getInt(InitBundle.TRAKTACTION)];

    // check for network connection
    if (!AndroidUtils.isNetworkConnected(mContext)) {
      Response r = new Response();
      r.status = TraktStatus.FAILURE;
      r.error = mContext.getString(R.string.offline);
      return r;
    }

    // check for valid credentials
    if (!ServiceUtils.isTraktCredentialsValid(mContext)) {
      // return null so a credentials dialog is displayed
      // it will call us again with valid credentials
      return null;
    }

    // get an authenticated trakt-java ServiceManager
    ServiceManager manager = ServiceUtils.getTraktServiceManagerWithAuth(mContext, false);
    if (manager == null) {
      // password could not be decrypted
      Response r = new Response();
      r.status = TraktStatus.FAILURE;
      r.error = mContext.getString(R.string.trakt_generalerror);
      return r;
    }

    // get values used by all actions
    final int showTvdbId = mArgs.getInt(InitBundle.SHOW_TVDBID);
    final int season = mArgs.getInt(InitBundle.SEASON);
    final int episode = mArgs.getInt(InitBundle.EPISODE);

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

    try {
      Response r = null;

      switch (mAction) {
        case CHECKIN_EPISODE:
          {
            final String message = mArgs.getString(InitBundle.MESSAGE);

            final CheckinBuilder checkinBuilder =
                manager.showService().checkin(showTvdbId).season(season).episode(episode);
            if (!TextUtils.isEmpty(message)) {
              checkinBuilder.message(message);
            }
            r = checkinBuilder.fire();

            if (TraktStatus.SUCCESS.equals(r.status)) {
              SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
              r.message =
                  mContext.getString(
                      R.string.checkin_success_trakt,
                      (r.show != null ? r.show.title + " " : "")
                          + Utils.getEpisodeNumber(prefs, season, episode));
            }

            break;
          }
        case CHECKIN_MOVIE:
          {
            final String imdbId = mArgs.getString(InitBundle.IMDB_ID);
            final String message = mArgs.getString(InitBundle.MESSAGE);

            final MovieService.CheckinBuilder checkinBuilder =
                manager.movieService().checkin(imdbId);
            if (!TextUtils.isEmpty(message)) {
              checkinBuilder.message(message);
            }
            r = checkinBuilder.fire();

            if (TraktStatus.SUCCESS.equals(r.status)) {
              r.message =
                  mContext.getString(
                      R.string.checkin_success_trakt, (r.movie != null ? r.movie.title + " " : ""));
            }

            break;
          }
        case RATE_EPISODE:
          {
            final Rating rating = Rating.fromValue(mArgs.getString(InitBundle.RATING));
            r =
                manager
                    .rateService()
                    .episode(showTvdbId)
                    .season(season)
                    .episode(episode)
                    .rating(rating)
                    .fire();
            break;
          }
        case RATE_SHOW:
          {
            final Rating rating = Rating.fromValue(mArgs.getString(InitBundle.RATING));
            r = manager.rateService().show(showTvdbId).rating(rating).fire();
            break;
          }
        case SHOUT:
          {
            final String shout = mArgs.getString(InitBundle.MESSAGE);
            final boolean isSpoiler = mArgs.getBoolean(InitBundle.ISSPOILER);

            if (episode == 0) {
              r =
                  manager
                      .commentService()
                      .show(showTvdbId)
                      .comment(shout)
                      .spoiler(isSpoiler)
                      .fire();
            } else {
              r =
                  manager
                      .commentService()
                      .episode(showTvdbId)
                      .season(season)
                      .episode(episode)
                      .comment(shout)
                      .spoiler(isSpoiler)
                      .fire();
            }
            break;
          }
        case WATCHLIST_MOVIE:
          {
            final int tmdbId = mArgs.getInt(InitBundle.TMDB_ID);
            manager.movieService().watchlist().movie(tmdbId).fire();
            // In case of failure this will just return an exception, so
            // we need to construct our own response
            r = new Response();
            r.status = TraktStatus.SUCCESS;
            r.message = mContext.getString(R.string.watchlist_added);
            break;
          }
        case UNWATCHLIST_MOVIE:
          {
            final int tmdbId = mArgs.getInt(InitBundle.TMDB_ID);
            manager.movieService().unwatchlist().movie(tmdbId).fire();
            // In case of failure this will just return an exception, so
            // we need to construct our own response
            r = new Response();
            r.status = TraktStatus.SUCCESS;
            r.message = mContext.getString(R.string.watchlist_removed);
            break;
          }
        default:
          break;
      }

      return r;
    } catch (TraktException e) {
      Utils.trackExceptionAndLog(mContext, TAG, e);
      Response r = new Response();
      r.status = TraktStatus.FAILURE;
      r.error = mContext.getString(R.string.trakt_generalerror);
      return r;
    } catch (ApiException e) {
      Utils.trackExceptionAndLog(mContext, TAG, e);
      Response r = new Response();
      r.status = TraktStatus.FAILURE;
      r.error = mContext.getString(R.string.trakt_generalerror);
      return r;
    }
  }