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.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo; import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView; import android.widget.ImageView;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.TypefaceManager; import com.moez.QKSMS.common.TypefaceManager;
@@ -53,6 +54,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
private String mInitial = "#"; private String mInitial = "#";
private Paint mPaint; private Paint mPaint;
private Drawable mDefaultDrawable; private Drawable mDefaultDrawable;
private int mYOffset;
/** /**
* When setImageDrawable is called with a drawable, we circle crop to size of this view and use * 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(); a.recycle();
mYOffset = (int) ((mPaint.descent() + mPaint.ascent()) / 2);
setOnClickListener(this); setOnClickListener(this);
LiveViewManager.registerView(QKPreference.THEME, this, key -> { LiveViewManager.registerView(QKPreference.THEME, this, key -> {
@@ -175,7 +179,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
if (mOriginalDrawable == null) super.setImageDrawable(null); if (mOriginalDrawable == null) super.setImageDrawable(null);
} else if (isPhoneNumberFormat(name)) { } else if (isPhoneNumberFormat(name)) {
mInitial = ""; mInitial = "";
super.setImageDrawable(mDefaultDrawable); if (mOriginalDrawable == null) super.setImageDrawable(mDefaultDrawable);
} else { } else {
mInitial = "" + name.toUpperCase().charAt(0); mInitial = "" + name.toUpperCase().charAt(0);
if (mOriginalDrawable == null) super.setImageDrawable(null); if (mOriginalDrawable == null) super.setImageDrawable(null);
@@ -185,9 +189,10 @@ public class AvatarView extends ImageView implements View.OnClickListener {
} }
private boolean isPhoneNumberFormat(String name) { private boolean isPhoneNumberFormat(String name) {
if (TextUtils.isEmpty(name)) { // We already checked it in first "if" condition in "setContactName"
return false; // if (TextUtils.isEmpty(name)) {
} // return false;
// }
char c = name.charAt(0); char c = name.charAt(0);
return !name.contains("@") && (c == '+' || c == '(' || Character.isDigit(c)); return !name.contains("@") && (c == '+' || c == '(' || Character.isDigit(c));
@@ -276,7 +281,7 @@ public class AvatarView extends ImageView implements View.OnClickListener {
if (getDrawable() == null && !isInEditMode()) { if (getDrawable() == null && !isInEditMode()) {
int xPos = (getWidth() / 2); 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); canvas.drawText("" + mInitial, xPos, yPos, mPaint);
} }
} }