package com.android.launcher3.debug;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.android.launcher3.R;

import androidx.annotation.NonNull;

/**
 * Author: roczheng
 * Date: 2019/11/28
 * Time: 14:10
 * Description:
 */
public class CustomDialog extends AlertDialog {
    private Context mContext;
    private int mResource;
    private View mDialogView;

    public CustomDialog(Context context, int resource) {
        super(context, R.style.Dialog_Fullscreen);//加载dialog的样式
        this.mContext = context;
        this.mResource = resource;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            } else {
                getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
            }
        } else {
            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

        if (mDialogView != null) {
            setContentView(mDialogView);
        } else if (mResource != 0) {
            mDialogView = LayoutInflater.from(mContext).inflate(mResource, null);
            mDialogView.setLayoutParams(layoutParams);
            setContentView(mDialogView, layoutParams);
        }
        mDialogView.setFocusable(false);
        mDialogView.setFocusableInTouchMode(false);
        setCanceledOnTouchOutside(false);

    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
