Skip to content

Commit

Permalink
show "( ! ) invalid content" when content is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Feb 5, 2020
1 parent e53e1e7 commit 5925133
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 11 deletions.
33 changes: 30 additions & 3 deletions app/src/main/java/co/tinode/tindroid/MessagesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.loader.app.LoaderManager;
Expand All @@ -36,6 +39,8 @@
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.IconMarginSpan;
import android.text.style.StyleSpan;
import android.util.Base64;
import android.util.Log;
Expand Down Expand Up @@ -109,6 +114,8 @@ public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.ViewHo
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

private static Spanned sInvalidContent = null;

private MessageActivity mActivity;
private RecyclerView mRecyclerView;

Expand Down Expand Up @@ -432,8 +439,12 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
(m.content != null && m.content.getEntReferences() != null);

mSpanFormatterClicker.setPosition(position);
holder.mText.setText(SpanFormatter.toSpanned(holder.mText, m.content,
disableEnt ? null : mSpanFormatterClicker));
Spanned text = SpanFormatter.toSpanned(holder.mText, m.content, disableEnt ? null : mSpanFormatterClicker);
if (text.length() == 0) {
text = invalidContentSpanned(mActivity);
}

holder.mText.setText(text);
if (SpanFormatter.hasClickableSpans(m.content)) {
holder.mText.setMovementMethod(LinkMovementMethod.getInstance());
holder.mText.setLinksClickable(true);
Expand Down Expand Up @@ -487,7 +498,7 @@ public void onClick(View v) {
holder.mAvatar.setImageDrawable(
new LetterTileDrawable(mActivity)
.setLetterAndColor(sub.pub.fn, sub.user)
.setContactTypeAndColor(LetterTileDrawable.TYPE_PERSON));
.setContactTypeAndColor(LetterTileDrawable.ContactType.PERSON));
}
}

Expand Down Expand Up @@ -557,6 +568,22 @@ public void onClick(View v) {
});
}

// Generates "( ! ) invalid content" message when Drafty fails to represent content.
private static Spanned invalidContentSpanned(Context ctx) {
if (sInvalidContent != null) {
return sInvalidContent;
}
SpannableString span = new SpannableString(ctx.getString(R.string.invalid_content));
span.setSpan(new StyleSpan(Typeface.ITALIC), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.rgb(0x75, 0x75, 0x75)),
0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Drawable icon = AppCompatResources.getDrawable(ctx, R.drawable.ic_error_gray);
span.setSpan(new IconMarginSpan(UiUtils.bitmapFromDrawable(icon), 24),
0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sInvalidContent = span;
return span;
}

// Must match position-to-item of getItemId.
private StoredMessage getMessage(int position) {
if (mCursor != null && !mCursor.isClosed() && mCursor.moveToPosition(position)) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/co/tinode/tindroid/TopicInfoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,9 @@ private void notifyContentChanged() {
new LetterTileDrawable(requireContext())
.setIsCircular(true)
.setContactTypeAndColor(
mTopic.getTopicType() == Topic.TopicType.P2P ? LetterTileDrawable.TYPE_PERSON :
LetterTileDrawable.TYPE_GROUP)
mTopic.getTopicType() == Topic.TopicType.P2P ?
LetterTileDrawable.ContactType.PERSON :
LetterTileDrawable.ContactType.GROUP)
.setLetterAndColor(pub != null ? pub.fn : null, mTopic.getName()));
}

Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/co/tinode/tindroid/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
Expand Down Expand Up @@ -62,6 +64,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.exifinterface.media.ExifInterface;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
Expand Down Expand Up @@ -614,6 +617,30 @@ static ByteArrayInputStream bitmapToStream(Bitmap bmp, String mimeType) {
return new ByteArrayInputStream(bos.toByteArray());
}

/**
* Convert drawable to bitmap.
*
* @param drawable vector drawable to convert to bitmap
* @return bitmap extracted from the drawable.
*/
static Bitmap bitmapFromDrawable(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

/**
* Identifies the start of the search string (needle) in the display name (haystack).
* E.g. If display name was "Adam" and search query was "da" this would
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
import co.tinode.tindroid.R;
import co.tinode.tindroid.UiUtils;
import co.tinode.tindroid.account.Utils;
import co.tinode.tindroid.db.BaseDb;
import co.tinode.tindroid.media.VxCard;
import co.tinode.tindroid.widgets.LetterTileDrawable;
import co.tinode.tindroid.widgets.RoundImageDrawable;
import co.tinode.tinodesdk.ComTopic;
import co.tinode.tinodesdk.Storage;
import co.tinode.tinodesdk.Tinode;
import co.tinode.tinodesdk.Topic;
import co.tinode.tinodesdk.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ Spanned toSpanned() {
}
}
}
if (cStyle != null || pStyle != null) {
if (spanned.length() > 0 && (cStyle != null || pStyle != null)) {
spanned.setSpan(cStyle != null ? cStyle : pStyle,
0, spanned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public Bitmap getBitmap(final int width, final int height) {
}

/**
* Attempt to create a drawable from the given vector drawable resource id, and the convert drawable
* to bitmap
* Load vector drawable from the given resource id, then convert drawable to bitmap.
*
* @param context context
* @param drawableId vector drawable resource id
* @return bitmap extracted from the drawable.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_error_gray.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#757575"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M11,15h2v2h-2zM11,7h2v6h-2zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/meta_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
tools:text="Lorem ipsum"
tools:ignore="RtlSymmetry"/>

</LinearLayout>
</LinearLayout>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@
<string name="content_deleted">сообщения удалены</string>
<string name="image_caption_hint">Название изображения</string>
<string name="action_download">Сохранить</string>
<string name="invalid_content">сообщение не читается</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,5 @@
<string name="default_contact_name">Tinode用户 %1$s</string>
<string name="image_caption_hint">图片标题</string>
<string name="action_download">保存</string>
<string name="invalid_content">无效内容</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,5 @@
<string name="image_caption_hint">Image caption</string>
<string name="action_download">Download</string>
<string name="tinode_image" translatable="false">tinode_image</string>
<string name="invalid_content">invalid content</string>
</resources>

0 comments on commit 5925133

Please sign in to comment.