PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/dialog-core/src/main/java/com/afollestad/materialdialogs/internal/MDTintHelper.java

https://bitbucket.org/josholadele/streetlaw360-android
Java | 191 lines | 177 code | 12 blank | 2 comment | 24 complexity | 2b648af2de904d748082998ffc67c8f0 MD5 | raw file
  1. package com.afollestad.materialdialogs.internal;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.res.ColorStateList;
  5. import android.graphics.PorterDuff;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.Build;
  8. import android.support.annotation.ColorInt;
  9. import android.support.v4.content.ContextCompat;
  10. import android.support.v4.graphics.drawable.DrawableCompat;
  11. import android.support.v7.widget.AppCompatEditText;
  12. import android.util.Log;
  13. import android.widget.CheckBox;
  14. import android.widget.EditText;
  15. import android.widget.ProgressBar;
  16. import android.widget.RadioButton;
  17. import android.widget.SeekBar;
  18. import android.widget.TextView;
  19. import com.afollestad.materialdialogs.R;
  20. import com.afollestad.materialdialogs.util.DialogUtils;
  21. import java.lang.reflect.Field;
  22. /** Tints widgets */
  23. @SuppressLint("PrivateResource")
  24. public class MDTintHelper {
  25. public static void setTint(RadioButton radioButton, ColorStateList colors) {
  26. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
  27. radioButton.setButtonTintList(colors);
  28. } else {
  29. Drawable radioDrawable =
  30. ContextCompat.getDrawable(radioButton.getContext(), R.drawable.abc_btn_radio_material);
  31. Drawable d = DrawableCompat.wrap(radioDrawable);
  32. DrawableCompat.setTintList(d, colors);
  33. radioButton.setButtonDrawable(d);
  34. }
  35. }
  36. public static void setTint(RadioButton radioButton, @ColorInt int color) {
  37. final int disabledColor = DialogUtils.getDisabledColor(radioButton.getContext());
  38. ColorStateList sl =
  39. new ColorStateList(
  40. new int[][] {
  41. new int[] {android.R.attr.state_enabled, -android.R.attr.state_checked},
  42. new int[] {android.R.attr.state_enabled, android.R.attr.state_checked},
  43. new int[] {-android.R.attr.state_enabled, -android.R.attr.state_checked},
  44. new int[] {-android.R.attr.state_enabled, android.R.attr.state_checked}
  45. },
  46. new int[] {
  47. DialogUtils.resolveColor(radioButton.getContext(), R.attr.colorControlNormal),
  48. color,
  49. disabledColor,
  50. disabledColor
  51. });
  52. setTint(radioButton, sl);
  53. }
  54. public static void setTint(CheckBox box, ColorStateList colors) {
  55. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
  56. box.setButtonTintList(colors);
  57. } else {
  58. Drawable checkDrawable =
  59. ContextCompat.getDrawable(box.getContext(), R.drawable.abc_btn_check_material);
  60. Drawable drawable = DrawableCompat.wrap(checkDrawable);
  61. DrawableCompat.setTintList(drawable, colors);
  62. box.setButtonDrawable(drawable);
  63. }
  64. }
  65. public static void setTint(CheckBox box, @ColorInt int color) {
  66. final int disabledColor = DialogUtils.getDisabledColor(box.getContext());
  67. ColorStateList sl =
  68. new ColorStateList(
  69. new int[][] {
  70. new int[] {android.R.attr.state_enabled, -android.R.attr.state_checked},
  71. new int[] {android.R.attr.state_enabled, android.R.attr.state_checked},
  72. new int[] {-android.R.attr.state_enabled, -android.R.attr.state_checked},
  73. new int[] {-android.R.attr.state_enabled, android.R.attr.state_checked}
  74. },
  75. new int[] {
  76. DialogUtils.resolveColor(box.getContext(), R.attr.colorControlNormal),
  77. color,
  78. disabledColor,
  79. disabledColor
  80. });
  81. setTint(box, sl);
  82. }
  83. public static void setTint(SeekBar seekBar, @ColorInt int color) {
  84. ColorStateList s1 = ColorStateList.valueOf(color);
  85. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  86. seekBar.setThumbTintList(s1);
  87. seekBar.setProgressTintList(s1);
  88. } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
  89. Drawable progressDrawable = DrawableCompat.wrap(seekBar.getProgressDrawable());
  90. seekBar.setProgressDrawable(progressDrawable);
  91. DrawableCompat.setTintList(progressDrawable, s1);
  92. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  93. Drawable thumbDrawable = DrawableCompat.wrap(seekBar.getThumb());
  94. DrawableCompat.setTintList(thumbDrawable, s1);
  95. seekBar.setThumb(thumbDrawable);
  96. }
  97. } else {
  98. PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
  99. if (seekBar.getIndeterminateDrawable() != null) {
  100. seekBar.getIndeterminateDrawable().setColorFilter(color, mode);
  101. }
  102. if (seekBar.getProgressDrawable() != null) {
  103. seekBar.getProgressDrawable().setColorFilter(color, mode);
  104. }
  105. }
  106. }
  107. public static void setTint(ProgressBar progressBar, @ColorInt int color) {
  108. setTint(progressBar, color, false);
  109. }
  110. private static void setTint(
  111. ProgressBar progressBar, @ColorInt int color, boolean skipIndeterminate) {
  112. ColorStateList sl = ColorStateList.valueOf(color);
  113. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  114. progressBar.setProgressTintList(sl);
  115. progressBar.setSecondaryProgressTintList(sl);
  116. if (!skipIndeterminate) {
  117. progressBar.setIndeterminateTintList(sl);
  118. }
  119. } else {
  120. PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
  121. if (!skipIndeterminate && progressBar.getIndeterminateDrawable() != null) {
  122. progressBar.getIndeterminateDrawable().setColorFilter(color, mode);
  123. }
  124. if (progressBar.getProgressDrawable() != null) {
  125. progressBar.getProgressDrawable().setColorFilter(color, mode);
  126. }
  127. }
  128. }
  129. private static ColorStateList createEditTextColorStateList(Context context, @ColorInt int color) {
  130. int[][] states = new int[3][];
  131. int[] colors = new int[3];
  132. int i = 0;
  133. states[i] = new int[] {-android.R.attr.state_enabled};
  134. colors[i] = DialogUtils.resolveColor(context, R.attr.colorControlNormal);
  135. i++;
  136. states[i] = new int[] {-android.R.attr.state_pressed, -android.R.attr.state_focused};
  137. colors[i] = DialogUtils.resolveColor(context, R.attr.colorControlNormal);
  138. i++;
  139. states[i] = new int[] {};
  140. colors[i] = color;
  141. return new ColorStateList(states, colors);
  142. }
  143. @SuppressLint("RestrictedApi")
  144. public static void setTint(EditText editText, @ColorInt int color) {
  145. ColorStateList editTextColorStateList =
  146. createEditTextColorStateList(editText.getContext(), color);
  147. if (editText instanceof AppCompatEditText) {
  148. //noinspection RestrictedApi
  149. ((AppCompatEditText) editText).setSupportBackgroundTintList(editTextColorStateList);
  150. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  151. editText.setBackgroundTintList(editTextColorStateList);
  152. }
  153. setCursorTint(editText, color);
  154. }
  155. private static void setCursorTint(EditText editText, @ColorInt int color) {
  156. try {
  157. Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
  158. fCursorDrawableRes.setAccessible(true);
  159. int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
  160. Field fEditor = TextView.class.getDeclaredField("mEditor");
  161. fEditor.setAccessible(true);
  162. Object editor = fEditor.get(editText);
  163. Class<?> clazz = editor.getClass();
  164. Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
  165. fCursorDrawable.setAccessible(true);
  166. Drawable[] drawables = new Drawable[2];
  167. drawables[0] = ContextCompat.getDrawable(editText.getContext(), mCursorDrawableRes);
  168. drawables[1] = ContextCompat.getDrawable(editText.getContext(), mCursorDrawableRes);
  169. drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
  170. drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
  171. fCursorDrawable.set(editor, drawables);
  172. } catch (NoSuchFieldException e1) {
  173. Log.d("MDTintHelper", "Device issue with cursor tinting: " + e1.getMessage());
  174. e1.printStackTrace();
  175. } catch (Exception e2) {
  176. e2.printStackTrace();
  177. }
  178. }
  179. }