1/* 2 * Copyright (C) 2013 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.v4.graphics.drawable; 18 19import android.graphics.drawable.Drawable; 20 21/** 22 * Helper for accessing features in {@link android.graphics.drawable.Drawable} 23 * introduced after API level 4 in a backwards compatible fashion. 24 */ 25public class DrawableCompat { 26 /** 27 * Interface for the full API. 28 */ 29 interface DrawableImpl { 30 void jumpToCurrentState(Drawable drawable); 31 void setAutoMirrored(Drawable drawable, boolean mirrored); 32 boolean isAutoMirrored(Drawable drawable); 33 } 34 35 /** 36 * Interface implementation that doesn't use anything about v4 APIs. 37 */ 38 static class BaseDrawableImpl implements DrawableImpl { 39 @Override 40 public void jumpToCurrentState(Drawable drawable) { 41 } 42 43 @Override 44 public void setAutoMirrored(Drawable drawable, boolean mirrored) { 45 } 46 47 @Override 48 public boolean isAutoMirrored(Drawable drawable) { 49 return false; 50 } 51 } 52 53 /** 54 * Interface implementation for devices with at least v11 APIs. 55 */ 56 static class HoneycombDrawableImpl extends BaseDrawableImpl { 57 @Override 58 public void jumpToCurrentState(Drawable drawable) { 59 DrawableCompatHoneycomb.jumpToCurrentState(drawable); 60 } 61 } 62 63 /** 64 * Interface implementation for devices with at least v11 APIs. 65 */ 66 static class KitKatDrawableImpl extends HoneycombDrawableImpl { 67 @Override 68 public void setAutoMirrored(Drawable drawable, boolean mirrored) { 69 DrawableCompatKitKat.setAutoMirrored(drawable, mirrored); 70 } 71 72 @Override 73 public boolean isAutoMirrored(Drawable drawable) { 74 return DrawableCompatKitKat.isAutoMirrored(drawable); 75 } 76 } 77 78 /** 79 * Select the correct implementation to use for the current platform. 80 */ 81 static final DrawableImpl IMPL; 82 static { 83 final int version = android.os.Build.VERSION.SDK_INT; 84 if (version >= 19) { 85 IMPL = new KitKatDrawableImpl(); 86 } else if (version >= 11) { 87 IMPL = new HoneycombDrawableImpl(); 88 } else { 89 IMPL = new BaseDrawableImpl(); 90 } 91 } 92 93 /** 94 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}. 95 * <p> 96 * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} 97 * device this method does nothing. 98 * 99 * @param drawable The Drawable against which to invoke the method. 100 */ 101 public static void jumpToCurrentState(Drawable drawable) { 102 IMPL.jumpToCurrentState(drawable); 103 } 104 105 /** 106 * Set whether this Drawable is automatically mirrored when its layout 107 * direction is RTL (right-to left). See 108 * {@link android.util.LayoutDirection}. 109 * <p> 110 * If running on a pre-{@link android.os.Build.VERSION_CODES#KITKAT} device 111 * this method does nothing. 112 * 113 * @param drawable The Drawable against which to invoke the method. 114 * @param mirrored Set to true if the Drawable should be mirrored, false if 115 * not. 116 */ 117 public static void setAutoMirrored(Drawable drawable, boolean mirrored) { 118 IMPL.setAutoMirrored(drawable, mirrored); 119 } 120 121 /** 122 * Tells if this Drawable will be automatically mirrored when its layout 123 * direction is RTL right-to-left. See {@link android.util.LayoutDirection}. 124 * <p> 125 * If running on a pre-{@link android.os.Build.VERSION_CODES#KITKAT} device 126 * this method returns false. 127 * 128 * @param drawable The Drawable against which to invoke the method. 129 * @return boolean Returns true if this Drawable will be automatically 130 * mirrored. 131 */ 132 public static boolean isAutoMirrored(Drawable drawable) { 133 return IMPL.isAutoMirrored(drawable); 134 } 135} 136