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