Search in sources :

Example 1 with RoundedAvatarDrawable

use of org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable in project Zom-Android by zom.

the class OnboardingActivity method setAvatar.

private void setAvatar(Bitmap bmp, OnboardingAccount account) {
    RoundedAvatarDrawable avatar = new RoundedAvatarDrawable(bmp);
    mImageAvatar.setImageDrawable(avatar);
    final ImApp app = ((ImApp) getApplication());
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        byte[] avatarBytesCompressed = stream.toByteArray();
        String avatarHash = "nohash";
        DatabaseUtils.insertAvatarBlob(getContentResolver(), Imps.Avatars.CONTENT_URI, account.providerId, account.accountId, avatarBytesCompressed, avatarHash, account.username + '@' + account.domain);
    } catch (Exception e) {
        Log.w(ImApp.LOG_TAG, "error loading image bytes", e);
    }
}
Also used : ImApp(org.awesomeapp.messenger.ImApp) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with RoundedAvatarDrawable

use of org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable in project Zom-Android by zom.

the class ConversationView method bindChat.

public void bindChat(long chatId, String address, String name) {
    // log("bind " + this + " " + chatId);
    mLastChatId = chatId;
    setViewType(VIEW_TYPE_CHAT);
    Uri contactUri = ContentUris.withAppendedId(Imps.Contacts.CONTENT_URI, chatId);
    Cursor c = mActivity.getContentResolver().query(contactUri, CHAT_PROJECTION, null, null, null);
    if (c == null)
        return;
    if (!c.moveToFirst()) {
        if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) {
            log("Failed to query chat: " + chatId);
        }
        mLastChatId = -1;
        c.close();
    } else {
        updateSessionInfo(c);
        if (mRemoteAvatar == null) {
            try {
                mRemoteAvatar = DatabaseUtils.getAvatarFromCursor(c, AVATAR_COLUMN, ImApp.DEFAULT_AVATAR_WIDTH, ImApp.DEFAULT_AVATAR_HEIGHT);
            } catch (Exception e) {
            }
            if (mRemoteAvatar == null) {
                mRemoteAvatar = new RoundedAvatarDrawable(BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.avatar_unknown));
            }
        }
        if (mRemoteHeader == null) {
            try {
                mRemoteHeader = DatabaseUtils.getHeaderImageFromCursor(c, AVATAR_COLUMN, ImApp.DEFAULT_AVATAR_WIDTH, ImApp.DEFAULT_AVATAR_HEIGHT);
            } catch (Exception e) {
            }
        }
        c.close();
        initSession();
        if (isGroupChat()) {
            updateGroupTitle();
        } else {
            if (mRemoteNickname == null)
                mRemoteNickname = name;
            try {
                mRemoteNickname = mRemoteNickname.split("@")[0].split("\\.")[0];
            } catch (Exception e) {
            // handle glitches in unicode nicknames
            }
        }
    }
}
Also used : RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable) Cursor(android.database.Cursor) Uri(android.net.Uri) RemoteException(android.os.RemoteException) IOException(java.io.IOException) CursorIndexOutOfBoundsException(android.database.CursorIndexOutOfBoundsException)

Example 3 with RoundedAvatarDrawable

use of org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable in project Zom-Android by zom.

the class ContactListItem method bind.

public void bind(ContactViewHolder holder, Cursor cursor, String underLineText, boolean showChatMsg, boolean scrolling) {
    address = cursor.getString(COLUMN_CONTACT_USERNAME);
    nickname = cursor.getString(COLUMN_CONTACT_NICKNAME);
    final int type = cursor.getInt(COLUMN_CONTACT_TYPE);
    // final String lastMsg = cursor.getString(COLUMN_LAST_MESSAGE);
    // long lastMsgDate = cursor.getLong(COLUMN_LAST_MESSAGE_DATE);
    final int presence = cursor.getInt(COLUMN_CONTACT_PRESENCE_STATUS);
    final int subType = cursor.getInt(COLUMN_SUBSCRIPTION_TYPE);
    final int subStatus = cursor.getInt(COLUMN_SUBSCRIPTION_STATUS);
    String statusText = cursor.getString(COLUMN_CONTACT_CUSTOM_STATUS);
    if (TextUtils.isEmpty(nickname)) {
        nickname = address.split("@")[0].split("\\.")[0];
    } else {
        nickname = nickname.split("@")[0].split("\\.")[0];
    }
    if (!TextUtils.isEmpty(underLineText)) {
        // highlight/underline the word being searched
        String lowercase = nickname.toLowerCase();
        int start = lowercase.indexOf(underLineText.toLowerCase());
        if (start >= 0) {
            int end = start + underLineText.length();
            SpannableString str = new SpannableString(nickname);
            str.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            holder.mLine1.setText(str);
        } else
            holder.mLine1.setText(nickname);
    } else
        holder.mLine1.setText(nickname);
    if (holder.mAvatar != null) {
        if (Imps.Contacts.TYPE_GROUP == type) {
            holder.mAvatar.setVisibility(View.VISIBLE);
            if (AVATAR_DEFAULT_GROUP == null)
                AVATAR_DEFAULT_GROUP = new RoundedAvatarDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.group_chat));
            holder.mAvatar.setImageDrawable(AVATAR_DEFAULT_GROUP);
        } else {
            Drawable avatar = null;
            try {
                avatar = DatabaseUtils.getAvatarFromAddress(this.getContext().getContentResolver(), address, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT);
            } catch (Exception e) {
                // problem decoding avatar
                Log.e(ImApp.LOG_TAG, "error decoding avatar", e);
            }
            try {
                if (avatar != null) {
                    if (avatar instanceof RoundedAvatarDrawable)
                        setAvatarBorder(presence, (RoundedAvatarDrawable) avatar);
                    holder.mAvatar.setImageDrawable(avatar);
                } else {
                    // int color = getAvatarBorder(presence);
                    int padding = 24;
                    LetterAvatar lavatar = new LetterAvatar(getContext(), nickname, padding);
                    holder.mAvatar.setImageDrawable(lavatar);
                }
                holder.mAvatar.setVisibility(View.VISIBLE);
            } catch (OutOfMemoryError ome) {
            // this seems to happen now and then even on tiny images; let's catch it and just not set an avatar
            }
        }
    }
    statusText = address;
    if (type == Imps.Contacts.TYPE_HIDDEN) {
        statusText += " | " + getContext().getString(R.string.action_archive);
    }
    if (holder.mLine2 != null)
        holder.mLine2.setText(statusText);
    if (Imps.Contacts.TYPE_NORMAL == type) {
        if (subStatus == Imps.ContactsColumns.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING) {
            if (subType == Imps.ContactsColumns.SUBSCRIPTION_TYPE_FROM) {
                if (holder.mSubBox != null) {
                    holder.mSubBox.setVisibility(View.VISIBLE);
                    holder.mButtonSubApprove.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            approveSubscription();
                            mHolder.itemView.performClick();
                        }
                    });
                    holder.mButtonSubDecline.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            declineSubscription();
                        }
                    });
                }
            } else {
                if (holder.mSubBox != null)
                    holder.mSubBox.setVisibility(View.GONE);
                if (holder.mLine2 != null)
                    holder.mLine2.setText(getContext().getString(R.string.title_pending));
            }
        } else {
            if (holder.mSubBox != null)
                holder.mSubBox.setVisibility(View.GONE);
        }
    }
    if (mShowPresence) {
        switch(presence) {
            case Presence.AVAILABLE:
                if (holder.mLine2 != null)
                    holder.mLine2.setText(getContext().getString(R.string.presence_available));
            case Presence.IDLE:
                if (holder.mLine2 != null)
                    holder.mLine2.setText(getContext().getString(R.string.presence_available));
            /**
             *                     case Presence.AWAY:
             *                     return (getResources().getColor(R.color.holo_orange_light));
             *
             *                     case Presence.DO_NOT_DISTURB:
             *                     return(getResources().getColor(R.color.holo_red_dark));
             *
             *                     case Presence.OFFLINE:
             *                     return(getResources().getColor(R.color.holo_grey_dark));
             */
            default:
        }
    }
    holder.mLine1.setVisibility(View.VISIBLE);
}
Also used : SpannableString(android.text.SpannableString) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable) Drawable(android.graphics.drawable.Drawable) LetterAvatar(org.awesomeapp.messenger.ui.widgets.LetterAvatar) SpannableString(android.text.SpannableString) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable) View(android.view.View) UnderlineSpan(android.text.style.UnderlineSpan) RemoteException(android.os.RemoteException)

Example 4 with RoundedAvatarDrawable

use of org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable in project Zom-Android by zom.

the class DatabaseUtils method decodeRoundAvatar.

private static RoundedAvatarDrawable decodeRoundAvatar(byte[] data, int width, int height) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, options);
    options.inSampleSize = calculateInSampleSize(options, width, height);
    options.inJustDecodeBounds = false;
    Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length, options);
    if (b != null) {
        RoundedAvatarDrawable avatar = new RoundedAvatarDrawable(b);
        return avatar;
    } else
        return null;
}
Also used : Bitmap(android.graphics.Bitmap) BitmapFactory(android.graphics.BitmapFactory) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable)

Example 5 with RoundedAvatarDrawable

use of org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable in project Zom-Android by zom.

the class ConversationListItem method bind.

public void bind(ConversationViewHolder holder, long contactId, long providerId, long accountId, String address, String nickname, int contactType, String message, long messageDate, String messageType, int presence, int subscription, String underLineText, boolean showChatMsg, boolean scrolling, boolean isMuted) {
    if (nickname == null) {
        nickname = address.split("@")[0].split("\\.")[0];
    } else {
        nickname = nickname.split("@")[0].split("\\.")[0];
    }
    if (isMuted) {
        nickname += " \uD83D\uDD15";
    }
    if (!TextUtils.isEmpty(underLineText)) {
        // highlight/underline the word being searched
        String lowercase = nickname.toLowerCase();
        int start = lowercase.indexOf(underLineText.toLowerCase());
        if (start >= 0) {
            int end = start + underLineText.length();
            SpannableString str = new SpannableString(nickname);
            str.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            holder.mLine1.setText(str);
        } else
            holder.mLine1.setText(nickname);
    } else
        holder.mLine1.setText(nickname);
    holder.mStatusIcon.setVisibility(View.GONE);
    if (holder.mAvatar != null) {
        if (Imps.Contacts.TYPE_GROUP == contactType) {
            holder.mAvatar.setVisibility(View.VISIBLE);
            try {
                String groupId = address.split("@")[0];
                Drawable avatar = new GroupAvatar(groupId);
                holder.mAvatar.setImageDrawable(avatar);
            } catch (Exception ignored) {
                if (AVATAR_DEFAULT_GROUP == null)
                    AVATAR_DEFAULT_GROUP = new RoundedAvatarDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.group_chat));
                holder.mAvatar.setImageDrawable(AVATAR_DEFAULT_GROUP);
            }
        } else // else if (cursor.getColumnIndex(Imps.Contacts.AVATAR_DATA)!=-1)
        {
            // holder.mAvatar.setVisibility(View.GONE);
            Drawable avatar = null;
            try {
                avatar = DatabaseUtils.getAvatarFromAddress(this.getContext().getContentResolver(), address, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT);
            // avatar = DatabaseUtils.getAvatarFromCursor(cursor, COLUMN_AVATAR_DATA, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT);
            } catch (Exception e) {
                // problem decoding avatar
                Log.e(ImApp.LOG_TAG, "error decoding avatar", e);
            }
            try {
                if (avatar != null) {
                    // if (avatar instanceof RoundedAvatarDrawable)
                    // setAvatarBorder(presence,(RoundedAvatarDrawable)avatar);
                    holder.mAvatar.setImageDrawable(avatar);
                } else {
                    // int color = getAvatarBorder(presence);
                    int padding = 24;
                    LetterAvatar lavatar = new LetterAvatar(getContext(), nickname, padding);
                    holder.mAvatar.setImageDrawable(lavatar);
                }
                holder.mAvatar.setVisibility(View.VISIBLE);
            } catch (OutOfMemoryError ome) {
            // this seems to happen now and then even on tiny images; let's catch it and just not set an avatar
            }
        }
    }
    if (showChatMsg && message != null) {
        holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
        holder.mMediaThumb.setVisibility(View.GONE);
        if (holder.mLine2 != null) {
            String vPath = message.split(" ")[0];
            if (SecureMediaStore.isVfsUri(vPath) || SecureMediaStore.isContentUri(vPath)) {
                if (TextUtils.isEmpty(messageType)) {
                    holder.mMediaThumb.setVisibility(View.VISIBLE);
                    holder.mMediaThumb.setImageResource(R.drawable.ic_attach_file_black_36dp);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    holder.mLine2.setText("");
                } else if (messageType.startsWith("image")) {
                    if (holder.mMediaThumb != null) {
                        holder.mMediaThumb.setVisibility(View.VISIBLE);
                        if (messageType != null && messageType.equals("image/png")) {
                            holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        } else {
                            holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        }
                        setThumbnail(getContext().getContentResolver(), holder, Uri.parse(vPath), true);
                        holder.mLine2.setVisibility(View.GONE);
                    }
                } else if (messageType.startsWith("audio")) {
                    mLastMediaUri = null;
                    holder.mMediaThumb.setVisibility(View.VISIBLE);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    holder.mMediaThumb.setImageResource(R.drawable.ic_volume_up_black_24dp);
                    holder.mLine2.setText("");
                } else if (messageType.startsWith("video")) {
                    mLastMediaUri = null;
                    holder.mMediaThumb.setVisibility(View.VISIBLE);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    holder.mMediaThumb.setImageResource(R.drawable.video256);
                    holder.mLine2.setText("");
                } else if (messageType.startsWith("application")) {
                    mLastMediaUri = null;
                    holder.mMediaThumb.setVisibility(View.VISIBLE);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    holder.mMediaThumb.setImageResource(R.drawable.ic_attach_file_black_36dp);
                    holder.mLine2.setText("");
                } else {
                    mLastMediaUri = null;
                    holder.mMediaThumb.setVisibility(View.GONE);
                    holder.mLine2.setText(messageType);
                }
            } else if ((!TextUtils.isEmpty(message)) && message.startsWith("/")) {
                String cmd = message.toString().substring(1);
                if (cmd.startsWith("sticker")) {
                    String[] cmds = cmd.split(":");
                    String mimeTypeSticker = "image/png";
                    Uri mediaUri = Uri.parse("asset://" + cmds[1]);
                    mLastMediaUri = null;
                    setThumbnail(getContext().getContentResolver(), holder, mediaUri, false);
                    holder.mLine2.setVisibility(View.GONE);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    holder.mMediaThumb.setVisibility(View.VISIBLE);
                }
            } else if ((!TextUtils.isEmpty(message)) && message.startsWith(":")) {
                String[] cmds = message.split(":");
                try {
                    String[] stickerParts = cmds[1].split("-");
                    String folder = stickerParts[0];
                    StringBuffer name = new StringBuffer();
                    for (int i = 1; i < stickerParts.length; i++) {
                        name.append(stickerParts[i]);
                        if (i + 1 < stickerParts.length)
                            name.append('-');
                    }
                    String stickerPath = "stickers/" + folder + "/" + name.toString() + ".png";
                    // make sure sticker exists
                    AssetFileDescriptor afd = getContext().getAssets().openFd(stickerPath);
                    afd.getLength();
                    afd.close();
                    // now setup the new URI for loading local sticker asset
                    Uri mediaUri = Uri.parse("asset://localhost/" + stickerPath);
                    mLastMediaUri = null;
                    setThumbnail(getContext().getContentResolver(), holder, mediaUri, false);
                    holder.mLine2.setVisibility(View.GONE);
                    holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
                } catch (Exception e) {
                }
            } else {
                if (holder.mMediaThumb != null)
                    holder.mMediaThumb.setVisibility(View.GONE);
                holder.mLine2.setVisibility(View.VISIBLE);
                try {
                    holder.mLine2.setText(android.text.Html.fromHtml(message).toString());
                } catch (RuntimeException re) {
                }
            }
        }
        if (messageDate != -1) {
            Date dateLast = new Date(messageDate);
            holder.mStatusText.setText(sPrettyTime.format(dateLast));
        } else {
            holder.mStatusText.setText("");
        }
    } else if (holder.mLine2 != null) {
        holder.mLine2.setText(address);
        if (holder.mMediaThumb != null)
            holder.mMediaThumb.setVisibility(View.GONE);
    }
    holder.mLine1.setVisibility(View.VISIBLE);
    if (providerId != -1)
        getEncryptionState(providerId, accountId, address, holder);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable) Drawable(android.graphics.drawable.Drawable) LetterAvatar(org.awesomeapp.messenger.ui.widgets.LetterAvatar) SpannableString(android.text.SpannableString) Uri(android.net.Uri) UnderlineSpan(android.text.style.UnderlineSpan) Date(java.util.Date) SpannableString(android.text.SpannableString) GroupAvatar(org.awesomeapp.messenger.ui.widgets.GroupAvatar) RoundedAvatarDrawable(org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable)

Aggregations

RoundedAvatarDrawable (org.awesomeapp.messenger.ui.widgets.RoundedAvatarDrawable)6 RemoteException (android.os.RemoteException)3 IOException (java.io.IOException)3 LetterAvatar (org.awesomeapp.messenger.ui.widgets.LetterAvatar)3 Drawable (android.graphics.drawable.Drawable)2 Uri (android.net.Uri)2 SpannableString (android.text.SpannableString)2 UnderlineSpan (android.text.style.UnderlineSpan)2 SuppressLint (android.annotation.SuppressLint)1 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Cursor (android.database.Cursor)1 CursorIndexOutOfBoundsException (android.database.CursorIndexOutOfBoundsException)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 View (android.view.View)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 ImApp (org.awesomeapp.messenger.ImApp)1 GroupAvatar (org.awesomeapp.messenger.ui.widgets.GroupAvatar)1