170 lines
5.3 KiB
Java
170 lines
5.3 KiB
Java
package com.yunbao.common.dialog;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.graphics.Color;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.lzf.easyfloat.EasyFloat;
|
|
import com.lzf.easyfloat.enums.ShowPattern;
|
|
import com.lzf.easyfloat.interfaces.OnPermissionResult;
|
|
import com.lzf.easyfloat.permission.PermissionUtils;
|
|
import com.yunbao.common.adapter.DebugDialogAdapter;
|
|
import com.yunbao.common.utils.AppManager;
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
|
|
public class DebugDialog {
|
|
RecyclerView recyclerView;
|
|
HashMap<String, View> params;
|
|
DebugDialogAdapter adapter;
|
|
private static DebugDialog debugDialog;
|
|
Context mContext;
|
|
private ShowPattern showPattern = ShowPattern.CURRENT_ACTIVITY;
|
|
|
|
private DebugDialogRunnable runnable;
|
|
|
|
public static void getInstance(DebugDialogRunnable runnable) {
|
|
if (debugDialog == null) {
|
|
debugDialog = new DebugDialog(runnable);
|
|
} else {
|
|
runnable.run(debugDialog);
|
|
}
|
|
debugDialog.showPattern = ShowPattern.CURRENT_ACTIVITY;
|
|
}
|
|
|
|
public static boolean checkShow() {
|
|
return EasyFloat.isShow("debug");
|
|
}
|
|
|
|
public DebugDialog clear() {
|
|
params.clear();
|
|
return this;
|
|
}
|
|
|
|
public void setParams(String tag, String msg) {
|
|
Log.i("debug弹窗", "setParams: " + tag + "|" + msg);
|
|
if (params.containsKey(tag)) {
|
|
((TextView) params.get(tag)).setText(tag + ":" + msg);
|
|
} else {
|
|
TextView textView = new TextView(mContext);
|
|
textView.setText(tag + ":" + msg);
|
|
params.put(tag, textView);
|
|
adapter.setParamMap(params);
|
|
}
|
|
EasyFloat.updateFloat("debug");
|
|
}
|
|
|
|
private DebugDialog(DebugDialogRunnable runnable) {
|
|
this.runnable = runnable;
|
|
if (params == null) {
|
|
Log.i("debug弹窗", "DebugDialog: 初始化参数");
|
|
params = new HashMap<>();
|
|
}
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
this.mContext = AppManager.getInstance().getMainActivity();
|
|
if (mContext == null) {
|
|
startWaitMainActivity();
|
|
return;
|
|
}
|
|
runnable.run(this);
|
|
// createView();
|
|
}
|
|
|
|
private void startWaitMainActivity() {
|
|
new Timer().schedule(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
Log.i("debug弹窗", "run: " + AppManager.getInstance().getMainActivity());
|
|
if (AppManager.getInstance().getMainActivity() != null) {
|
|
init();
|
|
cancel();
|
|
}
|
|
}
|
|
}, 0, 1000);
|
|
}
|
|
|
|
public void close() {
|
|
EasyFloat.dismiss("debug");
|
|
}
|
|
|
|
public void show() {
|
|
if (showPattern == ShowPattern.CURRENT_ACTIVITY) {
|
|
createView();
|
|
return;
|
|
}
|
|
if (PermissionUtils.checkPermission(mContext)) {
|
|
createView();
|
|
} else {
|
|
PermissionUtils.requestPermission((Activity) mContext, new OnPermissionResult() {
|
|
@Override
|
|
public void permissionResult(boolean b) {
|
|
ToastUtil.show("悬浮权限" + b);
|
|
if (b) {
|
|
createView();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
protected void createView() {
|
|
recyclerView = new RecyclerView(mContext);
|
|
adapter = new DebugDialogAdapter(mContext);
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
|
|
recyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
recyclerView.setAdapter(adapter);
|
|
recyclerView.setBackgroundColor(Color.WHITE);
|
|
TextView textView = new TextView(mContext);
|
|
textView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
ToastUtil.show("debug弹窗:" + params.size());
|
|
EasyFloat.updateFloat("debug");
|
|
}
|
|
});
|
|
params.put("debug弹窗", textView);
|
|
adapter.setParamMap(params);
|
|
|
|
EasyFloat.with(mContext)
|
|
.setTag("debug")
|
|
.setShowPattern(this.showPattern)
|
|
.setLayout(recyclerView)
|
|
.show();
|
|
runnable.run(this);
|
|
Log.i("debug弹窗", "createView: 创建");
|
|
}
|
|
|
|
public void setView(String value, View view, View.OnClickListener onClickListener) {
|
|
if (params.containsKey(value)) {
|
|
params.get(value).setOnClickListener(onClickListener);
|
|
} else {
|
|
view.setOnClickListener(onClickListener);
|
|
params.put(value, view);
|
|
}
|
|
adapter.setParamMap(params);
|
|
EasyFloat.updateFloat("debug");
|
|
}
|
|
|
|
public void setShowPattern(ShowPattern showPattern) {
|
|
this.showPattern = showPattern;
|
|
}
|
|
|
|
public interface DebugDialogRunnable {
|
|
void run(DebugDialog dialog);
|
|
}
|
|
}
|