DrawableCompatLollipop.java revision 7e82b99953680915596eaf0eb35927388e574ca8
17e82b99953680915596eaf0eb35927388e574ca8Chris Banes/*
27e82b99953680915596eaf0eb35927388e574ca8Chris Banes * Copyright (C) 2014 The Android Open Source Project
37e82b99953680915596eaf0eb35927388e574ca8Chris Banes *
47e82b99953680915596eaf0eb35927388e574ca8Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
57e82b99953680915596eaf0eb35927388e574ca8Chris Banes * you may not use this file except in compliance with the License.
67e82b99953680915596eaf0eb35927388e574ca8Chris Banes * You may obtain a copy of the License at
77e82b99953680915596eaf0eb35927388e574ca8Chris Banes *
87e82b99953680915596eaf0eb35927388e574ca8Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
97e82b99953680915596eaf0eb35927388e574ca8Chris Banes *
107e82b99953680915596eaf0eb35927388e574ca8Chris Banes * Unless required by applicable law or agreed to in writing, software
117e82b99953680915596eaf0eb35927388e574ca8Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
127e82b99953680915596eaf0eb35927388e574ca8Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137e82b99953680915596eaf0eb35927388e574ca8Chris Banes * See the License for the specific language governing permissions and
147e82b99953680915596eaf0eb35927388e574ca8Chris Banes * limitations under the License.
157e82b99953680915596eaf0eb35927388e574ca8Chris Banes */
167e82b99953680915596eaf0eb35927388e574ca8Chris Banes
177e82b99953680915596eaf0eb35927388e574ca8Chris Banespackage android.support.v4.graphics.drawable;
187e82b99953680915596eaf0eb35927388e574ca8Chris Banes
197e82b99953680915596eaf0eb35927388e574ca8Chris Banesimport android.content.res.ColorStateList;
207e82b99953680915596eaf0eb35927388e574ca8Chris Banesimport android.graphics.PorterDuff;
217e82b99953680915596eaf0eb35927388e574ca8Chris Banesimport android.graphics.drawable.Drawable;
227e82b99953680915596eaf0eb35927388e574ca8Chris Banesimport android.graphics.drawable.GradientDrawable;
237e82b99953680915596eaf0eb35927388e574ca8Chris Banes
247e82b99953680915596eaf0eb35927388e574ca8Chris Banes/**
257e82b99953680915596eaf0eb35927388e574ca8Chris Banes * Implementation of drawable compatibility that can call L APIs.
267e82b99953680915596eaf0eb35927388e574ca8Chris Banes */
277e82b99953680915596eaf0eb35927388e574ca8Chris Banesclass DrawableCompatLollipop {
287e82b99953680915596eaf0eb35927388e574ca8Chris Banes
297e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static void setHotspot(Drawable drawable, float x, float y) {
307e82b99953680915596eaf0eb35927388e574ca8Chris Banes        drawable.setHotspot(x, y);
317e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
327e82b99953680915596eaf0eb35927388e574ca8Chris Banes
337e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static void setHotspotBounds(Drawable drawable, int left, int top,
347e82b99953680915596eaf0eb35927388e574ca8Chris Banes            int right, int bottom) {
357e82b99953680915596eaf0eb35927388e574ca8Chris Banes        drawable.setHotspotBounds( left, top, right, bottom);
367e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
377e82b99953680915596eaf0eb35927388e574ca8Chris Banes
387e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static void setTint(Drawable drawable, int tint) {
397e82b99953680915596eaf0eb35927388e574ca8Chris Banes        if (drawable instanceof DrawableWrapperLollipop) {
407e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // GradientDrawable on Lollipop does not support tinting, so we'll use our compatible
417e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // functionality instead
427e82b99953680915596eaf0eb35927388e574ca8Chris Banes            DrawableCompatBase.setTint(drawable, tint);
437e82b99953680915596eaf0eb35927388e574ca8Chris Banes        } else {
447e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // Else, we'll use the framework API
457e82b99953680915596eaf0eb35927388e574ca8Chris Banes            drawable.setTint(tint);
467e82b99953680915596eaf0eb35927388e574ca8Chris Banes        }
477e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
487e82b99953680915596eaf0eb35927388e574ca8Chris Banes
497e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static void setTintList(Drawable drawable, ColorStateList tint) {
507e82b99953680915596eaf0eb35927388e574ca8Chris Banes        if (drawable instanceof DrawableWrapperLollipop) {
517e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // GradientDrawable on Lollipop does not support tinting, so we'll use our compatible
527e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // functionality instead
537e82b99953680915596eaf0eb35927388e574ca8Chris Banes            DrawableCompatBase.setTintList(drawable, tint);
547e82b99953680915596eaf0eb35927388e574ca8Chris Banes        } else {
557e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // Else, we'll use the framework API
567e82b99953680915596eaf0eb35927388e574ca8Chris Banes            drawable.setTintList(tint);
577e82b99953680915596eaf0eb35927388e574ca8Chris Banes        }
587e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
597e82b99953680915596eaf0eb35927388e574ca8Chris Banes
607e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static void setTintMode(Drawable drawable, PorterDuff.Mode tintMode) {
617e82b99953680915596eaf0eb35927388e574ca8Chris Banes        if (drawable instanceof GradientDrawable) {
627e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // GradientDrawable on Lollipop does not support tinting, so we'll use our compatible
637e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // functionality instead
647e82b99953680915596eaf0eb35927388e574ca8Chris Banes            DrawableCompatBase.setTintMode(drawable, tintMode);
657e82b99953680915596eaf0eb35927388e574ca8Chris Banes        } else {
667e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // Else, we'll use the framework API
677e82b99953680915596eaf0eb35927388e574ca8Chris Banes            drawable.setTintMode(tintMode);
687e82b99953680915596eaf0eb35927388e574ca8Chris Banes        }
697e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
707e82b99953680915596eaf0eb35927388e574ca8Chris Banes
717e82b99953680915596eaf0eb35927388e574ca8Chris Banes    public static Drawable wrapForTinting(Drawable drawable) {
727e82b99953680915596eaf0eb35927388e574ca8Chris Banes        if (drawable instanceof GradientDrawable) {
737e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // GradientDrawable on Lollipop does not support tinting, so we'll use our compatible
747e82b99953680915596eaf0eb35927388e574ca8Chris Banes            // functionality instead
757e82b99953680915596eaf0eb35927388e574ca8Chris Banes            return new DrawableWrapperLollipop(drawable);
767e82b99953680915596eaf0eb35927388e574ca8Chris Banes        }
777e82b99953680915596eaf0eb35927388e574ca8Chris Banes        return drawable;
787e82b99953680915596eaf0eb35927388e574ca8Chris Banes    }
797e82b99953680915596eaf0eb35927388e574ca8Chris Banes
807e82b99953680915596eaf0eb35927388e574ca8Chris Banes}
81