Example usage for android.graphics.drawable GradientDrawable setCornerRadius

List of usage examples for android.graphics.drawable GradientDrawable setCornerRadius

Introduction

In this page you can find the example usage for android.graphics.drawable GradientDrawable setCornerRadius.

Prototype

public void setCornerRadius(float radius) 

Source Link

Document

Specifies the radius for the corners of the gradient.

Usage

From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    GradientDrawable gradientDrawable = new GradientDrawable();
    inflateGradientRootElement(context, attrs, gradientDrawable);

    int type;//from   w ww.ja  va2s.c om
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        if (depth > innerDepth) {
            continue;
        }

        String name = parser.getName();

        if (name.equals("size")) {
            final int width = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final int height = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.height);
            gradientDrawable.setSize(width, height);
        } else if (name.equals("gradient")
                && Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            final float centerX = getAttrFloatOrFraction(context, attrs, android.R.attr.centerX, 0.5f, 1.0f,
                    1.0f);
            final float centerY = getAttrFloatOrFraction(context, attrs, android.R.attr.centerY, 0.5f, 1.0f,
                    1.0f);
            gradientDrawable.setGradientCenter(centerX, centerY);
            final boolean useLevel = DrawableUtils.getAttrBoolean(context, attrs, android.R.attr.useLevel,
                    false);
            gradientDrawable.setUseLevel(useLevel);
            final int gradientType = DrawableUtils.getAttrInt(context, attrs, android.R.attr.type, 0);
            gradientDrawable.setGradientType(gradientType);
            final int startColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.startColor,
                    Color.TRANSPARENT);
            final int centerColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.centerColor,
                    Color.TRANSPARENT);
            final int endColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.endColor,
                    Color.TRANSPARENT);
            if (!DrawableUtils.getAttrHasValue(context, attrs, android.R.attr.centerColor)) {
                gradientDrawable.setColors(new int[] { startColor, endColor });
            } else {
                gradientDrawable.setColors(new int[] { startColor, centerColor, endColor });
                setStGradientPositions(gradientDrawable.getConstantState(), 0.0f,
                        centerX != 0.5f ? centerX : centerY, 1f);
            }

            if (gradientType == GradientDrawable.LINEAR_GRADIENT) {
                int angle = (int) DrawableUtils.getAttrFloat(context, attrs, android.R.attr.angle, 0.0f);
                angle %= 360;

                if (angle % 45 != 0) {
                    throw new XmlPullParserException(
                            "<gradient> tag requires" + "'angle' attribute to " + "be a multiple of 45");
                }

                setStGradientAngle(gradientDrawable.getConstantState(), angle);

                switch (angle) {
                case 0:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                    break;
                case 45:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                    break;
                case 90:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                    break;
                case 135:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                    break;
                case 180:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                    break;
                case 225:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                    break;
                case 270:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                    break;
                case 315:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                    break;
                }
            } else {
                setGradientRadius(context, attrs, gradientDrawable, gradientType);
            }
        } else if (name.equals("solid")) {
            int color = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            gradientDrawable.setColor(getAlphaColor(color,
                    DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f)));
        } else if (name.equals("stroke")) {
            final float alphaMod = DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f);
            final int strokeColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color,
                    Color.TRANSPARENT);
            final int strokeWidth = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.width);
            final float dashWidth = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashWidth);
            if (dashWidth != 0.0f) {
                final float dashGap = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashGap);
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod), dashWidth,
                        dashGap);
            } else {
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod));
            }
        } else if (name.equals("corners")) {
            final int radius = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.radius);
            gradientDrawable.setCornerRadius(radius);

            final int topLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topLeftRadius, radius);
            final int topRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topRightRadius, radius);
            final int bottomLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomLeftRadius, radius);
            final int bottomRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius
                    || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                gradientDrawable.setCornerRadii(
                        new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
        } else if (name.equals("padding")) {
            final int paddingLeft = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.left);
            final int paddingTop = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.top);
            final int paddingRight = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.right);
            final int paddingBottom = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.bottom);
            if (paddingLeft != 0 || paddingTop != 0 || paddingRight != 0 || paddingBottom != 0) {
                final Rect pad = new Rect();
                pad.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
                try {
                    if (sPaddingField == null) {
                        sPaddingField = GradientDrawable.class.getDeclaredField("mPadding");
                        sPaddingField.setAccessible(true);
                    }
                    sPaddingField.set(gradientDrawable, pad);
                    if (sStPaddingField == null) {
                        sStPaddingField = Class
                                .forName("android.graphics.drawable.GradientDrawable$GradientState")
                                .getDeclaredField("mPadding");
                        sStPaddingField.setAccessible(true);
                    }
                    sStPaddingField.set(gradientDrawable.getConstantState(), pad);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    return gradientDrawable;
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajeLocation(int position, Context context, final Message message, View rowView,
        TextView icnMessageArrow) {// w  w w  . j  a va  2s.  com
    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    final ImageView messageContent = (ImageView) rowView.findViewById(R.id.message_content);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);
    TextView messageLocationText = (TextView) rowView.findViewById(R.id.message_location_text);
    TextView messageLocationIcon = (TextView) rowView.findViewById(R.id.message_location_icon);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageLocationIcon, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageLocationText, TFCache.TF_WHITNEY_MEDIUM);

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }
    double latitud = 0;
    double longitud = 0;
    try {
        JSONObject locationData = new JSONObject(message.mensaje);
        messageLocationText.setText(locationData.getString("direccion"));
        latitud = Double.parseDouble(locationData.getString("latitud"));
        longitud = Double.parseDouble(locationData.getString("longitud"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {
        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);

    if (message.emisor.equals(u.id))

    {
        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            JSONObject data = new JSONObject();
            try {
                data.put("message_id", message.mensajeId);
                data.put("source", message.emisor);
                data.put("source_email", message.emisorEmail);
                data.put("target_email", message.receptorEmail);
                data.put("target", message.receptor);
                data.put("message", message.mensaje);
                data.put("source_lang", message.emisorLang);
                data.put("delay", message.delay);
                data.put("type", message.tipo);
            } catch (JSONException e) {
                Log.w("--->", e.toString());
                e.printStackTrace();
            }
            if (SpeakSocket.mSocket != null)
                if (SpeakSocket.mSocket.connected()) {
                    message.status = 1;
                    SpeakSocket.mSocket.emit("message", data);
                } else {
                    message.status = -1;
                }
        }
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

    } else

    {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        drawable.setColor(BalloonFragment.getFriendBalloon(activity));
        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_PHOTO))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        //messageContent.setText(message.mensajeTrad);
        icnMesajeTempo.setVisibility(View.GONE);
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                icnMesajeCopy.setVisibility(View.VISIBLE);
                icnMesajeBack.setVisibility(View.VISIBLE);
                messageMenu.setVisibility(View.VISIBLE);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                        ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                set.setDuration(200).start();
                return false;
            }
        });
    }

    icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject notifyMessage = new JSONObject();
            try {
                Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                        .executeSingle();
                if (mensaje.status != 4) {
                    notifyMessage.put("type", mensaje.tipo);
                    notifyMessage.put("message_id", mensaje.mensajeId);
                    notifyMessage.put("source", mensaje.receptor);
                    notifyMessage.put("status", 5);
                    SpeakSocket.mSocket.emit("message-status", notifyMessage);
                }
                new Delete().from(Message.class).where("mensajeId = ?", mensaje.mensajeId).execute();
                Message message = null;
                if (mensaje.emisor.equals(u.id)) {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.receptor, mensaje.receptor)
                            .orderBy("emitedAt DESC").executeSingle();
                } else {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.emisor, mensaje.emisor)
                            .orderBy("emitedAt DESC").executeSingle();
                }
                if (message != null) {
                    Chats chat = null;
                    if (mensaje.emisor.equals(u.id)) {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.receptor)
                                .executeSingle();
                    } else {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.emisor)
                                .executeSingle();
                    }
                    if (chat != null) {
                        chat.mensajeId = message.mensajeId;
                        chat.lastStatus = message.status;
                        chat.emitedAt = message.emitedAt;
                        chat.lang = message.emisorLang;
                        chat.notRead = 0;
                        if (chat.idContacto.equals(message.emisor))
                            chat.lastMessage = message.mensajeTrad;
                        else
                            chat.lastMessage = message.mensaje;
                        chat.save();
                    }
                } else {
                    if (mensaje.emisor.equals(u.id)) {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.receptor).execute();
                    } else {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.emisor).execute();
                    }
                }

                View delete = messagesList.findViewWithTag(mensaje.mensajeId);
                if (delete != null)
                    messagesList.removeView(delete);
                messageSelected = null;
                ((MainActivity) activity).setOnBackPressedListener(null);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    );

    icnMesajeCopy.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            /*ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
            ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();*/
        }
    }

    );

    icnMesajeBack.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    }

    );

    final double finalLatitud = latitud;
    final double finalLongitud = longitud;
    rowView.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            } else {
                if (mensaje.status != -1) {
                    dontClose = true;
                    ((MainActivity) activity).dontClose = true;
                    hideKeyBoard();
                    Intent i = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("geo:" + finalLatitud + "," + finalLongitud));
                    i.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                    startActivity(i);
                }
            }
            if (mensaje.status == -1) {
                JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje.mensajeId);
                    data.put("source", mensaje.emisor);
                    data.put("source_email", mensaje.emisorEmail);
                    data.put("target_email", mensaje.receptorEmail);
                    data.put("target", mensaje.receptor);
                    data.put("message", message.mensaje);
                    data.put("source_lang", mensaje.emisorLang);
                    data.put("delay", mensaje.delay);
                    data.put("type", mensaje.tipo);
                } catch (JSONException e) {
                    Log.w("--->", e.toString());
                    e.printStackTrace();
                }
                if (SpeakSocket.mSocket != null)
                    if (SpeakSocket.mSocket.connected()) {
                        mensaje.status = 1;
                        SpeakSocket.mSocket.emit("message", data);
                    } else {
                        mensaje.status = -1;
                    }
                changeStatus(mensaje.mensajeId, mensaje.status);
            }
        }
    }

    );

}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajeContact(int position, final Context context, final Message message, View rowView,
        TextView icnMessageArrow) {/*w  w  w .  ja v a2 s .c  om*/

    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);
    TextView contactPhoto = (TextView) rowView.findViewById(R.id.contact_photo);
    TextView contactName = (TextView) rowView.findViewById(R.id.contact_name);
    final TextView contactPhone = (TextView) rowView.findViewById(R.id.contact_phone);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, contactPhoto, TFCache.TF_SPEAKALL);
    TFCache.apply(context, prevMessages, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, contactName, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, contactPhone, TFCache.TF_WHITNEY_MEDIUM);

    try {
        JSONObject contactData = new JSONObject(message.mensaje);
        contactName.setText(contactData.getString("nombre"));
        contactPhone.setText(Html.fromHtml("<u>" + contactData.getString("telefono") + "</u>"));
        if (contactPhone.length() > 0) {
            contactPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dontClose = true;
                    ((MainActivity) activity).dontClose = true;
                    hideKeyBoard();
                    Intent intent = new Intent(Intent.ACTION_CALL,
                            Uri.parse("tel:" + contactPhone.getText().toString()));
                    context.startActivity(intent);
                }
            });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {
        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);

    if (message.emisor.equals(u.id)) {
        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            JSONObject data = new JSONObject();
            try {
                data.put("message_id", message.mensajeId);
                data.put("source", message.emisor);
                data.put("source_email", message.emisorEmail);
                data.put("target_email", message.receptorEmail);
                data.put("target", message.receptor);
                data.put("message", message.mensaje);
                data.put("delay", message.delay);
                data.put("translation_required", message.translation);
                data.put("target_lang", message.receptorLang);
                data.put("type", message.tipo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (SpeakSocket.mSocket != null)
                if (SpeakSocket.mSocket.connected()) {
                    message.status = 1;
                    SpeakSocket.mSocket.emit("message", data);
                } else {
                    message.status = -1;
                }
        }
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

        icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject notifyMessage = new JSONObject();
                try {
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();

                    if (mensaje.status != 4) {
                        notifyMessage.put("type", mensaje.tipo);
                        notifyMessage.put("message_id", mensaje.mensajeId);
                        notifyMessage.put("source", mensaje.receptor);
                        notifyMessage.put("target", mensaje.emisor);
                        notifyMessage.put("status", 5);
                        SpeakSocket.mSocket.emit("message-status", notifyMessage);
                    }
                    new Delete().from(Message.class).where("mensajeId = ?", mensaje.mensajeId).execute();
                    Message message = null;
                    if (mensaje.emisor.equals(u.id)) {
                        message = new Select().from(Message.class)
                                .where("emisor = ? or receptor = ?", mensaje.receptor, mensaje.receptor)
                                .orderBy("emitedAt DESC").executeSingle();
                    } else {
                        message = new Select().from(Message.class)
                                .where("emisor = ? or receptor = ?", mensaje.emisor, mensaje.emisor)
                                .orderBy("emitedAt DESC").executeSingle();
                    }
                    if (message != null) {
                        Chats chat = null;
                        if (mensaje.emisor.equals(u.id)) {
                            chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.receptor)
                                    .executeSingle();
                        } else {
                            chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.emisor)
                                    .executeSingle();
                        }
                        if (chat != null) {
                            chat.mensajeId = message.mensajeId;
                            chat.lastStatus = message.status;
                            chat.emitedAt = message.emitedAt;
                            chat.lang = message.emisorLang;
                            chat.notRead = 0;
                            if (chat.idContacto.equals(message.emisor))
                                chat.lastMessage = message.mensajeTrad;
                            else
                                chat.lastMessage = message.mensaje;
                            chat.save();
                        }
                    } else {
                        if (mensaje.emisor.equals(u.id)) {
                            new Delete().from(Chats.class).where("idContacto = ?", mensaje.receptor).execute();
                        } else {
                            new Delete().from(Chats.class).where("idContacto = ?", mensaje.emisor).execute();
                        }
                    }

                    View delete = messagesList.findViewWithTag(mensaje.mensajeId);
                    if (delete != null)
                        messagesList.removeView(delete);
                    messageSelected = null;
                    ((MainActivity) activity).setOnBackPressedListener(null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    } else {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        drawable.setColor(BalloonFragment.getFriendBalloon(activity));
        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_TEXT))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    longclick = true;
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                icnMesajeTempo.setVisibility(View.GONE);
                icnMensajeTempoDivider.setVisibility(View.GONE);
                icnMesajeCopy.setVisibility(View.VISIBLE);
                icnMesajeBack.setVisibility(View.VISIBLE);
                messageMenu.setVisibility(View.VISIBLE);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                        ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                set.setDuration(200).start();
                longclick = true;
                return false;
            }
        });
        icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
            boolean translation = true;

            @Override
            public void onClick(View v) {
                translation = !translation;
                if (translation) {
                    icnMesajeTempo.setTextColor(getResources().getColor(R.color.speak_all_red));
                } else {
                    icnMesajeTempo.setTextColor(getResources().getColor(R.color.speak_all_white));
                }
            }
        });
    }

    /*icnMesajeCopy.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
          ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
          clipboard.setPrimaryClip(clip);
          Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();
      }
    });*/

    icnMesajeBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    });

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    longclick = true;
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            }
            Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (mensaje.status == -1) {
                String message = mensaje.mensaje;
                JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje.mensajeId);
                    data.put("source", mensaje.emisor);
                    data.put("source_email", mensaje.emisorEmail);
                    data.put("target_email", mensaje.receptorEmail);
                    data.put("target", mensaje.receptor);
                    data.put("message", mensaje.mensaje);
                    data.put("delay", mensaje.delay);
                    data.put("type", mensaje.tipo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (SpeakSocket.mSocket != null)
                    if (SpeakSocket.mSocket.connected()) {
                        mensaje.status = 1;
                        SpeakSocket.mSocket.emit("message", data);
                    } else {
                        mensaje.status = -1;
                    }
                mensaje.save();
                changeStatus(mensaje.mensajeId, mensaje.status);
            }
            if (!mensaje.emisor.equals(u.id)) {
                if (!longclick) {
                    showDialogAddContact(mensaje.mensaje);
                }
                longclick = false;
            }
        }
    });
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajePhoto(int position, Context context, final Message message, View rowView,
        TextView icnMessageArrow) {//from  w  w  w  . j a v  a  2  s  .c  o m
    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    final ImageView messageContent = (ImageView) rowView.findViewById(R.id.message_content);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);
    final ProgressBar progressBar = (ProgressBar) rowView.findViewById(R.id.progress_bar);
    final RelativeLayout progressLayout = (RelativeLayout) rowView.findViewById(R.id.progress_layout);
    final TextView progressText = (TextView) rowView.findViewById(R.id.progress_message);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, prevMessages, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {
        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);
    if (message.emisor.equals(u.id)) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (message.fileUploaded) {
                            if (message.photo != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photo, 0,
                                        message.photo.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        } else {
                            if (message.photoBlur != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photoBlur, 0,
                                        message.photoBlur.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        }
                    }
                });
            }
        }).start();
        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            try {
                final JSONObject mensaje = new JSONObject();
                mensaje.put("message_id", message.mensajeId);
                mensaje.put("source", message.emisor);
                mensaje.put("source_email", message.emisorEmail);
                mensaje.put("target_email", message.receptorEmail);
                mensaje.put("target", message.receptor);
                mensaje.put("type", message.tipo);
                mensaje.put("delay", message.delay);
                if (!message.fileUploaded) {
                    progressLayout.setVisibility(View.VISIBLE);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    uploadPhotoToServer(message.photoName, message, mensaje, progressBar,
                                            progressLayout, progressText, messageContent);
                                }
                            });
                        }
                    }).start();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

    } else {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        drawable.setColor(BalloonFragment.getFriendBalloon(activity));
        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_PHOTO))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        File archivo = new File(message.photoName);
                        if (archivo.exists()) {
                            if (message.photo != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photo, 0,
                                        message.photo.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        } else {
                            if (message.photoBlur != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photoBlur, 0,
                                        message.photoBlur.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        }
                    }
                });
            }
        }).start();
        //messageContent.setText(message.mensajeTrad);
        icnMesajeTempo.setVisibility(View.GONE);
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                icnMesajeCopy.setVisibility(View.VISIBLE);
                icnMesajeBack.setVisibility(View.VISIBLE);
                messageMenu.setVisibility(View.VISIBLE);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                        ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                set.setDuration(200).start();
                return false;
            }
        });
    }

    icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject notifyMessage = new JSONObject();
            try {
                Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                        .executeSingle();
                if (mensaje.status != 4) {
                    notifyMessage.put("type", mensaje.tipo);
                    notifyMessage.put("message_id", mensaje.mensajeId);
                    notifyMessage.put("source", mensaje.receptor);
                    notifyMessage.put("status", 5);
                    SpeakSocket.mSocket.emit("message-status", notifyMessage);
                }
                new Delete().from(Message.class).where("mensajeId = ?", mensaje.mensajeId).execute();
                Message message = null;
                if (mensaje.emisor.equals(u.id)) {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.receptor, mensaje.receptor)
                            .orderBy("emitedAt DESC").executeSingle();
                } else {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.emisor, mensaje.emisor)
                            .orderBy("emitedAt DESC").executeSingle();
                }
                if (message != null) {
                    Chats chat = null;
                    if (mensaje.emisor.equals(u.id)) {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.receptor)
                                .executeSingle();
                    } else {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.emisor)
                                .executeSingle();
                    }
                    if (chat != null) {
                        chat.mensajeId = message.mensajeId;
                        chat.lastStatus = message.status;
                        chat.emitedAt = message.emitedAt;
                        chat.lang = message.emisorLang;
                        chat.notRead = 0;
                        if (chat.idContacto.equals(message.emisor))
                            chat.lastMessage = message.mensajeTrad;
                        else
                            chat.lastMessage = message.mensaje;
                        chat.save();
                    }
                } else {
                    if (mensaje.emisor.equals(u.id)) {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.receptor).execute();
                    } else {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.emisor).execute();
                    }
                }

                View delete = messagesList.findViewWithTag(mensaje.mensajeId);
                if (delete != null)
                    messagesList.removeView(delete);
                messageSelected = null;
                ((MainActivity) activity).setOnBackPressedListener(null);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    );

    icnMesajeCopy.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            /*ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
            ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();*/
        }
    }

    );

    icnMesajeBack.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    }

    );

    rowView.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            } else {
                if (mensaje.fileDownloaded || mensaje.fileUploaded) {
                    if (mensaje.status != -1) {
                        dontClose = true;
                        ((MainActivity) activity).dontClose = true;
                        hideKeyBoard();
                        Intent i = new Intent();
                        i.setAction(android.content.Intent.ACTION_VIEW);
                        File videoFile2Play2 = new File(message.photoName);
                        i.setDataAndType(Uri.fromFile(videoFile2Play2), "image/*");
                        startActivity(i);
                    }
                }
            }
            if (mensaje.status == -1) {
                String message = mensaje.mensaje;
                JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje.mensajeId);
                    data.put("source", mensaje.emisor);
                    data.put("source_email", mensaje.emisorEmail);
                    data.put("target_email", mensaje.receptorEmail);
                    data.put("target", mensaje.receptor);
                    data.put("message", message);
                    data.put("source_lang", mensaje.emisorLang);
                    data.put("delay", mensaje.delay);
                    data.put("type", mensaje.tipo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (SpeakSocket.mSocket != null)
                    if (SpeakSocket.mSocket.connected()) {
                        mensaje.status = 1;
                        SpeakSocket.mSocket.emit("message", data);
                    } else {
                        mensaje.status = -1;
                    }
                changeStatus(mensaje.mensajeId, mensaje.status);
            }
        }
    }

    );

}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajeText(int position, Context context, final Message message, View rowView,
        TextView icnMessageArrow) {//from w w w  .  j a va 2  s  .c  om

    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    final TextView messageContent = (TextView) rowView.findViewById(R.id.message_content);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageContent, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, prevMessages, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);

    SharedPreferences pref = getActivity().getSharedPreferences(Finder.STRING.APP_PREF.toString(),
            Context.MODE_PRIVATE);

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {

        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);

    if (message.emisor.equals(u.id)) {

        messageContent.setText(message.mensaje);

        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                        set.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {

                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                        set.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {

                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            JSONObject data = new JSONObject();
            try {
                data.put("message_id", message.mensajeId);
                data.put("source", message.emisor);
                data.put("source_email", message.emisorEmail);
                data.put("target_email", message.receptorEmail);
                data.put("target", message.receptor);
                data.put("message", message.mensaje);
                data.put("source_lang", message.emisorLang);
                data.put("delay", message.delay);
                data.put("translation_required", message.translation);
                data.put("target_lang", message.receptorLang);
                data.put("type", message.tipo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (SpeakSocket.mSocket != null)
                if (SpeakSocket.mSocket.connected()) {
                    message.status = 1;
                    SpeakSocket.mSocket.emit("message", data);
                } else {
                    message.status = -1;
                }
        }
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

        icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject notifyMessage = new JSONObject();
                try {
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    Log.w("mensaje detail",
                            mensaje.status + " : " + mensaje.receptorEmail + " : " + mensaje.tipo);
                    if (mensaje.status != 4) {
                        notifyMessage.put("type", mensaje.tipo);
                        notifyMessage.put("message_id", mensaje.mensajeId);
                        notifyMessage.put("source", mensaje.emisor);
                        notifyMessage.put("target", mensaje.receptor);
                        notifyMessage.put("status", 5);
                        if (SpeakSocket.mSocket != null)
                            if (SpeakSocket.mSocket.connected())
                                SpeakSocket.mSocket.emit("message-status", notifyMessage);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    } else {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        if (message.tipo == 21)
            drawable.setColor(getResources().getColor(R.color.speakall_autoresponse));
        else
            drawable.setColor(BalloonFragment.getFriendBalloon(activity));

        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_TEXT))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        if (message.tipo == 21) {
            messageStatus.setTextSize(15);
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_white));
            TFCache.apply(activity, messageStatus, TFCache.TF_SPEAKON);
            messageStatus.setText(getResources().getString(R.string.icon_SpeakAll_rayo));
        }
        messageContent.setText(message.mensajeTrad);
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                if (!message.emisorLang.equals(message.receptorLang)) {
                    icnMesajeTempo.setVisibility(View.VISIBLE);
                    icnMesajeCopy.setVisibility(View.VISIBLE);
                    icnMesajeBack.setVisibility(View.VISIBLE);
                    messageMenu.setVisibility(View.VISIBLE);
                    AnimatorSet set = new AnimatorSet();
                    set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", -100, 0),
                            ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                            ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                    set.setDuration(200).start();
                    set.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {

                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                } else {
                    icnMesajeTempo.setVisibility(View.GONE);
                    icnMensajeTempoDivider.setVisibility(View.GONE);
                    icnMesajeCopy.setVisibility(View.VISIBLE);
                    icnMesajeBack.setVisibility(View.VISIBLE);
                    messageMenu.setVisibility(View.VISIBLE);
                    AnimatorSet set = new AnimatorSet();
                    set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                            ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                            ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                    set.setDuration(200).start();
                    set.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {

                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                }
                return false;
            }
        });
        icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
            boolean translation = true;

            @Override
            public void onClick(View v) {
                translation = !translation;
                if (translation) {
                    icnMesajeTempo.setTextColor(getResources().getColor(R.color.speak_all_red));
                    messageContent.setText(message.mensajeTrad);
                } else {
                    icnMesajeTempo.setTextColor(getResources().getColor(R.color.speak_all_white));
                    messageContent.setText(message.mensaje);
                }
            }
        });
    }

    if (message.orientation == 0) {
        messageContent.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (messageContent.getLineCount() > 1) {
                            mensajeLayout.setOrientation(LinearLayout.VERTICAL);
                            messageContent.setPadding(0, messageContent.getPaddingTop(), 0, 0);
                            new Update(Message.class).set("orientation = ?", 2)
                                    .where("mensajeId = ?", message.mensajeId).execute();
                            messageContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            new Update(Message.class).set("orientation = ?", 1)
                                    .where("mensajeId = ?", message.mensajeId).execute();
                            messageContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }
                    }
                });
    } else {
        if (message.orientation == 2) {
            mensajeLayout.setOrientation(LinearLayout.VERTICAL);
            messageContent.setPadding(0, messageContent.getPaddingTop(), 0, 0);
        }
    }

    icnMesajeCopy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();
        }
    });

    icnMesajeBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    });

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            }
            Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (mensaje.status == -1) {
                String message = mensaje.mensaje;
                JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje.mensajeId);
                    data.put("source", mensaje.emisor);
                    data.put("source_email", mensaje.emisorEmail);
                    data.put("target_email", mensaje.receptorEmail);
                    data.put("target", mensaje.receptor);
                    data.put("message", message);
                    data.put("source_lang", mensaje.emisorLang);
                    data.put("delay", mensaje.delay);
                    data.put("translation_required", mensaje.translation);
                    data.put("target_lang", mensaje.receptorLang);
                    data.put("type", mensaje.tipo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (SpeakSocket.mSocket != null)
                    if (SpeakSocket.mSocket.connected()) {
                        mensaje.status = 1;
                        SpeakSocket.mSocket.emit("message", data);
                    } else {
                        mensaje.status = -1;
                    }
                changeStatus(mensaje.mensajeId, mensaje.status);
            }
        }
    });
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajeVideo(int position, Context context, final Message message, View rowView,
        TextView icnMessageArrow) {/*from   w  ww  .  ja v  a 2  s  . c  om*/
    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    final ImageView messageContent = (ImageView) rowView.findViewById(R.id.message_content);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);
    final ProgressBar progressBar = (ProgressBar) rowView.findViewById(R.id.progress_bar);
    final RelativeLayout progressLayout = (RelativeLayout) rowView.findViewById(R.id.progress_layout);
    final TextView progressText = (TextView) rowView.findViewById(R.id.progress_message);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, prevMessages, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {

        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);

    if (message.emisor.equals(u.id)) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (message.fileUploaded) {
                            if (new File(message.videoName).exists()) {
                                if (message.photo != null) {
                                    Bitmap bmp = BitmapFactory.decodeByteArray(message.photo, 0,
                                            message.photo.length);
                                    messageContent.setImageBitmap(bmp);
                                    bmp = null;
                                }
                            } else {
                                if (message.photoBlur != null) {
                                    Bitmap bmp = BitmapFactory.decodeByteArray(message.photoBlur, 0,
                                            message.photoBlur.length);
                                    messageContent.setImageBitmap(bmp);
                                    bmp = null;
                                }
                            }
                        } else {
                            if (message.photoBlur != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photoBlur, 0,
                                        message.photoBlur.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        }
                    }
                });
            }
        }).start();
        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            try {
                final JSONObject mensaje = new JSONObject();
                mensaje.put("message_id", message.mensajeId);
                mensaje.put("source", message.emisor);
                mensaje.put("source_email", message.emisorEmail);
                mensaje.put("target_email", message.receptorEmail);
                mensaje.put("target", message.receptor);
                mensaje.put("type", message.tipo);
                mensaje.put("delay", message.delay);
                if (!message.fileUploaded) {
                    progressLayout.setVisibility(View.VISIBLE);
                    System.gc();
                    File fileVideo = new File(message.videoName);
                    if (fileVideo.length() > 10000000) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                runTranscodingUsingLoader(message.videoName, message, mensaje, progressBar,
                                        progressLayout, progressText, messageContent);
                            }
                        }).start();
                    } else {
                        uploadVideoToServer(message.videoName, message, mensaje, progressBar, progressLayout,
                                progressText, messageContent);
                    }

                } else {
                    mensaje.put("videos", message.fileName);
                    if (SpeakSocket.mSocket != null)
                        if (SpeakSocket.mSocket.connected()) {
                            message.status = 1;
                            SpeakSocket.mSocket.emit("message", mensaje);
                        } else {
                            message.status = -1;
                        }
                    message.save();
                    changeStatus(message.mensajeId, message.status);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Log.e("SIGUIENTE TAREA", "SIGUIENTE TAREA");
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

    } else {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        drawable.setColor(BalloonFragment.getFriendBalloon(activity));
        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_PHOTO))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        File archivo = new File(message.videoName);
                        if (archivo.exists()) {
                            if (message.photo != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photo, 0,
                                        message.photo.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        } else {
                            if (message.photoBlur != null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(message.photoBlur, 0,
                                        message.photoBlur.length);
                                messageContent.setImageBitmap(bmp);
                                bmp = null;
                            }
                        }
                    }
                });
            }
        }).start();
        //messageContent.setText(message.mensajeTrad);
        icnMesajeTempo.setVisibility(View.GONE);
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                icnMesajeCopy.setVisibility(View.VISIBLE);
                icnMesajeBack.setVisibility(View.VISIBLE);
                messageMenu.setVisibility(View.VISIBLE);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                        ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                set.setDuration(200).start();
                return false;
            }
        });
    }

    icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject notifyMessage = new JSONObject();
            try {
                Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                        .executeSingle();
                if (mensaje.status != 4) {
                    notifyMessage.put("type", mensaje.tipo);
                    notifyMessage.put("message_id", mensaje.mensajeId);
                    notifyMessage.put("source", mensaje.receptor);
                    notifyMessage.put("status", 5);
                    SpeakSocket.mSocket.emit("message-status", notifyMessage);
                }
                new Delete().from(Message.class).where("mensajeId = ?", mensaje.mensajeId).execute();
                Message message = null;
                if (mensaje.emisor.equals(u.id)) {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.receptor, mensaje.receptor)
                            .orderBy("emitedAt DESC").executeSingle();
                } else {
                    message = new Select().from(Message.class)
                            .where("emisor = ? or receptor = ?", mensaje.emisor, mensaje.emisor)
                            .orderBy("emitedAt DESC").executeSingle();
                }
                if (message != null) {
                    Chats chat = null;
                    if (mensaje.emisor.equals(u.id)) {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.receptor)
                                .executeSingle();
                    } else {
                        chat = new Select().from(Chats.class).where("idContacto = ?", mensaje.emisor)
                                .executeSingle();
                    }
                    if (chat != null) {
                        chat.mensajeId = message.mensajeId;
                        chat.lastStatus = message.status;
                        chat.emitedAt = message.emitedAt;
                        chat.lang = message.emisorLang;
                        chat.notRead = 0;
                        if (chat.idContacto.equals(message.emisor))
                            chat.lastMessage = message.mensajeTrad;
                        else
                            chat.lastMessage = message.mensaje;
                        chat.save();
                    }
                } else {
                    if (mensaje.emisor.equals(u.id)) {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.receptor).execute();
                    } else {
                        new Delete().from(Chats.class).where("idContacto = ?", mensaje.emisor).execute();
                    }
                }

                View delete = messagesList.findViewWithTag(mensaje.mensajeId);
                if (delete != null)
                    messagesList.removeView(delete);
                messageSelected = null;
                ((MainActivity) activity).setOnBackPressedListener(null);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    );

    icnMesajeCopy.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            /*ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
            ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();*/
        }
    }

    );

    icnMesajeBack.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    }

    );

    rowView.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick(View v) {
            final Message mensaje1 = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            } else {
                if (mensaje1.status != -1) {
                    if (mensaje1.fileDownloaded || mensaje1.fileUploaded) {
                        dontClose = true;
                        ((MainActivity) activity).dontClose = true;
                        hideKeyBoard();
                        Intent i = new Intent();
                        i.setAction(android.content.Intent.ACTION_VIEW);
                        File videoFile2Play2 = new File(message.videoName);
                        i.setDataAndType(Uri.fromFile(videoFile2Play2), "video/*");
                        startActivity(i);
                    }
                }
            }
            if (mensaje1.status == -1) {
                final JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje1.mensajeId);
                    data.put("source", mensaje1.emisor);
                    data.put("source_email", mensaje1.emisorEmail);
                    data.put("target_email", mensaje1.receptorEmail);
                    data.put("target", mensaje1.receptor);
                    data.put("type", mensaje1.tipo);
                    data.put("delay", mensaje1.delay);
                    if (!mensaje1.fileUploaded) {
                        Log.w("REENVIANDO VIDEO", "REENVIO VIDEO");
                        progressLayout.setVisibility(View.VISIBLE);
                        File fileVideo = new File(mensaje1.videoName);
                        if (fileVideo.length() > 10000000) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.w("COMPRESS VIDEO", "COMPRESS VIDEO");
                                    if (SpeakSocket.mSocket != null)
                                        if (SpeakSocket.mSocket.connected()) {
                                            mensaje1.status = 1;
                                        } else {
                                            mensaje1.status = -1;
                                            runTranscodingUsingLoader(mensaje1.videoName, mensaje1, data,
                                                    progressBar, progressLayout, progressText, messageContent);
                                        }
                                    mensaje1.save();
                                    changeStatus(mensaje1.mensajeId, mensaje1.status);
                                }
                            }).start();

                        } else {
                            uploadVideoToServer(mensaje1.videoName, mensaje1, data, progressBar, progressLayout,
                                    progressText, messageContent);
                        }
                    } else {
                        data.put("videos", message.fileName);
                        if (SpeakSocket.mSocket != null)
                            if (SpeakSocket.mSocket.connected()) {
                                mensaje1.status = 1;
                                SpeakSocket.mSocket.emit("message", data);
                            } else {
                                mensaje1.status = -1;
                            }
                        mensaje1.save();
                        changeStatus(mensaje1.mensajeId, mensaje1.status);
                    }
                } catch (JSONException e) {
                    Log.w("--->", e.toString());
                    e.printStackTrace();
                }
            }
        }
    }

    );

}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void showMensajeAudio(int position, Context context, final Message message, View rowView,
        TextView icnMessageArrow) {//from  w  w w  . jav  a2s  .c  o m
    final TextView prevMessages = (TextView) rowView.findViewById(R.id.showprev_messages);
    TextView messageTime = (TextView) rowView.findViewById(R.id.message_time);
    final TextView icnMesajeTempo = (TextView) rowView.findViewById(R.id.icn_message_tempo);
    final View icnMensajeTempoDivider = (View) rowView.findViewById(R.id.icn_message_tempo_divider);
    final TextView icnMesajeCopy = (TextView) rowView.findViewById(R.id.icn_message_copy);
    final TextView icnMesajeBack = (TextView) rowView.findViewById(R.id.icn_message_back);
    TextView messageStatus = (TextView) rowView.findViewById(R.id.message_status);
    final LinearLayout messageMenu = (LinearLayout) rowView.findViewById(R.id.messages_menu_layout);
    final LinearLayout mensajeLayout = (LinearLayout) rowView.findViewById(R.id.mensaje_layout);
    TextView messageDate = (TextView) rowView.findViewById(R.id.message_date);
    final TextView audioTime = (TextView) rowView.findViewById(R.id.message_audio_time);
    final SeekBar audioSeekBar = (SeekBar) rowView.findViewById(R.id.audio_seekbar);
    final TextView playAudio = (TextView) rowView.findViewById(R.id.play_audio);
    final ProgressBar progressBar = (ProgressBar) rowView.findViewById(R.id.progress_bar);
    final RelativeLayout progressLayout = (RelativeLayout) rowView.findViewById(R.id.progress_layout);
    final TextView progressText = (TextView) rowView.findViewById(R.id.progress_message);

    TFCache.apply(context, messageTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, icnMesajeTempo, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeCopy, TFCache.TF_SPEAKALL);
    TFCache.apply(context, icnMesajeBack, TFCache.TF_SPEAKALL);
    TFCache.apply(context, messageStatus, TFCache.TF_SPEAKALL);
    TFCache.apply(context, prevMessages, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, messageDate, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, audioTime, TFCache.TF_WHITNEY_MEDIUM);
    TFCache.apply(context, playAudio, TFCache.TF_SPEAKON);

    if (position == 0) {
        if (mensajesTotal > mensajesOffset) {
            prevMessages.setVisibility(View.VISIBLE);
            prevMessages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevMessages.setVisibility(View.GONE);
                    if (mensajesTotal - mensajesOffset >= 10) {
                        mensajesLimit = 10;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    } else {
                        mensajesLimit = mensajesTotal - mensajesOffset;
                        mensajesOffset += mensajesLimit;
                        showPreviousMessages(mensajesOffset);
                    }
                }
            });
        }
    }

    DateFormat dateDay = new SimpleDateFormat("d");
    DateFormat dateDayText = new SimpleDateFormat("EEEE");
    DateFormat dateMonth = new SimpleDateFormat("MMMM");
    DateFormat dateYear = new SimpleDateFormat("yyyy");
    DateFormat timeFormat = new SimpleDateFormat("h:mm a");

    Calendar fechamsj = Calendar.getInstance();
    fechamsj.setTimeInMillis(message.emitedAt);
    String time = timeFormat.format(fechamsj.getTime());
    String dayMessage = dateDay.format(fechamsj.getTime());
    String monthMessage = dateMonth.format(fechamsj.getTime());
    String yearMessage = dateYear.format(fechamsj.getTime());
    String dayMessageText = dateDayText.format(fechamsj.getTime());
    char[] caracteres = dayMessageText.toCharArray();
    caracteres[0] = Character.toUpperCase(caracteres[0]);
    dayMessageText = new String(caracteres);

    Calendar fechaActual = Calendar.getInstance();
    String dayActual = dateDay.format(fechaActual.getTime());
    String monthActual = dateMonth.format(fechaActual.getTime());
    String yearActual = dateYear.format(fechaActual.getTime());

    String fechaMostrar = "";
    if (dayMessage.equals(dayActual) && monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        fechaMostrar = getString(R.string.messages_today);
    } else if (monthMessage.equals(monthActual) && yearMessage.equals(yearActual)) {
        int days = Integer.parseInt(dayActual) - Integer.parseInt(dayMessage);
        if (days < 7) {
            switch (days) {
            case 1:
                fechaMostrar = getString(R.string.messages_yesterday);
                break;
            default:
                fechaMostrar = dayMessageText;
                break;
            }
        } else {
            fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
        }
    } else {
        fechaMostrar = (dayMessage + " " + monthMessage + " " + yearMessage).toUpperCase();
    }

    messageDate.setText(fechaMostrar);
    if (position != -1) {

        if (itemAnterior != null) {
            if (!fechaMostrar.equals(fechaAnterior)) {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.VISIBLE);
            } else {
                itemAnterior.findViewById(R.id.message_date).setVisibility(View.GONE);
            }
        }
        itemAnterior = rowView;
        fechaAnterior = fechaMostrar;
    } else {
        View v = messagesList.getChildAt(messagesList.getChildCount() - 1);
        if (v != null) {
            TextView tv = (TextView) v.findViewById(R.id.message_date);
            if (tv.getText().toString().equals(fechaMostrar)) {
                messageDate.setVisibility(View.GONE);
            } else {
                messageDate.setVisibility(View.VISIBLE);
            }
        }
    }
    messageTime.setText(time);

    if (message.emisor.equals(u.id)) {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable1 = (GradientDrawable) mensajeLayout.getBackground();
        drawable1.setCornerRadius(radius);
        drawable1.setColor(BalloonFragment.getBackgroundColor(activity));
        progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.speak_all_red),
                PorterDuff.Mode.SRC_IN);
        if (message.status != -1) {
            rowView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (messageSelected != null) {
                        View vista = messagesList.findViewWithTag(messageSelected);
                        vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                View vista = messagesList.findViewWithTag(messageSelected);
                                vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                ((MainActivity) activity).setOnBackPressedListener(null);
                                messageSelected = null;
                            }
                        });
                    } else {
                        messageSelected = message.mensajeId;
                        ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                            @Override
                            public void doBack() {
                                if (messageSelected != null) {
                                    View vista = messagesList.findViewWithTag(messageSelected);
                                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                                    ((MainActivity) activity).setOnBackPressedListener(null);
                                    messageSelected = null;
                                }
                            }
                        });
                    }

                    Vibrator x = (Vibrator) activity.getApplicationContext()
                            .getSystemService(Context.VIBRATOR_SERVICE);
                    x.vibrate(30);
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    if (mensaje.status != 4) {
                        icnMesajeTempo.setVisibility(View.VISIBLE);
                        icnMensajeTempoDivider.setVisibility(View.VISIBLE);
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeTempo, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeTempo, "translationX", 150, 0),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    } else {
                        icnMesajeCopy.setVisibility(View.VISIBLE);
                        icnMesajeBack.setVisibility(View.VISIBLE);
                        messageMenu.setVisibility(View.VISIBLE);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                                ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", 100, 0),
                                ObjectAnimator.ofFloat(icnMesajeBack, "translationX", 50, 0));
                        set.setDuration(200).start();
                    }

                    return false;
                }
            });
        }
        if (message.status == 1) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setTextColor(getResources().getColor(R.color.speak_all_ligh_gray));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SENDING.toString());
            ValueAnimator colorAnim = ObjectAnimator.ofFloat(messageStatus, "alpha", 1, 0);
            colorAnim.setRepeatCount(ValueAnimator.INFINITE);
            colorAnim.setRepeatMode(ValueAnimator.REVERSE);
            colorAnim.setDuration(1000).start();
            animaciones.put(message.mensajeId, colorAnim);
            final JSONObject data = new JSONObject();
            try {
                data.put("message_id", message.mensajeId);
                data.put("source", message.emisor);
                data.put("source_email", message.emisorEmail);
                data.put("target_email", message.receptorEmail);
                data.put("target", message.receptor);
                data.put("type", message.tipo);
                if (!message.fileUploaded) {
                    progressLayout.setVisibility(View.VISIBLE);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    uploadAudioToServer(message.audioName, message, data, progressBar,
                                            progressLayout, progressText);
                                }
                            });
                        }
                    }).start();
                } else {
                    data.put("videos", message.fileName);
                    if (SpeakSocket.mSocket != null)
                        if (SpeakSocket.mSocket.connected()) {
                            message.status = 1;
                            SpeakSocket.mSocket.emit("message", data);
                        } else {
                            message.status = -1;
                        }
                    message.save();
                    changeStatus(message.mensajeId, message.status);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (message.status == -1) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_ERROR.toString());
        }
        if (message.status == 3) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_CLOSED.toString());
        }
        if (message.status == 2) {
            messageStatus.setTextSize(16);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_SEND.toString());
        }
        if (message.status == 4) {
            messageStatus.setTextSize(11);
            mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
            GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
            drawable.setCornerRadius(radius);
            drawable.setColor(BalloonFragment.getBackgroundColor(activity));
            icnMessageArrow.setTextColor(BalloonFragment.getBackgroundColor(activity));
            messageStatus.setText(Finder.STRING.ICN_MSJ_OPENED.toString());
            icnMensajeTempoDivider.setVisibility(View.GONE);
            icnMesajeTempo.setVisibility(View.GONE);
        } else {
            icnMensajeTempoDivider.setVisibility(View.INVISIBLE);
            icnMesajeTempo.setVisibility(View.INVISIBLE);
        }

        icnMesajeTempo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject notifyMessage = new JSONObject();
                try {
                    Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                            .executeSingle();
                    Log.w("mensaje detail",
                            mensaje.status + " : " + mensaje.receptorEmail + " : " + mensaje.tipo);
                    if (mensaje.status != 4) {
                        notifyMessage.put("type", mensaje.tipo);
                        notifyMessage.put("message_id", mensaje.mensajeId);
                        notifyMessage.put("source", mensaje.emisor);
                        notifyMessage.put("target", mensaje.receptor);
                        notifyMessage.put("status", 5);
                        if (SpeakSocket.mSocket != null)
                            if (SpeakSocket.mSocket.connected())
                                SpeakSocket.mSocket.emit("message-status", notifyMessage);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    } else {
        mensajeLayout.setBackgroundResource(R.drawable.message_out_background);
        GradientDrawable drawable = (GradientDrawable) mensajeLayout.getBackground();
        drawable.setCornerRadius(radius);
        drawable.setColor(BalloonFragment.getFriendBalloon(activity));
        if (message.tipo == Integer.parseInt(getString(R.string.MSG_TYPE_BROADCAST_AUDIO))) {
            messageStatus.setTextSize(15);
            messageStatus.setText(Finder.STRING.ICN_BROADCAST_FILL.toString());
        }
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (messageSelected != null) {
                    View vista = messagesList.findViewWithTag(messageSelected);
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                } else {
                    messageSelected = message.mensajeId;
                    ((MainActivity) activity).setOnBackPressedListener(new BackPressedListener(activity) {
                        @Override
                        public void doBack() {
                            View vista = messagesList.findViewWithTag(messageSelected);
                            vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                            ((MainActivity) activity).setOnBackPressedListener(null);
                            messageSelected = null;
                        }
                    });
                }

                Vibrator x = (Vibrator) activity.getApplicationContext()
                        .getSystemService(Context.VIBRATOR_SERVICE);
                x.vibrate(20);
                icnMesajeTempo.setVisibility(View.GONE);
                icnMensajeTempoDivider.setVisibility(View.GONE);
                icnMesajeCopy.setVisibility(View.VISIBLE);
                icnMesajeBack.setVisibility(View.VISIBLE);
                messageMenu.setVisibility(View.VISIBLE);
                AnimatorSet set = new AnimatorSet();
                set.playTogether(ObjectAnimator.ofFloat(icnMesajeCopy, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeBack, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(messageMenu, "alpha", 0, 1),
                        ObjectAnimator.ofFloat(icnMesajeCopy, "translationX", -100, 0),
                        ObjectAnimator.ofFloat(icnMesajeBack, "translationX", -150, 0));
                set.setDuration(200).start();
                return false;
            }
        });
    }

    /*icnMesajeCopy.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          ClipData clip = ClipData.newPlainText("text", messageContent.getText().toString());
          ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
          clipboard.setPrimaryClip(clip);
          Toast.makeText(activity, "Copiado", Toast.LENGTH_SHORT).show();
      }
    });*/

    icnMesajeBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.emisor.equals(u.id)) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensaje))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            } else {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container2, ForwarForFragment.newInstance(message.mensajeTrad))
                        .addToBackStack(null).commit();
                ((MainActivity) activity).setOnBackPressedListener(null);
            }
        }
    });

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (messageSelected != null) {
                View vista = messagesList.findViewWithTag(messageSelected);
                if (!messageSelected.equals(message.mensajeId)) {
                    vista.findViewById(messageMenu.getId()).setVisibility(View.INVISIBLE);
                    ((MainActivity) activity).setOnBackPressedListener(null);
                    messageSelected = null;
                }
            }
            final Message mensaje = new Select().from(Message.class).where("mensajeId = ?", message.mensajeId)
                    .executeSingle();
            if (mensaje.status == -1) {
                final JSONObject data = new JSONObject();
                try {
                    data.put("message_id", mensaje.mensajeId);
                    data.put("source", mensaje.emisor);
                    data.put("source_email", mensaje.emisorEmail);
                    data.put("target_email", mensaje.receptorEmail);
                    data.put("target", mensaje.receptor);
                    data.put("type", mensaje.tipo);
                    if (!mensaje.fileUploaded) {
                        progressLayout.setVisibility(View.VISIBLE);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        uploadAudioToServer(mensaje.audioName, mensaje, data, progressBar,
                                                progressLayout, progressText);
                                    }
                                });
                            }
                        }).start();
                    } else {
                        data.put("audios", mensaje.fileName);
                        if (SpeakSocket.mSocket != null)
                            if (SpeakSocket.mSocket.connected()) {
                                mensaje.status = 1;
                                SpeakSocket.mSocket.emit("message", data);
                            } else {
                                mensaje.status = -1;
                            }
                        mensaje.save();
                        changeStatus(mensaje.mensajeId, mensaje.status);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    File audioFile = null;
    final MediaPlayer mediaPlayer1 = new MediaPlayer();
    audioFile = new File(message.audioName);
    try {
        mediaPlayer1.setDataSource(audioFile.getAbsolutePath());
        mediaPlayer1.prepare();
        long timePlayer = mediaPlayer1.getDuration();
        long days, hours, minutes, seconds;
        long secondsTotal = timePlayer / 1000;
        days = (Math.round(secondsTotal) / 86400);
        hours = (Math.round(secondsTotal) / 3600) - (days * 24);
        minutes = (Math.round(secondsTotal) / 60) - (days * 1440) - (hours * 60);
        seconds = Math.round(secondsTotal) % 60;
        audioTime.setText(String.format("%02d", minutes) + " : " + String.format("%02d", seconds));
        audioSeekBar.setMax(mediaPlayer1.getDuration());
        audioSeekBar.setTag(message.mensajeId);
        mediaPlayer1.release();
    } catch (IOException e) {
        e.printStackTrace();
    }

    final File finalAudioFile = audioFile;
    playAudio.setOnClickListener(new View.OnClickListener() {
        boolean isplayAudio = false;

        @Override
        public void onClick(View v) {

            if (activeAudioSeekBarr != null) {
                if (!activeAudioSeekBarr.getTag().equals(audioSeekBar.getTag())) {
                    isplayAudio = false;
                }
            }
            if (!isplayAudio) {
                try {
                    if (activeAudioSeekBarr != null) {
                        if (activeAudioSeekBarr.getTag().equals(audioSeekBar.getTag())) {
                            seekHandler.postDelayed(runAudio, 1000);
                            playAudio.setText(Finder.STRING.ICN_PAUSE.toString());
                            mediaPlayer.start();
                        } else {
                            if (mediaPlayer != null) {
                                mediaPlayer.stop();
                                mediaPlayer.release();
                            }
                            if (activeAudioSeekBarr != null) {
                                activeAudioSeekBarr.setProgress(0);
                                activeAudioSeekBarr.setOnSeekBarChangeListener(null);
                            }
                            if (activeAudioTime != null) {
                                activeAudioTime.setText(activeAudioDuration);
                            }
                            if (!activeAudioDuration.equals("")) {
                                activeAudioDuration = "";
                            }
                            if (activePlayer != null) {
                                activePlayer.setText(Finder.STRING.ICN_PLAY.toString());
                            }
                            seekHandler.removeCallbacks(runAudio);
                            mediaPlayer = new MediaPlayer();
                            mediaPlayer.setDataSource(finalAudioFile.getAbsolutePath());
                            mediaPlayer.prepare();
                            activeAudioDuration = audioTime.getText().toString();
                            audioSeekBar.setProgress(mediaPlayer.getCurrentPosition());
                            audioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                                @Override
                                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                                    if (fromUser) {
                                        mediaPlayer.seekTo(progress);
                                    }
                                }

                                @Override
                                public void onStartTrackingTouch(SeekBar seekBar) {

                                }

                                @Override
                                public void onStopTrackingTouch(SeekBar seekBar) {

                                }
                            });
                            activeAudioSeekBarr = audioSeekBar;
                            activePlayer = playAudio;
                            activeAudioTime = audioTime;
                            seekHandler.postDelayed(runAudio, 1000);
                            mediaPlayer.start();
                            playAudio.setText(Finder.STRING.ICN_PAUSE.toString());
                        }
                    } else {
                        if (mediaPlayer != null) {
                            mediaPlayer.stop();
                            mediaPlayer.release();
                        }
                        if (activeAudioSeekBarr != null) {
                            activeAudioSeekBarr.setProgress(0);
                            activeAudioSeekBarr.setOnSeekBarChangeListener(null);
                        }
                        if (activeAudioTime != null) {
                            activeAudioTime.setText(activeAudioDuration);
                        }
                        if (!activeAudioDuration.equals("")) {
                            activeAudioDuration = "";
                        }
                        seekHandler.removeCallbacks(runAudio);
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setDataSource(finalAudioFile.getAbsolutePath());
                        mediaPlayer.prepare();
                        activeAudioDuration = audioTime.getText().toString();
                        audioSeekBar.setProgress(mediaPlayer.getCurrentPosition());
                        audioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                            @Override
                            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                                if (fromUser) {
                                    mediaPlayer.seekTo(progress);
                                }
                            }

                            @Override
                            public void onStartTrackingTouch(SeekBar seekBar) {

                            }

                            @Override
                            public void onStopTrackingTouch(SeekBar seekBar) {

                            }
                        });
                        activeAudioSeekBarr = audioSeekBar;
                        activePlayer = playAudio;
                        activeAudioTime = audioTime;
                        seekHandler.postDelayed(runAudio, 1000);
                        mediaPlayer.start();
                        playAudio.setText(Finder.STRING.ICN_PAUSE.toString());
                    }
                    //file2.delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                isplayAudio = true;
            } else {
                playAudio.setText(Finder.STRING.ICN_PLAY.toString());
                mediaPlayer.pause();
                seekHandler.removeCallbacks(runAudio);
                isplayAudio = false;
            }
        }
    });

}