Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 android.view; |
| 18 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.XmlRes; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.content.res.XmlResourceParser; |
| 25 | import android.graphics.Bitmap; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.AnimationDrawable; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.BitmapDrawable; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
| 31 | import android.util.Log; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 32 | import android.util.SparseArray; |
| 33 | |
| 34 | import com.android.internal.util.XmlUtils; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Represents an icon that can be used as a mouse pointer. |
| 38 | * <p> |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 39 | * Pointer icons can be provided either by the system using system types, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 40 | * or by applications using bitmaps or application resources. |
| 41 | * </p> |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 42 | */ |
| 43 | public final class PointerIcon implements Parcelable { |
| 44 | private static final String TAG = "PointerIcon"; |
| 45 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 46 | /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */ |
| 47 | public static final int TYPE_CUSTOM = -1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 48 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 49 | /** Type constant: Null icon. It has no bitmap. */ |
| 50 | public static final int TYPE_NULL = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 51 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 52 | /** Type constant: no icons are specified. If all views uses this, then falls back |
| 53 | * to the default type, but this is helpful to distinguish a view explicitly want |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 54 | * to have the default icon. |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 55 | * @hide |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 56 | */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 57 | public static final int TYPE_NOT_SPECIFIED = 1; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 58 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 59 | /** Type constant: Arrow icon. (Default mouse pointer) */ |
| 60 | public static final int TYPE_ARROW = 1000; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 61 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 62 | /** {@hide} Type constant: Spot hover icon for touchpads. */ |
| 63 | public static final int TYPE_SPOT_HOVER = 2000; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 64 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 65 | /** {@hide} Type constant: Spot touch icon for touchpads. */ |
| 66 | public static final int TYPE_SPOT_TOUCH = 2001; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 67 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 68 | /** {@hide} Type constant: Spot anchor icon for touchpads. */ |
| 69 | public static final int TYPE_SPOT_ANCHOR = 2002; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 70 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 71 | // Type constants for additional predefined icons for mice. |
| 72 | /** Type constant: context-menu. */ |
| 73 | public static final int TYPE_CONTEXT_MENU = 1001; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 74 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 75 | /** Type constant: hand. */ |
| 76 | public static final int TYPE_HAND = 1002; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 77 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 78 | /** Type constant: help. */ |
| 79 | public static final int TYPE_HELP = 1003; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 80 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 81 | /** Type constant: wait. */ |
| 82 | public static final int TYPE_WAIT = 1004; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 83 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 84 | /** Type constant: cell. */ |
| 85 | public static final int TYPE_CELL = 1006; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 86 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 87 | /** Type constant: crosshair. */ |
| 88 | public static final int TYPE_CROSSHAIR = 1007; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 89 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 90 | /** Type constant: text. */ |
| 91 | public static final int TYPE_TEXT = 1008; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 92 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 93 | /** Type constant: vertical-text. */ |
| 94 | public static final int TYPE_VERTICAL_TEXT = 1009; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 95 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 96 | /** Type constant: alias (indicating an alias of/shortcut to something is |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 97 | * to be created. */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 98 | public static final int TYPE_ALIAS = 1010; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 99 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 100 | /** Type constant: copy. */ |
| 101 | public static final int TYPE_COPY = 1011; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 102 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 103 | /** Type constant: no-drop. */ |
| 104 | public static final int TYPE_NO_DROP = 1012; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 105 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 106 | /** Type constant: all-scroll. */ |
| 107 | public static final int TYPE_ALL_SCROLL = 1013; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 108 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 109 | /** Type constant: horizontal double arrow mainly for resizing. */ |
| 110 | public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 111 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 112 | /** Type constant: vertical double arrow mainly for resizing. */ |
| 113 | public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 114 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 115 | /** Type constant: diagonal double arrow -- top-right to bottom-left. */ |
| 116 | public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 117 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 118 | /** Type constant: diagonal double arrow -- top-left to bottom-right. */ |
| 119 | public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 120 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 121 | /** Type constant: zoom-in. */ |
| 122 | public static final int TYPE_ZOOM_IN = 1018; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 123 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 124 | /** Type constant: zoom-out. */ |
| 125 | public static final int TYPE_ZOOM_OUT = 1019; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 126 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 127 | /** Type constant: grab. */ |
| 128 | public static final int TYPE_GRAB = 1020; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 129 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 130 | /** Type constant: grabbing. */ |
| 131 | public static final int TYPE_GRABBING = 1021; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 132 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 133 | // OEM private types should be defined starting at this range to avoid |
| 134 | // conflicts with any system types that may be defined in the future. |
| 135 | private static final int TYPE_OEM_FIRST = 10000; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 136 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 137 | /** The default pointer icon. */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 138 | public static final int TYPE_DEFAULT = TYPE_ARROW; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 139 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 140 | private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL); |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 141 | private static final SparseArray<PointerIcon> gSystemIcons = new SparseArray<PointerIcon>(); |
Jun Mukai | 1f3dbff | 2015-12-16 14:41:25 -0800 | [diff] [blame] | 142 | private static boolean sUseLargeIcons = false; |
Jun Mukai | e4e75da | 2015-12-15 16:19:20 -0800 | [diff] [blame] | 143 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 144 | private final int mType; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 145 | private int mSystemIconResourceId; |
| 146 | private Bitmap mBitmap; |
| 147 | private float mHotSpotX; |
| 148 | private float mHotSpotY; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 149 | // The bitmaps for the additional frame of animated pointer icon. Note that the first frame |
| 150 | // will be stored in mBitmap. |
| 151 | private Bitmap mBitmapFrames[]; |
| 152 | private int mDurationPerFrame; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 153 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 154 | private PointerIcon(int type) { |
| 155 | mType = type; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Gets a special pointer icon that has no bitmap. |
| 160 | * |
| 161 | * @return The null pointer icon. |
| 162 | * |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 163 | * @see #TYPE_NULL |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 164 | * @hide |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 165 | */ |
| 166 | public static PointerIcon getNullIcon() { |
| 167 | return gNullIcon; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Gets the default pointer icon. |
| 172 | * |
| 173 | * @param context The context. |
| 174 | * @return The default pointer icon. |
| 175 | * |
| 176 | * @throws IllegalArgumentException if context is null. |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 177 | * @hide |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 178 | */ |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 179 | public static PointerIcon getDefaultIcon(@NonNull Context context) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 180 | return getSystemIcon(context, TYPE_DEFAULT); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 184 | * Gets a system pointer icon for the given type. |
| 185 | * If typeis not recognized, returns the default pointer icon. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 186 | * |
| 187 | * @param context The context. |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 188 | * @param type The pointer icon type. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 189 | * @return The pointer icon. |
| 190 | * |
| 191 | * @throws IllegalArgumentException if context is null. |
| 192 | */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 193 | public static PointerIcon getSystemIcon(@NonNull Context context, int type) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 194 | if (context == null) { |
| 195 | throw new IllegalArgumentException("context must not be null"); |
| 196 | } |
| 197 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 198 | if (type == TYPE_NULL) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 199 | return gNullIcon; |
| 200 | } |
| 201 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 202 | PointerIcon icon = gSystemIcons.get(type); |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 203 | if (icon != null) { |
| 204 | return icon; |
| 205 | } |
| 206 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 207 | int typeIndex = getSystemIconTypeIndex(type); |
| 208 | if (typeIndex == 0) { |
| 209 | typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jun Mukai | e4e75da | 2015-12-15 16:19:20 -0800 | [diff] [blame] | 212 | int defStyle = sUseLargeIcons ? |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 213 | com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 214 | TypedArray a = context.obtainStyledAttributes(null, |
| 215 | com.android.internal.R.styleable.Pointer, |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 216 | 0, defStyle); |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 217 | int resourceId = a.getResourceId(typeIndex, -1); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 218 | a.recycle(); |
| 219 | |
| 220 | if (resourceId == -1) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 221 | Log.w(TAG, "Missing theme resources for pointer icon type " + type); |
| 222 | return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 225 | icon = new PointerIcon(type); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 226 | if ((resourceId & 0xff000000) == 0x01000000) { |
| 227 | icon.mSystemIconResourceId = resourceId; |
| 228 | } else { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 229 | icon.loadResource(context, context.getResources(), resourceId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 230 | } |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 231 | gSystemIcons.append(type, icon); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 232 | return icon; |
| 233 | } |
| 234 | |
| 235 | /** |
Jun Mukai | 1f3dbff | 2015-12-16 14:41:25 -0800 | [diff] [blame] | 236 | * Updates wheter accessibility large icons are used or not. |
| 237 | * @hide |
| 238 | */ |
| 239 | public static void setUseLargeIcons(boolean use) { |
| 240 | sUseLargeIcons = use; |
| 241 | gSystemIcons.clear(); |
| 242 | } |
| 243 | |
| 244 | /** |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 245 | * Creates a custom pointer icon from the given bitmap and hotspot information. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 246 | * |
| 247 | * @param bitmap The bitmap for the icon. |
Alan Viverette | 376d10e | 2014-09-02 10:45:30 -0700 | [diff] [blame] | 248 | * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 249 | * Must be within the [0, bitmap.getWidth()) range. |
Alan Viverette | 376d10e | 2014-09-02 10:45:30 -0700 | [diff] [blame] | 250 | * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 251 | * Must be within the [0, bitmap.getHeight()) range. |
| 252 | * @return A pointer icon for this bitmap. |
| 253 | * |
| 254 | * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot |
| 255 | * parameters are invalid. |
| 256 | */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 257 | public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 258 | if (bitmap == null) { |
| 259 | throw new IllegalArgumentException("bitmap must not be null"); |
| 260 | } |
| 261 | validateHotSpot(bitmap, hotSpotX, hotSpotY); |
| 262 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 263 | PointerIcon icon = new PointerIcon(TYPE_CUSTOM); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 264 | icon.mBitmap = bitmap; |
| 265 | icon.mHotSpotX = hotSpotX; |
| 266 | icon.mHotSpotY = hotSpotY; |
| 267 | return icon; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Loads a custom pointer icon from an XML resource. |
| 272 | * <p> |
| 273 | * The XML resource should have the following form: |
| 274 | * <code> |
| 275 | * <?xml version="1.0" encoding="utf-8"?> |
| 276 | * <pointer-icon xmlns:android="http://schemas.android.com/apk/res/android" |
| 277 | * android:bitmap="@drawable/my_pointer_bitmap" |
| 278 | * android:hotSpotX="24" |
| 279 | * android:hotSpotY="24" /> |
| 280 | * </code> |
| 281 | * </p> |
| 282 | * |
| 283 | * @param resources The resources object. |
| 284 | * @param resourceId The resource id. |
| 285 | * @return The pointer icon. |
| 286 | * |
| 287 | * @throws IllegalArgumentException if resources is null. |
| 288 | * @throws Resources.NotFoundException if the resource was not found or the drawable |
| 289 | * linked in the resource was not found. |
| 290 | */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 291 | public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 292 | if (resources == null) { |
| 293 | throw new IllegalArgumentException("resources must not be null"); |
| 294 | } |
| 295 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 296 | PointerIcon icon = new PointerIcon(TYPE_CUSTOM); |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 297 | icon.loadResource(null, resources, resourceId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 298 | return icon; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded. |
| 303 | * Returns a pointer icon (not necessarily the same instance) with the information filled in. |
| 304 | * |
| 305 | * @param context The context. |
| 306 | * @return The loaded pointer icon. |
| 307 | * |
| 308 | * @throws IllegalArgumentException if context is null. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 309 | * @hide |
| 310 | */ |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 311 | public PointerIcon load(@NonNull Context context) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 312 | if (context == null) { |
| 313 | throw new IllegalArgumentException("context must not be null"); |
| 314 | } |
| 315 | |
| 316 | if (mSystemIconResourceId == 0 || mBitmap != null) { |
| 317 | return this; |
| 318 | } |
| 319 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 320 | PointerIcon result = new PointerIcon(mType); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 321 | result.mSystemIconResourceId = mSystemIconResourceId; |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 322 | result.loadResource(context, context.getResources(), mSystemIconResourceId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 323 | return result; |
| 324 | } |
| 325 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 326 | /** @hide */ |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 327 | public int getType() { |
| 328 | return mType; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 331 | public static final Parcelable.Creator<PointerIcon> CREATOR |
| 332 | = new Parcelable.Creator<PointerIcon>() { |
| 333 | public PointerIcon createFromParcel(Parcel in) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 334 | int type = in.readInt(); |
| 335 | if (type == TYPE_NULL) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 336 | return getNullIcon(); |
| 337 | } |
| 338 | |
| 339 | int systemIconResourceId = in.readInt(); |
| 340 | if (systemIconResourceId != 0) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 341 | PointerIcon icon = new PointerIcon(type); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 342 | icon.mSystemIconResourceId = systemIconResourceId; |
| 343 | return icon; |
| 344 | } |
| 345 | |
| 346 | Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in); |
| 347 | float hotSpotX = in.readFloat(); |
| 348 | float hotSpotY = in.readFloat(); |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 349 | return PointerIcon.create(bitmap, hotSpotX, hotSpotY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | public PointerIcon[] newArray(int size) { |
| 353 | return new PointerIcon[size]; |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | public int describeContents() { |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | public void writeToParcel(Parcel out, int flags) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 362 | out.writeInt(mType); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 363 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 364 | if (mType != TYPE_NULL) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 365 | out.writeInt(mSystemIconResourceId); |
| 366 | if (mSystemIconResourceId == 0) { |
| 367 | mBitmap.writeToParcel(out, flags); |
| 368 | out.writeFloat(mHotSpotX); |
| 369 | out.writeFloat(mHotSpotY); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public boolean equals(Object other) { |
| 376 | if (this == other) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | if (other == null || !(other instanceof PointerIcon)) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | PointerIcon otherIcon = (PointerIcon) other; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 385 | if (mType != otherIcon.mType |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 386 | || mSystemIconResourceId != otherIcon.mSystemIconResourceId) { |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap |
| 391 | || mHotSpotX != otherIcon.mHotSpotX |
| 392 | || mHotSpotY != otherIcon.mHotSpotY)) { |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | return true; |
| 397 | } |
| 398 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 399 | private void loadResource(Context context, Resources resources, @XmlRes int resourceId) { |
Alan Viverette | 376d10e | 2014-09-02 10:45:30 -0700 | [diff] [blame] | 400 | final XmlResourceParser parser = resources.getXml(resourceId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 401 | final int bitmapRes; |
| 402 | final float hotSpotX; |
| 403 | final float hotSpotY; |
| 404 | try { |
| 405 | XmlUtils.beginDocument(parser, "pointer-icon"); |
| 406 | |
Alan Viverette | 376d10e | 2014-09-02 10:45:30 -0700 | [diff] [blame] | 407 | final TypedArray a = resources.obtainAttributes( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 408 | parser, com.android.internal.R.styleable.PointerIcon); |
| 409 | bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0); |
Alan Viverette | 376d10e | 2014-09-02 10:45:30 -0700 | [diff] [blame] | 410 | hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0); |
| 411 | hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 412 | a.recycle(); |
| 413 | } catch (Exception ex) { |
| 414 | throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex); |
| 415 | } finally { |
| 416 | parser.close(); |
| 417 | } |
| 418 | |
| 419 | if (bitmapRes == 0) { |
| 420 | throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute."); |
| 421 | } |
| 422 | |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 423 | Drawable drawable; |
| 424 | if (context == null) { |
| 425 | drawable = resources.getDrawable(bitmapRes); |
| 426 | } else { |
| 427 | drawable = context.getDrawable(bitmapRes); |
| 428 | } |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 429 | if (drawable instanceof AnimationDrawable) { |
| 430 | // Extract animation frame bitmaps. |
| 431 | final AnimationDrawable animationDrawable = (AnimationDrawable) drawable; |
| 432 | final int frames = animationDrawable.getNumberOfFrames(); |
| 433 | drawable = animationDrawable.getFrame(0); |
| 434 | if (frames == 1) { |
| 435 | Log.w(TAG, "Animation icon with single frame -- simply treating the first " |
| 436 | + "frame as a normal bitmap icon."); |
| 437 | } else { |
| 438 | // Assumes they have the exact duration. |
| 439 | mDurationPerFrame = animationDrawable.getDuration(0); |
| 440 | mBitmapFrames = new Bitmap[frames - 1]; |
| 441 | final int width = drawable.getIntrinsicWidth(); |
| 442 | final int height = drawable.getIntrinsicHeight(); |
| 443 | for (int i = 1; i < frames; ++i) { |
| 444 | Drawable drawableFrame = animationDrawable.getFrame(i); |
| 445 | if (!(drawableFrame instanceof BitmapDrawable)) { |
| 446 | throw new IllegalArgumentException("Frame of an animated pointer icon " |
| 447 | + "must refer to a bitmap drawable."); |
| 448 | } |
| 449 | if (drawableFrame.getIntrinsicWidth() != width || |
| 450 | drawableFrame.getIntrinsicHeight() != height) { |
| 451 | throw new IllegalArgumentException("The bitmap size of " + i + "-th frame " |
| 452 | + "is different. All frames should have the exact same size and " |
| 453 | + "share the same hotspot."); |
| 454 | } |
| 455 | mBitmapFrames[i - 1] = ((BitmapDrawable)drawableFrame).getBitmap(); |
| 456 | } |
| 457 | } |
| 458 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 459 | if (!(drawable instanceof BitmapDrawable)) { |
| 460 | throw new IllegalArgumentException("<pointer-icon> bitmap attribute must " |
| 461 | + "refer to a bitmap drawable."); |
| 462 | } |
| 463 | |
| 464 | // Set the properties now that we have successfully loaded the icon. |
| 465 | mBitmap = ((BitmapDrawable)drawable).getBitmap(); |
| 466 | mHotSpotX = hotSpotX; |
| 467 | mHotSpotY = hotSpotY; |
| 468 | } |
| 469 | |
| 470 | private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) { |
| 471 | if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) { |
| 472 | throw new IllegalArgumentException("x hotspot lies outside of the bitmap area"); |
| 473 | } |
| 474 | if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) { |
| 475 | throw new IllegalArgumentException("y hotspot lies outside of the bitmap area"); |
| 476 | } |
| 477 | } |
| 478 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 479 | private static int getSystemIconTypeIndex(int type) { |
| 480 | switch (type) { |
| 481 | case TYPE_ARROW: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 482 | return com.android.internal.R.styleable.Pointer_pointerIconArrow; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 483 | case TYPE_SPOT_HOVER: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 484 | return com.android.internal.R.styleable.Pointer_pointerIconSpotHover; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 485 | case TYPE_SPOT_TOUCH: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 486 | return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 487 | case TYPE_SPOT_ANCHOR: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 488 | return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 489 | case TYPE_HAND: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 490 | return com.android.internal.R.styleable.Pointer_pointerIconHand; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 491 | case TYPE_CONTEXT_MENU: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 492 | return com.android.internal.R.styleable.Pointer_pointerIconContextMenu; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 493 | case TYPE_HELP: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 494 | return com.android.internal.R.styleable.Pointer_pointerIconHelp; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 495 | case TYPE_WAIT: |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 496 | return com.android.internal.R.styleable.Pointer_pointerIconWait; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 497 | case TYPE_CELL: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 498 | return com.android.internal.R.styleable.Pointer_pointerIconCell; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 499 | case TYPE_CROSSHAIR: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 500 | return com.android.internal.R.styleable.Pointer_pointerIconCrosshair; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 501 | case TYPE_TEXT: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 502 | return com.android.internal.R.styleable.Pointer_pointerIconText; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 503 | case TYPE_VERTICAL_TEXT: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 504 | return com.android.internal.R.styleable.Pointer_pointerIconVerticalText; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 505 | case TYPE_ALIAS: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 506 | return com.android.internal.R.styleable.Pointer_pointerIconAlias; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 507 | case TYPE_COPY: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 508 | return com.android.internal.R.styleable.Pointer_pointerIconCopy; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 509 | case TYPE_ALL_SCROLL: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 510 | return com.android.internal.R.styleable.Pointer_pointerIconAllScroll; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 511 | case TYPE_NO_DROP: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 512 | return com.android.internal.R.styleable.Pointer_pointerIconNodrop; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 513 | case TYPE_HORIZONTAL_DOUBLE_ARROW: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 514 | return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 515 | case TYPE_VERTICAL_DOUBLE_ARROW: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 516 | return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 517 | case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 518 | return com.android.internal.R.styleable. |
| 519 | Pointer_pointerIconTopRightDiagonalDoubleArrow; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 520 | case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 521 | return com.android.internal.R.styleable. |
| 522 | Pointer_pointerIconTopLeftDiagonalDoubleArrow; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 523 | case TYPE_ZOOM_IN: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 524 | return com.android.internal.R.styleable.Pointer_pointerIconZoomIn; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 525 | case TYPE_ZOOM_OUT: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 526 | return com.android.internal.R.styleable.Pointer_pointerIconZoomOut; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 527 | case TYPE_GRAB: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 528 | return com.android.internal.R.styleable.Pointer_pointerIconGrab; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 529 | case TYPE_GRABBING: |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 530 | return com.android.internal.R.styleable.Pointer_pointerIconGrabbing; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 531 | default: |
| 532 | return 0; |
| 533 | } |
| 534 | } |
| 535 | } |