2014-07-03 07:48:39 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView.ScaleType;
|
|
|
|
|
|
|
|
import org.mariotaku.twidere.R;
|
|
|
|
import org.mariotaku.twidere.adapter.iface.IDirectMessagesAdapter;
|
|
|
|
import org.mariotaku.twidere.app.TwidereApplication;
|
|
|
|
import org.mariotaku.twidere.model.ParcelableDirectMessage;
|
|
|
|
import org.mariotaku.twidere.util.ImageLoaderWrapper;
|
|
|
|
import org.mariotaku.twidere.util.ImageLoadingHandler;
|
|
|
|
import org.mariotaku.twidere.util.MultiSelectManager;
|
2015-01-03 16:16:50 +01:00
|
|
|
import org.mariotaku.twidere.util.Utils;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.view.holder.DirectMessageConversationViewHolder;
|
|
|
|
|
2014-08-07 19:58:42 +02:00
|
|
|
import static org.mariotaku.twidere.util.Utils.configBaseCardAdapter;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.findDirectMessageInDatabases;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.formatToLongTimeString;
|
2015-01-23 17:09:03 +01:00
|
|
|
import static org.mariotaku.twidere.util.Utils.openMedia;
|
2014-08-07 19:58:42 +02:00
|
|
|
|
2014-07-03 07:48:39 +02:00
|
|
|
public class DirectMessagesConversationAdapter extends BaseCursorAdapter implements IDirectMessagesAdapter,
|
2014-08-07 19:58:42 +02:00
|
|
|
OnClickListener {
|
|
|
|
private ScaleType mImagePreviewScaleType;
|
|
|
|
|
|
|
|
private final ImageLoaderWrapper mImageLoader;
|
|
|
|
private final Context mContext;
|
|
|
|
private final MultiSelectManager mMultiSelectManager;
|
|
|
|
private final ImageLoadingHandler mImageLoadingHandler;
|
|
|
|
|
|
|
|
private ParcelableDirectMessage.CursorIndices mIndices;
|
|
|
|
|
|
|
|
public DirectMessagesConversationAdapter(final Context context) {
|
|
|
|
super(context, R.layout.card_item_message_conversation, null, new String[0], new int[0], 0);
|
|
|
|
mContext = context;
|
|
|
|
final TwidereApplication app = TwidereApplication.getInstance(context);
|
|
|
|
mMultiSelectManager = app.getMultiSelectManager();
|
|
|
|
mImageLoader = app.getImageLoaderWrapper();
|
2014-11-25 16:15:05 +01:00
|
|
|
mImageLoadingHandler = new ImageLoadingHandler(R.id.media_preview_progress);
|
2014-08-07 19:58:42 +02:00
|
|
|
configBaseCardAdapter(context, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bindView(final View view, final Context context, final Cursor cursor) {
|
|
|
|
final int position = cursor.getPosition();
|
|
|
|
final DirectMessageConversationViewHolder holder = (DirectMessageConversationViewHolder) view.getTag();
|
|
|
|
|
|
|
|
final String firstMedia = cursor.getString(mIndices.first_media);
|
|
|
|
|
|
|
|
final long accountId = cursor.getLong(mIndices.account_id);
|
|
|
|
final long timestamp = cursor.getLong(mIndices.message_timestamp);
|
|
|
|
final boolean is_outgoing = cursor.getInt(mIndices.is_outgoing) == 1;
|
|
|
|
|
2014-11-25 16:15:05 +01:00
|
|
|
holder.setOutgoing(is_outgoing);
|
2014-08-07 19:58:42 +02:00
|
|
|
holder.setTextSize(getTextSize());
|
2014-11-25 16:15:05 +01:00
|
|
|
holder.text.setText(Html.fromHtml(cursor.getString(mIndices.text)));
|
|
|
|
getLinkify().applyAllLinks(holder.text, accountId, false);
|
|
|
|
holder.text.setMovementMethod(null);
|
|
|
|
holder.time.setText(formatToLongTimeString(mContext, timestamp));
|
2014-08-07 19:58:42 +02:00
|
|
|
|
|
|
|
if (firstMedia == null) {
|
2014-11-25 16:15:05 +01:00
|
|
|
mImageLoader.cancelDisplayTask(holder.media_preview);
|
|
|
|
holder.media_preview_container.setVisibility(View.GONE);
|
2014-08-07 19:58:42 +02:00
|
|
|
} else {
|
2014-11-25 16:15:05 +01:00
|
|
|
mImageLoader.cancelDisplayTask(holder.media_preview);
|
|
|
|
holder.media_preview_container.setVisibility(View.VISIBLE);
|
2014-08-07 19:58:42 +02:00
|
|
|
if (mImagePreviewScaleType != null) {
|
2014-11-25 16:15:05 +01:00
|
|
|
holder.media_preview.setScaleType(mImagePreviewScaleType);
|
2014-08-07 19:58:42 +02:00
|
|
|
}
|
2014-11-25 16:15:05 +01:00
|
|
|
if (!firstMedia.equals(mImageLoadingHandler.getLoadingUri(holder.media_preview))) {
|
|
|
|
holder.media_preview.setBackgroundResource(0);
|
|
|
|
mImageLoader.displayPreviewImageWithCredentials(holder.media_preview, firstMedia,
|
|
|
|
accountId, mImageLoadingHandler);
|
2014-08-07 19:58:42 +02:00
|
|
|
}
|
2014-11-25 16:15:05 +01:00
|
|
|
holder.media_preview.setTag(position);
|
2014-08-07 19:58:42 +02:00
|
|
|
}
|
|
|
|
super.bindView(view, context, cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ParcelableDirectMessage findItem(final long id) {
|
|
|
|
for (int i = 0, count = getCount(); i < count; i++) {
|
|
|
|
if (getItemId(i) == id) return getDirectMessage(i);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ParcelableDirectMessage getDirectMessage(final int position) {
|
|
|
|
final Cursor c = getCursor();
|
|
|
|
if (c == null || c.isClosed()) return null;
|
|
|
|
c.moveToPosition(position);
|
|
|
|
final long account_id = c.getLong(mIndices.account_id);
|
|
|
|
final long message_id = c.getLong(mIndices.message_id);
|
|
|
|
return findDirectMessageInDatabases(mContext, account_id, message_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View newView(final Context context, final Cursor cursor, final ViewGroup parent) {
|
|
|
|
final View view = super.newView(context, cursor, parent);
|
|
|
|
final Object tag = view.getTag();
|
|
|
|
if (!(tag instanceof DirectMessageConversationViewHolder)) {
|
|
|
|
final DirectMessageConversationViewHolder holder = new DirectMessageConversationViewHolder(view);
|
2014-11-25 16:15:05 +01:00
|
|
|
holder.media_preview.setOnClickListener(this);
|
2014-08-07 19:58:42 +02:00
|
|
|
view.setTag(holder);
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(final View view) {
|
|
|
|
if (mMultiSelectManager.isActive()) return;
|
|
|
|
final Object tag = view.getTag();
|
|
|
|
final int position = tag instanceof Integer ? (Integer) tag : -1;
|
|
|
|
if (position == -1) return;
|
|
|
|
switch (view.getId()) {
|
2014-11-25 16:15:05 +01:00
|
|
|
case R.id.media_preview: {
|
2014-08-07 19:58:42 +02:00
|
|
|
final ParcelableDirectMessage message = getDirectMessage(position);
|
2015-01-23 17:09:03 +01:00
|
|
|
if (message == null || message.media == null) return;
|
|
|
|
openMedia(mContext, message.account_id, false, null, message.media);
|
2014-08-07 19:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDisplayImagePreview(final boolean display) {
|
|
|
|
// Images in DM are always enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setImagePreviewScaleType(final String scaleTypeString) {
|
2015-01-03 16:16:50 +01:00
|
|
|
final ScaleType scaleType;
|
|
|
|
switch (Utils.getMediaPreviewStyle(scaleTypeString)) {
|
|
|
|
case VALUE_MEDIA_PREVIEW_STYLE_CODE_CROP: {
|
|
|
|
scaleType = ScaleType.CENTER_CROP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VALUE_MEDIA_PREVIEW_STYLE_CODE_SCALE: {
|
|
|
|
scaleType = ScaleType.CENTER_INSIDE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 19:58:42 +02:00
|
|
|
mImagePreviewScaleType = scaleType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor swapCursor(final Cursor cursor) {
|
|
|
|
if (cursor != null) {
|
|
|
|
mIndices = new ParcelableDirectMessage.CursorIndices(cursor);
|
|
|
|
} else {
|
|
|
|
mIndices = null;
|
|
|
|
}
|
|
|
|
return super.swapCursor(cursor);
|
|
|
|
}
|
2014-07-03 07:48:39 +02:00
|
|
|
}
|