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