DialogUtils.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.afollestad.materialdialogs.util;
  2. import android.content.Context;
  3. import android.content.DialogInterface;
  4. import android.content.res.ColorStateList;
  5. import android.content.res.TypedArray;
  6. import android.graphics.Color;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Build;
  9. import android.support.annotation.AttrRes;
  10. import android.support.annotation.ColorRes;
  11. import android.util.TypedValue;
  12. import android.view.View;
  13. import android.view.inputmethod.InputMethodManager;
  14. import com.afollestad.materialdialogs.GravityEnum;
  15. import com.afollestad.materialdialogs.MaterialDialog;
  16. /**
  17. * @author Aidan Follestad (afollestad)
  18. */
  19. public class DialogUtils {
  20. public static int adjustAlpha(int color, @SuppressWarnings("SameParameterValue") float factor) {
  21. int alpha = Math.round(Color.alpha(color) * factor);
  22. int red = Color.red(color);
  23. int green = Color.green(color);
  24. int blue = Color.blue(color);
  25. return Color.argb(alpha, red, green, blue);
  26. }
  27. public static int resolveColor(Context context, @AttrRes int attr) {
  28. return resolveColor(context, attr, 0);
  29. }
  30. public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
  31. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  32. try {
  33. return a.getColor(0, fallback);
  34. } finally {
  35. a.recycle();
  36. }
  37. }
  38. // Try to resolve the colorAttr attribute.
  39. public static ColorStateList resolveActionTextColorStateList(Context context, @AttrRes int colorAttr, ColorStateList fallback) {
  40. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{colorAttr});
  41. try {
  42. final TypedValue value = a.peekValue(0);
  43. if (value == null) {
  44. return fallback;
  45. }
  46. if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
  47. return getActionTextStateList(context, value.data);
  48. } else {
  49. final ColorStateList stateList = a.getColorStateList(0);
  50. if (stateList != null) {
  51. return stateList;
  52. } else {
  53. return fallback;
  54. }
  55. }
  56. } finally {
  57. a.recycle();
  58. }
  59. }
  60. // Get the specified color resource, creating a ColorStateList if the resource
  61. // points to a color value.
  62. public static ColorStateList getActionTextColorStateList(Context context, @ColorRes int colorId) {
  63. final TypedValue value = new TypedValue();
  64. context.getResources().getValue(colorId, value, true);
  65. if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
  66. return getActionTextStateList(context, value.data);
  67. } else {
  68. return context.getResources().getColorStateList(colorId);
  69. }
  70. }
  71. public static String resolveString(Context context, @AttrRes int attr) {
  72. TypedValue v = new TypedValue();
  73. context.getTheme().resolveAttribute(attr, v, true);
  74. return (String) v.string;
  75. }
  76. private static int gravityEnumToAttrInt(GravityEnum value) {
  77. switch (value) {
  78. case CENTER:
  79. return 1;
  80. case END:
  81. return 2;
  82. default:
  83. return 0;
  84. }
  85. }
  86. public static GravityEnum resolveGravityEnum(Context context, @AttrRes int attr, GravityEnum defaultGravity) {
  87. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  88. try {
  89. switch (a.getInt(0, gravityEnumToAttrInt(defaultGravity))) {
  90. case 1:
  91. return GravityEnum.CENTER;
  92. case 2:
  93. return GravityEnum.END;
  94. default:
  95. return GravityEnum.START;
  96. }
  97. } finally {
  98. a.recycle();
  99. }
  100. }
  101. public static Drawable resolveDrawable(Context context, @AttrRes int attr) {
  102. return resolveDrawable(context, attr, null);
  103. }
  104. private static Drawable resolveDrawable(Context context, @AttrRes int attr, @SuppressWarnings("SameParameterValue") Drawable fallback) {
  105. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  106. try {
  107. Drawable d = a.getDrawable(0);
  108. if (d == null && fallback != null)
  109. d = fallback;
  110. return d;
  111. } finally {
  112. a.recycle();
  113. }
  114. }
  115. public static int resolveDimension(Context context, @AttrRes int attr) {
  116. return resolveDimension(context, attr, -1);
  117. }
  118. private static int resolveDimension(Context context, @AttrRes int attr, int fallback) {
  119. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  120. try {
  121. return a.getDimensionPixelSize(0, fallback);
  122. } finally {
  123. a.recycle();
  124. }
  125. }
  126. public static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) {
  127. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  128. try {
  129. return a.getBoolean(0, fallback);
  130. } finally {
  131. a.recycle();
  132. }
  133. }
  134. public static boolean resolveBoolean(Context context, @AttrRes int attr) {
  135. return resolveBoolean(context, attr, false);
  136. }
  137. public static boolean isColorDark(int color) {
  138. double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
  139. return darkness >= 0.5;
  140. }
  141. public static void setBackgroundCompat(View view, Drawable d) {
  142. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  143. //noinspection deprecation
  144. view.setBackgroundDrawable(d);
  145. } else {
  146. view.setBackground(d);
  147. }
  148. }
  149. public static void showKeyboard(DialogInterface di, final MaterialDialog.Builder builder) {
  150. final MaterialDialog dialog = (MaterialDialog) di;
  151. if (dialog.getInputEditText() == null) return;
  152. dialog.getInputEditText().post(new Runnable() {
  153. @Override
  154. public void run() {
  155. dialog.getInputEditText().requestFocus();
  156. InputMethodManager imm = (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  157. if (imm != null)
  158. imm.showSoftInput(dialog.getInputEditText(), InputMethodManager.SHOW_IMPLICIT);
  159. }
  160. });
  161. }
  162. public static void hideKeyboard(DialogInterface di, final MaterialDialog.Builder builder) {
  163. final MaterialDialog dialog = (MaterialDialog) di;
  164. if (dialog.getInputEditText() == null) return;
  165. dialog.getInputEditText().post(new Runnable() {
  166. @Override
  167. public void run() {
  168. dialog.getInputEditText().requestFocus();
  169. InputMethodManager imm = (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  170. if (imm != null)
  171. imm.hideSoftInputFromWindow(dialog.getInputEditText().getWindowToken(), 0);
  172. }
  173. });
  174. }
  175. public static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {
  176. final int fallBackButtonColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
  177. if (newPrimaryColor == 0) newPrimaryColor = fallBackButtonColor;
  178. int[][] states = new int[][]{
  179. new int[]{-android.R.attr.state_enabled}, // disabled
  180. new int[]{} // enabled
  181. };
  182. int[] colors = new int[]{
  183. DialogUtils.adjustAlpha(newPrimaryColor, 0.4f),
  184. newPrimaryColor
  185. };
  186. return new ColorStateList(states, colors);
  187. }
  188. }