瀏覽代碼

Avoid holding onto Context in Builder

Aidan Follestad 7 年之前
父節點
當前提交
fac4d7e14c

+ 5 - 4
core/src/main/java/com/afollestad/materialdialogs/DefaultRvAdapter.java

@@ -2,6 +2,7 @@ package com.afollestad.materialdialogs;
 
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
+import android.content.Context;
 import android.content.res.Configuration;
 import android.os.Build;
 import android.support.annotation.LayoutRes;
@@ -125,7 +126,7 @@ class DefaultRvAdapter extends RecyclerView.Adapter<DefaultRvAdapter.DefaultVH>
 
     if (view.getChildCount() == 2) {
       if (itemGravity == GravityEnum.END
-          && !isRTL()
+          && !isRTL(view.getContext())
           && view.getChildAt(0) instanceof CompoundButton) {
         CompoundButton first = (CompoundButton) view.getChildAt(0);
         view.removeView(first);
@@ -141,7 +142,7 @@ class DefaultRvAdapter extends RecyclerView.Adapter<DefaultRvAdapter.DefaultVH>
         view.addView(second);
         view.addView(first);
       } else if (itemGravity == GravityEnum.START
-          && isRTL()
+          && isRTL(view.getContext())
           && view.getChildAt(1) instanceof CompoundButton) {
         CompoundButton first = (CompoundButton) view.getChildAt(1);
         view.removeView(first);
@@ -161,11 +162,11 @@ class DefaultRvAdapter extends RecyclerView.Adapter<DefaultRvAdapter.DefaultVH>
   }
 
   @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-  private boolean isRTL() {
+  private boolean isRTL(Context context) {
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
       return false;
     }
-    Configuration config = dialog.getBuilder().getContext().getResources().getConfiguration();
+    Configuration config = context.getResources().getConfiguration();
     return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
   }
 

+ 1 - 1
core/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -86,7 +86,7 @@ class DialogInit {
     if (builder.backgroundColor == 0) {
       builder.backgroundColor =
           DialogUtils.resolveColor(
-              builder.context,
+              dialog.getView().getContext(),
               R.attr.md_background_color,
               DialogUtils.resolveColor(dialog.getContext(), R.attr.colorBackgroundFloating));
     }

+ 22 - 24
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -88,9 +88,12 @@ public class MaterialDialog extends DialogBase
     super(builder.context, DialogInit.getTheme(builder));
     handler = new Handler();
     this.builder = builder;
-    final LayoutInflater inflater = LayoutInflater.from(builder.context);
+    final LayoutInflater inflater = LayoutInflater.from(getContext());
     view = (MDRootLayout) inflater.inflate(DialogInit.getInflateLayout(builder), null);
     DialogInit.init(this);
+
+    // Don't keep a Context reference in the Builder after this point
+    builder.context = null;
   }
 
   public final Builder getBuilder() {
@@ -278,10 +281,9 @@ public class MaterialDialog extends DialogBase
 
   final Drawable getListSelector() {
     if (builder.listSelector != 0) {
-      return ResourcesCompat.getDrawable(
-          builder.context.getResources(), builder.listSelector, null);
+      return ResourcesCompat.getDrawable(getContext().getResources(), builder.listSelector, null);
     }
-    final Drawable d = DialogUtils.resolveDrawable(builder.context, R.attr.md_list_selector);
+    final Drawable d = DialogUtils.resolveDrawable(getContext(), R.attr.md_list_selector);
     if (d != null) {
       return d;
     }
@@ -306,10 +308,9 @@ public class MaterialDialog extends DialogBase
     if (isStacked) {
       if (builder.btnSelectorStacked != 0) {
         return ResourcesCompat.getDrawable(
-            builder.context.getResources(), builder.btnSelectorStacked, null);
+            getContext().getResources(), builder.btnSelectorStacked, null);
       }
-      final Drawable d =
-          DialogUtils.resolveDrawable(builder.context, R.attr.md_btn_stacked_selector);
+      final Drawable d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_stacked_selector);
       if (d != null) {
         return d;
       }
@@ -320,10 +321,9 @@ public class MaterialDialog extends DialogBase
           {
             if (builder.btnSelectorPositive != 0) {
               return ResourcesCompat.getDrawable(
-                  builder.context.getResources(), builder.btnSelectorPositive, null);
+                  getContext().getResources(), builder.btnSelectorPositive, null);
             }
-            Drawable d =
-                DialogUtils.resolveDrawable(builder.context, R.attr.md_btn_positive_selector);
+            Drawable d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_positive_selector);
             if (d != null) {
               return d;
             }
@@ -337,10 +337,9 @@ public class MaterialDialog extends DialogBase
           {
             if (builder.btnSelectorNeutral != 0) {
               return ResourcesCompat.getDrawable(
-                  builder.context.getResources(), builder.btnSelectorNeutral, null);
+                  getContext().getResources(), builder.btnSelectorNeutral, null);
             }
-            Drawable d =
-                DialogUtils.resolveDrawable(builder.context, R.attr.md_btn_neutral_selector);
+            Drawable d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_neutral_selector);
             if (d != null) {
               return d;
             }
@@ -354,10 +353,9 @@ public class MaterialDialog extends DialogBase
           {
             if (builder.btnSelectorNegative != 0) {
               return ResourcesCompat.getDrawable(
-                  builder.context.getResources(), builder.btnSelectorNegative, null);
+                  getContext().getResources(), builder.btnSelectorNegative, null);
             }
-            Drawable d =
-                DialogUtils.resolveDrawable(builder.context, R.attr.md_btn_negative_selector);
+            Drawable d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_negative_selector);
             if (d != null) {
               return d;
             }
@@ -593,12 +591,12 @@ public class MaterialDialog extends DialogBase
   @UiThread
   @Override
   public final void setTitle(@StringRes int newTitleRes) {
-    setTitle(builder.context.getString(newTitleRes));
+    setTitle(getContext().getString(newTitleRes));
   }
 
   @UiThread
   public final void setTitle(@StringRes int newTitleRes, @Nullable Object... formatArgs) {
-    setTitle(builder.context.getString(newTitleRes, formatArgs));
+    setTitle(getContext().getString(newTitleRes, formatArgs));
   }
 
   @UiThread
@@ -615,7 +613,7 @@ public class MaterialDialog extends DialogBase
 
   @UiThread
   public void setIconAttribute(@AttrRes int attrId) {
-    Drawable d = DialogUtils.resolveDrawable(builder.context, attrId);
+    Drawable d = DialogUtils.resolveDrawable(getContext(), attrId);
     setIcon(d);
   }
 
@@ -627,12 +625,12 @@ public class MaterialDialog extends DialogBase
 
   @UiThread
   public final void setContent(@StringRes int newContentRes) {
-    setContent(builder.context.getString(newContentRes));
+    setContent(getContext().getString(newContentRes));
   }
 
   @UiThread
   public final void setContent(@StringRes int newContentRes, @Nullable Object... formatArgs) {
-    setContent(builder.context.getString(newContentRes, formatArgs));
+    setContent(getContext().getString(newContentRes, formatArgs));
   }
 
   @Nullable
@@ -891,7 +889,7 @@ public class MaterialDialog extends DialogBase
   @Override
   public final void onShow(DialogInterface dialog) {
     if (input != null) {
-      DialogUtils.showKeyboard(this, builder);
+      DialogUtils.showKeyboard(this);
       if (input.getText().length() > 0) {
         input.setSelection(input.getText().length());
       }
@@ -955,7 +953,7 @@ public class MaterialDialog extends DialogBase
   @Override
   public void dismiss() {
     if (input != null) {
-      DialogUtils.hideKeyboard(this, builder);
+      DialogUtils.hideKeyboard(this);
     }
     super.dismiss();
   }
@@ -1039,7 +1037,7 @@ public class MaterialDialog extends DialogBase
   @SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", "ConstantConditions"})
   public static class Builder {
 
-    protected final Context context;
+    protected Context context;
     protected CharSequence title;
     protected GravityEnum titleGravity = GravityEnum.START;
     protected GravityEnum contentGravity = GravityEnum.START;

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

@@ -221,7 +221,7 @@ public class DialogUtils {
     }
   }
 
-  public static void showKeyboard(final DialogInterface di, final MaterialDialog.Builder builder) {
+  public static void showKeyboard(final DialogInterface di) {
     final MaterialDialog dialog = (MaterialDialog) di;
     if (dialog.getInputEditText() == null) {
       return;
@@ -235,7 +235,7 @@ public class DialogUtils {
                 dialog.getInputEditText().requestFocus();
                 InputMethodManager imm =
                     (InputMethodManager)
-                        builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+                        dialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                 if (imm != null) {
                   imm.showSoftInput(dialog.getInputEditText(), InputMethodManager.SHOW_IMPLICIT);
                 }
@@ -243,13 +243,13 @@ public class DialogUtils {
             });
   }
 
-  public static void hideKeyboard(final DialogInterface di, final MaterialDialog.Builder builder) {
+  public static void hideKeyboard(final DialogInterface di) {
     final MaterialDialog dialog = (MaterialDialog) di;
     if (dialog.getInputEditText() == null) {
       return;
     }
     InputMethodManager imm =
-        (InputMethodManager) builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+        (InputMethodManager) dialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
     if (imm != null) {
       final View currentFocus = dialog.getCurrentFocus();
       IBinder windowToken = null;