AppCompatBackgroundHelper.java revision 7e4e8b664820f773bc96e37ee1d2bbf500d64e69
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v7.widget; 18 19import android.content.res.ColorStateList; 20import android.content.res.TypedArray; 21import android.graphics.PorterDuff; 22import android.graphics.drawable.Drawable; 23import android.support.v4.view.ViewCompat; 24import android.support.v7.appcompat.R; 25import android.support.v7.graphics.drawable.DrawableUtils; 26import android.support.v7.internal.widget.AppCompatDrawableManager; 27import android.support.v7.internal.widget.TintInfo; 28import android.util.AttributeSet; 29import android.view.View; 30 31class AppCompatBackgroundHelper { 32 33 private final View mView; 34 private final AppCompatDrawableManager mDrawableManager; 35 36 private TintInfo mInternalBackgroundTint; 37 private TintInfo mBackgroundTint; 38 39 AppCompatBackgroundHelper(View view, AppCompatDrawableManager drawableManager) { 40 mView = view; 41 mDrawableManager = drawableManager; 42 } 43 44 void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { 45 TypedArray a = mView.getContext().obtainStyledAttributes(attrs, 46 R.styleable.ViewBackgroundHelper, defStyleAttr, 0); 47 try { 48 if (a.hasValue(R.styleable.ViewBackgroundHelper_android_background)) { 49 ColorStateList tint = mDrawableManager.getTintList(mView.getContext(), 50 a.getResourceId(R.styleable.ViewBackgroundHelper_android_background, -1)); 51 if (tint != null) { 52 setInternalBackgroundTint(tint); 53 } 54 } 55 if (a.hasValue(R.styleable.ViewBackgroundHelper_backgroundTint)) { 56 ViewCompat.setBackgroundTintList(mView, 57 a.getColorStateList(R.styleable.ViewBackgroundHelper_backgroundTint)); 58 } 59 if (a.hasValue(R.styleable.ViewBackgroundHelper_backgroundTintMode)) { 60 ViewCompat.setBackgroundTintMode(mView, 61 DrawableUtils.parseTintMode( 62 a.getInt(R.styleable.ViewBackgroundHelper_backgroundTintMode, -1), 63 null)); 64 } 65 } finally { 66 a.recycle(); 67 } 68 } 69 70 void onSetBackgroundResource(int resId) { 71 // Update the default background tint 72 setInternalBackgroundTint(mDrawableManager != null 73 ? mDrawableManager.getTintList(mView.getContext(), resId) 74 : null); 75 } 76 77 void onSetBackgroundDrawable(Drawable background) { 78 // We don't know that this drawable is, so we need to clear the default background tint 79 setInternalBackgroundTint(null); 80 } 81 82 void setSupportBackgroundTintList(ColorStateList tint) { 83 if (mBackgroundTint == null) { 84 mBackgroundTint = new TintInfo(); 85 } 86 mBackgroundTint.mTintList = tint; 87 mBackgroundTint.mHasTintList = true; 88 89 applySupportBackgroundTint(); 90 } 91 92 ColorStateList getSupportBackgroundTintList() { 93 return mBackgroundTint != null ? mBackgroundTint.mTintList : null; 94 } 95 96 void setSupportBackgroundTintMode(PorterDuff.Mode tintMode) { 97 if (mBackgroundTint == null) { 98 mBackgroundTint = new TintInfo(); 99 } 100 mBackgroundTint.mTintMode = tintMode; 101 mBackgroundTint.mHasTintMode = true; 102 103 applySupportBackgroundTint(); 104 } 105 106 PorterDuff.Mode getSupportBackgroundTintMode() { 107 return mBackgroundTint != null ? mBackgroundTint.mTintMode : null; 108 } 109 110 void applySupportBackgroundTint() { 111 final Drawable background = mView.getBackground(); 112 if (background != null) { 113 if (mBackgroundTint != null) { 114 AppCompatDrawableManager 115 .tintDrawable(background, mBackgroundTint, mView.getDrawableState()); 116 } else if (mInternalBackgroundTint != null) { 117 AppCompatDrawableManager.tintDrawable(background, mInternalBackgroundTint, 118 mView.getDrawableState()); 119 } 120 } 121 } 122 123 void setInternalBackgroundTint(ColorStateList tint) { 124 if (tint != null) { 125 if (mInternalBackgroundTint == null) { 126 mInternalBackgroundTint = new TintInfo(); 127 } 128 mInternalBackgroundTint.mTintList = tint; 129 mInternalBackgroundTint.mHasTintList = true; 130 } else { 131 mInternalBackgroundTint = null; 132 } 133 applySupportBackgroundTint(); 134 } 135} 136