1/** 2 * Copyright (C) 2016 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.view; 18 19import static android.os.Build.VERSION.SDK_INT; 20import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 21 22import android.content.Context; 23import android.content.res.Resources; 24import android.graphics.Bitmap; 25import android.support.annotation.RestrictTo; 26import android.view.PointerIcon; 27 28/** 29 * Helper for accessing features in {@link android.view.PointerIcon} in a backwards compatible 30 * fashion. 31 */ 32public final class PointerIconCompat { 33 /** Synonym for {@link android.view.PointerIcon#TYPE_NULL} */ 34 public static final int TYPE_NULL = 0; 35 36 /** Synonym for {@link android.view.PointerIcon#TYPE_ARROW} */ 37 public static final int TYPE_ARROW = 1000; 38 39 /** Synonym for {@link android.view.PointerIcon#TYPE_CONTEXT_MENU} */ 40 public static final int TYPE_CONTEXT_MENU = 1001; 41 42 /** Synonym for {@link android.view.PointerIcon#TYPE_HAND} */ 43 public static final int TYPE_HAND = 1002; 44 45 /** Synonym for {@link android.view.PointerIcon#TYPE_HELP} */ 46 public static final int TYPE_HELP = 1003; 47 48 /** Synonym for {@link android.view.PointerIcon#TYPE_WAIT} */ 49 public static final int TYPE_WAIT = 1004; 50 51 /** Synonym for {@link android.view.PointerIcon#TYPE_CELL} */ 52 public static final int TYPE_CELL = 1006; 53 54 /** Synonym for {@link android.view.PointerIcon#TYPE_CROSSHAIR} */ 55 public static final int TYPE_CROSSHAIR = 1007; 56 57 /** Synonym for {@link android.view.PointerIcon#TYPE_TEXT} */ 58 public static final int TYPE_TEXT = 1008; 59 60 /** Synonym for {@link android.view.PointerIcon#TYPE_VERTICAL_TEXT} */ 61 public static final int TYPE_VERTICAL_TEXT = 1009; 62 63 /** Synonym for {@link android.view.PointerIcon#TYPE_ALIAS} */ 64 public static final int TYPE_ALIAS = 1010; 65 66 /** Synonym for {@link android.view.PointerIcon#TYPE_COPY} */ 67 public static final int TYPE_COPY = 1011; 68 69 /** Synonym for {@link android.view.PointerIcon#TYPE_NO_DROP} */ 70 public static final int TYPE_NO_DROP = 1012; 71 72 /** Synonym for {@link android.view.PointerIcon#TYPE_ALL_SCROLL} */ 73 public static final int TYPE_ALL_SCROLL = 1013; 74 75 /** Synonym for {@link android.view.PointerIcon#TYPE_HORIZONTAL_DOUBLE_ARROW} */ 76 public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014; 77 78 /** Synonym for {@link android.view.PointerIcon#TYPE_VERTICAL_DOUBLE_ARROW} */ 79 public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015; 80 81 /** Synonym for {@link android.view.PointerIcon#TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW} */ 82 public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016; 83 84 /** Synonym for {@link android.view.PointerIcon#TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW} */ 85 public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017; 86 87 /** Synonym for {@link android.view.PointerIcon#TYPE_ZOOM_IN} */ 88 public static final int TYPE_ZOOM_IN = 1018; 89 90 /** Synonym for {@link android.view.PointerIcon#TYPE_ZOOM_OUT} */ 91 public static final int TYPE_ZOOM_OUT = 1019; 92 93 /** Synonym for {@link android.view.PointerIcon#TYPE_GRAB} */ 94 public static final int TYPE_GRAB = 1020; 95 96 /** Synonym for {@link android.view.PointerIcon#TYPE_GRABBING} */ 97 public static final int TYPE_GRABBING = 1021; 98 99 /** Synonym for {@link android.view.PointerIcon#TYPE_DEFAULT} */ 100 public static final int TYPE_DEFAULT = TYPE_ARROW; 101 102 103 private Object mPointerIcon; 104 105 private PointerIconCompat(Object pointerIcon) { 106 mPointerIcon = pointerIcon; 107 } 108 109 /** 110 * @hide 111 */ 112 @RestrictTo(LIBRARY_GROUP) 113 public Object getPointerIcon() { 114 return mPointerIcon; 115 } 116 117 /** 118 * Gets a system pointer icon for the given style. 119 * If style is not recognized, returns the default pointer icon. 120 * 121 * @param context The context. 122 * @param style The pointer icon style. 123 * @return The pointer icon. 124 * 125 * @throws IllegalArgumentException if context is null. 126 */ 127 public static PointerIconCompat getSystemIcon(Context context, int style) { 128 if (SDK_INT >= 24) { 129 return new PointerIconCompat(PointerIcon.getSystemIcon(context, style)); 130 } else { 131 return new PointerIconCompat(null); 132 } 133 } 134 135 /** 136 * Creates a custom pointer from the given bitmap and hotspot information. 137 * 138 * @param bitmap The bitmap for the icon. 139 * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap. 140 * Must be within the [0, bitmap.getWidth()) range. 141 * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap. 142 * Must be within the [0, bitmap.getHeight()) range. 143 * @return A pointer icon for this bitmap. 144 * 145 * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot 146 * parameters are invalid. 147 */ 148 public static PointerIconCompat create(Bitmap bitmap, float hotSpotX, float hotSpotY) { 149 if (SDK_INT >= 24) { 150 return new PointerIconCompat(PointerIcon.create(bitmap, hotSpotX, hotSpotY)); 151 } else { 152 return new PointerIconCompat(null); 153 } 154 } 155 156 /** 157 * Loads a custom pointer icon from an XML resource. 158 * <p> 159 * The XML resource should have the following form: 160 * <code> 161 * <?xml version="1.0" encoding="utf-8"?> 162 * <pointer-icon xmlns:android="http://schemas.android.com/apk/res/android" 163 * android:bitmap="@drawable/my_pointer_bitmap" 164 * android:hotSpotX="24" 165 * android:hotSpotY="24" /> 166 * </code> 167 * </p> 168 * 169 * @param resources The resources object. 170 * @param resourceId The resource id. 171 * @return The pointer icon. 172 * 173 * @throws IllegalArgumentException if resources is null. 174 * @throws Resources.NotFoundException if the resource was not found or the drawable 175 * linked in the resource was not found. 176 */ 177 public static PointerIconCompat load(Resources resources, int resourceId) { 178 if (SDK_INT >= 24) { 179 return new PointerIconCompat(PointerIcon.load(resources, resourceId)); 180 } else { 181 return new PointerIconCompat(null); 182 } 183 } 184} 185