18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
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
package com.example.basecore.util;
 
 
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.example.basecore.R;
 
import java.lang.reflect.Field;
 
/**
 * 视频业务toast
 */
public class ToastUtils {
    private static Field sFieldTN = null;
    private static Field sFiledHandler = null;
 
    static {
        if (deviceNeedHookToast()) {
            try {
                sFieldTN = Toast.class.getDeclaredField("mTN");
                sFieldTN.setAccessible(true);
 
                sFiledHandler = sFieldTN.getType().getDeclaredField("mHandler");
                sFiledHandler.setAccessible(true);
            } catch (NoSuchFieldException e) {
                // ignore
            }
        }
    }
 
    private static boolean deviceNeedHookToast() {
        return Build.VERSION.SDK_INT <= 25;
    }
 
    private static String oldMsg;
    protected static Toast toastInstance = null;
 
 
    /**
     * 中间单个toast显示
     */
    public static void showToast(Context context,String s) {
        try {
            if (toastInstance != null){
                toastInstance.cancel();
                toastInstance = null;
            }
            toastInstance = makeToast(context, s, Toast.LENGTH_SHORT);
            show(toastInstance);
        } catch (Exception e) {
        }
    }
 
    private static Toast makeToast(Context context, CharSequence text, int duration) {
        Toast toast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflate.inflate(R.layout.video_toast, null);
        FrameLayout toastContainer = contentView.findViewById(R.id.toastContainer);
        TextView textView = contentView.findViewById(R.id.textView);
        textView.setText(text);
        toast.setView(contentView);
        toast.setDuration(duration);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            toast.addCallback(new Toast.Callback() {
                @Override
                public void onToastHidden() {
                    super.onToastHidden();
                    if (toastInstance != null) {
                        toastInstance.cancel();
                        toastInstance = null;
                    }
                }
            });
        }
 
        if (isVertical(context)) {
            int height = ScreenUtils.getDeviceScreenHeight(context);
            toast.setGravity(Gravity.TOP, 0, height / 2);
 
            ViewGroup.LayoutParams params = toastContainer.getLayoutParams();
            params.height = height / 4;
            toastContainer.setLayoutParams(params);
        } else {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point sizes = new Point();
            windowManager.getDefaultDisplay().getRealSize(sizes);
            ViewGroup.LayoutParams params = toastContainer.getLayoutParams();
            params.width = sizes.x;
            params.height = sizes.y -ScreenUtils.getDeviceScreenHeight(context) * 2;
            toastContainer.setLayoutParams(params);
        }
 
        checkIfNeedHookToastHandler(toast);
 
        return toast;
    }
 
    private static boolean isVertical(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }
 
    //检查是否需要对 toast 进行 hook,如果需要则进行 hook
    private static void checkIfNeedHookToastHandler(Toast toast) {
        if (deviceNeedHookToast()) {
            hookToastHandler(toast);
        }
    }
 
    private static void hookToastHandler(Toast toast) {
        try {
            Object tn = sFieldTN.get(toast);
            Handler preHandler = (Handler) sFiledHandler.get(tn);
            sFiledHandler.set(tn, new ToastHandlerWarp(preHandler));
 
        } catch (Exception e) {
            // ignore
        }
    }
 
    private static void show(Toast toast) {
        checkIfNeedHookToastHandler(toast);
        toast.show();
    }
}