mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Show preview for image links
Major credit to dethstar for starting this and getting the image loading set up. https://github.com/dethstar/qksms/commit/92ce8517580c0dd9c1c2454f0798b16 9677669c8
This commit is contained in:
@@ -6,6 +6,7 @@ import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.Telephony;
|
||||
@@ -26,13 +27,16 @@ import com.android.mms.transaction.TransactionService;
|
||||
import com.android.mms.util.DownloadManager;
|
||||
import com.google.android.mms.ContentType;
|
||||
import com.google.android.mms.pdu_alt.PduHeaders;
|
||||
import com.koushikdutta.ion.Ion;
|
||||
import com.moez.QKSMS.QKSMSApp;
|
||||
import com.moez.QKSMS.R;
|
||||
import com.moez.QKSMS.common.LiveViewManager;
|
||||
import com.moez.QKSMS.common.emoji.EmojiRegistry;
|
||||
import com.moez.QKSMS.common.utils.CursorUtils;
|
||||
import com.moez.QKSMS.common.utils.LinkifyUtils;
|
||||
import com.moez.QKSMS.common.utils.MessageUtils;
|
||||
import com.moez.QKSMS.data.Contact;
|
||||
import com.moez.QKSMS.enums.QKPreference;
|
||||
import com.moez.QKSMS.transaction.SmsHelper;
|
||||
import com.moez.QKSMS.ui.ThemeManager;
|
||||
import com.moez.QKSMS.ui.base.QKActivity;
|
||||
@@ -50,13 +54,15 @@ import java.util.regex.Pattern;
|
||||
public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHolder, MessageItem> {
|
||||
private final String TAG = "MessageListAdapter";
|
||||
|
||||
public static final int INCOMING_ITEM_TYPE_SMS = 0;
|
||||
public static final int OUTGOING_ITEM_TYPE_SMS = 1;
|
||||
public static final int INCOMING_ITEM_TYPE_MMS = 2;
|
||||
public static final int OUTGOING_ITEM_TYPE_MMS = 3;
|
||||
public static final int INCOMING_ITEM = 0;
|
||||
public static final int OUTGOING_ITEM = 1;
|
||||
|
||||
private ArrayList<Long> mSelectedConversations = new ArrayList<>();
|
||||
|
||||
private static final Pattern urlPattern = Pattern.compile(
|
||||
"\\b(https?:\\/\\/\\S+(?:png|jpe?g|gif)\\S*)\\b",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
|
||||
|
||||
private MessageItemCache mMessageItemCache;
|
||||
private MessageColumns.ColumnsMap mColumnsMap;
|
||||
|
||||
@@ -114,7 +120,7 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
|
||||
int resource;
|
||||
boolean sent;
|
||||
|
||||
if (viewType == INCOMING_ITEM_TYPE_SMS || viewType == INCOMING_ITEM_TYPE_MMS) {
|
||||
if (viewType == INCOMING_ITEM) {
|
||||
resource = R.layout.list_item_message_in;
|
||||
sent = false;
|
||||
} else {
|
||||
@@ -158,6 +164,10 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
|
||||
}
|
||||
}
|
||||
|
||||
LiveViewManager.registerView(QKPreference.BACKGROUND, this, key -> {
|
||||
holder.mMmsView.getForeground().setColorFilter(ThemeManager.getBackgroundColor(), PorterDuff.Mode.SRC_ATOP);
|
||||
});
|
||||
|
||||
return holder;
|
||||
}
|
||||
|
||||
@@ -183,13 +193,13 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
|
||||
boolean pduLoaded = messageItem.isSms() || messageItem.mSlideshow != null;
|
||||
|
||||
bindGrouping(holder, messageItem);
|
||||
bindBody(holder, messageItem);
|
||||
bindTimestamp(holder, messageItem);
|
||||
|
||||
if (pduLoaded) {
|
||||
bindAvatar(holder, messageItem);
|
||||
}
|
||||
bindMmsView(holder, messageItem);
|
||||
bindBody(holder, messageItem);
|
||||
bindIndicators(holder, messageItem);
|
||||
bindVcard(holder, messageItem);
|
||||
|
||||
@@ -360,6 +370,23 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
|
||||
|
||||
if (!TextUtils.isEmpty(buf)) {
|
||||
holder.mBodyTextView.setText(buf);
|
||||
Matcher matcher = urlPattern.matcher(holder.mBodyTextView.getText());
|
||||
if (matcher.find()) { //only find the image to the first link
|
||||
int matchStart = matcher.start(1);
|
||||
int matchEnd = matcher.end();
|
||||
String imageUrl = buf.subSequence(matchStart, matchEnd).toString();
|
||||
Ion.with(mContext).load(imageUrl).withBitmap().asBitmap().setCallback((e, result) -> {
|
||||
try {
|
||||
holder.setImage("url_img" + holder.getItemId(), result);
|
||||
holder.mImageView.setOnClickListener(v -> {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(imageUrl));
|
||||
mContext.startActivity(i);
|
||||
});
|
||||
} catch (NullPointerException imageException) {
|
||||
imageException.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
LinkifyUtils.addLinks(holder.mBodyTextView);
|
||||
}
|
||||
holder.mBodyTextView.setVisibility(TextUtils.isEmpty(buf) ? View.GONE : View.VISIBLE);
|
||||
@@ -552,15 +579,15 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
|
||||
|
||||
if (item.isSms()) {
|
||||
if (boxId == TextBasedSmsColumns.MESSAGE_TYPE_INBOX || boxId == TextBasedSmsColumns.MESSAGE_TYPE_ALL) {
|
||||
return INCOMING_ITEM_TYPE_SMS;
|
||||
return INCOMING_ITEM;
|
||||
} else {
|
||||
return OUTGOING_ITEM_TYPE_SMS;
|
||||
return OUTGOING_ITEM;
|
||||
}
|
||||
} else {
|
||||
if (boxId == Telephony.Mms.MESSAGE_BOX_ALL || boxId == Telephony.Mms.MESSAGE_BOX_INBOX) {
|
||||
return INCOMING_ITEM_TYPE_MMS;
|
||||
return INCOMING_ITEM;
|
||||
} else {
|
||||
return OUTGOING_ITEM_TYPE_MMS;
|
||||
return OUTGOING_ITEM;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
@@ -36,9 +37,7 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
|
||||
protected AvatarView mAvatarView;
|
||||
protected LinearLayout mMessageBlock;
|
||||
protected View mSpace;
|
||||
// MMS only views. These are nested within ViewStubs, so we don't need to obtain a reference
|
||||
// to them right away. We'll inflate the stub only when needed
|
||||
protected View mMmsView;
|
||||
protected FrameLayout mMmsView;
|
||||
protected ImageView mImageView;
|
||||
protected ImageButton mSlideShowButton;
|
||||
protected Button mDownloadButton;
|
||||
@@ -59,30 +58,14 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
|
||||
mAvatarView = (AvatarView) view.findViewById(R.id.avatar);
|
||||
mMessageBlock = (LinearLayout) view.findViewById(R.id.message_block);
|
||||
mSpace = view.findViewById(R.id.space);
|
||||
mMmsView = (FrameLayout) view.findViewById(R.id.mms_view);
|
||||
mImageView = (ImageView) view.findViewById(R.id.image_view);
|
||||
mSlideShowButton = (ImageButton) view.findViewById(R.id.play_slideshow_button);
|
||||
}
|
||||
|
||||
protected void showMmsView(boolean visible) {
|
||||
if (mMmsView == null) {
|
||||
mMmsView = mRoot.findViewById(R.id.mms_view);
|
||||
// if mMmsView is still null here, that mean the mms section hasn't been inflated
|
||||
|
||||
if (visible && mMmsView == null) {
|
||||
// inflate the mms view_stub
|
||||
View mmsStub = mRoot.findViewById(R.id.mms_layout_view_stub);
|
||||
mmsStub.setVisibility(View.VISIBLE);
|
||||
mMmsView = mRoot.findViewById(R.id.mms_view);
|
||||
}
|
||||
}
|
||||
if (mMmsView != null) {
|
||||
if (mImageView == null) {
|
||||
mImageView = (ImageView) mRoot.findViewById(R.id.image_view);
|
||||
}
|
||||
if (mSlideShowButton == null) {
|
||||
mSlideShowButton = (ImageButton) mRoot.findViewById(R.id.play_slideshow_button);
|
||||
}
|
||||
mMmsView.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
mImageView.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
mMmsView.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
mImageView.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
protected void inflateDownloadControls() {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.moez.QKSMS.ui.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageView;
|
||||
import com.moez.QKSMS.common.utils.ImageUtils;
|
||||
|
||||
public class MmsImageView extends ImageView {
|
||||
private final String TAG = "MmsImageView";
|
||||
|
||||
public MmsImageView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public MmsImageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImageDrawable(Drawable drawable) {
|
||||
if (drawable != null) {
|
||||
int width = getWidth();
|
||||
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
|
||||
|
||||
if (bitmap != null) {
|
||||
float ratio = (float) width / (float) bitmap.getWidth();
|
||||
int height = (int) (bitmap.getHeight() * ratio);
|
||||
|
||||
if (width > 0 && height > 0) {
|
||||
super.setImageDrawable(ImageUtils.getRoundedDrawable(getContext(), Bitmap.createScaledBitmap(bitmap, width, height, true), 4));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
super.setImageDrawable(drawable);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user