package com.dss.tpservice.controller;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import com.dss.tpservice.WindowInfo;
import com.dss.tpservice.bean.ForbidDrawArea;
import com.dss.tpservice.utils.GsonUtil;
import com.seewo.vcommons.data.PropertiesUtils;
import java.util.HashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class PreventScratchController {
    private static final String KEY_NOT_ERASE_AREA_MAP = "KEY_NOT_ERASE_AREA_MAP";
    private static final String PERSIST_SYS_ENABLE_PREVENT_SCRATCH = "persist.sys.enable_scratch";
    private final Context mContext;
    private Handler mDataHandler;
    private Map<String, Map<String, ForbidDrawArea>> mDataMap;
    private final boolean mEnablePreventScratch;
    private HandlerThread mHandlerDataThread;
    private Rect mRectZero;
    private final int CREATE_LAYER = 2457;
    private final int BLOCK_TRANSFER = 4096;
    private final int DELETE_LAYER = 4097;

    public PreventScratchController(Context context) {
        Context applicationContext = context.getApplicationContext();
        this.mContext = applicationContext;
        boolean property = PropertiesUtils.getProperty(PERSIST_SYS_ENABLE_PREVENT_SCRATCH, false);
        this.mEnablePreventScratch = property;
        if (property) {
            this.mRectZero = new Rect(0, 0, 0, 0);
            this.mDataMap = new HashMap();
            Settings.Global.putString(applicationContext.getContentResolver(), KEY_NOT_ERASE_AREA_MAP, GsonUtil.toJson(this.mDataMap));
            HandlerThread handlerThread = new HandlerThread("Handle_Scratch_Thread");
            this.mHandlerDataThread = handlerThread;
            handlerThread.start();
            this.mDataHandler = new Handler(this.mHandlerDataThread.getLooper()) { // from class: com.dss.tpservice.controller.PreventScratchController.1
                @Override // android.os.Handler
                public void handleMessage(Message message) {
                    Map map;
                    WindowInfo windowInfo = (WindowInfo) message.obj;
                    if (message.what == 2457) {
                        Map map2 = (Map) PreventScratchController.this.mDataMap.get(windowInfo.getAttrs().packageName);
                        if (map2 != null) {
                            ForbidDrawArea forbidDrawArea = (ForbidDrawArea) map2.get(windowInfo.getBinder());
                            if (forbidDrawArea != null) {
                                forbidDrawArea.setArea(PreventScratchController.this.getCorrectArea(windowInfo));
                                forbidDrawArea.setWindowType(windowInfo.getAttrs().type);
                                forbidDrawArea.setDisplayId(windowInfo.getDisplayId());
                                forbidDrawArea.setWindowTitle(String.valueOf(windowInfo.getAttrs().getTitle()));
                            } else {
                                forbidDrawArea = new ForbidDrawArea(windowInfo.getDisplayId(), PreventScratchController.this.getCorrectArea(windowInfo), windowInfo.getAttrs().type, String.valueOf(windowInfo.getAttrs().getTitle()));
                            }
                            map2.put(windowInfo.getBinder(), forbidDrawArea);
                        } else {
                            map2 = new HashMap();
                            map2.put(windowInfo.getBinder(), new ForbidDrawArea(windowInfo.getDisplayId(), PreventScratchController.this.getCorrectArea(windowInfo), windowInfo.getAttrs().type, String.valueOf(windowInfo.getAttrs().getTitle())));
                        }
                        PreventScratchController.this.mDataMap.put(windowInfo.getAttrs().packageName, map2);
                        Settings.Global.putString(PreventScratchController.this.mContext.getContentResolver(), PreventScratchController.KEY_NOT_ERASE_AREA_MAP, GsonUtil.toJson(PreventScratchController.this.mDataMap));
                        return;
                    }
                    if (message.what == 4096) {
                        Map map3 = (Map) PreventScratchController.this.mDataMap.get(windowInfo.getAttrs().packageName);
                        if (map3 != null) {
                            ForbidDrawArea forbidDrawArea2 = (ForbidDrawArea) map3.get(windowInfo.getBinder());
                            if (forbidDrawArea2 != null) {
                                forbidDrawArea2.setArea(PreventScratchController.this.getCorrectArea(windowInfo));
                                forbidDrawArea2.setWindowType(windowInfo.getAttrs().type);
                                forbidDrawArea2.setDisplayId(windowInfo.getDisplayId());
                                forbidDrawArea2.setWindowTitle(String.valueOf(windowInfo.getAttrs().getTitle()));
                            } else {
                                forbidDrawArea2 = new ForbidDrawArea(windowInfo.getDisplayId(), PreventScratchController.this.getCorrectArea(windowInfo), windowInfo.getAttrs().type, String.valueOf(windowInfo.getAttrs().getTitle()));
                            }
                            map3.put(windowInfo.getBinder(), forbidDrawArea2);
                        } else {
                            map3 = new HashMap();
                            map3.put(windowInfo.getBinder(), new ForbidDrawArea(windowInfo.getDisplayId(), PreventScratchController.this.getCorrectArea(windowInfo), windowInfo.getAttrs().type, String.valueOf(windowInfo.getAttrs().getTitle())));
                        }
                        PreventScratchController.this.mDataMap.put(windowInfo.getAttrs().packageName, map3);
                        Settings.Global.putString(PreventScratchController.this.mContext.getContentResolver(), PreventScratchController.KEY_NOT_ERASE_AREA_MAP, GsonUtil.toJson(PreventScratchController.this.mDataMap));
                        return;
                    }
                    if (message.what != 4097 || (map = (Map) PreventScratchController.this.mDataMap.get(windowInfo.getAttrs().packageName)) == null || ((ForbidDrawArea) map.get(windowInfo.getBinder())) == null) {
                        return;
                    }
                    map.remove(windowInfo.getBinder());
                    if (map.isEmpty()) {
                        PreventScratchController.this.mDataMap.remove(windowInfo.getAttrs().packageName);
                    } else {
                        PreventScratchController.this.mDataMap.put(windowInfo.getAttrs().packageName, map);
                    }
                    Settings.Global.putString(PreventScratchController.this.mContext.getContentResolver(), PreventScratchController.KEY_NOT_ERASE_AREA_MAP, GsonUtil.toJson(PreventScratchController.this.mDataMap));
                }
            };
        }
    }

    public void handleAppCreate(WindowInfo windowInfo) {
        if (!this.mEnablePreventScratch || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !isLayersInValid(windowInfo)) {
            return;
        }
        Message messageObtain = Message.obtain();
        messageObtain.what = 2457;
        messageObtain.obj = windowInfo;
        this.mDataHandler.sendMessage(messageObtain);
    }

    public void blockTransfer(WindowInfo windowInfo) {
        if (!this.mEnablePreventScratch || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !isLayersInValid(windowInfo)) {
            return;
        }
        Message messageObtain = Message.obtain();
        messageObtain.what = 4096;
        messageObtain.obj = windowInfo;
        this.mDataHandler.sendMessage(messageObtain);
    }

    public void handleAppDestroy(WindowInfo windowInfo) {
        if (!this.mEnablePreventScratch || windowInfo == null || windowInfo.getAttrs() == null || TextUtils.isEmpty(windowInfo.getAttrs().packageName) || !isLayersInValid(windowInfo)) {
            return;
        }
        Message messageObtain = Message.obtain();
        messageObtain.what = 4097;
        messageObtain.obj = windowInfo;
        this.mDataHandler.sendMessage(messageObtain);
    }

    private boolean isLayersInValid(WindowInfo windowInfo) {
        return (2000 > windowInfo.getAttrs().type || windowInfo.getAttrs().type == 2013 || isSpecialWindow(windowInfo)) ? false : true;
    }

    private boolean isSpecialWindow(WindowInfo windowInfo) {
        return (windowInfo.getAttrs().packageName.equals("com.android.launcher3") && windowInfo.getAttrs().type == 2024 && "Taskbar".contentEquals(windowInfo.getAttrs().getTitle())) || (windowInfo.getAttrs().packageName.equals("com.android.systemui") && windowInfo.getAttrs().type == 2000 && "StatusBar".contentEquals(windowInfo.getAttrs().getTitle())) || (windowInfo.getAttrs().packageName.equals(PropertiesUtils.PC_OFF_ACTION_CHANGE_ANDROID) && windowInfo.getAttrs().type == 2015 && !TextUtils.isEmpty(windowInfo.getAttrs().getTitle()) && windowInfo.getAttrs().getTitle().toString().contains("PointerLocation"));
    }

    public void release() {
        if (this.mEnablePreventScratch) {
            this.mDataHandler.removeCallbacksAndMessages(null);
            this.mHandlerDataThread.quitSafely();
        }
    }

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