|
@@ -18,10 +18,13 @@ import android.support.v4.content.ContextCompat;
|
|
|
import android.util.TypedValue;
|
|
|
import android.view.View;
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
+
|
|
|
import com.afollestad.materialdialogs.GravityEnum;
|
|
|
import com.afollestad.materialdialogs.MaterialDialog;
|
|
|
|
|
|
-/** @author Aidan Follestad (afollestad) */
|
|
|
+/**
|
|
|
+ * @author Aidan Follestad (afollestad)
|
|
|
+ */
|
|
|
public class DialogUtils {
|
|
|
|
|
|
// @SuppressWarnings("ConstantConditions")
|
|
@@ -43,7 +46,7 @@ public class DialogUtils {
|
|
|
|
|
|
@ColorInt
|
|
|
public static int adjustAlpha(
|
|
|
- @ColorInt int color, @SuppressWarnings("SameParameterValue") float factor) {
|
|
|
+ @ColorInt int color, @SuppressWarnings("SameParameterValue") float factor) {
|
|
|
int alpha = Math.round(Color.alpha(color) * factor);
|
|
|
int red = Color.red(color);
|
|
|
int green = Color.green(color);
|
|
@@ -58,7 +61,7 @@ public class DialogUtils {
|
|
|
|
|
|
@ColorInt
|
|
|
public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {attr});
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
|
|
|
try {
|
|
|
return a.getColor(0, fallback);
|
|
|
} finally {
|
|
@@ -68,15 +71,15 @@ public class DialogUtils {
|
|
|
|
|
|
// Try to resolve the colorAttr attribute.
|
|
|
public static ColorStateList resolveActionTextColorStateList(
|
|
|
- Context context, @AttrRes int colorAttr, ColorStateList fallback) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {colorAttr});
|
|
|
+ Context context, @AttrRes int colorAttr, ColorStateList fallback) {
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{colorAttr});
|
|
|
try {
|
|
|
final TypedValue value = a.peekValue(0);
|
|
|
if (value == null) {
|
|
|
return fallback;
|
|
|
}
|
|
|
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT
|
|
|
- && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
|
|
|
+ && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
|
|
|
return getActionTextStateList(context, value.data);
|
|
|
} else {
|
|
|
final ColorStateList stateList = a.getColorStateList(0);
|
|
@@ -97,7 +100,7 @@ public class DialogUtils {
|
|
|
final TypedValue value = new TypedValue();
|
|
|
context.getResources().getValue(colorId, value, true);
|
|
|
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT
|
|
|
- && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
|
|
|
+ && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
|
|
|
return getActionTextStateList(context, value.data);
|
|
|
} else {
|
|
|
|
|
@@ -117,7 +120,7 @@ public class DialogUtils {
|
|
|
* the specified Context's theme.
|
|
|
*
|
|
|
* @param colorId The desired resource identifier, as generated by the aapt tool. This integer
|
|
|
- * encodes the package, type, and resource entry. The value 0 is an invalid identifier.
|
|
|
+ * encodes the package, type, and resource entry. The value 0 is an invalid identifier.
|
|
|
* @return A single color value in the form 0xAARRGGBB.
|
|
|
*/
|
|
|
@ColorInt
|
|
@@ -143,8 +146,8 @@ public class DialogUtils {
|
|
|
}
|
|
|
|
|
|
public static GravityEnum resolveGravityEnum(
|
|
|
- Context context, @AttrRes int attr, GravityEnum defaultGravity) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {attr});
|
|
|
+ Context context, @AttrRes int attr, GravityEnum defaultGravity) {
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
|
|
|
try {
|
|
|
switch (a.getInt(0, gravityEnumToAttrInt(defaultGravity))) {
|
|
|
case 1:
|
|
@@ -164,10 +167,10 @@ public class DialogUtils {
|
|
|
}
|
|
|
|
|
|
private static Drawable resolveDrawable(
|
|
|
- Context context,
|
|
|
- @AttrRes int attr,
|
|
|
- @SuppressWarnings("SameParameterValue") Drawable fallback) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {attr});
|
|
|
+ Context context,
|
|
|
+ @AttrRes int attr,
|
|
|
+ @SuppressWarnings("SameParameterValue") Drawable fallback) {
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
|
|
|
try {
|
|
|
Drawable d = a.getDrawable(0);
|
|
|
if (d == null && fallback != null) {
|
|
@@ -184,7 +187,7 @@ public class DialogUtils {
|
|
|
}
|
|
|
|
|
|
private static int resolveDimension(Context context, @AttrRes int attr, int fallback) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {attr});
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
|
|
|
try {
|
|
|
return a.getDimensionPixelSize(0, fallback);
|
|
|
} finally {
|
|
@@ -193,7 +196,7 @@ public class DialogUtils {
|
|
|
}
|
|
|
|
|
|
public static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) {
|
|
|
- TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {attr});
|
|
|
+ TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
|
|
|
try {
|
|
|
return a.getBoolean(0, fallback);
|
|
|
} finally {
|
|
@@ -207,9 +210,9 @@ public class DialogUtils {
|
|
|
|
|
|
public static boolean isColorDark(@ColorInt int color) {
|
|
|
double darkness =
|
|
|
- 1
|
|
|
- - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color))
|
|
|
- / 255;
|
|
|
+ 1
|
|
|
+ - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color))
|
|
|
+ / 255;
|
|
|
return darkness >= 0.5;
|
|
|
}
|
|
|
|
|
@@ -223,40 +226,44 @@ public class DialogUtils {
|
|
|
}
|
|
|
|
|
|
public static void showKeyboard(
|
|
|
- @NonNull final DialogInterface di, @NonNull final MaterialDialog.Builder builder) {
|
|
|
+ @NonNull final DialogInterface di, @NonNull final MaterialDialog.Builder builder) {
|
|
|
final MaterialDialog dialog = (MaterialDialog) di;
|
|
|
if (dialog.getInputEditText() == null) {
|
|
|
return;
|
|
|
}
|
|
|
dialog
|
|
|
- .getInputEditText()
|
|
|
- .post(
|
|
|
- new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- dialog.getInputEditText().requestFocus();
|
|
|
- InputMethodManager imm =
|
|
|
- (InputMethodManager)
|
|
|
- builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
- if (imm != null) {
|
|
|
- imm.showSoftInput(dialog.getInputEditText(), InputMethodManager.SHOW_IMPLICIT);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ .getInputEditText()
|
|
|
+ .post(
|
|
|
+ new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ dialog.getInputEditText().requestFocus();
|
|
|
+ InputMethodManager imm =
|
|
|
+ (InputMethodManager)
|
|
|
+ builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
+ if (imm != null) {
|
|
|
+ imm.showSoftInput(dialog.getInputEditText(), InputMethodManager.SHOW_IMPLICIT);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public static void hideKeyboard(
|
|
|
- @NonNull final DialogInterface di, @NonNull final MaterialDialog.Builder builder) {
|
|
|
+ @NonNull final DialogInterface di, @NonNull final MaterialDialog.Builder builder) {
|
|
|
final MaterialDialog dialog = (MaterialDialog) di;
|
|
|
if (dialog.getInputEditText() == null) {
|
|
|
return;
|
|
|
}
|
|
|
InputMethodManager imm =
|
|
|
- (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
+ (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
if (imm != null) {
|
|
|
final View currentFocus = dialog.getCurrentFocus();
|
|
|
- final IBinder windowToken =
|
|
|
- currentFocus != null ? currentFocus.getWindowToken() : dialog.getView().getWindowToken();
|
|
|
+ IBinder windowToken = null;
|
|
|
+ if (currentFocus != null) {
|
|
|
+ windowToken = currentFocus.getWindowToken();
|
|
|
+ } else if (dialog.getView() != null) {
|
|
|
+ windowToken = dialog.getView().getWindowToken();
|
|
|
+ }
|
|
|
if (windowToken != null) {
|
|
|
imm.hideSoftInputFromWindow(windowToken, 0);
|
|
|
}
|
|
@@ -265,16 +272,16 @@ public class DialogUtils {
|
|
|
|
|
|
public static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {
|
|
|
final int fallBackButtonColor =
|
|
|
- DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
|
|
|
+ DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
|
|
|
if (newPrimaryColor == 0) {
|
|
|
newPrimaryColor = fallBackButtonColor;
|
|
|
}
|
|
|
int[][] states =
|
|
|
- new int[][] {
|
|
|
- new int[] {-android.R.attr.state_enabled}, // disabled
|
|
|
- new int[] {} // enabled
|
|
|
- };
|
|
|
- int[] colors = new int[] {DialogUtils.adjustAlpha(newPrimaryColor, 0.4f), newPrimaryColor};
|
|
|
+ new int[][]{
|
|
|
+ new int[]{-android.R.attr.state_enabled}, // disabled
|
|
|
+ new int[]{} // enabled
|
|
|
+ };
|
|
|
+ int[] colors = new int[]{DialogUtils.adjustAlpha(newPrimaryColor, 0.4f), newPrimaryColor};
|
|
|
return new ColorStateList(states, colors);
|
|
|
}
|
|
|
|