blob: aa879cdce69c71f1a63751e6d8dee562c62d904a [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view;
import android.annotation.NonNull;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.SparseArray;
import com.android.internal.util.XmlUtils;
import android.annotation.XmlRes;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
/**
* Represents an icon that can be used as a mouse pointer.
* <p>
* Pointer icons can be provided either by the system using system styles,
* or by applications using bitmaps or application resources.
* </p>
*/
public final class PointerIcon implements Parcelable {
private static final String TAG = "PointerIcon";
/** {@hide} Style constant: Custom icon with a user-supplied bitmap. */
public static final int STYLE_CUSTOM = -1;
/** Style constant: Null icon. It has no bitmap. */
public static final int STYLE_NULL = 0;
/** Style constant: no icons are specified. If all views uses this, then falls back
* to the default style, but this is helpful to distinguish a view explicitly want
* to have the default icon.
* @hide
*/
public static final int STYLE_NOT_SPECIFIED = 1;
/** Style constant: Arrow icon. (Default mouse pointer) */
public static final int STYLE_ARROW = 1000;
/** {@hide} Style constant: Spot hover icon for touchpads. */
public static final int STYLE_SPOT_HOVER = 2000;
/** {@hide} Style constant: Spot touch icon for touchpads. */
public static final int STYLE_SPOT_TOUCH = 2001;
/** {@hide} Style constant: Spot anchor icon for touchpads. */
public static final int STYLE_SPOT_ANCHOR = 2002;
// Style constants for additional predefined icons for mice.
/** Style constant: context-menu. */
public static final int STYLE_CONTEXT_MENU = 1001;
/** Style constant: hand. */
public static final int STYLE_HAND = 1002;
/** Style constant: help. */
public static final int STYLE_HELP = 1003;
/** Style constant: wait. */
public static final int STYLE_WAIT = 1004;
/** Style constant: cell. */
public static final int STYLE_CELL = 1006;
/** Style constant: crosshair. */
public static final int STYLE_CROSSHAIR = 1007;
/** Style constant: text. */
public static final int STYLE_TEXT = 1008;
/** Style constant: vertical-text. */
public static final int STYLE_VERTICAL_TEXT = 1009;
/** Style constant: alias (indicating an alias of/shortcut to something is
* to be created. */
public static final int STYLE_ALIAS = 1010;
/** Style constant: copy. */
public static final int STYLE_COPY = 1011;
/** Style constant: no-drop. */
public static final int STYLE_NO_DROP = 1012;
/** Style constant: all-scroll. */
public static final int STYLE_ALL_SCROLL = 1013;
/** Style constant: horizontal double arrow mainly for resizing. */
public static final int STYLE_HORIZONTAL_DOUBLE_ARROW = 1014;
/** Style constant: vertical double arrow mainly for resizing. */
public static final int STYLE_VERTICAL_DOUBLE_ARROW = 1015;
/** Style constant: diagonal double arrow -- top-right to bottom-left. */
public static final int STYLE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
/** Style constant: diagonal double arrow -- top-left to bottom-right. */
public static final int STYLE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
/** Style constant: zoom-in. */
public static final int STYLE_ZOOM_IN = 1018;
/** Style constant: zoom-out. */
public static final int STYLE_ZOOM_OUT = 1019;
/** Style constant: grab. */
public static final int STYLE_GRAB = 1020;
/** Style constant: grabbing. */
public static final int STYLE_GRABBING = 1021;
// OEM private styles should be defined starting at this range to avoid
// conflicts with any system styles that may be defined in the future.
private static final int STYLE_OEM_FIRST = 10000;
/** The default pointer icon. */
public static final int STYLE_DEFAULT = STYLE_ARROW;
private static final PointerIcon gNullIcon = new PointerIcon(STYLE_NULL);
private static final SparseArray<PointerIcon> gSystemIcons = new SparseArray<PointerIcon>();
private final int mStyle;
private int mSystemIconResourceId;
private Bitmap mBitmap;
private float mHotSpotX;
private float mHotSpotY;
// The bitmaps for the additional frame of animated pointer icon. Note that the first frame
// will be stored in mBitmap.
private Bitmap mBitmapFrames[];
private int mDurationPerFrame;
private PointerIcon(int style) {
mStyle = style;
}
/**
* Gets a special pointer icon that has no bitmap.
*
* @return The null pointer icon.
*
* @see #STYLE_NULL
* @hide
*/
public static PointerIcon getNullIcon() {
return gNullIcon;
}
/**
* Gets the default pointer icon.
*
* @param context The context.
* @return The default pointer icon.
*
* @throws IllegalArgumentException if context is null.
* @hide
*/
public static PointerIcon getDefaultIcon(@NonNull Context context) {
return getSystemIcon(context, STYLE_DEFAULT);
}
/**
* Gets a system pointer icon for the given style.
* If style is not recognized, returns the default pointer icon.
*
* @param context The context.
* @param style The pointer icon style.
* @return The pointer icon.
*
* @throws IllegalArgumentException if context is null.
*/
public static PointerIcon getSystemIcon(@NonNull Context context, int style) {
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
if (style == STYLE_NULL) {
return gNullIcon;
}
PointerIcon icon = gSystemIcons.get(style);
if (icon != null) {
return icon;
}
int styleIndex = getSystemIconStyleIndex(style);
if (styleIndex == 0) {
styleIndex = getSystemIconStyleIndex(STYLE_DEFAULT);
}
int accessibilityConfig = Settings.Secure.getIntForUser(
context.getContentResolver(), Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON,
0, UserHandle.USER_CURRENT);
int defStyle = (accessibilityConfig == 1) ?
com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
TypedArray a = context.obtainStyledAttributes(null,
com.android.internal.R.styleable.Pointer,
0, defStyle);
int resourceId = a.getResourceId(styleIndex, -1);
a.recycle();
if (resourceId == -1) {
Log.w(TAG, "Missing theme resources for pointer icon style " + style);
return style == STYLE_DEFAULT ? gNullIcon : getSystemIcon(context, STYLE_DEFAULT);
}
icon = new PointerIcon(style);
if ((resourceId & 0xff000000) == 0x01000000) {
icon.mSystemIconResourceId = resourceId;
} else {
icon.loadResource(context, context.getResources(), resourceId);
}
gSystemIcons.append(style, icon);
return icon;
}
/**
* Creates a custom pointer from the given bitmap and hotspot information.
*
* @param bitmap The bitmap for the icon.
* @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
* Must be within the [0, bitmap.getWidth()) range.
* @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
* Must be within the [0, bitmap.getHeight()) range.
* @return A pointer icon for this bitmap.
*
* @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
* parameters are invalid.
*/
public static PointerIcon createCustomIcon(
@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
if (bitmap == null) {
throw new IllegalArgumentException("bitmap must not be null");
}
validateHotSpot(bitmap, hotSpotX, hotSpotY);
PointerIcon icon = new PointerIcon(STYLE_CUSTOM);
icon.mBitmap = bitmap;
icon.mHotSpotX = hotSpotX;
icon.mHotSpotY = hotSpotY;
return icon;
}
/**
* Loads a custom pointer icon from an XML resource.
* <p>
* The XML resource should have the following form:
* <code>
* &lt;?xml version="1.0" encoding="utf-8"?&gt;
* &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
* android:bitmap="@drawable/my_pointer_bitmap"
* android:hotSpotX="24"
* android:hotSpotY="24" /&gt;
* </code>
* </p>
*
* @param resources The resources object.
* @param resourceId The resource id.
* @return The pointer icon.
*
* @throws IllegalArgumentException if resources is null.
* @throws Resources.NotFoundException if the resource was not found or the drawable
* linked in the resource was not found.
*/
public static PointerIcon loadCustomIcon(@NonNull Resources resources, @XmlRes int resourceId) {
if (resources == null) {
throw new IllegalArgumentException("resources must not be null");
}
PointerIcon icon = new PointerIcon(STYLE_CUSTOM);
icon.loadResource(null, resources, resourceId);
return icon;
}
/**
* Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
* Returns a pointer icon (not necessarily the same instance) with the information filled in.
*
* @param context The context.
* @return The loaded pointer icon.
*
* @throws IllegalArgumentException if context is null.
* @hide
*/
public PointerIcon load(@NonNull Context context) {
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
if (mSystemIconResourceId == 0 || mBitmap != null) {
return this;
}
PointerIcon result = new PointerIcon(mStyle);
result.mSystemIconResourceId = mSystemIconResourceId;
result.loadResource(context, context.getResources(), mSystemIconResourceId);
return result;
}
/** @hide */
public int getStyle() {
return mStyle;
}
public static final Parcelable.Creator<PointerIcon> CREATOR
= new Parcelable.Creator<PointerIcon>() {
public PointerIcon createFromParcel(Parcel in) {
int style = in.readInt();
if (style == STYLE_NULL) {
return getNullIcon();
}
int systemIconResourceId = in.readInt();
if (systemIconResourceId != 0) {
PointerIcon icon = new PointerIcon(style);
icon.mSystemIconResourceId = systemIconResourceId;
return icon;
}
Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
float hotSpotX = in.readFloat();
float hotSpotY = in.readFloat();
return PointerIcon.createCustomIcon(bitmap, hotSpotX, hotSpotY);
}
public PointerIcon[] newArray(int size) {
return new PointerIcon[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mStyle);
if (mStyle != STYLE_NULL) {
out.writeInt(mSystemIconResourceId);
if (mSystemIconResourceId == 0) {
mBitmap.writeToParcel(out, flags);
out.writeFloat(mHotSpotX);
out.writeFloat(mHotSpotY);
}
}
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof PointerIcon)) {
return false;
}
PointerIcon otherIcon = (PointerIcon) other;
if (mStyle != otherIcon.mStyle
|| mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
return false;
}
if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
|| mHotSpotX != otherIcon.mHotSpotX
|| mHotSpotY != otherIcon.mHotSpotY)) {
return false;
}
return true;
}
private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
final XmlResourceParser parser = resources.getXml(resourceId);
final int bitmapRes;
final float hotSpotX;
final float hotSpotY;
try {
XmlUtils.beginDocument(parser, "pointer-icon");
final TypedArray a = resources.obtainAttributes(
parser, com.android.internal.R.styleable.PointerIcon);
bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
a.recycle();
} catch (Exception ex) {
throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
} finally {
parser.close();
}
if (bitmapRes == 0) {
throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
}
Drawable drawable;
if (context == null) {
drawable = resources.getDrawable(bitmapRes);
} else {
drawable = context.getDrawable(bitmapRes);
}
if (drawable instanceof AnimationDrawable) {
// Extract animation frame bitmaps.
final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
final int frames = animationDrawable.getNumberOfFrames();
drawable = animationDrawable.getFrame(0);
if (frames == 1) {
Log.w(TAG, "Animation icon with single frame -- simply treating the first "
+ "frame as a normal bitmap icon.");
} else {
// Assumes they have the exact duration.
mDurationPerFrame = animationDrawable.getDuration(0);
mBitmapFrames = new Bitmap[frames - 1];
final int width = drawable.getIntrinsicWidth();
final int height = drawable.getIntrinsicHeight();
for (int i = 1; i < frames; ++i) {
Drawable drawableFrame = animationDrawable.getFrame(i);
if (!(drawableFrame instanceof BitmapDrawable)) {
throw new IllegalArgumentException("Frame of an animated pointer icon "
+ "must refer to a bitmap drawable.");
}
if (drawableFrame.getIntrinsicWidth() != width ||
drawableFrame.getIntrinsicHeight() != height) {
throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
+ "is different. All frames should have the exact same size and "
+ "share the same hotspot.");
}
mBitmapFrames[i - 1] = ((BitmapDrawable)drawableFrame).getBitmap();
}
}
}
if (!(drawable instanceof BitmapDrawable)) {
throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
+ "refer to a bitmap drawable.");
}
// Set the properties now that we have successfully loaded the icon.
mBitmap = ((BitmapDrawable)drawable).getBitmap();
mHotSpotX = hotSpotX;
mHotSpotY = hotSpotY;
}
private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
}
if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
}
}
private static int getSystemIconStyleIndex(int style) {
switch (style) {
case STYLE_ARROW:
return com.android.internal.R.styleable.Pointer_pointerIconArrow;
case STYLE_SPOT_HOVER:
return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
case STYLE_SPOT_TOUCH:
return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
case STYLE_SPOT_ANCHOR:
return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
case STYLE_HAND:
return com.android.internal.R.styleable.Pointer_pointerIconHand;
case STYLE_CONTEXT_MENU:
return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
case STYLE_HELP:
return com.android.internal.R.styleable.Pointer_pointerIconHelp;
case STYLE_WAIT:
return com.android.internal.R.styleable.Pointer_pointerIconWait;
case STYLE_CELL:
return com.android.internal.R.styleable.Pointer_pointerIconCell;
case STYLE_CROSSHAIR:
return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
case STYLE_TEXT:
return com.android.internal.R.styleable.Pointer_pointerIconText;
case STYLE_VERTICAL_TEXT:
return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
case STYLE_ALIAS:
return com.android.internal.R.styleable.Pointer_pointerIconAlias;
case STYLE_COPY:
return com.android.internal.R.styleable.Pointer_pointerIconCopy;
case STYLE_ALL_SCROLL:
return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
case STYLE_NO_DROP:
return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
case STYLE_HORIZONTAL_DOUBLE_ARROW:
return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
case STYLE_VERTICAL_DOUBLE_ARROW:
return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
case STYLE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
return com.android.internal.R.styleable.
Pointer_pointerIconTopRightDiagonalDoubleArrow;
case STYLE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
return com.android.internal.R.styleable.
Pointer_pointerIconTopLeftDiagonalDoubleArrow;
case STYLE_ZOOM_IN:
return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
case STYLE_ZOOM_OUT:
return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
case STYLE_GRAB:
return com.android.internal.R.styleable.Pointer_pointerIconGrab;
case STYLE_GRABBING:
return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
default:
return 0;
}
}
}