package com.dss.tpservice.controller;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.dss.tpservice.WindowInfo;
import com.dss.tpservice.bean.PreventTouchPenetrationBean;
import com.dss.tpservice.utils.GsonUtil;
import com.dss.tpservice.utils.ResourceUtil;
import com.seewo.sdk.OpenSDK;
import com.seewo.sdk.SDKTouchHelper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class TPController implements ITPController {
    private static final String ACTION_PRINT_DATA = "action.print.tpservice_data";
    private static final String TAG = "TPController";
    private final Context mContext;
    private Handler mDataHandler;
    private Map<String, WindowInfo> mHandleMap;
    private HandlerThread mHandlerDataThread;
    private final List<PreventTouchPenetrationBean> mJustHandleList;
    private MyReceiver mMyReceiver;
    private Rect mRectZero;
    private final int CREATE_LAYER = 2457;
    private final int BLOCK_TRANSFER = 4096;
    private final int DELETE_LAYER = 4097;
    private final boolean mIsDebug = false;

    public TPController(Context context) {
        this.mContext = context;
        List<PreventTouchPenetrationBean> list = (List) GsonUtil.fromJson(ResourceUtil.readAssets2String("prevent_touch_penetration_config.json"), GsonUtil.getType(List.class, PreventTouchPenetrationBean.class));
        this.mJustHandleList = list;
        if (list == null) {
            Log.e(TAG, "assets//prevent_touch_penetration_config.json not exist!");
            return;
        }
        this.mHandleMap = new HashMap();
        HandlerThread handlerThread = new HandlerThread("Handle_Data_Thread");
        this.mHandlerDataThread = handlerThread;
        handlerThread.start();
        this.mDataHandler = new Handler(this.mHandlerDataThread.getLooper()) { // from class: com.dss.tpservice.controller.TPController.1
            @Override // android.os.Handler
            public void handleMessage(Message message) {
                WindowInfo windowInfo = (WindowInfo) message.obj;
                if (message.what == 2457) {
                    SDKTouchHelper.I.addOrUpdateNoTouchArea(windowInfo.getBinder().hashCode(), TPController.this.getCorrectArea(windowInfo));
                    TPController.this.mHandleMap.put(windowInfo.getBinder(), windowInfo);
                } else if (message.what == 4096) {
                    SDKTouchHelper.I.addOrUpdateNoTouchArea(windowInfo.getBinder().hashCode(), TPController.this.getCorrectArea(windowInfo));
                    TPController.this.mHandleMap.put(windowInfo.getBinder(), windowInfo);
                } else if (message.what == 4097) {
                    SDKTouchHelper.I.removeNoTouchArea(windowInfo.getBinder().hashCode());
                    TPController.this.mHandleMap.remove(windowInfo.getBinder());
                }
            }
        };
        this.mRectZero = new Rect(0, 0, 0, 0);
        this.mMyReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_PRINT_DATA);
        context.registerReceiver(this.mMyReceiver, intentFilter, 2);
    }

    private boolean isLayersInValid(WindowInfo windowInfo) {
        int i = windowInfo.getAttrs().type;
        return (2000 <= i && i < 2005) || (2005 < i && i <= 2038);
    }

    @Override // com.dss.tpservice.controller.ITPController
    public void createLayer(WindowInfo windowInfo) {
        if (this.mJustHandleList == null || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !checkedAppNeedHandled(windowInfo)) {
            return;
        }
        if (OpenSDK.getInstance().isConnected()) {
            Message messageObtain = Message.obtain();
            messageObtain.what = 2457;
            messageObtain.obj = windowInfo;
            this.mDataHandler.sendMessage(messageObtain);
            return;
        }
        Log.i(TAG, "createLayer调用时，opensdk还没有初始化完成");
    }

    @Override // com.dss.tpservice.controller.ITPController
    public void blockTransfer(WindowInfo windowInfo) {
        if (this.mJustHandleList == null || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !checkedAppNeedHandled(windowInfo)) {
            return;
        }
        if (OpenSDK.getInstance().isConnected()) {
            Message messageObtain = Message.obtain();
            messageObtain.what = 4096;
            messageObtain.obj = windowInfo;
            this.mDataHandler.sendMessage(messageObtain);
            return;
        }
        Log.i(TAG, "blockTransfer调用时，opensdk还没有初始化完成");
    }

    @Override // com.dss.tpservice.controller.ITPController
    public void deleteLayer(WindowInfo windowInfo) {
        if (this.mJustHandleList == null || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !checkedAppNeedHandled(windowInfo)) {
            return;
        }
        if (OpenSDK.getInstance().isConnected()) {
            Message messageObtain = Message.obtain();
            messageObtain.what = 4097;
            messageObtain.obj = windowInfo;
            this.mDataHandler.sendMessage(messageObtain);
            return;
        }
        Log.i(TAG, "deleteLayer调用时，opensdk还没有初始化完成");
    }

    private boolean checkedAppNeedHandled(WindowInfo windowInfo) {
        for (int i = 0; i < this.mJustHandleList.size(); i++) {
            if (this.mJustHandleList.get(i).getPkgName().equals(windowInfo.getAttrs().packageName)) {
                if (TextUtils.isEmpty(this.mJustHandleList.get(i).getWinType()) && TextUtils.isEmpty(this.mJustHandleList.get(i).getWinTitle())) {
                    if (isLayersInValid(windowInfo)) {
                        return true;
                    }
                } else if (!TextUtils.isEmpty(this.mJustHandleList.get(i).getWinType()) && TextUtils.isEmpty(this.mJustHandleList.get(i).getWinTitle())) {
                    if (windowInfo.getAttrs().type == Integer.parseInt(this.mJustHandleList.get(i).getWinType())) {
                        return true;
                    }
                } else if (TextUtils.isEmpty(this.mJustHandleList.get(i).getWinType()) && !TextUtils.isEmpty(this.mJustHandleList.get(i).getWinTitle())) {
                    if (this.mJustHandleList.get(i).getWinTitle().contentEquals(windowInfo.getAttrs().getTitle())) {
                        return true;
                    }
                } else if (!TextUtils.isEmpty(this.mJustHandleList.get(i).getWinType()) && !TextUtils.isEmpty(this.mJustHandleList.get(i).getWinTitle()) && windowInfo.getAttrs().type == Integer.parseInt(this.mJustHandleList.get(i).getWinType()) && this.mJustHandleList.get(i).getWinTitle().contentEquals(windowInfo.getAttrs().getTitle())) {
                    return true;
                }
            }
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public Rect getCorrectArea(WindowInfo windowInfo) {
        if (windowInfo.getVisibility() != 0) {
            return this.mRectZero;
        }
        return windowInfo.getContentFrame();
    }

    @Override // com.dss.tpservice.controller.ITPController
    public void release() {
        if (this.mJustHandleList == null) {
            return;
        }
        this.mDataHandler.removeCallbacksAndMessages(null);
        this.mHandlerDataThread.quitSafely();
        this.mContext.unregisterReceiver(this.mMyReceiver);
    }

    private class MyReceiver extends BroadcastReceiver {
        private MyReceiver() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if (TPController.ACTION_PRINT_DATA.equals(intent.getAction())) {
                for (WindowInfo windowInfo : TPController.this.mHandleMap.values()) {
                    Log.i(TPController.TAG, "Layer--->Binder = " + windowInfo.getBinder() + "，Rect = " + TPController.this.getCorrectArea(windowInfo) + "，pkg = " + windowInfo.getAttrs().packageName + "，type = " + windowInfo.getAttrs().type + "，title = " + ((Object) windowInfo.getAttrs().getTitle()));
                }
            }
        }
    }
}
