From b80cdf9f1e8bf18f676e9b8e060351fd4a89ec28 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Sat, 14 Nov 2015 18:44:50 -0500 Subject: [PATCH] Simplified obtainTypeface method --- .../moez/QKSMS/common/TypefaceManager.java | 162 ++---------------- .../ui/view/AutoCompleteContactView.java | 2 +- .../com/moez/QKSMS/ui/view/QKEditText.java | 4 +- .../com/moez/QKSMS/ui/view/QKTextView.java | 4 +- 4 files changed, 24 insertions(+), 148 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/common/TypefaceManager.java b/QKSMS/src/main/java/com/moez/QKSMS/common/TypefaceManager.java index fba75449..7fcc492d 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/common/TypefaceManager.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/common/TypefaceManager.java @@ -20,174 +20,59 @@ public class TypefaceManager { return typeface; } - public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight, int textStyle) throws IllegalArgumentException { - int typeface; - if (fontFamily == FontFamily.ROBOTO) { - if (textWeight == TextWeight.NORMAL) { - switch (textStyle) { - case TextStyle.NORMAL: + public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight) throws IllegalArgumentException { + int typeface = Typefaces.ROBOTO_REGULAR; + switch (fontFamily) { + case FontFamily.ROBOTO: + case FontFamily.LATO: + switch (textWeight) { + case TextWeight.NORMAL: typeface = Typefaces.ROBOTO_REGULAR; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.THIN) { - switch (textStyle) { - case TextStyle.NORMAL: + case TextWeight.THIN: typeface = Typefaces.ROBOTO_THIN; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.LIGHT) { - switch (textStyle) { - case TextStyle.NORMAL: + case TextWeight.LIGHT: typeface = Typefaces.ROBOTO_LIGHT; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.MEDIUM) { - switch (textStyle) { - case TextStyle.NORMAL: + case TextWeight.MEDIUM: typeface = Typefaces.ROBOTO_MEDIUM; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); } - } else if (textWeight == TextWeight.BOLD) { - switch (textStyle) { - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else { - throw new IllegalArgumentException("`textWeight` attribute value " + textWeight + - " is not supported for this font family " + fontFamily); - } - } else if (fontFamily == FontFamily.ROBOTO_CONDENSED) { - if (textWeight == TextWeight.NORMAL) { - switch (textStyle) { - case TextStyle.NORMAL: + break; + + case FontFamily.ROBOTO_CONDENSED: + switch (textWeight) { + case TextWeight.LIGHT: // Condensed light is no longer supported + case TextWeight.NORMAL: typeface = Typefaces.ROBOTO_CONDENSED_REGULAR; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.LIGHT) { - switch (textStyle) { - case TextStyle.NORMAL: - typeface = Typefaces.ROBOTO_CONDENSED_LIGHT; - break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.BOLD) { - switch (textStyle) { - case TextStyle.NORMAL: + case TextWeight.BOLD: typeface = Typefaces.ROBOTO_CONDENSED_BOLD; break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); } - } else { - throw new IllegalArgumentException("`textWeight` attribute value " + textWeight + - " is not supported for this font family " + fontFamily); - } - } else if (fontFamily == FontFamily.LATO) { - if (textWeight == TextWeight.NORMAL) { - switch (textStyle) { - case TextStyle.NORMAL: - typeface = Typefaces.LATO_REGULAR; - break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.THIN) { - switch (textStyle) { - case TextStyle.NORMAL: - typeface = Typefaces.LATO_THIN; - break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.LIGHT) { - switch (textStyle) { - case TextStyle.NORMAL: - typeface = Typefaces.LATO_LIGHT; - break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.MEDIUM) { - switch (textStyle) { - case TextStyle.NORMAL: - typeface = Typefaces.LATO_MEDIUM; - break; - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else if (textWeight == TextWeight.BOLD) { - switch (textStyle) { - default: - throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + - " is not supported for this fontFamily " + fontFamily + - " and textWeight " + textWeight); - } - } else { - throw new IllegalArgumentException("`textWeight` attribute value " + textWeight + - " is not supported for this font family " + fontFamily); - } - } else { - throw new IllegalArgumentException("Unknown `fontFamily` attribute value " + fontFamily); + break; } + return obtainTypeface(typeface); } private static Typeface createTypeface(int typefaceValue) throws IllegalArgumentException { switch (typefaceValue) { - case Typefaces.LATO_THIN: case Typefaces.ROBOTO_THIN: return Typeface.create("sans-serif-thin", Typeface.NORMAL); - case Typefaces.LATO_LIGHT: case Typefaces.ROBOTO_LIGHT: return Typeface.create("sans-serif-light", Typeface.NORMAL); - case Typefaces.LATO_REGULAR: case Typefaces.ROBOTO_REGULAR: return Typeface.create("sans-serif", Typeface.NORMAL); - case Typefaces.LATO_MEDIUM: case Typefaces.ROBOTO_MEDIUM: String name = Build.VERSION.SDK_INT >= 21 ? "sans-serif-medium" : "sans-serif"; int style = Build.VERSION.SDK_INT >= 21 ? Typeface.NORMAL : Typeface.BOLD; return Typeface.create(name, style); - case Typefaces.ROBOTO_CONDENSED_LIGHT: case Typefaces.ROBOTO_CONDENSED_REGULAR: return Typeface.create("sans-serif-condensed", Typeface.NORMAL); @@ -208,13 +93,8 @@ public class TypefaceManager { public final static int ROBOTO_LIGHT = 2; public final static int ROBOTO_REGULAR = 4; public final static int ROBOTO_MEDIUM = 6; - @Deprecated public final static int ROBOTO_CONDENSED_LIGHT = 12; public final static int ROBOTO_CONDENSED_REGULAR = 14; public final static int ROBOTO_CONDENSED_BOLD = 16; - @Deprecated public final static int LATO_THIN = 22; - @Deprecated public final static int LATO_LIGHT = 24; - @Deprecated public final static int LATO_REGULAR = 26; - @Deprecated public final static int LATO_MEDIUM = 28; } public class FontFamily { @@ -231,8 +111,4 @@ public class TypefaceManager { public static final int BOLD = 4; } - public class TextStyle { - public static final int NORMAL = 0; - } - } diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java index fe930494..9415444f 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java @@ -58,7 +58,7 @@ public class AutoCompleteContactView extends RecipientEditTextView implements Li @Override public void refresh() { setTypeface(TypefaceManager.obtainTypeface(FontManager.getFontFamily(mContext), - FontManager.getFontWeight(mContext, false), TypefaceManager.TextStyle.NORMAL)); + FontManager.getFontWeight(mContext, false))); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); setTextColor(ThemeManager.getTextOnBackgroundPrimary()); diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java index 6e2cfd0a..85bb5a07 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java @@ -97,8 +97,8 @@ public class QKEditText extends android.widget.EditText implements LiveView { // Typeface and colors int fontFamily = FontManager.getFontFamily(mContext); int fontWeight = FontManager.getFontWeight(mContext, false); - setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight, - TypefaceManager.TextStyle.NORMAL)); + setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight + )); setTextColor(ThemeManager.getTextOnBackgroundPrimary()); setHintTextColor(ThemeManager.getTextOnBackgroundSecondary()); diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java index 5599ef6f..8876c05f 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java @@ -162,8 +162,8 @@ public class QKTextView extends TextView implements LiveView { // Typeface int fontFamily = FontManager.getFontFamily(mContext); int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType)); - setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight, - TypefaceManager.TextStyle.NORMAL)); + setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight + )); // Text size and color setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));