Fix bug with empty avatars for contacts, whose names starts with "+"

There were problem, that if you have several contacts, whose names
starts with "+", their avatars not displayed in conversations list, or
displays as white circles.
Also minor tweak for "onDraw" function
This commit is contained in:
Ivan
2016-10-10 19:28:21 +03:00
parent 6d9532ada3
commit 7979684588
@@ -22,6 +22,7 @@ import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView;
import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.TypefaceManager;
@@ -53,6 +54,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
private String mInitial = "#";
private Paint mPaint;
private Drawable mDefaultDrawable;
private int mYOffset;
/**
* When setImageDrawable is called with a drawable, we circle crop to size of this view and use
@@ -89,6 +91,8 @@ public class AvatarView extends ImageView implements View.OnClickListener {
}
a.recycle();
mYOffset = (int) ((mPaint.descent() + mPaint.ascent()) / 2);
setOnClickListener(this);
LiveViewManager.registerView(QKPreference.THEME, this, key -> {
@@ -175,7 +179,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
if (mOriginalDrawable == null) super.setImageDrawable(null);
} else if (isPhoneNumberFormat(name)) {
mInitial = "";
super.setImageDrawable(mDefaultDrawable);
if (mOriginalDrawable == null) super.setImageDrawable(mDefaultDrawable);
} else {
mInitial = "" + name.toUpperCase().charAt(0);
if (mOriginalDrawable == null) super.setImageDrawable(null);
@@ -185,9 +189,10 @@ public class AvatarView extends ImageView implements View.OnClickListener {
}
private boolean isPhoneNumberFormat(String name) {
if (TextUtils.isEmpty(name)) {
return false;
}
// We already checked it in first "if" condition in "setContactName"
// if (TextUtils.isEmpty(name)) {
// return false;
// }
char c = name.charAt(0);
return !name.contains("@") && (c == '+' || c == '(' || Character.isDigit(c));
@@ -276,7 +281,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
if (getDrawable() == null && !isInEditMode()) {
int xPos = (getWidth() / 2);
int yPos = (int) ((getHeight() / 2) - ((mPaint.descent() + mPaint.ascent()) / 2));
int yPos = (getHeight() / 2) - mYOffset;
canvas.drawText("" + mInitial, xPos, yPos, mPaint);
}
}