wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
package com.mm.android.deviceaddmodule.views.popwindow;
 
import android.app.Activity;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.widget.CommonTitle;
 
public class PopWindowFactory {
 
    private final String TAG=PopWindowFactory.this.getClass().getSimpleName();
 
    public enum PopWindowType {
        LOADING,        //加载框
        OPTION1,        //更多选项
        OPTION2,        //切换有线
        OPTION3,         //切换无线
        OPTION4,         //切换软AP
        OPTION5,         //手动选择配网方式
        CHOSETYPE,         //手动选择
    }
 
    public BasePopWindow createPopWindow(final Activity context, CommonTitle commonTitle, PopWindowType type) {
        BasePopWindow popupWindow = null;
        final WindowManager.LayoutParams params = context.getWindow().getAttributes();
        if (type != PopWindowType.LOADING) {
            params.alpha = 0.5f;//设置popwindow弹出时背景
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//兼容华为手机
            context.getWindow().setAttributes(params);
        }
        switch (type) {
            case LOADING:
                popupWindow = createLoadingPop(context, commonTitle);
                break;
            case OPTION1:
            case OPTION2:
            case OPTION3:
            case OPTION4:
                popupWindow = createOptionPop(context, type);
                break;
            case CHOSETYPE:
                popupWindow = createTypeChosePop(context, type);
                break;
            default:
                break;
        }
 
        if (popupWindow != null) {
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    params.alpha = 1.0f;
                    context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    context.getWindow().setAttributes(params);
                }
            });
        }
        return popupWindow;
    }
 
    public BasePopWindow createLoadingPopWindow(final Activity context, View title) {
        final WindowManager.LayoutParams params = context.getWindow().getAttributes();
        BasePopWindow popupWindow = createLoadingPop(context, (CommonTitle) title);
        if (popupWindow != null) {
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    params.alpha = 1.0f;
                    context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    context.getWindow().setAttributes(params);
                }
            });
        }
        return popupWindow;
    }
 
    /**
     * 加载框
     *
     * @param context
     * @return
     */
    private BasePopWindow createLoadingPop(final Activity context, final CommonTitle commonTitle) {
        View view = LayoutInflater.from(context).inflate(R.layout.common_progressdialog_layout1, null);
        final BasePopWindow popupWindow = new LoadingPopWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        LogUtil.debugLog(TAG,"createLoadingPop--->" + popupWindow);
        int[] location = new int[2];
        View decorView = context.getWindow().getDecorView();
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        int screenHeight = rect.bottom;
        commonTitle.getLocationOnScreen(location);
        int height = screenHeight - location[1] - commonTitle.getMeasuredHeight();
        popupWindow.setHeight(height);
        popupWindow.showAsDropDown(commonTitle);
        return popupWindow;
    }
 
    /**
     * 选项框
     *
     * @param context
     * @return
     */
    private BasePopWindow createOptionPop(Activity context, PopWindowType type) {
        View view = LayoutInflater.from(context).inflate(R.layout.more_options_layout, null);
        BasePopWindow popupWindow = new MoreOptionsPopWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ((MoreOptionsPopWindow) popupWindow).setType(type);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        popupWindow.drawContent(context);
        return popupWindow;
    }
 
    /**
     * 选项框
     *
     * @param context
     * @return
     */
    private BasePopWindow createTypeChosePop(Activity context, PopWindowType type) {
        View view = LayoutInflater.from(context).inflate(R.layout.chose_type_layout, null);
        BasePopWindow popupWindow = new ChoseTypePopWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ((ChoseTypePopWindow) popupWindow).setType(type);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        popupWindow.drawContent(context);
        return popupWindow;
    }
}