DialogUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.afollestad.materialdialogs.util;
  2. import android.content.Context;
  3. import android.content.DialogInterface;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Color;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.Build;
  8. import android.support.annotation.AttrRes;
  9. import android.util.TypedValue;
  10. import android.view.View;
  11. import android.view.inputmethod.InputMethodManager;
  12. import com.afollestad.materialdialogs.GravityEnum;
  13. import com.afollestad.materialdialogs.MaterialDialog;
  14. /**
  15. * @author Aidan Follestad (afollestad)
  16. */
  17. public class DialogUtils {
  18. public static int adjustAlpha(int color, @SuppressWarnings("SameParameterValue") float factor) {
  19. int alpha = Math.round(Color.alpha(color) * factor);
  20. int red = Color.red(color);
  21. int green = Color.green(color);
  22. int blue = Color.blue(color);
  23. return Color.argb(alpha, red, green, blue);
  24. }
  25. public static int resolveColor(Context context, @AttrRes int attr) {
  26. return resolveColor(context, attr, 0);
  27. }
  28. public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
  29. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  30. try {
  31. return a.getColor(0, fallback);
  32. } finally {
  33. a.recycle();
  34. }
  35. }
  36. public static String resolveString(Context context, @AttrRes int attr) {
  37. TypedValue v = new TypedValue();
  38. context.getTheme().resolveAttribute(attr, v, true);
  39. return (String) v.string;
  40. }
  41. private static int gravityEnumToAttrInt(GravityEnum value) {
  42. switch (value) {
  43. case CENTER:
  44. return 1;
  45. case END:
  46. return 2;
  47. default:
  48. return 0;
  49. }
  50. }
  51. public static GravityEnum resolveGravityEnum(Context context, @AttrRes int attr, GravityEnum defaultGravity) {
  52. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  53. try {
  54. switch (a.getInt(0, gravityEnumToAttrInt(defaultGravity))) {
  55. case 1:
  56. return GravityEnum.CENTER;
  57. case 2:
  58. return GravityEnum.END;
  59. default:
  60. return GravityEnum.START;
  61. }
  62. } finally {
  63. a.recycle();
  64. }
  65. }
  66. public static Drawable resolveDrawable(Context context, @AttrRes int attr) {
  67. return resolveDrawable(context, attr, null);
  68. }
  69. private static Drawable resolveDrawable(Context context, @AttrRes int attr, @SuppressWarnings("SameParameterValue") Drawable fallback) {
  70. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  71. try {
  72. Drawable d = a.getDrawable(0);
  73. if (d == null && fallback != null)
  74. d = fallback;
  75. return d;
  76. } finally {
  77. a.recycle();
  78. }
  79. }
  80. public static int resolveDimension(Context context, @AttrRes int attr) {
  81. return resolveDimension(context, attr, -1);
  82. }
  83. private static int resolveDimension(Context context, @AttrRes int attr, int fallback) {
  84. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  85. try {
  86. return a.getDimensionPixelSize(0, fallback);
  87. } finally {
  88. a.recycle();
  89. }
  90. }
  91. public static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) {
  92. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
  93. try {
  94. return a.getBoolean(0, fallback);
  95. } finally {
  96. a.recycle();
  97. }
  98. }
  99. public static boolean resolveBoolean(Context context, @AttrRes int attr) {
  100. return resolveBoolean(context, attr, false);
  101. }
  102. public static boolean isColorDark(int color) {
  103. double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
  104. return darkness >= 0.5;
  105. }
  106. public static void setBackgroundCompat(View view, Drawable d) {
  107. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  108. //noinspection deprecation
  109. view.setBackgroundDrawable(d);
  110. } else {
  111. view.setBackground(d);
  112. }
  113. }
  114. public static void showKeyboard(DialogInterface di, final MaterialDialog.Builder builder) {
  115. final MaterialDialog dialog = (MaterialDialog) di;
  116. if (dialog.getInputEditText() == null) return;
  117. dialog.getInputEditText().post(new Runnable() {
  118. @Override
  119. public void run() {
  120. dialog.getInputEditText().requestFocus();
  121. InputMethodManager imm = (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  122. if (imm != null)
  123. imm.showSoftInput(dialog.getInputEditText(), InputMethodManager.SHOW_IMPLICIT);
  124. }
  125. });
  126. }
  127. public static void hideKeyboard(DialogInterface di, final MaterialDialog.Builder builder) {
  128. final MaterialDialog dialog = (MaterialDialog) di;
  129. if (dialog.getInputEditText() == null) return;
  130. dialog.getInputEditText().post(new Runnable() {
  131. @Override
  132. public void run() {
  133. dialog.getInputEditText().requestFocus();
  134. InputMethodManager imm = (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  135. if (imm != null)
  136. imm.hideSoftInputFromWindow(dialog.getInputEditText().getWindowToken(), 0);
  137. }
  138. });
  139. }
  140. }