summaryrefslogtreecommitdiff
path: root/core/java/android/view/PointerIcon.java
diff options
context:
space:
mode:
authorJun Mukai <mukai@google.com>2015-10-30 15:54:33 -0700
committerJun Mukai <mukai@google.com>2015-12-14 19:45:10 -0800
commitd4eaef7f4c5a5d281de4fff272cd33e892e26264 (patch)
tree7141708f5759a078b74932d487dc8fe83c29887f /core/java/android/view/PointerIcon.java
parent40aa9f1b989a7896fb683a5a7dd664aa3174012d (diff)
Make public pointer icon API with custom icons.
BUG: 25778347, 23804184 Change-Id: If138b97c750c912e9848412c27b65004899961eb
Diffstat (limited to 'core/java/android/view/PointerIcon.java')
-rw-r--r--core/java/android/view/PointerIcon.java106
1 files changed, 22 insertions, 84 deletions
diff --git a/core/java/android/view/PointerIcon.java b/core/java/android/view/PointerIcon.java
index d2a7d4ad4859..aa879cdce69c 100644
--- a/core/java/android/view/PointerIcon.java
+++ b/core/java/android/view/PointerIcon.java
@@ -16,8 +16,10 @@
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;
@@ -39,13 +41,11 @@ import android.util.Log;
* Pointer icons can be provided either by the system using system styles,
* or by applications using bitmaps or application resources.
* </p>
- *
- * @hide
*/
public final class PointerIcon implements Parcelable {
private static final String TAG = "PointerIcon";
- /** Style constant: Custom icon with a user-supplied bitmap. */
+ /** {@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. */
@@ -54,6 +54,7 @@ public final class PointerIcon implements Parcelable {
/** 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;
@@ -135,10 +136,11 @@ public final class PointerIcon implements Parcelable {
// conflicts with any system styles that may be defined in the future.
private static final int STYLE_OEM_FIRST = 10000;
- /** {@hide} The default pointer icon. */
+ /** 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;
@@ -160,6 +162,7 @@ public final class PointerIcon implements Parcelable {
* @return The null pointer icon.
*
* @see #STYLE_NULL
+ * @hide
*/
public static PointerIcon getNullIcon() {
return gNullIcon;
@@ -172,8 +175,9 @@ public final class PointerIcon implements Parcelable {
* @return The default pointer icon.
*
* @throws IllegalArgumentException if context is null.
+ * @hide
*/
- public static PointerIcon getDefaultIcon(Context context) {
+ public static PointerIcon getDefaultIcon(@NonNull Context context) {
return getSystemIcon(context, STYLE_DEFAULT);
}
@@ -187,7 +191,7 @@ public final class PointerIcon implements Parcelable {
*
* @throws IllegalArgumentException if context is null.
*/
- public static PointerIcon getSystemIcon(Context context, int style) {
+ public static PointerIcon getSystemIcon(@NonNull Context context, int style) {
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
@@ -196,6 +200,11 @@ public final class PointerIcon implements Parcelable {
return gNullIcon;
}
+ PointerIcon icon = gSystemIcons.get(style);
+ if (icon != null) {
+ return icon;
+ }
+
int styleIndex = getSystemIconStyleIndex(style);
if (styleIndex == 0) {
styleIndex = getSystemIconStyleIndex(STYLE_DEFAULT);
@@ -217,12 +226,13 @@ public final class PointerIcon implements Parcelable {
return style == STYLE_DEFAULT ? gNullIcon : getSystemIcon(context, STYLE_DEFAULT);
}
- PointerIcon icon = new PointerIcon(style);
+ icon = new PointerIcon(style);
if ((resourceId & 0xff000000) == 0x01000000) {
icon.mSystemIconResourceId = resourceId;
} else {
icon.loadResource(context, context.getResources(), resourceId);
}
+ gSystemIcons.append(style, icon);
return icon;
}
@@ -239,7 +249,8 @@ public final class PointerIcon implements Parcelable {
* @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
* parameters are invalid.
*/
- public static PointerIcon createCustomIcon(Bitmap bitmap, float hotSpotX, float hotSpotY) {
+ public static PointerIcon createCustomIcon(
+ @NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
if (bitmap == null) {
throw new IllegalArgumentException("bitmap must not be null");
}
@@ -273,7 +284,7 @@ public final class PointerIcon implements Parcelable {
* @throws Resources.NotFoundException if the resource was not found or the drawable
* linked in the resource was not found.
*/
- public static PointerIcon loadCustomIcon(Resources resources, @XmlRes int resourceId) {
+ public static PointerIcon loadCustomIcon(@NonNull Resources resources, @XmlRes int resourceId) {
if (resources == null) {
throw new IllegalArgumentException("resources must not be null");
}
@@ -291,10 +302,9 @@ public final class PointerIcon implements Parcelable {
* @return The loaded pointer icon.
*
* @throws IllegalArgumentException if context is null.
- * @see #isLoaded()
* @hide
*/
- public PointerIcon load(Context context) {
+ public PointerIcon load(@NonNull Context context) {
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
@@ -309,83 +319,11 @@ public final class PointerIcon implements Parcelable {
return result;
}
- /**
- * Returns true if the pointer icon style is {@link #STYLE_NULL}.
- *
- * @return True if the pointer icon style is {@link #STYLE_NULL}.
- */
- public boolean isNullIcon() {
- return mStyle == STYLE_NULL;
- }
-
- /**
- * Returns true if the pointer icon has been loaded and its bitmap and hotspot
- * information are available.
- *
- * @return True if the pointer icon is loaded.
- * @see #load(Context)
- */
- public boolean isLoaded() {
- return mBitmap != null || mStyle == STYLE_NULL;
- }
-
- /**
- * Gets the style of the pointer icon.
- *
- * @return The pointer icon style.
- */
+ /** @hide */
public int getStyle() {
return mStyle;
}
- /**
- * Gets the bitmap of the pointer icon.
- *
- * @return The pointer icon bitmap, or null if the style is {@link #STYLE_NULL}.
- *
- * @throws IllegalStateException if the bitmap is not loaded.
- * @see #isLoaded()
- * @see #load(Context)
- */
- public Bitmap getBitmap() {
- throwIfIconIsNotLoaded();
- return mBitmap;
- }
-
- /**
- * Gets the X offset of the pointer icon hotspot.
- *
- * @return The hotspot X offset.
- *
- * @throws IllegalStateException if the bitmap is not loaded.
- * @see #isLoaded()
- * @see #load(Context)
- */
- public float getHotSpotX() {
- throwIfIconIsNotLoaded();
- return mHotSpotX;
- }
-
- /**
- * Gets the Y offset of the pointer icon hotspot.
- *
- * @return The hotspot Y offset.
- *
- * @throws IllegalStateException if the bitmap is not loaded.
- * @see #isLoaded()
- * @see #load(Context)
- */
- public float getHotSpotY() {
- throwIfIconIsNotLoaded();
- return mHotSpotY;
- }
-
- private void throwIfIconIsNotLoaded() {
- if (!isLoaded()) {
- throw new IllegalStateException("The icon is not loaded.");
- }
- }
-
public static final Parcelable.Creator<PointerIcon> CREATOR
= new Parcelable.Creator<PointerIcon>() {
public PointerIcon createFromParcel(Parcel in) {