软件平台:Android11
硬件平台:高通
需求由来:项目中支持电磁笔,发现电磁笔接触屏幕的点系统都会显示黑色的鼠标小箭头,需要将这个屏蔽掉;
这种问题从资源名称着手比较好,而且这种全局的资源一般在SystemUI或者Framework的res目录下的概率较大,我们找到了类似鼠标图片资源的文件:pointer_arrow.png,搜索使用该素材的xml如下:
kongbo@kanyun-SA5112M5:~/qcom-android/LINUX/android/frameworks/base/core/res$ grep pointer_arrow -nrw .
./res/drawable/pointer_arrow_icon.xml:3: android:bitmap="@drawable/pointer_arrow"
进而查找pointer_arrow_icon.xml的加载位置,
kongbo@kanyun-SA5112M5:~/qcom-android/LINUX/android/frameworks/base$ grep pointerIconArrow -nrw .
Binary file ./tools/aapt2/integration-tests/CommandTests/android-28.jar matches
./core/res/res/values/styles.xml:1356: <item name="pointerIconArrow">@drawable/pointer_arrow_icon</item>
./core/res/res/values/styles.xml:1391: <item name="pointerIconArrow">@drawable/pointer_arrow_large_icon</item>
./core/res/res/values/attrs.xml:8633: <attr name="pointerIconArrow" format="reference" />
kongbo@kanyun-SA5112M5:~/qcom-android/LINUX/android/frameworks/base$ grep pointerIconArrow -nr .
Binary file ./tools/aapt2/integration-tests/CommandTests/android-28.jar matches
./config/hiddenapi-greylist-max-o.txt:96158:Lcom/android/internal/R$styleable;->Pointer_pointerIconArrow:I
./core/java/android/view/PointerIcon.java:550: return com.android.internal.R.styleable.Pointer_pointerIconArrow;
./core/res/res/values/styles.xml:1356: <item name="pointerIconArrow">@drawable/pointer_arrow_icon</item>
./core/res/res/values/styles.xml:1391: <item name="pointerIconArrow">@drawable/pointer_arrow_large_icon</item>
./core/res/res/values/attrs.xml:8633: <attr name="pointerIconArrow" format="reference" />
锁定是在PointerIcon.java中控制显示的,进入到PointerIcon类中详细分析,该类中主要的方法有getNullIcon() 获取空图标 ;getDefaultIcon()获取默认图标;getSystemIcon()获取系统图标等,认真读几遍代码,不难发现获取鼠标的方法最终都会调用到getSystemIcon()这个方法里
/**
* Gets a system pointer icon for the given type.
* If typeis not recognized, returns the default pointer icon.
*
* @param context The context.
* @param type The pointer icon type.
* @return The pointer icon.
*
* @throws IllegalArgumentException if context is null.
*/
public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
if (true) {
return gNullIcon;
}
if (type == TYPE_NULL) {
return gNullIcon;
}
if (sDisplayListener == null) {
registerDisplayListener(context);
}
final int displayId = context.getDisplayId();
SparseArray<PointerIcon> systemIcons = gSystemIconsByDisplay.get(displayId);
if (systemIcons == null) {
systemIcons = new SparseArray<>();
gSystemIconsByDisplay.put(displayId, systemIcons);
}
PointerIcon icon = systemIcons.get(type);
// Reload if not in the same display.
if (icon != null) {
return icon;
}
int typeIndex = getSystemIconTypeIndex(type);
if (typeIndex == 0) {
typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
}
int defStyle = sUseLargeIcons ?
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(typeIndex, -1);
逻辑上,该方法返回空即可,具体改动如下:
index 18d0d7b98a4..c00c3836e92 100644
--- a/core/java/android/view/PointerIcon.java
+++ b/core/java/android/view/PointerIcon.java
@@ -215,6 +215,10 @@ public final class PointerIcon implements Parcelable {
throw new IllegalArgumentException("context must not be null");
}
+ if (true) {
+ return gNullIcon;
+ }
+
if (type == TYPE_NULL) {
return gNullIcon;
}
编译验证即可。