ソースを参照

All MDTintHelper methods have the same name, just different overloads. Also resolved #456, the library won't self destruct if font assets are taken out.

Aidan Follestad 10 年 前
コミット
9fd0d665e5

+ 2 - 2
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -337,7 +337,7 @@ class DialogInit {
         if (builder.indeterminateProgress || builder.progress > -2) {
             dialog.mProgress = (ProgressBar) dialog.view.findViewById(android.R.id.progress);
             if (dialog.mProgress == null) return;
-            MDTintHelper.setProgressBarTint(dialog.mProgress, builder.widgetColor);
+            MDTintHelper.setTint(dialog.mProgress, builder.widgetColor);
 
             if (!builder.indeterminateProgress) {
                 dialog.mProgress.setProgress(0);
@@ -391,7 +391,7 @@ class DialogInit {
         dialog.input.setSingleLine();
         dialog.input.setTextColor(builder.contentColor);
         dialog.input.setHintTextColor(DialogUtils.adjustAlpha(builder.contentColor, 0.3f));
-        MDTintHelper.setEditTextTint(dialog.input, dialog.mBuilder.widgetColor);
+        MDTintHelper.setTint(dialog.input, dialog.mBuilder.widgetColor);
     }
 
     private static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {

+ 9 - 3
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -551,11 +551,17 @@ public class MaterialDialog extends DialogBase implements
          * @return The Builder instance so you can chain calls to it.
          */
         public Builder typeface(String medium, String regular) {
-            if (medium != null)
+            if (medium != null) {
                 this.mediumFont = TypefaceHelper.get(this.context, medium);
-            if (regular != null)
+                if (this.mediumFont == null)
+                    throw new RuntimeException("No font asset found for " + medium);
+            }
+            if (regular != null) {
                 this.regularFont = TypefaceHelper.get(this.context, regular);
-            this.useCustomFonts = true;
+                if (this.regularFont == null)
+                    throw new RuntimeException("No font asset found for " + regular);
+            }
+            this.useCustomFonts = this.mediumFont != null || this.regularFont != null;
             return this;
         }
 

+ 2 - 2
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogAdapter.java

@@ -47,7 +47,7 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
                 @SuppressLint("CutPasteId")
                 RadioButton radio = (RadioButton) view.findViewById(R.id.control);
                 boolean selected = dialog.mBuilder.selectedIndex == index;
-                MDTintHelper.setRadioButtonTint(radio, dialog.mBuilder.widgetColor);
+                MDTintHelper.setTint(radio, dialog.mBuilder.widgetColor);
                 radio.setChecked(selected);
                 break;
             }
@@ -55,7 +55,7 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
                 @SuppressLint("CutPasteId")
                 CheckBox checkbox = (CheckBox) view.findViewById(R.id.control);
                 boolean selected = dialog.selectedIndicesList.contains(index);
-                MDTintHelper.setCheckBoxTint(checkbox, dialog.mBuilder.widgetColor);
+                MDTintHelper.setTint(checkbox, dialog.mBuilder.widgetColor);
                 checkbox.setChecked(selected);
                 break;
             }

+ 18 - 4
library/src/main/java/com/afollestad/materialdialogs/internal/MDTintHelper.java

@@ -9,6 +9,7 @@ import android.widget.CheckBox;
 import android.widget.EditText;
 import android.widget.ProgressBar;
 import android.widget.RadioButton;
+import android.widget.SeekBar;
 
 import com.afollestad.materialdialogs.R;
 import com.afollestad.materialdialogs.util.DialogUtils;
@@ -18,7 +19,7 @@ import com.afollestad.materialdialogs.util.DialogUtils;
  */
 public class MDTintHelper {
 
-    public static void setRadioButtonTint(RadioButton radioButton, int color) {
+    public static void setTint(RadioButton radioButton, int color) {
         ColorStateList sl = new ColorStateList(new int[][]{
                 new int[]{-android.R.attr.state_checked},
                 new int[]{android.R.attr.state_checked}
@@ -36,7 +37,7 @@ public class MDTintHelper {
         }
     }
 
-    public static void setProgressBarTint(ProgressBar progressBar, int color) {
+    public static void setTint(ProgressBar progressBar, int color) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             ColorStateList stateList = ColorStateList.valueOf(color);
             progressBar.setProgressTintList(stateList);
@@ -54,7 +55,7 @@ public class MDTintHelper {
         }
     }
 
-    public static void setEditTextTint(EditText editText, int color) {
+    public static void setTint(EditText editText, int color) {
         ColorStateList s1 = ColorStateList.valueOf(color);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             editText.setBackgroundTintList(s1);
@@ -66,7 +67,20 @@ public class MDTintHelper {
         }
     }
 
-    public static void setCheckBoxTint(CheckBox box, int color) {
+    public static void setTint(SeekBar seekBar, int color) {
+        ColorStateList s1 = ColorStateList.valueOf(color);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            seekBar.setThumbTintList(s1);
+            seekBar.setProgressTintList(s1);
+        } else {
+            seekBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
+                seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
+
+        }
+    }
+
+    public static void setTint(CheckBox box, int color) {
         ColorStateList sl = new ColorStateList(new int[][]{
                 new int[]{-android.R.attr.state_checked},
                 new int[]{android.R.attr.state_checked}

+ 1 - 1
library/src/main/java/com/afollestad/materialdialogs/prefs/MaterialEditTextPreference.java

@@ -95,7 +95,7 @@ public class MaterialEditTextPreference extends EditTextPreference {
         View layout = LayoutInflater.from(getContext()).inflate(R.layout.md_stub_inputpref, null);
         onBindDialogView(layout);
 
-        MDTintHelper.setEditTextTint(getEditText(), mColor);
+        MDTintHelper.setTint(getEditText(), mColor);
 
         TextView message = (TextView) layout.findViewById(android.R.id.message);
         if (getDialogMessage() != null && getDialogMessage().toString().length() > 0) {

+ 8 - 4
library/src/main/java/com/afollestad/materialdialogs/util/TypefaceHelper.java

@@ -31,10 +31,14 @@ public class TypefaceHelper {
     public static Typeface get(Context c, String name) {
         synchronized (cache) {
             if (!cache.containsKey(name)) {
-                Typeface t = Typeface.createFromAsset(
-                        c.getAssets(), String.format("fonts/%s.ttf", name));
-                cache.put(name, t);
-                return t;
+                try {
+                    Typeface t = Typeface.createFromAsset(
+                            c.getAssets(), String.format("fonts/%s.ttf", name));
+                    cache.put(name, t);
+                    return t;
+                } catch (RuntimeException e) {
+                    return null;
+                }
             }
             return cache.get(name);
         }

+ 2 - 2
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -506,10 +506,10 @@ public class MainActivity extends ActionBarActivity implements
 
         //Workaround for CheckBox theming  on API 10 until AppCompat fix it
         int widgetColor = ThemeSingleton.get().widgetColor;
-        MDTintHelper.setCheckBoxTint(checkbox,
+        MDTintHelper.setTint(checkbox,
                 widgetColor == 0 ? getResources().getColor(R.color.material_pink_500) : widgetColor);
 
-        MDTintHelper.setEditTextTint(passwordInput,
+        MDTintHelper.setTint(passwordInput,
                 widgetColor == 0 ? getResources().getColor(R.color.material_pink_500) : widgetColor);
 
         dialog.show();