package com.ifpdos.factorytest;

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

/* JADX INFO: loaded from: classes.dex */
public class CustomNavigationBar extends FrameLayout implements View.OnClickListener, View.OnLongClickListener {
    private static final String KEY_LOCATION_X = "location_x";
    private static final String KEY_LOCATION_Y = "location_y";
    private boolean mRemoveAble;
    private SharedPreferences mSharedPreferences;
    private float mStartX;
    private float mStartY;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWmLayoutParams;

    public CustomNavigationBar(Context context) {
        this(context, null);
    }

    public CustomNavigationBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initView(context);
    }

    private void initView(Context context) {
        View viewInflate = LayoutInflater.from(context).inflate(R.layout.nav_bar, (ViewGroup) null, false);
        viewInflate.findViewById(R.id.nav_bar_back).setOnClickListener(this);
        viewInflate.findViewById(R.id.nav_bar_home).setOnClickListener(this);
        viewInflate.findViewById(R.id.custom_float_linear_layout).setOnLongClickListener(this);
        viewInflate.findViewById(R.id.nav_bar_back).setOnLongClickListener(this);
        viewInflate.findViewById(R.id.nav_bar_home).setOnLongClickListener(this);
        addView(viewInflate);
        this.mWindowManager = (WindowManager) context.getSystemService("window");
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        this.mWmLayoutParams = layoutParams;
        layoutParams.type = 2002;
        this.mWmLayoutParams.flags = 8;
        this.mWmLayoutParams.width = -2;
        this.mWmLayoutParams.height = -2;
        this.mWmLayoutParams.gravity = 8388659;
        this.mWmLayoutParams.format = 1;
        Point point = new Point();
        this.mWindowManager.getDefaultDisplay().getRealSize(point);
        this.mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        int width = point.x - getWidth();
        int height = point.y - getHeight();
        this.mWmLayoutParams.x = width - 350;
        this.mWmLayoutParams.y = height;
    }

    public void add() {
        if (getParent() == null) {
            Log.e("Sean", "Sean ############ float nav add()");
            this.mWindowManager.addView(this, this.mWmLayoutParams);
        }
    }

    public void remove() {
        if (getParent() != null) {
            Log.e("Sean", "Sean ############ float nav remove()");
            this.mWindowManager.removeView(this);
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.nav_bar_back) {
            sendKeyCode(getContext(), 4);
        } else if (id == R.id.nav_bar_home) {
            sendKeyCode(getContext(), 3);
        }
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action != 1) {
            if (action == 2 && this.mRemoveAble) {
                this.mWmLayoutParams.x = (int) (motionEvent.getRawX() - this.mStartX);
                this.mWmLayoutParams.y = (int) (motionEvent.getRawY() - this.mStartY);
                this.mWindowManager.updateViewLayout(this, this.mWmLayoutParams);
            }
        } else {
            this.mRemoveAble = false;
            setBackground(null);
            this.mSharedPreferences.edit().putInt(KEY_LOCATION_X, this.mWmLayoutParams.x).putInt(KEY_LOCATION_Y, this.mWmLayoutParams.y).apply();
        }
        return true;
    }

    @Override // android.view.ViewGroup
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == 0) {
            this.mStartX = motionEvent.getX();
            this.mStartY = motionEvent.getY();
        } else if (action == 2) {
            int iAbs = Math.abs((int) (motionEvent.getX() - this.mStartX));
            int iAbs2 = Math.abs((int) (motionEvent.getY() - this.mStartY));
            if (iAbs >= 10 && iAbs2 >= 10) {
                return true;
            }
        }
        return super.onInterceptTouchEvent(motionEvent);
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        int id = view.getId();
        if (id != R.id.custom_float_linear_layout && id != R.id.nav_bar_back && id != R.id.nav_bar_home) {
            return false;
        }
        setBackgroundResource(R.drawable.nv_bar_bg);
        this.mRemoveAble = true;
        return false;
    }

    public static void sendKeyCode(Context context, final int i) {
        new Thread(new Runnable() { // from class: com.ifpdos.factorytest.CustomNavigationBar.1
            @Override // java.lang.Runnable
            public void run() {
                try {
                    new Instrumentation().sendKeyDownUpSync(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
