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