Android View使用startDragAndDrop 异常 Unable to initiate drag

Android View使用startDragAndDrop 异常 Unable to initiate drag

设备环境:Android13

源码来源:android-13.0.0_r3

View在使用startDragAndDrop出现如下异常
shell 复制代码
01-01 12:44:12.243 24031 24031 E View    : Unable to initiate drag
01-01 12:44:12.243 24031 24031 E View    : java.lang.IllegalStateException: Need to validate before calling identify is cleared
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Parcel.createExceptionOrNull(Parcel.java:3019)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Parcel.createException(Parcel.java:2995)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Parcel.readException(Parcel.java:2978)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Parcel.readException(Parcel.java:2920)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.IWindowSession$Stub$Proxy.performDrag(IWindowSession.java:1678)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.View.startDragAndDrop(View.java:27325)
01-01 12:44:12.243 24031 24031 E View    :      at com.zhonghong.ac.view.style.adapter.AcStyleAdapter$1.onLongClick(AcStyleAdapter.java:91)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.View.performLongClickInternal(View.java:7586)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.View.performLongClick(View.java:7544)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.View.performLongClick(View.java:7562)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.View$CheckForLongPress.run(View.java:29297)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Handler.handleCallback(Handler.java:942)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Looper.loopOnce(Looper.java:201)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Looper.loop(Looper.java:288)
01-01 12:44:12.243 24031 24031 E View    :      at android.app.ActivityThread.main(ActivityThread.java:7944)
01-01 12:44:12.243 24031 24031 E View    :      at java.lang.reflect.Method.invoke(Native Method)
01-01 12:44:12.243 24031 24031 E View    :      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
01-01 12:44:12.243 24031 24031 E View    :      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
01-01 12:44:12.243 24031 24031 E View    : Caused by: android.os.RemoteException: Remote stack trace:
01-01 12:44:12.243 24031 24031 E View    :      at com.android.server.wm.Session.validateAndResolveDragMimeTypeExtras(Session.java:369)
01-01 12:44:12.243 24031 24031 E View    :      at com.android.server.wm.Session.performDrag(Session.java:324)
01-01 12:44:12.243 24031 24031 E View    :      at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:876)
01-01 12:44:12.243 24031 24031 E View    :      at com.android.server.wm.Session.onTransact(Session.java:175)
01-01 12:44:12.243 24031 24031 E View    :      at android.os.Binder.execTransactInternal(Binder.java:1280)
01-01 12:44:12.243 24031 24031 E View    : 

使用Demo验证功能正常😀,然后集成到应用后就抛出异常无法使用😒,在百度、谷歌、Bing上来回找原因,并且反复核对代码,终于还是没有解决🤦‍♂️

不过还在定位到了原因,这个bug有人提出了issue issuetracker.google.com/issues/2456...

原因分析

根据上面的issue最终定位到了最终根源所在👇

java 复制代码
//services/core/java/com/android/server/wm/Session.java

@Override
public IBinder performDrag(IWindow window, int flags, SurfaceControl surface, int touchSource,
        float touchX, float touchY, float thumbCenterX, float thumbCenterY, ClipData data) {
    final int callingUid = Binder.getCallingUid();
    final int callingPid = Binder.getCallingPid();
    // Validate and resolve ClipDescription data before clearing the calling identity
    validateAndResolveDragMimeTypeExtras(data, callingUid, callingPid, mPackageName);
    validateDragFlags(flags, callingUid);
    final long ident = Binder.clearCallingIdentity();
    try {
        return mDragDropController.performDrag(mPid, mUid, window, flags, surface, touchSource,
                touchX, touchY, thumbCenterX, thumbCenterY, data);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}

/**
 * Validates the given drag flags.
 */
@VisibleForTesting
void validateDragFlags(int flags, int callingUid) {
    if (callingUid == Process.SYSTEM_UID) {
        throw new IllegalStateException("Need to validate before calling identify is cleared");
    }

    if ((flags & View.DRAG_FLAG_REQUEST_SURFACE_FOR_RETURN_ANIMATION) != 0) {
        if (!mCanStartTasksFromRecents) {
            throw new SecurityException("Requires START_TASKS_FROM_RECENTS permission");
        }
    }
}

/**
 * Validates the given drag data.
 */
@VisibleForTesting
void validateAndResolveDragMimeTypeExtras(ClipData data, int callingUid, int callingPid,
        String callingPackage) {
    if (callingUid == Process.SYSTEM_UID) {
        throw new IllegalStateException("Need to validate before calling identify is cleared");
    }
    final ClipDescription desc = data != null ? data.getDescription() : null;
    if (desc == null) {
        return;
    }
    //省略其他...
}

我去无语了,系统进程应用这是直接不让用啊🤷‍♂️

java 复制代码
if (callingUid == Process.SYSTEM_UID) {
    throw new IllegalStateException("Need to validate before calling identify is cleared");
}

看下面有人反馈该问题已经修复了,随后我去看了一下Android14的源码,好家伙不拦截了🤢

Android14 Session

结尾

所以主要原因是我的应用是系统应用导致在当前版本上无法使用,初步来看是在Android13及以下都无法使用!

算了,不搞了,也不想打扰framework,我也不涉及多窗口之类的拖拽,还是使用RecyclerView吧🧨

相关推荐
咒法师无翅鱼25 分钟前
【leetcode详解】T598 区间加法
算法·leetcode·职场和发展
砂糖はいかがですか。41 分钟前
关于合并两个有序链表
数据结构·算法·链表
不会打代码呜呜呜呜1 小时前
小白零基础--CPP多线程
开发语言·c++·算法
莫名有雪2 小时前
BUUCTF_[网鼎杯 2020 朱雀组]phpweb(反序列化绕过命令)
android
辰尘_星启2 小时前
【单层神经网络】基于MXNet的线性回归实现(底层实现)
算法·线性回归·mxnet
kcwqxx2 小时前
day37|完全背包基础+leetcode 518.零钱兑换II ,377.组合总和II
c++·算法·leetcode·动态规划
程序趣谈2 小时前
算法随笔_36: 复写零
数据结构·python·算法
九亿AI算法优化工作室&2 小时前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
python算法(魔法师版)4 小时前
基于机器学习鉴别中药材的方法
深度学习·线性代数·算法·机器学习·支持向量机·数据挖掘·动态规划
爱写代码的山山5 小时前
虚幻UE5手机安卓Android Studio开发设置2025
android·ue5·虚幻