Fix all usages of deprecated setBackgroundDrawable.

- Adds ViewCompat#setBackground that will call setBackgroundDrawable
  or setBackground depending on the version.
- Replaced usages of setBackgroundDrawable with this ViewCompat version.

Bug: 30074170
Change-Id: I756c8558608da752acd6f3ca7121114d3cd1416a
diff --git a/api/current.txt b/api/current.txt
index b30851b..75af7a4 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -6381,6 +6381,7 @@
     method public static void setAccessibilityLiveRegion(android.view.View, int);
     method public static void setActivated(android.view.View, boolean);
     method public static void setAlpha(android.view.View, float);
+    method public static void setBackground(android.view.View, android.graphics.drawable.Drawable);
     method public static void setBackgroundTintList(android.view.View, android.content.res.ColorStateList);
     method public static void setBackgroundTintMode(android.view.View, android.graphics.PorterDuff.Mode);
     method public static void setChildrenDrawingOrderEnabled(android.view.ViewGroup, boolean);
diff --git a/compat/java/android/support/v4/view/ViewCompat.java b/compat/java/android/support/v4/view/ViewCompat.java
index 7af945d..d9d3cfb 100644
--- a/compat/java/android/support/v4/view/ViewCompat.java
+++ b/compat/java/android/support/v4/view/ViewCompat.java
@@ -21,6 +21,7 @@
 import android.graphics.Paint;
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.support.annotation.FloatRange;
 import android.support.annotation.IdRes;
@@ -456,6 +457,7 @@
         void setSaveFromParentEnabled(View view, boolean enabled);
         void setActivated(View view, boolean activated);
         boolean isPaddingRelative(View view);
+        void setBackground(View view, Drawable background);
         ColorStateList getBackgroundTintList(View view);
         void setBackgroundTintList(View view, ColorStateList tintList);
         PorterDuff.Mode getBackgroundTintMode(View view);
@@ -976,6 +978,11 @@
         }
 
         @Override
+        public void setBackground(View view, Drawable background) {
+            view.setBackgroundDrawable(background);
+        }
+
+        @Override
         public ColorStateList getBackgroundTintList(View view) {
             return ViewCompatBase.getBackgroundTintList(view);
         }
@@ -1493,6 +1500,11 @@
         public boolean hasOverlappingRendering(View view) {
             return ViewCompatJB.hasOverlappingRendering(view);
         }
+
+        @Override
+        public void setBackground(View view, Drawable background) {
+            ViewCompatJB.setBackground(view, background);
+        }
     }
 
     static class JbMr1ViewCompatImpl extends JBViewCompatImpl {
@@ -3094,6 +3106,16 @@
     }
 
     /**
+     * Set the background of the {@code view} to a given Drawable, or remove the background. If the
+     * background has padding, {@code view}'s padding is set to the background's padding. However,
+     * when a background is removed, this View's padding isn't touched. If setting the padding is
+     * desired, please use{@code setPadding(int, int, int, int)}.
+     */
+    public static void setBackground(View view, Drawable background) {
+        IMPL.setBackground(view, background);
+    }
+
+    /**
      * Return the tint applied to the background drawable, if specified.
      * <p>
      * Only returns meaningful info when running on API v21 or newer, or if {@code view}
diff --git a/compat/jellybean/android/support/v4/view/ViewCompatJB.java b/compat/jellybean/android/support/v4/view/ViewCompatJB.java
index f1c7315..3d64da1 100644
--- a/compat/jellybean/android/support/v4/view/ViewCompatJB.java
+++ b/compat/jellybean/android/support/v4/view/ViewCompatJB.java
@@ -16,6 +16,7 @@
 
 package android.support.v4.view;
 
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.view.View;
 import android.view.ViewParent;
@@ -89,4 +90,8 @@
     public static boolean hasOverlappingRendering(View view) {
         return view.hasOverlappingRendering();
     }
+
+    public static void setBackground(View view, Drawable drawable) {
+        view.setBackground(drawable);
+    }
 }
diff --git a/core-ui/java/android/support/v4/widget/CircleImageView.java b/core-ui/java/android/support/v4/widget/CircleImageView.java
index b04b56e..176df1b 100644
--- a/core-ui/java/android/support/v4/widget/CircleImageView.java
+++ b/core-ui/java/android/support/v4/widget/CircleImageView.java
@@ -71,7 +71,7 @@
             setPadding(padding, padding, padding, padding);
         }
         circle.getPaint().setColor(color);
-        setBackgroundDrawable(circle);
+        ViewCompat.setBackground(this, circle);
     }
 
     private boolean elevationSupported() {
diff --git a/design/src/android/support/design/internal/BottomNavigationItemView.java b/design/src/android/support/design/internal/BottomNavigationItemView.java
index 14bd62e..18c6036 100644
--- a/design/src/android/support/design/internal/BottomNavigationItemView.java
+++ b/design/src/android/support/design/internal/BottomNavigationItemView.java
@@ -25,6 +25,7 @@
 import android.support.design.R;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.graphics.drawable.DrawableCompat;
+import android.support.v4.view.ViewCompat;
 import android.support.v4.view.animation.LinearOutSlowInInterpolator;
 import android.support.v7.view.menu.MenuItemImpl;
 import android.support.v7.view.menu.MenuView;
@@ -193,7 +194,7 @@
     public void setItemBackground(int background) {
         Drawable backgroundDrawable = background == 0
                 ? null : ContextCompat.getDrawable(getContext(), background);
-        setBackgroundDrawable(backgroundDrawable);
+        ViewCompat.setBackground(this, backgroundDrawable);
     }
 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
diff --git a/design/src/android/support/design/internal/NavigationMenuItemView.java b/design/src/android/support/design/internal/NavigationMenuItemView.java
index d1e6027..61fbd91 100644
--- a/design/src/android/support/design/internal/NavigationMenuItemView.java
+++ b/design/src/android/support/design/internal/NavigationMenuItemView.java
@@ -103,7 +103,7 @@
         setVisibility(itemData.isVisible() ? VISIBLE : GONE);
 
         if (getBackground() == null) {
-            setBackgroundDrawable(createDefaultBackground());
+            ViewCompat.setBackground(this, createDefaultBackground());
         }
 
         setCheckable(itemData.isCheckable());
diff --git a/design/src/android/support/design/internal/NavigationMenuPresenter.java b/design/src/android/support/design/internal/NavigationMenuPresenter.java
index de2a7c5..b33bb6a 100644
--- a/design/src/android/support/design/internal/NavigationMenuPresenter.java
+++ b/design/src/android/support/design/internal/NavigationMenuPresenter.java
@@ -408,7 +408,7 @@
                     if (mTextColor != null) {
                         itemView.setTextColor(mTextColor);
                     }
-                    itemView.setBackgroundDrawable(mItemBackground != null ?
+                    ViewCompat.setBackground(itemView, mItemBackground != null ?
                             mItemBackground.getConstantState().newDrawable() : null);
                     NavigationMenuTextItem item = (NavigationMenuTextItem) mItems.get(position);
                     itemView.setNeedsEmptyIcon(item.needsEmptyIcon);
diff --git a/design/src/android/support/design/widget/AppBarLayout.java b/design/src/android/support/design/widget/AppBarLayout.java
index c253029..0e48be5 100644
--- a/design/src/android/support/design/widget/AppBarLayout.java
+++ b/design/src/android/support/design/widget/AppBarLayout.java
@@ -166,7 +166,7 @@
 
         final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppBarLayout,
                 0, R.style.Widget_Design_AppBarLayout);
-        setBackgroundDrawable(a.getDrawable(R.styleable.AppBarLayout_android_background));
+        ViewCompat.setBackground(this, a.getDrawable(R.styleable.AppBarLayout_android_background));
         if (a.hasValue(R.styleable.AppBarLayout_expanded)) {
             setExpanded(a.getBoolean(R.styleable.AppBarLayout_expanded, false));
         }
diff --git a/design/src/android/support/design/widget/NavigationView.java b/design/src/android/support/design/widget/NavigationView.java
index e53b7e8..7d78ffb 100644
--- a/design/src/android/support/design/widget/NavigationView.java
+++ b/design/src/android/support/design/widget/NavigationView.java
@@ -110,8 +110,8 @@
                 R.styleable.NavigationView, defStyleAttr,
                 R.style.Widget_Design_NavigationView);
 
-        //noinspection deprecation
-        setBackgroundDrawable(a.getDrawable(R.styleable.NavigationView_android_background));
+        ViewCompat.setBackground(
+                this, a.getDrawable(R.styleable.NavigationView_android_background));
         if (a.hasValue(R.styleable.NavigationView_elevation)) {
             ViewCompat.setElevation(this, a.getDimensionPixelSize(
                     R.styleable.NavigationView_elevation, 0));
diff --git a/design/src/android/support/design/widget/TabLayout.java b/design/src/android/support/design/widget/TabLayout.java
index 35d59ff..1123a04 100755
--- a/design/src/android/support/design/widget/TabLayout.java
+++ b/design/src/android/support/design/widget/TabLayout.java
@@ -1495,7 +1495,8 @@
         public TabView(Context context) {
             super(context);
             if (mTabBackgroundResId != 0) {
-                setBackgroundDrawable(AppCompatResources.getDrawable(context, mTabBackgroundResId));
+                ViewCompat.setBackground(
+                        this, AppCompatResources.getDrawable(context, mTabBackgroundResId));
             }
             ViewCompat.setPaddingRelative(this, mTabPaddingStart, mTabPaddingTop,
                     mTabPaddingEnd, mTabPaddingBottom);
diff --git a/design/src/android/support/design/widget/TextInputLayout.java b/design/src/android/support/design/widget/TextInputLayout.java
index 77adabb..8114a4d 100644
--- a/design/src/android/support/design/widget/TextInputLayout.java
+++ b/design/src/android/support/design/widget/TextInputLayout.java
@@ -852,7 +852,7 @@
                 // as the background. This has the unfortunate side-effect of wiping out any
                 // user set padding, but I'd hope that use of custom padding on an EditText
                 // is limited.
-                mEditText.setBackgroundDrawable(newBg);
+                ViewCompat.setBackground(mErrorView, newBg);
                 mHasReconstructedEditTextBackground = true;
             }
         }
diff --git a/graphics/drawable/animated/tests/src/android/support/graphics/drawable/tests/AnimatedVectorDrawableTest.java b/graphics/drawable/animated/tests/src/android/support/graphics/drawable/tests/AnimatedVectorDrawableTest.java
index 4105d82..2e50823 100644
--- a/graphics/drawable/animated/tests/src/android/support/graphics/drawable/tests/AnimatedVectorDrawableTest.java
+++ b/graphics/drawable/animated/tests/src/android/support/graphics/drawable/tests/AnimatedVectorDrawableTest.java
@@ -27,6 +27,7 @@
 import android.support.test.InstrumentationRegistry;
 import android.support.test.rule.ActivityTestRule;
 import android.support.test.runner.AndroidJUnit4;
+import android.support.v4.view.ViewCompat;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.util.AttributeSet;
 import android.util.Log;
@@ -201,7 +202,7 @@
             public void run() {
                 AnimatedVectorDrawableCompat avd = AnimatedVectorDrawableCompat.create(mContext,
                         R.drawable.animated_color_fill);
-                imageButton.setBackgroundDrawable(avd);
+                ViewCompat.setBackground(imageButton, avd);
                 avd.start();
             }
         });
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/graphics/PaletteActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/graphics/PaletteActivity.java
index e82aa3a..0759e33 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/graphics/PaletteActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/graphics/PaletteActivity.java
@@ -29,6 +29,7 @@
 import android.support.v4.app.LoaderManager;
 import android.support.v4.content.CursorLoader;
 import android.support.v4.content.Loader;
+import android.support.v4.view.ViewCompat;
 import android.support.v4.widget.ResourceCursorAdapter;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.graphics.Palette;
@@ -190,12 +191,12 @@
                 ImageView imageView = (ImageView) view.findViewById(R.id.image);
                 imageView.setImageDrawable(null);
 
-                view.findViewById(R.id.text_vibrant).setBackgroundDrawable(null);
-                view.findViewById(R.id.text_muted).setBackgroundDrawable(null);
-                view.findViewById(R.id.text_light_vibrant).setBackgroundDrawable(null);
-                view.findViewById(R.id.text_light_muted).setBackgroundDrawable(null);
-                view.findViewById(R.id.text_dark_vibrant).setBackgroundDrawable(null);
-                view.findViewById(R.id.text_dark_muted).setBackgroundDrawable(null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_vibrant), null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_muted), null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_light_vibrant), null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_light_muted), null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_dark_vibrant), null);
+                ViewCompat.setBackground(view.findViewById(R.id.text_dark_muted), null);
 
                 final long id = cursor.getLong(
                         cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID));
diff --git a/v7/appcompat/src/android/support/v7/view/menu/ListMenuItemView.java b/v7/appcompat/src/android/support/v7/view/menu/ListMenuItemView.java
index 2d6def1..48bbbdc 100644
--- a/v7/appcompat/src/android/support/v7/view/menu/ListMenuItemView.java
+++ b/v7/appcompat/src/android/support/v7/view/menu/ListMenuItemView.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.graphics.drawable.Drawable;
+import android.support.v4.view.ViewCompat;
 import android.support.v7.appcompat.R;
 import android.support.v7.widget.TintTypedArray;
 import android.util.AttributeSet;
@@ -84,7 +85,7 @@
     protected void onFinishInflate() {
         super.onFinishInflate();
 
-        setBackgroundDrawable(mBackground);
+        ViewCompat.setBackground(this, mBackground);
 
         mTitleView = (TextView) findViewById(R.id.title);
         if (mTextAppearance != -1) {
diff --git a/v7/appcompat/src/android/support/v7/widget/ActionBarContainer.java b/v7/appcompat/src/android/support/v7/widget/ActionBarContainer.java
index 00e7d4c..bb94460 100644
--- a/v7/appcompat/src/android/support/v7/widget/ActionBarContainer.java
+++ b/v7/appcompat/src/android/support/v7/widget/ActionBarContainer.java
@@ -20,6 +20,7 @@
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
+import android.support.v4.view.ViewCompat;
 import android.support.v7.appcompat.R;
 import android.support.v7.view.ActionMode;
 import android.util.AttributeSet;
@@ -57,7 +58,7 @@
         final Drawable bg = Build.VERSION.SDK_INT >= 21
                 ? new ActionBarBackgroundDrawableV21(this)
                 : new ActionBarBackgroundDrawable(this);
-        setBackgroundDrawable(bg);
+        ViewCompat.setBackground(this, bg);
 
         TypedArray a = context.obtainStyledAttributes(attrs,
                 R.styleable.ActionBar);
diff --git a/v7/appcompat/src/android/support/v7/widget/ActionBarContextView.java b/v7/appcompat/src/android/support/v7/widget/ActionBarContextView.java
index 4f45c2a..52391d1 100644
--- a/v7/appcompat/src/android/support/v7/widget/ActionBarContextView.java
+++ b/v7/appcompat/src/android/support/v7/widget/ActionBarContextView.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.os.Build;
+import android.support.v4.view.ViewCompat;
 import android.support.v7.appcompat.R;
 import android.support.v7.view.ActionMode;
 import android.support.v7.view.menu.MenuBuilder;
@@ -62,8 +63,7 @@
 
         final TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs,
                 R.styleable.ActionMode, defStyle, 0);
-        setBackgroundDrawable(a.getDrawable(
-                R.styleable.ActionMode_background));
+        ViewCompat.setBackground(this, a.getDrawable(R.styleable.ActionMode_background));
         mTitleStyleRes = a.getResourceId(
                 R.styleable.ActionMode_titleTextStyle, 0);
         mSubtitleStyleRes = a.getResourceId(
@@ -179,7 +179,7 @@
                 LayoutParams.MATCH_PARENT);
         menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
         mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
-        mMenuView.setBackgroundDrawable(null);
+        ViewCompat.setBackground(mMenuView, null);
         addView(mMenuView, layoutParams);
     }
 
diff --git a/v7/appcompat/src/android/support/v7/widget/SearchView.java b/v7/appcompat/src/android/support/v7/widget/SearchView.java
index bb703cc..d823324 100644
--- a/v7/appcompat/src/android/support/v7/widget/SearchView.java
+++ b/v7/appcompat/src/android/support/v7/widget/SearchView.java
@@ -46,6 +46,7 @@
 import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.support.v4.view.AbsSavedState;
 import android.support.v4.view.KeyEventCompat;
+import android.support.v4.view.ViewCompat;
 import android.support.v4.widget.CursorAdapter;
 import android.support.v7.appcompat.R;
 import android.support.v7.view.CollapsibleActionView;
@@ -313,8 +314,10 @@
         mCollapsedIcon = (ImageView) findViewById(R.id.search_mag_icon);
 
         // Set up icons and backgrounds.
-        mSearchPlate.setBackgroundDrawable(a.getDrawable(R.styleable.SearchView_queryBackground));
-        mSubmitArea.setBackgroundDrawable(a.getDrawable(R.styleable.SearchView_submitBackground));
+        ViewCompat.setBackground(mSearchPlate,
+                a.getDrawable(R.styleable.SearchView_queryBackground));
+        ViewCompat.setBackground(mSubmitArea,
+                a.getDrawable(R.styleable.SearchView_submitBackground));
         mSearchButton.setImageDrawable(a.getDrawable(R.styleable.SearchView_searchIcon));
         mGoButton.setImageDrawable(a.getDrawable(R.styleable.SearchView_goIcon));
         mCloseButton.setImageDrawable(a.getDrawable(R.styleable.SearchView_closeIcon));
diff --git a/v7/appcompat/src/android/support/v7/widget/ToolbarWidgetWrapper.java b/v7/appcompat/src/android/support/v7/widget/ToolbarWidgetWrapper.java
index 283151b..040fc90 100644
--- a/v7/appcompat/src/android/support/v7/widget/ToolbarWidgetWrapper.java
+++ b/v7/appcompat/src/android/support/v7/widget/ToolbarWidgetWrapper.java
@@ -643,8 +643,7 @@
 
     @Override
     public void setBackgroundDrawable(Drawable d) {
-        //noinspection deprecation
-        mToolbar.setBackgroundDrawable(d);
+        ViewCompat.setBackground(mToolbar, d);
     }
 
     @Override
diff --git a/v7/preference/src/android/support/v7/preference/PreferenceGroupAdapter.java b/v7/preference/src/android/support/v7/preference/PreferenceGroupAdapter.java
index d419dd2..4762548 100644
--- a/v7/preference/src/android/support/v7/preference/PreferenceGroupAdapter.java
+++ b/v7/preference/src/android/support/v7/preference/PreferenceGroupAdapter.java
@@ -19,6 +19,7 @@
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.os.Handler;
+import android.support.v4.view.ViewCompat;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
@@ -295,7 +296,7 @@
 
         final View view = inflater.inflate(pl.resId, parent, false);
         if (view.getBackground() == null) {
-            view.setBackgroundDrawable(background);
+            ViewCompat.setBackground(view, background);
         }
 
         final ViewGroup widgetFrame = (ViewGroup) view.findViewById(android.R.id.widget_frame);