whyclj
2019-12-31 d2272f21a9d4491f22b3d3c9d6dbfebcecff76e3
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
package com.mytestapp;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
 
import java.lang.ref.WeakReference;
 
public class MyLoadingDialog extends Dialog implements DialogInterface.OnCancelListener {
 
    private WeakReference<Context> mContext = new WeakReference<>(null);
    private volatile static MyLoadingDialog sDialog;
 
    private MyLoadingDialog(Context context, CharSequence message) {
        super(context, R.style.progress_dialog);
 
        mContext = new WeakReference<>(context);
 
        applyCompat();
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.dialog, null);
        TextView tvMessage = (TextView) view.findViewById(R.id.id_tv_loadingmsg);
        if (message != null && message.length() > 0) {
            tvMessage.setText(message);
        }
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addContentView(view, lp);
 
        setOnCancelListener(this);
 
    }
 
 
 
    @Override
    public void onCancel(DialogInterface dialog) {
        // 点手机返回键等触发Dialog消失,应该取消正在进行的网络请求等
        Context context = mContext.get();
        if (context != null) {
            Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show();
        }
 
    }
 
    public static synchronized void showLoading(Context context) {
        showLoading(context, "loading...");
    }
 
    public static synchronized void showLoading(Context context, CharSequence message) {
        showLoading(context, message, true);
    }
 
    public static synchronized void showLoading(Context context, CharSequence message, boolean cancelable) {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
 
        if (context == null || !(context instanceof Activity)) {
            return;
        }
        sDialog = new MyLoadingDialog(context, message);
        sDialog.setCancelable(cancelable);
 
        if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) {
            sDialog.show();
        }
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        this.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
    }
 
    public static synchronized void stopLoading() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
        sDialog = null;
    }
 
 
    private void fullScreenImmersive(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }
 
    @Override
    public void show() {
        Window window = getWindow();
        MyUtil.focusNotAle(window);
 
        super.show();
        MyUtil.hideNavigationBar(window);
        MyUtil.clearFocusNotAle(window);
    }
 
    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        // 隐藏状态栏不占位
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
 
}