• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.support.v7.widget;
18 
19 import android.content.res.ColorStateList;
20 import android.content.res.TypedArray;
21 import android.graphics.PorterDuff;
22 import android.graphics.drawable.Drawable;
23 import android.support.v4.view.ViewCompat;
24 import android.support.v7.appcompat.R;
25 import android.support.v7.graphics.drawable.DrawableUtils;
26 import android.support.v7.internal.widget.TintInfo;
27 import android.support.v7.internal.widget.TintManager;
28 import android.util.AttributeSet;
29 import android.view.View;
30 
31 class AppCompatBackgroundHelper {
32 
33     private final View mView;
34     private final TintManager mTintManager;
35 
36     private TintInfo mInternalBackgroundTint;
37     private TintInfo mBackgroundTint;
38 
AppCompatBackgroundHelper(View view, TintManager tintManager)39     AppCompatBackgroundHelper(View view, TintManager tintManager) {
40         mView = view;
41         mTintManager = tintManager;
42     }
43 
loadFromAttributes(AttributeSet attrs, int defStyleAttr)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 = mTintManager.getTintList(
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 
onSetBackgroundResource(int resId)70     void onSetBackgroundResource(int resId) {
71         // Update the default background tint
72         setInternalBackgroundTint(mTintManager != null ? mTintManager.getTintList(resId) : null);
73     }
74 
onSetBackgroundDrawable(Drawable background)75     void onSetBackgroundDrawable(Drawable background) {
76         // We don't know that this drawable is, so we need to clear the default background tint
77         setInternalBackgroundTint(null);
78     }
79 
setSupportBackgroundTintList(ColorStateList tint)80     void setSupportBackgroundTintList(ColorStateList tint) {
81         if (mBackgroundTint == null) {
82             mBackgroundTint = new TintInfo();
83         }
84         mBackgroundTint.mTintList = tint;
85         mBackgroundTint.mHasTintList = true;
86 
87         applySupportBackgroundTint();
88     }
89 
getSupportBackgroundTintList()90     ColorStateList getSupportBackgroundTintList() {
91         return mBackgroundTint != null ? mBackgroundTint.mTintList : null;
92     }
93 
setSupportBackgroundTintMode(PorterDuff.Mode tintMode)94     void setSupportBackgroundTintMode(PorterDuff.Mode tintMode) {
95         if (mBackgroundTint == null) {
96             mBackgroundTint = new TintInfo();
97         }
98         mBackgroundTint.mTintMode = tintMode;
99         mBackgroundTint.mHasTintMode = true;
100 
101         applySupportBackgroundTint();
102     }
103 
getSupportBackgroundTintMode()104     PorterDuff.Mode getSupportBackgroundTintMode() {
105         return mBackgroundTint != null ? mBackgroundTint.mTintMode : null;
106     }
107 
applySupportBackgroundTint()108     void applySupportBackgroundTint() {
109         if (mView.getBackground() != null) {
110             if (mBackgroundTint != null) {
111                 TintManager.tintViewBackground(mView, mBackgroundTint);
112             } else if (mInternalBackgroundTint != null) {
113                 TintManager.tintViewBackground(mView, mInternalBackgroundTint);
114             }
115         }
116     }
117 
setInternalBackgroundTint(ColorStateList tint)118     void setInternalBackgroundTint(ColorStateList tint) {
119         if (tint != null) {
120             if (mInternalBackgroundTint == null) {
121                 mInternalBackgroundTint = new TintInfo();
122             }
123             mInternalBackgroundTint.mTintList = tint;
124             mInternalBackgroundTint.mHasTintList = true;
125         } else {
126             mInternalBackgroundTint = null;
127         }
128         applySupportBackgroundTint();
129     }
130 }
131