Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae48e81b authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Fix crash in StreamItemEntry.checkDecoded()

This method is meant to check if decodeHtml() was called prior to
getDecodedText() / getDecodedComments() calls.

The logic was flawed because HtmlUtils.fromHtml will return null
for empty strings, so even when decodeHtml() was called and mComments
wasn't null, mDecodedComments may still be null.

Let's just not try to be smart and introduce a new flag for this purpose.

Also changed the test-only constructor to a static method to make sure
it won't get called from the main apk.

Bug 6367860

Change-Id: I9a774e010666bd5a83ef916e817b50052afd325f
parent c019a80d
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -39,13 +39,15 @@ public class StreamItemEntry implements Comparable<StreamItemEntry> {
    private final long mId;
    private final String mText;
    private final String mComments;
    private CharSequence mDecodedText;
    private CharSequence mDecodedComments;
    private final long mTimestamp;
    private final String mAccountType;
    private final String mAccountName;
    private final String mDataSet;

    private boolean mDecoded;
    private CharSequence mDecodedText;
    private CharSequence mDecodedComments;

    // Package references for label and icon resources.
    private final String mResPackage;
    private final String mIconRes;
@@ -55,7 +57,14 @@ public class StreamItemEntry implements Comparable<StreamItemEntry> {
    private List<StreamItemPhotoEntry> mPhotos;

    @NeededForTesting
    public StreamItemEntry(long id, String text, String comments, long timestamp,
    public static StreamItemEntry createForTest(long id, String text, String comments,
            long timestamp, String accountType, String accountName, String dataSet,
            String resPackage, String iconRes, String labelRes) {
        return new StreamItemEntry(id, text, comments, timestamp, accountType, accountName, dataSet,
                resPackage, iconRes, labelRes);
    }

    private StreamItemEntry(long id, String text, String comments, long timestamp,
            String accountType, String accountName, String dataSet, String resPackage,
            String iconRes, String labelRes) {
        mId = id;
@@ -155,20 +164,21 @@ public class StreamItemEntry implements Comparable<StreamItemEntry> {
        if (mComments != null) {
            mDecodedComments = HtmlUtils.fromHtml(context, mComments, imageGetter, null);
        }
        mDecoded = true;
    }

    public CharSequence getDecodedText() {
        checkDecoded(mText, mDecodedText);
        checkDecoded();
        return mDecodedText;
    }

    public CharSequence getDecodedComments() {
        checkDecoded(mComments, mDecodedComments);
        checkDecoded();
        return mDecodedComments;
    }

    private static void checkDecoded(CharSequence original, CharSequence decoded) {
        if (original != null && decoded == null) {
    private void checkDecoded() {
        if (!mDecoded) {
            throw new IllegalStateException("decodeHtml must have been called");
        }
    }
+2 −2
Original line number Diff line number Diff line
@@ -63,8 +63,8 @@ public class StreamItemEntryBuilder {
    }

    public StreamItemEntry build(Context context) {
        StreamItemEntry ret = new StreamItemEntry(mId, mText, mComment, mTimestamp, mAccountType,
                mAccountName, mDataSet, mResPackage, mIconRes, mLabelRes);
        StreamItemEntry ret = StreamItemEntry.createForTest(mId, mText, mComment, mTimestamp,
                mAccountType, mAccountName, mDataSet, mResPackage, mIconRes, mLabelRes);
        ret.decodeHtml(context);
        return ret;
    }