private void parseOP(final ThingInfo data, final ThingInfo contextOP) {
    data.setIndent(0);
    data.setClicked(Common.isClicked(mActivity, data.getUrl()));

    mActivity.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            mActivity.mObjectStates.mCommentsList.add(0, data);
            if (mJumpToCommentContext > 0) {
              // Need to add a fake comment to get it to draw in the comments list properly, without
              // 'hacking' the adapter to work properly.
              ThingInfo tempInfo = new ThingInfo();
              tempInfo.setIsContext(true);
              // Set the parent ID to allow the user to view the parent thread.
              tempInfo.setParent_id(contextOP.getParent_id());
              tempInfo.setId(mJumpToCommentId);
              mActivity.mObjectStates.mCommentsList.add(1, tempInfo);
            }
          }
        });

    if (data.isIs_self() && data.getSelftext_html() != null) {
      // HTML to Spanned
      String unescapedHtmlSelftext = Html.fromHtml(data.getSelftext_html()).toString();
      Spanned selftext = Html.fromHtml(Util.convertHtmlTags(unescapedHtmlSelftext));

      // remove last 2 newline characters
      if (selftext.length() > 2)
        data.setSpannedSelftext(selftext.subSequence(0, selftext.length() - 2));
      else data.setSpannedSelftext("");
    }

    // We might not have a title if we've intercepted a plain link to a thread.
    mThreadTitle = data.getTitle();
    mActivity.setThreadTitle(mThreadTitle);
    mSubreddit = data.getSubreddit();
    mThreadId = data.getId();

    mOpThingInfo = data;
  }
  /**
   * Recursive method to insert comment tree into the mCommentsList, with proper list order and
   * indentation
   */
  int insertNestedComment(
      ThingListing commentThingListing, int indentLevel, int insertedCommentIndex) {
    ThingInfo ci = commentThingListing.getData();
    // First test for moderator distinguished
    if (Constants.DISTINGUISHED_MODERATOR.equalsIgnoreCase(ci.getDistinguished())) {
      SpannableString distSS = new SpannableString(ci.getAuthor() + " [M]");
      distSS.setSpan(
          Util.getModeratorSpan(mActivity.getApplicationContext()),
          0,
          distSS.length(),
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      ci.setSSAuthor(distSS);
    } else if (Constants.DISTINGUISHED_ADMIN.equalsIgnoreCase(ci.getDistinguished())) {
      SpannableString distSS = new SpannableString(ci.getAuthor() + " [A]");
      distSS.setSpan(
          Util.getAdminSpan(mActivity.getApplicationContext()),
          0,
          distSS.length(),
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      ci.setSSAuthor(distSS);
    } else if (mOpThingInfo != null && mOpThingInfo.getAuthor().equalsIgnoreCase(ci.getAuthor())) {
      if (m_OPSpan == null) {
        m_OPSpan = new SpannableString(mOpThingInfo.getAuthor() + " [S]");
        m_OPSpan.setSpan(
            Util.getOPSpan(mActivity.getApplicationContext(), mSettings.getTheme()),
            0,
            m_OPSpan.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      }
      ci.setSSAuthor(m_OPSpan);
    }
    // Add comment to deferred append/replace list
    if (isInsertingEntireThread()) deferCommentAppend(ci);
    else deferCommentReplacement(ci);

    // Keep track of jump target
    if (isHasJumpTarget()) {
      if (!isFoundJumpTargetComment() && mJumpToCommentId.equals(ci.getId()))
        processJumpTarget(ci, insertedCommentIndex);
    }

    if (isHasJumpTarget()) {
      // if we have found the jump target, then we did the messy stuff already. just append to main
      // processing list.
      if (isFoundJumpTargetComment()) {
        mProcessCommentsTask.addDeferred(new DeferredCommentProcessing(ci, insertedCommentIndex));
      }
      // try to handle the context search, if we want context
      else if (mJumpToCommentContext > 0) {
        // any comment could be in the context; we don't know yet. so append to the high-priority
        // "context" list
        mProcessCommentsTask.addDeferredHighPriority(
            new DeferredCommentProcessing(ci, insertedCommentIndex));

        // we push overflow onto the low priority list, since overflow will end up above the jump
        // target, off the top of the screen.
        // TODO don't use LinkedList.size()
        mProcessCommentsTask.moveHighPriorityOverflowToLowPriority(mJumpToCommentContext);
      }
      // if no context search, then push comments to low priority list until we find the jump target
      // comment
      else {
        mProcessCommentsTask.addDeferredLowPriority(
            new DeferredCommentProcessing(ci, insertedCommentIndex));
      }
    }
    // if there is no jump target, there's just a single deferred-processing list to worry about.
    else {
      mProcessCommentsTask.addDeferred(new DeferredCommentProcessing(ci, insertedCommentIndex));
    }

    // Formatting that applies to all items, both real comments and "more" entries
    ci.setIndent(mIndentation + indentLevel);

    // Handle "more" entry
    if (Constants.MORE_KIND.equals(commentThingListing.getKind())) {
      ci.setLoadMoreCommentsPlaceholder(true);
      if (Constants.LOGGING) Log.v(TAG, "new more position at " + (insertedCommentIndex));
      return insertedCommentIndex;
    }

    // Regular comment

    // Skip things that are not comments, which shouldn't happen
    if (!Constants.COMMENT_KIND.equals(commentThingListing.getKind())) {
      if (Constants.LOGGING)
        Log.e(
            TAG,
            "comment whose kind is \""
                + commentThingListing.getKind()
                + "\" (expected "
                + Constants.COMMENT_KIND
                + ")");
      return insertedCommentIndex;
    }

    // handle the replies
    Listing repliesListing = ci.getReplies();
    if (repliesListing == null) return insertedCommentIndex;
    ListingData repliesListingData = repliesListing.getData();
    if (repliesListingData == null) return insertedCommentIndex;
    ThingListing[] replyThingListings = repliesListingData.getChildren();
    if (replyThingListings == null) return insertedCommentIndex;

    for (ThingListing replyThingListing : replyThingListings) {
      insertedCommentIndex =
          insertNestedComment(replyThingListing, indentLevel + 1, insertedCommentIndex + 1);
    }
    return insertedCommentIndex;
  }
Beispiel #3
0
  @Override
  protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);

    switch (id) {
      case Constants.DIALOG_SAVED_COMMENTS:
        final TextView author = (TextView) dialog.findViewById(R.id.submitter);
        final TextView body = (TextView) dialog.findViewById(R.id.body);

        author.setText(currentSavedContent.getAuthor());

        StringBuilder bodyText = new StringBuilder();
        String[] pieces = currentSavedContent.getBody().split("[\\r\\n]+");
        int charCount = 0;
        for (int i = 0; i < pieces.length && i < 9; i++) {
          if (charCount > 200) {
            break;
          }
          bodyText.append(pieces[i]);
          charCount += pieces[i].length();
        }
        body.setText(createSpanned(bodyText.toString().trim()));

        final Button viewComment = (Button) dialog.findViewById(R.id.view_saved_comment);
        final Button unsaveComment = (Button) dialog.findViewById(R.id.unsave_comment);
        final Button linkButton = (Button) dialog.findViewById(R.id.thread_link_button);
        viewComment.setEnabled(true);
        unsaveComment.setEnabled(true);

        ThingInfo ti = new ThingInfo();
        new Markdown().getURLs(currentSavedContent.getBody(), ti.getUrls());
        linkToEmbeddedURLs(linkButton, ti.getUrls());

        unsaveComment.setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                sdbh.deleteSavedContent(currentSavedContent);
                removeDialog(Constants.DIALOG_SAVED_COMMENTS);
                relaunchActivity();
              }
            });

        viewComment.setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                ThingInfo commentThing = new ThingInfo();
                commentThing.setLink_id(currentSavedContent.getLinkId());
                commentThing.setId(currentSavedContent.getCommentId());

                removeDialog(Constants.DIALOG_SAVED_COMMENTS);

                Intent i = new Intent(SavedCommentsActivity.this, CommentsListActivity.class);
                i.setData(Util.createCommentUriNoContext(commentThing));
                i.putExtra(Constants.EXTRA_SUBREDDIT, currentSavedContent.getSubreddit());
                startActivity(i);
              }
            });
        break;
      default:
        throw new IllegalArgumentException("Unexpected dialog id " + id);
    }
  }