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();
|
}
|
}
|