Android 13屏蔽Activity或包的手势禁止滑动退出

2023-12-26 16:39:17

在Android设备中,左/右滑两次会出现一个箭头然后退出Activity页面,有些Activity页面不想出现箭头及退出.
以下是对项目中的别人已实现的代码解析.

在config.xml中配置要禁用的Activity

packages/apps/Launcher3/quickstep/res/values/config.xml
resources中gesture_blocking_activitiesg添加Activity到item中,此例中为GestureSandboxActivity

<resources>
    <string name="overscroll_plugin_factory_class" translatable="false" />
    <string name="task_overlay_factory_class" translatable="false"/>

    <!-- Activities which block home gesture -->
    <string-array name="gesture_blocking_activities" translatable="false">
        <item>com.android.launcher3/com.android.quickstep.interaction.GestureSandboxActivity</item>
    </string-array>

后面所有代码都在这个java文件中
frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/EdgeBackGestureHandler.java

config.xml中禁用内容读出来

EdgeBackGestureHandler构造函数中把config.xml中内容读出来,把禁用的Activity读到mGestureBlockingActivities.contains变量中.

    // Activities which should not trigger Back gesture.
    private final List<ComponentName> mGestureBlockingActivities = new ArrayList<>();
    
        ComponentName recentsComponentName = ComponentName.unflattenFromString(
                context.getString(com.android.internal.R.string.config_recentsComponentName));
        if (recentsComponentName != null) {
            String recentsPackageName = recentsComponentName.getPackageName();
            PackageManager manager = context.getPackageManager();
            try {
                Resources resources = manager.getResourcesForApplication(
                        manager.getApplicationInfo(recentsPackageName,
                                PackageManager.MATCH_UNINSTALLED_PACKAGES
                                        | PackageManager.MATCH_DISABLED_COMPONENTS
                                        | PackageManager.GET_SHARED_LIBRARY_FILES));
                int resId = resources.getIdentifier(
                        "gesture_blocking_activities", "array", recentsPackageName);

                if (resId == 0) {
                    Log.e(TAG, "No resource found for gesture-blocking activities");
                } else {
                    String[] gestureBlockingActivities = resources.getStringArray(resId);
                    for (String gestureBlockingActivity : gestureBlockingActivities) {
                        mGestureBlockingActivities.add(
                                ComponentName.unflattenFromString(gestureBlockingActivity));
                    }
                }
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Failed to add gesture blocking activities", e);
            }
        }


函数判断是否要对当前Activity进行手势禁用

isGestureBlockingActivityRunning函数用于判断是否要对当前Activity进行手势禁用.

    private boolean isGestureBlockingActivityRunning() {
        ActivityManager.RunningTaskInfo runningTask =
                ActivityManagerWrapper.getInstance().getRunningTask();
        ComponentName topActivity = runningTask == null ? null : runningTask.topActivity;
        if (topActivity != null) {
            mPackageName = topActivity.getPackageName();
        } else {
            mPackageName = "_UNKNOWN";
        }
        return topActivity != null && mGestureBlockingActivities.contains(topActivity);
    }

监听应用任务变化,判断当前是否要禁用手势

mGestureBlockingActivityRunning为true则要禁用.

    private boolean mGestureBlockingActivityRunning;
    
    
    private TaskStackChangeListener mTaskStackListener = new TaskStackChangeListener() {
        @Override
        public void onTaskStackChanged() {
            mGestureBlockingActivityRunning = isGestureBlockingActivityRunning();
        }
        @Override
        public void onTaskCreated(int taskId, ComponentName componentName) {
            if (componentName != null) {
                mPackageName = componentName.getPackageName();
            } else {
                mPackageName = "_UNKNOWN";
            }
        }

        @Override
        public void onActivityPinned(String packageName, int userId, int taskId, int stackId) {
            mIsInPipMode = true;
        }

        @Override
        public void onActivityUnpinned() {
            mIsInPipMode = false;
        }
    };

禁用

在onMotionEvent函数中,设置mAllowGesture为false就会禁用手势,其中 mGestureBlockingActivityRunning为true时会设置mAllowGesture为false,这个变量赋值后面会提到.

    private void onMotionEvent(MotionEvent ev) {
        int action = ev.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            if (DEBUG_MISSING_GESTURE) {
                Log.d(DEBUG_MISSING_GESTURE_TAG, "Start gesture: " + ev);
            }

            // Verify if this is in within the touch region and we aren't in immersive mode, and
            // either the bouncer is showing or the notification panel is hidden
            mInputEventReceiver.setBatchingEnabled(false);
            mIsOnLeftEdge = ev.getX() <= mEdgeWidthLeft + mLeftInset;
            mMLResults = 0;
            mLogGesture = false;
            mInRejectedExclusion = false;
            boolean isWithinInsets = isWithinInsets((int) ev.getX(), (int) ev.getY());
            mAllowGesture = !mDisabledForQuickstep && mIsBackGestureAllowed && isWithinInsets
                    && !mGestureBlockingActivityRunning
                    && !QuickStepContract.isBackGestureDisabled(mSysUiFlags)
                    && isWithinTouchRegion((int) ev.getX(), (int) ev.getY());
            if (mAllowGesture) {
                mEdgeBackPlugin.setIsLeftPanel(mIsOnLeftEdge);
                mEdgeBackPlugin.onMotionEvent(ev);
            }

作者:帅得不敢出门 csdn原创谢绝第三方网站转载及收录

文章来源:https://blog.csdn.net/zmlovelx/article/details/135224509
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。