如何通过accessibilty服务覆盖来制作触摸事件?

时间:2018-05-16 13:26:36

标签: android service touch overlay

我目前正在开发一个辅助功能服务,它有两个视图。一个按钮和一个简单的彩色视图。当我触摸按钮时,彩色视图出现/消失。 这是服务的代码 -

package com.hardik.accessibiltyservicetest;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.FingerprintGestureController;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import in.championswimmer.sfg.lib.SimpleFingerGestures;

public class Service extends AccessibilityService {

    FrameLayout mLayout;
    WindowManager.LayoutParams lp;
    LayoutInflater inflater;
    WindowManager wm;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        // Create an overlay and display the action bar
        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mLayout = new FrameLayout(this);
        lp = new WindowManager.LayoutParams();
        lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.CENTER;
        inflater = LayoutInflater.from(this);
        inflater.inflate(R.layout.action_bar, mLayout);
        wm.addView(mLayout, lp);
        configurePowerButton();

    }

    boolean is = true;

    private void configurePowerButton() {
        SimpleFingerGestures obj = new SimpleFingerGestures();
        obj.setDebug(true);
        obj.setConsumeTouchEvents(true);
        obj.setOnFingerGestureListener(new SimpleFingerGestures.OnFingerGestureListener() {
            @Override
            public boolean onSwipeUp(int i, long l, double v) {
                Log.e("Swipe", "Up "+i);
                return false;
            }

            @Override
            public boolean onSwipeDown(int i, long l, double v) {
                return false;
            }

            @Override
            public boolean onSwipeLeft(int i, long l, double v) {
                return false;
            }

            @Override
            public boolean onSwipeRight(int i, long l, double v) {
                return false;
            }

            @Override
            public boolean onPinch(int i, long l, double v) {
                return false;
            }

            @Override
            public boolean onUnpinch(int i, long l, double v) {
                return false;
            }

            @Override
            public boolean onDoubleTap(int i) {
                return false;
            }
        });


        Button powerButton = mLayout.findViewById(R.id.power);
        final View img = mLayout.findViewById(R.id.view);
        powerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);
                Toast.makeText(getApplicationContext(), "yus", Toast.LENGTH_SHORT).show();
                if (is){
                    is = false;
                    img.setVisibility(View.GONE);
                }
                else{
                    is =true;
                    img.setVisibility(View.VISIBLE);
                }

            }
        });

        img.setOnTouchListener(obj);


    }


}

这是布局文件 -

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/power"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:background="#80FFFFFF"
        android:text="Test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

触摸事件,一切正常。但问题是,当前windowmanager参数的长度和宽度设置为匹配parent,覆盖阻止触摸事件到android ui本身,换句话说,android ui的任何部分都不会响应,除非覆盖停止,因为覆盖消耗所有触摸输入。而最大的挫折是我希望叠加层就像我在布局中设计的一样,并且通过设置任何其他长度和宽度参数来启动背景触摸问题,即它再次开始消耗触摸事件。

当我设置参数以包装内容时,它不会消耗所有触摸事件,但这并不能为我提供所需的服务ui。

也不介意使用手势库,代码是我个人项目的一部分。

提前感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决办法。只为每个元素制作单独的布局