コード例 #1
0
ファイル: Utils.java プロジェクト: hopefulcd/SeriesGuide
  public static String toSHA1(Context context, String message) {
    try {
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      byte[] messageBytes = message.getBytes("UTF-8");
      byte[] digest = md.digest(messageBytes);

      String result = "";
      for (int i = 0; i < digest.length; i++) {
        result += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
      }

      return result;
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
      Utils.trackExceptionAndLog(context, TAG, e);
    }
    return null;
  }
コード例 #2
0
ファイル: TraktTask.java プロジェクト: beck0505/SeriesGuide
  @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;
    }
  }
コード例 #3
0
  @Override
  protected Integer doInBackground(Void... params) {
    // Ensure external storage
    if (!AndroidUtils.isExtStorageAvailable()) {
      return ERROR_STORAGE_ACCESS;
    }

    // Ensure no large database ops are running
    TaskManager tm = TaskManager.getInstance(mContext);
    if (SgSyncAdapter.isSyncActive(mContext, false) || tm.isAddTaskRunning()) {
      return ERROR_LARGE_DB_OP;
    }

    // Ensure JSON file is available
    File path = JsonExportTask.getExportPath(mIsAutoBackupMode);
    File backup = new File(path, JsonExportTask.EXPORT_JSON_FILE_SHOWS);
    if (!backup.exists() || !backup.canRead()) {
      return ERROR_FILE_ACCESS;
    }

    // Clean out all existing tables
    mContext.getContentResolver().delete(Shows.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(Seasons.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(Episodes.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(SeriesContract.Lists.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(ListItems.CONTENT_URI, null, null);

    // Access JSON from backup folder to create new database
    try {
      InputStream in = new FileInputStream(backup);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        Show show = gson.fromJson(reader, Show.class);
        addShowToDatabase(show);
      }

      reader.endArray();
      reader.close();

    } catch (JsonParseException e) {
      // the given Json might not be valid or unreadable
      Utils.trackExceptionAndLog(mContext, TAG, e);
      return ERROR;
    } catch (IOException e) {
      Utils.trackExceptionAndLog(mContext, TAG, e);
      return ERROR;
    }

    /*
     * Lists
     */
    File backupLists = new File(path, JsonExportTask.EXPORT_JSON_FILE_LISTS);
    if (!backupLists.exists() || !backupLists.canRead()) {
      // Skip lists if the file is not accessible
      return SUCCESS;
    }

    // Access JSON from backup folder to create new database
    try {
      InputStream in = new FileInputStream(backupLists);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        List list = gson.fromJson(reader, List.class);
        addListToDatabase(list);
      }

      reader.endArray();
      reader.close();

    } catch (JsonParseException e) {
      // the given Json might not be valid or unreadable
      Utils.trackExceptionAndLog(mContext, TAG, e);
      return ERROR;
    } catch (IOException e) {
      Utils.trackExceptionAndLog(mContext, TAG, e);
      return ERROR;
    }

    // Renew search table
    mContext
        .getContentResolver()
        .query(EpisodeSearch.CONTENT_URI_RENEWFTSTABLE, null, null, null, null);

    return SUCCESS;
  }