Browse Source

Revert changes to accessing color resources

The switch to ContextCompat.getColor( ) forced users to depend on support-v4:23.x.x. To accommodate users who don't want to upgrade from support-v4:22.x.x, we manually switch off SDK version, to call deprecated context.getResources().getColor( ) method. This means we no longer need ContextCompat.getColor( ).
Jason Atwood 9 years ago
parent
commit
55be169f11

+ 3 - 2
commons/src/main/java/com/afollestad/materialdialogs/color/CircleView.java

@@ -15,11 +15,12 @@ import android.os.Build;
 import android.support.annotation.ColorInt;
 import android.support.annotation.ColorRes;
 import android.support.annotation.FloatRange;
-import android.support.v4.content.ContextCompat;
 import android.util.AttributeSet;
 import android.util.TypedValue;
 import android.widget.FrameLayout;
 
+import com.afollestad.materialdialogs.util.DialogUtils;
+
 public class CircleView extends FrameLayout {
 
     private final int borderWidthSmall;
@@ -82,7 +83,7 @@ public class CircleView extends FrameLayout {
 
     @Override
     public void setBackgroundResource(@ColorRes int color) {
-        setBackgroundColor(ContextCompat.getColor(getContext(), color));
+        setBackgroundColor(DialogUtils.getColor(getContext(), color));
     }
 
     /**

+ 1 - 1
commons/src/main/java/com/afollestad/materialdialogs/simplelist/MaterialSimpleListItem.java

@@ -95,7 +95,7 @@ public class MaterialSimpleListItem {
         }
 
         public Builder backgroundColorRes(@ColorRes int colorRes) {
-            return backgroundColor(ContextCompat.getColor(mContext, colorRes));
+            return backgroundColor(DialogUtils.getColor(mContext, colorRes));
         }
 
         public Builder backgroundColorAttr(@AttrRes int colorAttr) {

+ 10 - 11
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -21,7 +21,6 @@ import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.annotation.StringRes;
 import android.support.annotation.UiThread;
-import android.support.v4.content.ContextCompat;
 import android.support.v4.content.res.ResourcesCompat;
 import android.text.Editable;
 import android.text.TextUtils;
@@ -489,7 +488,7 @@ public class MaterialDialog extends DialogBase implements
 
         public Builder(@NonNull Context context) {
             this.context = context;
-            final int materialBlue = ContextCompat.getColor(context, R.color.md_material_blue_600);
+            final int materialBlue = DialogUtils.getColor(context, R.color.md_material_blue_600);
 
             // Retrieve default accent colors, which are used on the action buttons and progress bars
             this.widgetColor = DialogUtils.resolveColor(context, R.attr.colorAccent, materialBlue);
@@ -608,7 +607,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder buttonRippleColorRes(@ColorRes int colorRes) {
-            return buttonRippleColor(ContextCompat.getColor(this.context, colorRes));
+            return buttonRippleColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder buttonRippleColorAttr(@AttrRes int colorAttr) {
@@ -622,7 +621,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder titleColorRes(@ColorRes int colorRes) {
-            return titleColor(ContextCompat.getColor(this.context, colorRes));
+            return titleColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder titleColorAttr(@AttrRes int colorAttr) {
@@ -704,7 +703,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder contentColorRes(@ColorRes int colorRes) {
-            contentColor(ContextCompat.getColor(this.context, colorRes));
+            contentColor(DialogUtils.getColor(this.context, colorRes));
             return this;
         }
 
@@ -749,7 +748,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder itemColorRes(@ColorRes int colorRes) {
-            return itemColor(ContextCompat.getColor(this.context, colorRes));
+            return itemColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder itemColorAttr(@AttrRes int colorAttr) {
@@ -1036,7 +1035,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder widgetColorRes(@ColorRes int colorRes) {
-            return widgetColor(ContextCompat.getColor(this.context, colorRes));
+            return widgetColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder widgetColorAttr(@AttrRes int colorAttr) {
@@ -1050,7 +1049,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder dividerColorRes(@ColorRes int colorRes) {
-            return dividerColor(ContextCompat.getColor(this.context, colorRes));
+            return dividerColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder dividerColorAttr(@AttrRes int colorAttr) {
@@ -1063,7 +1062,7 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder backgroundColorRes(@ColorRes int colorRes) {
-            return backgroundColor(ContextCompat.getColor(this.context, colorRes));
+            return backgroundColor(DialogUtils.getColor(this.context, colorRes));
         }
 
         public Builder backgroundColorAttr(@AttrRes int colorAttr) {
@@ -1244,7 +1243,7 @@ public class MaterialDialog extends DialogBase implements
             this.inputMinLength = minLength;
             this.inputMaxLength = maxLength;
             if (errorColor == 0) {
-                this.inputRangeErrorColor = ContextCompat.getColor(context, R.color.md_edittext_error);
+                this.inputRangeErrorColor = DialogUtils.getColor(context, R.color.md_edittext_error);
             } else {
                 this.inputRangeErrorColor = errorColor;
             }
@@ -1257,7 +1256,7 @@ public class MaterialDialog extends DialogBase implements
         public Builder inputRangeRes(@IntRange(from = 0, to = Integer.MAX_VALUE) int minLength,
                                      @IntRange(from = 1, to = Integer.MAX_VALUE) int maxLength,
                                      @ColorRes int errorColor) {
-            return inputRange(minLength, maxLength, ContextCompat.getColor(context, errorColor));
+            return inputRange(minLength, maxLength, DialogUtils.getColor(context, errorColor));
         }
 
         public Builder alwaysCallInputCallback() {

+ 27 - 3
core/src/main/java/com/afollestad/materialdialogs/util/DialogUtils.java

@@ -9,7 +9,6 @@ import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.support.annotation.AttrRes;
 import android.support.annotation.ColorRes;
-import android.support.v4.content.ContextCompat;
 import android.util.TypedValue;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
@@ -84,7 +83,33 @@ public class DialogUtils {
         if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
             return getActionTextStateList(context, value.data);
         } else {
-            return ContextCompat.getColorStateList(context, colorId);
+
+            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
+                //noinspection deprecation
+                return context.getResources().getColorStateList(colorId);
+            } else {
+                return context.getColorStateList(colorId);
+            }
+        }
+    }
+
+    /**
+     * Returns a color associated with a particular resource ID
+     * <p/>
+     * Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
+     * color will be styled for 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.
+     * @return A single color value in the form 0xAARRGGBB.
+     */
+    public static int getColor(Context context, @ColorRes int colorId) {
+        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
+            //noinspection deprecation
+            return context.getResources().getColor(colorId);
+        } else {
+            return context.getColor(colorId);
         }
     }
 
@@ -205,7 +230,6 @@ public class DialogUtils {
         });
     }
 
-
     public static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {
         final int fallBackButtonColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
         if (newPrimaryColor == 0) newPrimaryColor = fallBackButtonColor;