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.TintInfo;
27import android.support.v7.internal.widget.TintManager;
28import android.util.AttributeSet;
29import android.view.View;
30
31class AppCompatBackgroundHelper {
32
33    private final View mView;
34    private final TintManager mTintManager;
35
36    private TintInfo mInternalBackgroundTint;
37    private TintInfo mBackgroundTint;
38
39    AppCompatBackgroundHelper(View view, TintManager tintManager) {
40        mView = view;
41        mTintManager = tintManager;
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 = 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
70    void onSetBackgroundResource(int resId) {
71        // Update the default background tint
72        setInternalBackgroundTint(mTintManager != null ? mTintManager.getTintList(resId) : null);
73    }
74
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
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
90    ColorStateList getSupportBackgroundTintList() {
91        return mBackgroundTint != null ? mBackgroundTint.mTintList : null;
92    }
93
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
104    PorterDuff.Mode getSupportBackgroundTintMode() {
105        return mBackgroundTint != null ? mBackgroundTint.mTintMode : null;
106    }
107
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
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