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 com.android.messaging.util; 18 19 import android.content.res.ColorStateList; 20 import android.graphics.Color; 21 import android.graphics.PorterDuff; 22 import android.graphics.drawable.Drawable; 23 import android.support.v7.graphics.drawable.DrawableWrapper; 24 25 /* 26 * This is directly copied from v7/appcompat/src/android/support/v7/internal/widget/TintManager.java 27 */ 28 29 /** 30 * A {@link DrawableWrapper} which updates it's color filter using a {@link ColorStateList}. 31 */ 32 class TintDrawableWrapper extends DrawableWrapper { 33 private final ColorStateList mTintStateList; 34 private final PorterDuff.Mode mTintMode; 35 private int mCurrentColor; TintDrawableWrapper(Drawable drawable, ColorStateList tintStateList)36 public TintDrawableWrapper(Drawable drawable, ColorStateList tintStateList) { 37 this(drawable, tintStateList, PorterDuff.Mode.SRC_IN); 38 } TintDrawableWrapper(Drawable drawable, ColorStateList tintStateList, PorterDuff.Mode tintMode)39 public TintDrawableWrapper(Drawable drawable, ColorStateList tintStateList, 40 PorterDuff.Mode tintMode) { 41 super(drawable); 42 mTintStateList = tintStateList; 43 mTintMode = tintMode; 44 } 45 @Override isStateful()46 public boolean isStateful() { 47 return (mTintStateList != null && mTintStateList.isStateful()) || super.isStateful(); 48 } 49 @Override setState(int[] stateSet)50 public boolean setState(int[] stateSet) { 51 boolean handled = super.setState(stateSet); 52 handled = updateTint(stateSet) || handled; 53 return handled; 54 } updateTint(int[] state)55 private boolean updateTint(int[] state) { 56 if (mTintStateList != null) { 57 final int color = mTintStateList.getColorForState(state, mCurrentColor); 58 if (color != mCurrentColor) { 59 if (color != Color.TRANSPARENT) { 60 setColorFilter(color, mTintMode); 61 } else { 62 clearColorFilter(); 63 } 64 mCurrentColor = color; 65 return true; 66 } 67 } 68 return false; 69 } 70 } 71