Browse Source

Switched uses of getContext() to custom getThemedContext() which returns themed wrapper

Aidan Follestad 10 years ago
parent
commit
0f6dbba34a

+ 6 - 6
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -67,9 +67,9 @@ class DialogInit {
         // Check if default library fonts should be used
         if (!builder.useCustomFonts) {
             if (builder.mediumFont == null)
-                builder.mediumFont = TypefaceHelper.get(dialog.getContext(), "Roboto-Medium");
+                builder.mediumFont = TypefaceHelper.get(dialog.getThemedContext(), "Roboto-Medium");
             if (builder.regularFont == null)
-                builder.regularFont = TypefaceHelper.get(dialog.getContext(), "Roboto-Regular");
+                builder.regularFont = TypefaceHelper.get(dialog.getThemedContext(), "Roboto-Regular");
         }
 
         // Set cancelable flag and dialog background color
@@ -155,7 +155,7 @@ class DialogInit {
         }
 
         // Setup divider color in case content scrolls
-        final int dividerFallback = DialogUtils.resolveColor(dialog.getContext(), R.attr.md_divider);
+        final int dividerFallback = DialogUtils.resolveColor(dialog.getThemedContext(), R.attr.md_divider);
         builder.dividerColor = DialogUtils.resolveColor(builder.context, R.attr.md_divider_color, dividerFallback);
         dialog.view.setDividerColor(builder.dividerColor);
 
@@ -180,7 +180,7 @@ class DialogInit {
             dialog.setTypeface(dialog.content, builder.regularFont);
             dialog.content.setLineSpacing(0f, builder.contentLineSpacingMultiplier);
             if (builder.positiveColor == 0) {
-                dialog.content.setLinkTextColor(DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorPrimary));
+                dialog.content.setLinkTextColor(DialogUtils.resolveColor(dialog.getThemedContext(), android.R.attr.textColorPrimary));
             } else {
                 dialog.content.setLinkTextColor(builder.positiveColor);
             }
@@ -294,9 +294,9 @@ class DialogInit {
             if (builder.wrapCustomViewInScroll) {
                 /* Apply the frame padding to the content, this allows the ScrollView to draw it's
                    overscroll glow without clipping */
-                final Resources r = dialog.getContext().getResources();
+                final Resources r = dialog.getThemedContext().getResources();
                 final int framePadding = r.getDimensionPixelSize(R.dimen.md_dialog_frame_margin);
-                final ScrollView sv = new ScrollView(dialog.getContext());
+                final ScrollView sv = new ScrollView(dialog.getThemedContext());
                 int paddingTop = r.getDimensionPixelSize(R.dimen.md_content_padding_top);
                 int paddingBottom = r.getDimensionPixelSize(R.dimen.md_content_padding_bottom);
                 sv.setClipToPadding(false);

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

@@ -79,8 +79,8 @@ public class MaterialDialog extends DialogBase implements
     protected MaterialDialog(Builder builder) {
         super(builder.context);
         mBuilder = builder;
-        final ContextThemeWrapper themedContext = DialogInit.getTheme(builder);
-        final LayoutInflater inflater = LayoutInflater.from(themedContext);
+        mThemedContext = DialogInit.getTheme(builder);
+        final LayoutInflater inflater = LayoutInflater.from(getThemedContext());
         view = (MDRootLayout) inflater.inflate(DialogInit.getInflateLayout(builder), null);
         DialogInit.init(this);
     }
@@ -237,7 +237,7 @@ public class MaterialDialog extends DialogBase implements
             return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.listSelector, null);
         final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_list_selector);
         if (d != null) return d;
-        return DialogUtils.resolveDrawable(getContext(), R.attr.md_list_selector);
+        return DialogUtils.resolveDrawable(getThemedContext(), R.attr.md_list_selector);
     }
 
     /* package */ Drawable getButtonSelector(DialogAction which, boolean isStacked) {
@@ -246,7 +246,7 @@ public class MaterialDialog extends DialogBase implements
                 return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorStacked, null);
             final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_stacked_selector);
             if (d != null) return d;
-            return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_stacked_selector);
+            return DialogUtils.resolveDrawable(getThemedContext(), R.attr.md_btn_stacked_selector);
         } else {
             switch (which) {
                 default: {
@@ -254,21 +254,21 @@ public class MaterialDialog extends DialogBase implements
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorPositive, null);
                     final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_positive_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_positive_selector);
+                    return DialogUtils.resolveDrawable(getThemedContext(), R.attr.md_btn_positive_selector);
                 }
                 case NEUTRAL: {
                     if (mBuilder.btnSelectorNeutral != 0)
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNeutral, null);
                     final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_neutral_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_neutral_selector);
+                    return DialogUtils.resolveDrawable(getThemedContext(), R.attr.md_btn_neutral_selector);
                 }
                 case NEGATIVE: {
                     if (mBuilder.btnSelectorNegative != 0)
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNegative, null);
                     final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_negative_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_negative_selector);
+                    return DialogUtils.resolveDrawable(getThemedContext(), R.attr.md_btn_negative_selector);
                 }
             }
         }
@@ -1042,11 +1042,11 @@ public class MaterialDialog extends DialogBase implements
     public Button getButton(int whichButton) {
         Log.w("MaterialDialog", "Warning: getButton() is a deprecated method that does not return valid references to action buttons.");
         if (whichButton == AlertDialog.BUTTON_POSITIVE) {
-            return mBuilder.positiveText != null ? new Button(getContext()) : null;
+            return mBuilder.positiveText != null ? new Button(getThemedContext()) : null;
         } else if (whichButton == AlertDialog.BUTTON_NEUTRAL) {
-            return mBuilder.neutralText != null ? new Button(getContext()) : null;
+            return mBuilder.neutralText != null ? new Button(getThemedContext()) : null;
         } else {
-            return mBuilder.negativeText != null ? new Button(getContext()) : null;
+            return mBuilder.negativeText != null ? new Button(getThemedContext()) : null;
         }
     }
 
@@ -1129,7 +1129,7 @@ public class MaterialDialog extends DialogBase implements
      * @param titleRes The string resource of the new title of the action button.
      */
     public final void setActionButton(DialogAction which, @StringRes int titleRes) {
-        setActionButton(which, getContext().getString(titleRes));
+        setActionButton(which, getThemedContext().getString(titleRes));
     }
 
     /**

+ 7 - 0
library/src/main/java/com/afollestad/materialdialogs/base/DialogBase.java

@@ -4,6 +4,7 @@ import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.Message;
+import android.view.ContextThemeWrapper;
 import android.view.View;
 import android.view.ViewGroup;
 
@@ -11,7 +12,9 @@ import android.view.ViewGroup;
  * @author Aidan Follestad (afollestad)
  */
 public class DialogBase extends AlertDialog implements DialogInterface.OnShowListener {
+
     private OnShowListener mShowListener;
+    protected ContextThemeWrapper mThemedContext;
 
     protected DialogBase(Context context) {
         super(context);
@@ -32,6 +35,10 @@ public class DialogBase extends AlertDialog implements DialogInterface.OnShowLis
             view.setLayoutParams(params);
     }
 
+    public final Context getThemedContext() {
+        return mThemedContext;
+    }
+
     /**
      * @deprecated Not supported by the Material dialog.
      */