From fcb0892f12ed239db4a0ca2b12963955286fe517 Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Sat, 14 Nov 2015 08:31:48 +0300 Subject: [PATCH] phonespan added --- .../components/telephony/PhoneSpan.java | 42 +++++++++++++++++++ .../roboswag/components/utils/Typefaces.java | 18 ++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/roboswag/components/telephony/PhoneSpan.java diff --git a/src/main/java/org/roboswag/components/telephony/PhoneSpan.java b/src/main/java/org/roboswag/components/telephony/PhoneSpan.java new file mode 100644 index 0000000..7c3e234 --- /dev/null +++ b/src/main/java/org/roboswag/components/telephony/PhoneSpan.java @@ -0,0 +1,42 @@ +package org.roboswag.components.telephony; + +import android.content.Intent; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.text.TextPaint; +import android.text.style.URLSpan; +import android.view.View; + +import org.roboswag.core.log.Lc; + +/** + * Created by Gavriil Sitnikov on 14/11/2015. + * TODO: fill description + */ +public class PhoneSpan extends URLSpan { + + public PhoneSpan(@NonNull final String phoneNumber) { + super(phoneNumber); + } + + @SuppressWarnings("PMD.AvoidCatchingThrowable") + @Override + public void onClick(final View widget) { + super.onClick(widget); + try { + final Intent intent = new Intent(Intent.ACTION_DIAL); + intent.setData(Uri.parse(getURL())); + widget.getContext().startActivity(intent); + // it should catch throwable to not crash in production if there are problems with startActivity() + } catch (Throwable throwable) { + Lc.fatalException(throwable); + } + } + + @Override + public void updateDrawState(final TextPaint ds) { + super.updateDrawState(ds); + ds.setUnderlineText(false); + } + +} diff --git a/src/main/java/org/roboswag/components/utils/Typefaces.java b/src/main/java/org/roboswag/components/utils/Typefaces.java index 94c326f..83c1a04 100644 --- a/src/main/java/org/roboswag/components/utils/Typefaces.java +++ b/src/main/java/org/roboswag/components/utils/Typefaces.java @@ -29,6 +29,8 @@ import android.widget.TextView; import org.roboswag.components.R; import org.roboswag.components.views.TypefacedText; +import org.roboswag.core.log.Lc; +import org.roboswag.core.utils.ShouldNotHappenException; import java.io.IOException; import java.util.Arrays; @@ -44,6 +46,12 @@ public final class Typefaces { private static final Map TYPEFACES_MAP = new HashMap<>(); + private static boolean allowEmptyCustomTypeface = true; + + public static void setAllowEmptyCustomTypeface(final boolean allowDefaultTypefacedText) { + Typefaces.allowEmptyCustomTypeface = allowDefaultTypefacedText; + } + /* Returns typeface by name from assets 'fonts' folder */ @NonNull public static Typeface getByName(@NonNull final Context context, @NonNull final String name) { @@ -80,9 +88,13 @@ public final class Typefaces { typedArray.recycle(); } - if (customTypeface != null && !typefacedText.isInEditMode()) { - final Typeface typeface = typefacedText.getTypeface(); - typefacedText.setTypeface(customTypeface, typeface == null ? Typeface.NORMAL : typeface.getStyle()); + if (customTypeface != null) { + if (!typefacedText.isInEditMode()) { + final Typeface typeface = typefacedText.getTypeface(); + typefacedText.setTypeface(customTypeface, typeface == null ? Typeface.NORMAL : typeface.getStyle()); + } + } else if (!allowEmptyCustomTypeface) { + Lc.fatalException(new ShouldNotHappenException("TypefacedText has no customTypeface attribute: " + typefacedText)); } }