Beispiel #1
0
  static {
    try {
      argsClassesOnCommentsUpdateFinished[0] = UUID.class;
      argsClassesOnCommentsUpdateFinished[1] = int.class;
      methodOnCommentsUpdateFinished =
          CommentsUpdateListener.class.getMethod(
              "OnCommentsUpdateFinished", argsClassesOnCommentsUpdateFinished);

      argsClassesOnCommentsUpdateFirstEtries[0] = UUID.class;
      argsClassesOnCommentsUpdateFirstEtries[1] = int.class;
      argsClassesOnCommentsUpdateFirstEtries[2] = int.class;
      argsClassesOnCommentsUpdateFirstEtries[3] = int.class;
      methodOnCommentsUpdateFirstEtries =
          CommentsUpdateListener.class.getMethod(
              "OnCommentsUpdateFirstEntries", argsClassesOnCommentsUpdateFirstEtries);

      argsClassesOnCommentsUpdateBegin[0] = UUID.class;
      methodOnCommentsUpdateBegin =
          CommentsUpdateListener.class.getMethod(
              "OnCommentsUpdateBegin", argsClassesOnCommentsUpdateBegin);

      argsClassesOnCommentsUpdate[0] = UUID.class;
      argsClassesOnCommentsUpdate[1] = int.class;
      methodOnCommentsUpdate =
          CommentsUpdateListener.class.getMethod("OnCommentsUpdate", argsClassesOnCommentsUpdate);
    } catch (Throwable t) {
      Logger.e(t);
    }
  }
Beispiel #2
0
  @Override
  protected Throwable doInBackground(Void... arg0) {
    long startTime = System.currentTimeMillis();

    BufferedInputStream stream = null;
    FileInputStream fileStream = null;
    File file = null;

    try {
      ServerWorker.Instance().clearCommentsById(post.getId());
      notifyAboutCommentsUpdateBegin();

      userName = SettingsWorker.Instance().loadUserName();
      postAuthor = post.getAuthor();

      final String pref = "<div id=\"XXXXXXXX\" ";
      final String postTree = "class=\"comment ";

      try {
        // TODO CHANGE TO NORMAL PARSING

        stream =
            new BufferedInputStream(
                ServerWorker.Instance().getContentStream(post.getUrl()), BUFFER_SIZE);
        file =
            new FileCache(LepraDroidApplication.getInstance())
                .getFile(post.getId().toString() + ".comments");
        FileOutputStream fos = new FileOutputStream(file);
        byte[] chars = new byte[BUFFER_SIZE];
        int len = 0;

        while (len != -1) {
          if (isCancelled()) break;

          fos.write(chars, 0, len);

          len = stream.read(chars, 0, BUFFER_SIZE);
          totalBytesReaded += len;
        }

        fos.close();

        post.setNewComments((short) -1);

        String pageA = null, pageB;
        int start = -1;
        int end = -1;
        fileStream = new FileInputStream(file);

        while (true) {
          if (isCancelled()) break;

          if ((len = readBytesToBuff_WithoutNonLatinCharsAtTheEnd(fileStream, chars)) < 0) {
            if (start >= 0 && end < 0) {
              parseRecord(pageA); // to read last record
            }

            break;
          }

          totalBytesParsed += len;

          if (len == 0) continue;
          else if (pageA == null) {
            pageA = new String(chars, 0, len, "UTF-8");
            continue;
          } else {
            pageB = new String(chars, 0, len, "UTF-8");
          }

          while (true) {
            if (isCancelled()) break;

            String html = pageA + pageB;

            start = start >= 0 ? start : html.indexOf(postTree, 0) - pref.length();
            if (start < 0) {
              if (!TextUtils.isEmpty(pageB)) pageA = pageB;

              break;
            }
            if (start > 0) {
              html = html.substring(start, html.length());
              start = 0;
            }

            end = html.length() > 500 ? html.indexOf(postTree, 500) - pref.length() : -1;
            if (end < 0) {
              pageA = html;

              break;
            }

            pageA = html.substring(end, html.length());
            pageB = "";

            parseRecord(html.substring(start, end));
          }
        }
      } finally {
        if (stream != null) stream.close();
        if (fileStream != null) fileStream.close();
        if (file != null)
          if (!file.delete()) {
            Logger.e("Can't remove tmp file");
          }
      }
    } catch (Throwable t) {
      Logger.e(t);
      setException(t);
    } finally {
      if (!isCancelled()) notifyAboutCommentsUpdateFinished();

      new UpdateBadgeCounterTask().execute();

      Logger.d(
          "GetCommentsTask time:"
              + Long.toString((System.currentTimeMillis() - startTime) / 1000)
              + "s");
    }

    return e;
  }