endian11
2020-09-15 389853ce971a3da0e806dddd909bb43f5928c90c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package safeluck.drive.evaluation.widget;
 
import android.app.Dialog;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
 
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
 
 
/**
 * DriveJudge
 * Created by lzw on 2020/9/15. 10:49:12
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public  class MyWidgetDialog extends DialogFragment {
 
 
    protected DialogParams mDialogParams;
 
    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        hideBottomUIMenu();
        window.setAttributes(params);
    }
 
    protected void show(AppCompatActivity activity,String tag){
        if (!TextUtils.isEmpty(tag)){
            show(activity.getSupportFragmentManager(),tag);
        }else{
            show(activity.getSupportFragmentManager(),activity.getClass().getSimpleName());
        }
    }
    /**
     * 隐藏虚拟按键,并且全屏
     */
    protected void hideBottomUIMenu() { //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = getDialog().getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) { //for new api versions.
            View decorView =getDialog().getWindow().getDecorView();
            int  uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
    protected boolean isNonEmpty(String content) {
        return !TextUtils.isEmpty(content);
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        dismiss();
        super.onConfigurationChanged(newConfig);
    }
 
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View dialogLayout = LayoutInflater.from(getContext()).inflate(setLayoutRes(),null);
        builder.setView(dialogLayout);
        onViewCreated(dialogLayout, null);
        return builder.create();
    }
 
    public class DialogParams {
        AppCompatActivity activity;
        int style;
        int animations;
        int contentView;
        String tag;
        boolean isCancelable;
        String prompt;
        int type = -1;
    }
    protected  int setLayoutRes() {
        return mDialogParams.contentView;
    }
    @StyleRes
    protected int setAnimations() {
        return mDialogParams.animations;
    }
 
 
    @Nullable
    @Override
    public final View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(setCancelable());
        Window window = getDialog().getWindow();
        int animationsRes = setAnimations();
        if (animationsRes != 0 && animationsRes != -1) {
            window.setWindowAnimations(animationsRes);
        }
        setCancelable(setCancelable());
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        int gravity = setGravity();
 
        //即未设置
        if (gravity != -1 && gravity != 0) {
            window.setGravity(gravity);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }
 
    private int setGravity() {
 
        return Gravity.CENTER;
    }
 
 
    private boolean setCancelable() {
        return mDialogParams.isCancelable;
    }
    public MyWidgetDialog() {
        this.mDialogParams = new DialogParams();
    }
    protected  static class Builder{
        private  MyWidgetDialog dialog;
        private DialogParams params;
 
 
        public Builder(AppCompatActivity activity){
            dialog = new MyWidgetDialog();
            params = dialog.mDialogParams;
            params.activity = activity;
        }
 
 
 
        public Builder setStyle(int val){
            params.style = val;
            return this;
        }
        public Builder setAnimations(int val) {
            params.animations = val;
            return this;
        }
 
        public Builder setContentView(@LayoutRes int val) {
            params.contentView = val;
            return this;
        }
 
        public Builder setCancelable(boolean val) {
            params.isCancelable = val;
            return this;
        }
 
        public MyWidgetDialog build() {
            if (params.contentView == -1) {
                throw new IllegalArgumentException("Please set setContentView");
            }
            dialog.show(params.activity, params.tag);
            return dialog;
        }
 
    }
 
    protected void show(AppCompatActivity activity) {
        show(activity.getSupportFragmentManager(), activity.getClass().getSimpleName());
    }
 
}