org.mariotaku.twidere.adapter.DirectMessagesConversationAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.mariotaku.twidere.adapter.DirectMessagesConversationAdapter.java

Source

/*
 *            Twidere - Twitter client for Android
 * 
 * Copyright (C) 2012 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 static org.mariotaku.twidere.util.Utils.findDirectMessageInDatabases;
import static org.mariotaku.twidere.util.Utils.formatToLongTimeString;
import static org.mariotaku.twidere.util.Utils.openUserProfile;

import org.mariotaku.twidere.R;
import org.mariotaku.twidere.adapter.iface.IDirectMessagesAdapter;
import org.mariotaku.twidere.model.DirectMessageCursorIndices;
import org.mariotaku.twidere.model.ParcelableDirectMessage;
import org.mariotaku.twidere.util.LazyImageLoader;
import org.mariotaku.twidere.util.OnLinkClickHandler;
import org.mariotaku.twidere.util.TwidereLinkify;
import org.mariotaku.twidere.view.holder.DirectMessageConversationViewHolder;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

public class DirectMessagesConversationAdapter extends SimpleCursorAdapter
        implements IDirectMessagesAdapter, OnClickListener {

    private boolean mDisplayProfileImage;
    private final LazyImageLoader mImageLoader;
    private float mTextSize;
    private final Context mContext;
    private DirectMessageCursorIndices mIndices;
    private int mNameDisplayOption;

    public DirectMessagesConversationAdapter(final Context context, final LazyImageLoader loader) {
        super(context, R.layout.direct_message_list_item, null, new String[0], new int[0], 0);
        mContext = context;
        mImageLoader = loader;
    }

    @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 long account_id = cursor.getLong(mIndices.account_id);
        final long message_timestamp = cursor.getLong(mIndices.message_timestamp);
        final boolean is_outgoing = cursor.getInt(mIndices.is_outgoing) == 1;
        final String name = cursor.getString(mIndices.sender_name);
        final String screen_name = cursor.getString(mIndices.sender_screen_name);
        holder.setTextSize(mTextSize);
        switch (mNameDisplayOption) {
        case NAME_DISPLAY_OPTION_CODE_NAME: {
            holder.name.setText(name);
            holder.screen_name.setText(null);
            holder.screen_name.setVisibility(View.GONE);
            break;
        }
        case NAME_DISPLAY_OPTION_CODE_SCREEN_NAME: {
            holder.name.setText("@" + screen_name);
            holder.screen_name.setText(null);
            holder.screen_name.setVisibility(View.GONE);
            break;
        }
        default: {
            holder.name.setText(name);
            holder.screen_name.setText("@" + screen_name);
            holder.screen_name.setVisibility(View.VISIBLE);
            break;
        }
        }
        final FrameLayout.LayoutParams lp = (LayoutParams) holder.name_container.getLayoutParams();
        lp.gravity = is_outgoing ? Gravity.LEFT : Gravity.RIGHT;
        holder.name_container.setLayoutParams(lp);
        holder.text.setText(Html.fromHtml(cursor.getString(mIndices.text)));
        final TwidereLinkify linkify = new TwidereLinkify(holder.text);
        linkify.setOnLinkClickListener(new OnLinkClickHandler(context, account_id));
        linkify.addAllLinks();
        holder.text.setMovementMethod(null);
        holder.text.setGravity(is_outgoing ? Gravity.LEFT : Gravity.RIGHT);
        holder.time.setText(formatToLongTimeString(mContext, message_timestamp));
        holder.time.setGravity(is_outgoing ? Gravity.RIGHT : Gravity.LEFT);
        holder.profile_image_left.setVisibility(mDisplayProfileImage && is_outgoing ? View.VISIBLE : View.GONE);
        holder.profile_image_right.setVisibility(mDisplayProfileImage && !is_outgoing ? View.VISIBLE : View.GONE);
        if (mDisplayProfileImage) {
            final String profile_image_url_string = cursor.getString(mIndices.sender_profile_image_url);
            mImageLoader.displayImage(holder.profile_image_left, profile_image_url_string);
            mImageLoader.displayImage(holder.profile_image_right, profile_image_url_string);
            holder.profile_image_left.setTag(position);
            holder.profile_image_right.setTag(position);
        }

        super.bindView(view, context, cursor);
    }

    @Override
    public ParcelableDirectMessage findItem(final long id) {
        final int count = getCount();
        for (int i = 0; i < count; i++) {
            if (getItemId(i) == id)
                return getDirectMessage(i);
        }
        return null;
    }

    public ParcelableDirectMessage getDirectMessage(final int position) {
        final Cursor item = getItem(position);
        final long account_id = item.getLong(mIndices.account_id);
        final long message_id = item.getLong(mIndices.message_id);
        return findDirectMessageInDatabases(mContext, account_id, message_id);
    }

    @Override
    public Cursor getItem(final int position) {
        return (Cursor) super.getItem(position);
    }

    @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);
            view.setTag(holder);
            holder.profile_image_left.setOnClickListener(this);
            holder.profile_image_right.setOnClickListener(this);
        }
        return view;
    }

    @Override
    public void onClick(final View view) {
        final Object tag = view.getTag();
        final ParcelableDirectMessage status = tag instanceof Integer ? getDirectMessage((Integer) tag) : null;
        if (status == null)
            return;
        switch (view.getId()) {
        case R.id.profile_image_left:
        case R.id.profile_image_right: {
            if (mContext instanceof Activity) {
                openUserProfile((Activity) mContext, status.account_id, status.sender_id,
                        status.sender_screen_name);
            }
            break;
        }
        }
    }

    @Override
    public void setDisplayProfileImage(final boolean display) {
        if (display != mDisplayProfileImage) {
            mDisplayProfileImage = display;
            notifyDataSetChanged();
        }
    }

    @Override
    public void setNameDisplayOption(final String option) {
        if (NAME_DISPLAY_OPTION_NAME.equals(option)) {
            mNameDisplayOption = NAME_DISPLAY_OPTION_CODE_NAME;
        } else if (NAME_DISPLAY_OPTION_SCREEN_NAME.equals(option)) {
            mNameDisplayOption = NAME_DISPLAY_OPTION_CODE_SCREEN_NAME;
        } else {
            mNameDisplayOption = 0;
        }
    }

    @Override
    public void setTextSize(final float text_size) {
        if (text_size != mTextSize) {
            mTextSize = text_size;
            notifyDataSetChanged();
        }
    }

    @Override
    public Cursor swapCursor(final Cursor cursor) {
        if (cursor != null) {
            mIndices = new DirectMessageCursorIndices(cursor);
        } else {
            mIndices = null;
        }
        return super.swapCursor(cursor);
    }
}