1148 lines
42 KiB
Java
1148 lines
42 KiB
Java
package com.ckkj.water.base;
|
||
|
||
import android.Manifest;
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Activity;
|
||
import android.app.AlertDialog;
|
||
import android.app.Dialog;
|
||
import android.content.Context;
|
||
import android.content.DialogInterface;
|
||
import android.content.Intent;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.text.InputFilter;
|
||
import android.text.InputType;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.view.Gravity;
|
||
import android.view.KeyEvent;
|
||
import android.view.LayoutInflater;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.view.Window;
|
||
import android.view.WindowManager;
|
||
import android.view.inputmethod.InputMethodManager;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.annotation.Nullable;
|
||
import androidx.core.app.ActivityCompat;
|
||
import androidx.fragment.app.Fragment;
|
||
import androidx.viewbinding.ViewBinding;
|
||
|
||
import com.ckkj.water.utils.show.ToastUtil;
|
||
import com.esafirm.imagepicker.features.ImagePicker;
|
||
import com.ckkj.water.AppConfig;
|
||
import com.ckkj.water.ConstantUtil;
|
||
import com.ckkj.water.EventMessage;
|
||
import com.ckkj.water.R;
|
||
import com.ckkj.water.dialog.LoadingDialog;
|
||
import com.ckkj.water.login.LoginActivity;
|
||
import com.ckkj.water.utils.dataUtil.SharePreUtil;
|
||
import com.ckkj.water.utils.manager.ActivityManager;
|
||
|
||
import java.lang.reflect.InvocationTargetException;
|
||
import java.lang.reflect.Method;
|
||
import java.lang.reflect.ParameterizedType;
|
||
import java.util.List;
|
||
import java.util.concurrent.atomic.AtomicBoolean;
|
||
|
||
import de.greenrobot.event.EventBus;
|
||
import de.greenrobot.event.Subscribe;
|
||
import de.greenrobot.event.ThreadMode;
|
||
import pub.devrel.easypermissions.EasyPermissions;
|
||
|
||
/**
|
||
* 所有Fragment基类
|
||
* @param <P>
|
||
*/
|
||
|
||
@SuppressWarnings({ "unchecked" })
|
||
public abstract class BaseFragment<T extends ViewBinding,P extends IPresenter> extends Fragment implements IView, EasyPermissions.PermissionCallbacks {
|
||
protected String TAG = "MING_DENG";
|
||
protected Activity activity;
|
||
protected P mPresenter;
|
||
protected Dialog loadingDialog;
|
||
protected T viewBinding;
|
||
protected Activity mContext;
|
||
protected final int REQUEST_CODE_100 = 100;
|
||
protected final int REQUEST_CODE_200 = 200;
|
||
protected static final int REQUEST_EVENT_CODE_10001 = 10001;
|
||
protected static final int REQUEST_EVENT_CODE_10002 = 10002;
|
||
protected static final int REQUEST_EVENT_CODE_10003 = 10003;
|
||
|
||
public static final int INPUT_TYPE_NORMAL = 0; // 默认
|
||
public static final int INPUT_TYPE_NUMBER = 1; // 数字
|
||
public static final int INPUT_TYPE_LETTER = 2; // 英文字母
|
||
public static final int INPUT_TYPE_CHINESE = 3; // 中文
|
||
public static final int INPUT_TYPE_NUMBER_LETTER = 4;//数字+字母
|
||
|
||
private static final AtomicBoolean isDialogShown = new AtomicBoolean(false); // 使用 AtomicBoolean 保证线程安全
|
||
|
||
|
||
|
||
@Override
|
||
public void onAttach(Activity activity) {
|
||
super.onAttach(activity);
|
||
initArgs(getArguments());
|
||
}
|
||
|
||
@Override
|
||
public void onResume() {
|
||
super.onResume();
|
||
onResumeInitData();
|
||
}
|
||
|
||
public boolean hasRecordPermission(String permission) {
|
||
return EasyPermissions.hasPermissions(mContext, permission);
|
||
}
|
||
|
||
public boolean hasRecordPermission(String[] permission) {
|
||
return EasyPermissions.hasPermissions(mContext, permission);
|
||
}
|
||
|
||
/**
|
||
* 获取权限
|
||
*
|
||
* @param permis
|
||
* @param title
|
||
* @param content
|
||
* @param requestCode String... permis
|
||
*/
|
||
public void getPermissionRequst(String permis, String title, String content, int requestCode) {
|
||
if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
// 已有权限,不需要处理
|
||
return;
|
||
}
|
||
// 还没权限,弹自定义确认弹窗
|
||
showDialogtext(mContext.getResources().getDrawable(R.mipmap.img_location),
|
||
false, false,
|
||
title,
|
||
content,
|
||
getString(R.string.button_deny),
|
||
getString(R.string.button_allow),
|
||
new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
// 用户点了"不允许",什么都不用做
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
showPermissionDialog(mContext.getResources().getDrawable(R.mipmap.img_permission_file),
|
||
true, title, content);
|
||
// 用户点了"允许",继续判断
|
||
if (ActivityCompat.shouldShowRequestPermissionRationale(mContext, permis)) {
|
||
// 用户之前拒绝过,但没勾"不再询问" ➔ 直接请求权限
|
||
// 修复:第一个参数应该是Fragment本身,而不是Activity的context
|
||
EasyPermissions.requestPermissions(BaseFragment.this, content, requestCode, permis);
|
||
} else {
|
||
// 可能是第一次申请,也可能是勾了"不再询问"
|
||
// 用EasyPermissions继续请求,系统自己决定弹不弹
|
||
// 修复:第一个参数应该是Fragment本身,而不是Activity的context
|
||
EasyPermissions.requestPermissions(BaseFragment.this, content, requestCode, permis);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 一次获取多个权限
|
||
*
|
||
* @param permis
|
||
* @param title
|
||
* @param content
|
||
* @param requestCode String... permis
|
||
*/
|
||
public void getMorePermissionRequst(String[] permis, String title, String content, int requestCode) {
|
||
if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
// 已有权限,不需要处理
|
||
return;
|
||
}
|
||
// 还没权限,弹自定义确认弹窗
|
||
showDialogtextInterBackKey(
|
||
null,
|
||
false,
|
||
false,
|
||
title,
|
||
content,
|
||
getString(R.string.button_deny),
|
||
getString(R.string.button_allow),
|
||
new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
showPermissionDialog(mContext.getResources().getDrawable(R.mipmap.img_permission_file),
|
||
true, title, content);
|
||
boolean shouldShowRationale = false;
|
||
for (String p : permis) {
|
||
if (ActivityCompat.shouldShowRequestPermissionRationale(mContext, p)) {
|
||
shouldShowRationale = true;
|
||
break;
|
||
}
|
||
}
|
||
// 使用 EasyPermissions 请求权限(系统决定是否弹系统框)
|
||
EasyPermissions.requestPermissions(mContext, content, requestCode, permis);
|
||
}
|
||
},false);
|
||
}
|
||
|
||
/**
|
||
* 判断Fragment是否真正对用户可见
|
||
*/
|
||
protected boolean isFragmentVisible() {
|
||
return isVisible() && isResumed();
|
||
}
|
||
|
||
/**
|
||
* 可见时初始化数据
|
||
*/
|
||
protected void onResumeInitData() {
|
||
}
|
||
|
||
@Override
|
||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
createLoadingDialog(getActivity());
|
||
mPresenter = createPresenter();//创建presenter
|
||
}
|
||
@Nullable
|
||
@Override
|
||
public View onCreateView(@Nullable LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle state) {
|
||
mContext = getActivity();
|
||
activity = getActivity();
|
||
EventBus.getDefault().register(this);
|
||
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
|
||
Class cls = (Class) type.getActualTypeArguments()[0];
|
||
try {
|
||
Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
|
||
viewBinding = (T) inflate.invoke(null, inflater, container, false);
|
||
} catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
|
||
e.printStackTrace();
|
||
}
|
||
initView();
|
||
return viewBinding.getRoot();
|
||
}
|
||
|
||
|
||
@Subscribe(threadMode = ThreadMode.MainThread)
|
||
public void onGetMessage(EventMessage message) {
|
||
Log.d("DINGDING", "BaseFragment收到EventBus消息: " + message.msg);
|
||
|
||
if (message.msg.equals(EventMessage.USER_LOGIN_LOST)) {
|
||
|
||
Log.d("DINGDING","Fragment接收一次");
|
||
Log.d("DINGDING", "当前Fragment状态: isDetached=" + isDetached() + ", isRemoving=" + isRemoving());
|
||
|
||
// 检查Fragment状态,如果正在销毁则不处理
|
||
if (isDetached() || isRemoving()) {
|
||
Log.d("DINGDING", "Fragment正在销毁,跳过处理");
|
||
return;
|
||
}
|
||
|
||
// 检查Activity状态,如果Activity正在销毁则不处理
|
||
if (mContext == null || mContext.isFinishing() || mContext.isDestroyed()) {
|
||
Log.d("DINGDING", "Fragment所属Activity正在销毁,跳过处理");
|
||
return;
|
||
}
|
||
|
||
if (isDialogShown.compareAndSet(false, true)) {
|
||
Log.d("DINGDING","Fragment触发弹窗");
|
||
AppConfig.USER_LOGIN = false;
|
||
AppConfig.MEMBER_ID = 0;
|
||
AppConfig.USER_TOKEN = "";
|
||
AppConfig.USER_ID = "";
|
||
SharePreUtil.putBoolean(ConstantUtil.SHARE_PRE_NAME, mContext, ConstantUtil.USER_LOGIN, AppConfig.USER_LOGIN);
|
||
|
||
// 确保在主线程中执行
|
||
mContext.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
// 再次检查Fragment和Activity状态
|
||
if (isDetached() || isRemoving() || mContext == null || mContext.isFinishing() || mContext.isDestroyed()) {
|
||
Log.d("DINGDING", "Fragment或Activity已销毁,取消弹窗");
|
||
isDialogShown.set(false);
|
||
return;
|
||
}
|
||
|
||
showDialogtextInterBackKey(null, false, false,
|
||
"提示",
|
||
"检测到您的账号在异地登录。若非本人操作,请立即修改密码并检查账号安全!",
|
||
"我知道了", "", // 交换参数位置
|
||
new OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
Log.d("DINGDING", "用户点击取消");
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
Log.d("DINGDING", "用户点击确定");
|
||
ActivityManager.removeAllActivityExceptMain();
|
||
goTo(mContext, LoginActivity.class, null);
|
||
isDialogShown.set(false);
|
||
}
|
||
}, true);
|
||
Log.d("DINGDING", "Fragment弹窗显示成功");
|
||
} catch (Exception e) {
|
||
Log.e("DINGDING", "Fragment弹窗显示失败", e);
|
||
isDialogShown.set(false);
|
||
}
|
||
}
|
||
});
|
||
} else {
|
||
Log.d("DINGDING", "Fragment弹窗已显示,跳过");
|
||
}
|
||
} else if (message.msg.equals(EventMessage.CODE_LOGIN_LOST)) {
|
||
// 检查是否满足跳转条件
|
||
// if (MyApplication.canJump()) { // MyApplication is not defined in this file
|
||
// }
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 初始化控件
|
||
*/
|
||
protected void initView() {
|
||
}
|
||
|
||
|
||
/**
|
||
* activity跳转
|
||
*/
|
||
protected void goToWihoutNoPresent(Context from, Class<? extends BaseActivityNoP> to, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if (data != null) i.putExtras(data);
|
||
from.startActivity(i);
|
||
}
|
||
|
||
|
||
protected LoadingDialog mLoadingDialog;
|
||
public void showLoadingDialog() {
|
||
LoadingDialog dialog = mLoadingDialog;
|
||
if (dialog == null&& mContext != null) {
|
||
dialog = new LoadingDialog(mContext);
|
||
// 不可触摸取消
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
// 强制取消关闭界面
|
||
dialog.setCancelable(true);
|
||
// dialog.setOnCancelListener(dialog1 -> finish());
|
||
mLoadingDialog = dialog;
|
||
} else {
|
||
dialog.dismiss();
|
||
}
|
||
if (dialog != null && mContext != null) {
|
||
try {
|
||
dialog.show();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
protected void createLoadingDialog(Context context){
|
||
LayoutInflater inflater = LayoutInflater.from(context);
|
||
@SuppressLint("InflateParams") View v = inflater.inflate(R.layout.dialog_loading, null);
|
||
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_loading_view);
|
||
ImageView imgLoading = v.findViewById(R.id.img_loading);
|
||
loadingDialog = new Dialog(context, R.style.MyDialogStyle);
|
||
loadingDialog.setCanceledOnTouchOutside(false);
|
||
loadingDialog.setContentView(layout,
|
||
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
|
||
LinearLayout.LayoutParams.MATCH_PARENT));
|
||
Window window = loadingDialog.getWindow();
|
||
if (window != null) {
|
||
WindowManager.LayoutParams lp = window.getAttributes();
|
||
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||
window.setGravity(Gravity.CENTER);
|
||
window.setAttributes(lp);
|
||
window.setWindowAnimations(R.style.PopWindowAnimStyle);
|
||
}
|
||
loadingDialog.setCanceledOnTouchOutside(false);
|
||
}
|
||
|
||
@Override
|
||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||
super.onViewCreated(view, savedInstanceState);
|
||
if (mPresenter != null) {
|
||
mPresenter.attachView(this);
|
||
}
|
||
initData(savedInstanceState);
|
||
}
|
||
|
||
|
||
public void imagePicker(int limitCount,int requestCode,Fragment fragment){
|
||
//获取相册
|
||
ImagePicker.create(fragment)
|
||
.folderMode(false)
|
||
.showCamera(false).limit(limitCount)
|
||
.start(requestCode);
|
||
String[] permis = {Manifest.permission.READ_EXTERNAL_STORAGE};
|
||
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||
// //获取到了权限,可以请求地址显示
|
||
// if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
//
|
||
// }else{
|
||
// showDialogtext(mContext.getResources().getDrawable(R.mipmap.img_location), false, false,
|
||
// "相册访问权限", "超越仓需要您的授权,访问相册来上传资料",
|
||
// "不允许", "允许", new BaseFragment.OnDialogClick() {
|
||
// @Override
|
||
// public void onCancelClick() {
|
||
//
|
||
// }
|
||
//
|
||
// @Override
|
||
// public void onConfirmClick() {
|
||
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||
//// Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
||
//// mContext.startActivity(intent);
|
||
// EasyPermissions.requestPermissions(mContext, "应用需要获相册授权",
|
||
// ConstantUtil.READ_EXTERNAL_STORAGE
|
||
// , Manifest.permission.READ_EXTERNAL_STORAGE);
|
||
// } else {
|
||
// EasyPermissions.requestPermissions(mContext, "应用需要获相册授权",
|
||
// ConstantUtil.READ_EXTERNAL_STORAGE
|
||
// , Manifest.permission.READ_EXTERNAL_STORAGE);
|
||
// }
|
||
// }
|
||
// });
|
||
// }
|
||
//// XXPermissions.with(mContext)
|
||
//// // 申请单个权限
|
||
//// .permission(Permission.READ_EXTERNAL_STORAGE)
|
||
//// .interceptor(new PermissionInterceptor())
|
||
//// .request(new OnPermissionCallback() {
|
||
//// @Override
|
||
//// public void onGranted(List<String> permissions, boolean all) {
|
||
////
|
||
//// }
|
||
////
|
||
//// @Override
|
||
//// public void onDenied(List<String> permissions, boolean never) {
|
||
//// if (never) {
|
||
//// Toast.makeText(mContext, "被永久拒绝授权,请手动授予权限", Toast.LENGTH_SHORT).show();
|
||
//// // 如果是被永久拒绝就跳转到应用权限系统设置页面
|
||
//// XXPermissions.startPermissionActivity(mContext, permissions);
|
||
//// } else {
|
||
//// Toast.makeText(mContext, "获取权限失败", Toast.LENGTH_SHORT).show();
|
||
//// }
|
||
//// return;
|
||
//// }
|
||
//// });
|
||
//
|
||
// } else {
|
||
// //获取到了权限,可以请求地址显示
|
||
// if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
// //获取相册
|
||
// ImagePicker.create(fragment)
|
||
// .folderMode(false)
|
||
// .showCamera(false).limit(limitCount)
|
||
// .start(requestCode);
|
||
// } else {
|
||
// showDialogtext(mContext.getResources().getDrawable(R.mipmap.img_location), false, false,
|
||
// "相册访问权限", "超越仓需要您的授权,访问相册来上传资料",
|
||
// "不允许", "允许", new BaseFragment.OnDialogClick() {
|
||
// @Override
|
||
// public void onCancelClick() {
|
||
//
|
||
// }
|
||
//
|
||
// @Override
|
||
// public void onConfirmClick() {
|
||
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||
// Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
||
// mContext.startActivity(intent);
|
||
// } else {
|
||
// EasyPermissions.requestPermissions(mContext, "应用需要获相册授权",
|
||
// ConstantUtil.READ_EXTERNAL_STORAGE
|
||
// , Manifest.permission.READ_EXTERNAL_STORAGE);
|
||
// }
|
||
// }
|
||
// });
|
||
// }
|
||
// }
|
||
}
|
||
|
||
|
||
public Dialog dialog = null;
|
||
// 弹出一个确认对话框 并可以设置取消按钮事件
|
||
public void showDialogtext(Drawable imgRes,boolean isIconShow, boolean isTextShow, String title,
|
||
String content, String okText, String cancelText,
|
||
final OnDialogClick click) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_index_permis, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_dialog_icon);
|
||
TextView tvOther = view.findViewById(R.id.tv_click);
|
||
TextView tvLeft = view.findViewById(R.id.btnLeft);
|
||
if(isIconShow){
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if(imgRes != null ){
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
}else{
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
if(isTextShow){
|
||
tvOther.setVisibility(View.VISIBLE);
|
||
}else{
|
||
tvOther.setVisibility(View.GONE);
|
||
}
|
||
|
||
|
||
|
||
setTextView(R.id.tv_title, title, view);
|
||
setTextView(R.id.tv_content, content, view);
|
||
// ((EditText) view.findViewById(R.id.txtContent)).setText(cancelText);
|
||
if (!TextUtils.isEmpty(okText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(okText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(cancelText);
|
||
}
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClick();
|
||
}
|
||
});
|
||
if(!TextUtils.isEmpty(cancelText)){
|
||
tvLeft.setVisibility(View.VISIBLE);
|
||
}else{
|
||
tvLeft.setVisibility(View.GONE);
|
||
|
||
}
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 强制登出,并且拦截返回键
|
||
* @param imgRes
|
||
* @param isIconShow
|
||
* @param isTextShow
|
||
* @param title
|
||
* @param content
|
||
* @param okText
|
||
* @param cancelText
|
||
* @param click
|
||
* @param interceptBackKey
|
||
*/
|
||
public void showDialogtextInterBackKey(Drawable imgRes, boolean isIconShow, boolean isTextShow, String title,
|
||
String content, String okText, String cancelText,
|
||
final OnDialogClick click, boolean interceptBackKey) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_index_permis, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_dialog_icon);
|
||
TextView tvOther = view.findViewById(R.id.tv_click);
|
||
TextView tvLeft = view.findViewById(R.id.btnLeft);
|
||
|
||
if (isIconShow) {
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if (imgRes != null) {
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
} else {
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
|
||
tvOther.setVisibility(isTextShow ? View.VISIBLE : View.GONE);
|
||
|
||
setTextView(R.id.tv_title, title, view);
|
||
setTextView(R.id.tv_content, content, view);
|
||
|
||
if (!TextUtils.isEmpty(okText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(okText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(cancelText);
|
||
}
|
||
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClick();
|
||
}
|
||
});
|
||
|
||
tvLeft.setVisibility(TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
|
||
|
||
AlertDialog.Builder builder;
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
|
||
// 🔒 返回键拦截逻辑
|
||
if (interceptBackKey) {
|
||
dialog.setCancelable(false);
|
||
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
|
||
@Override
|
||
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
|
||
return keyCode == KeyEvent.KEYCODE_BACK;
|
||
}
|
||
});
|
||
}
|
||
try {
|
||
((View) dialog.getWindow().getDecorView()
|
||
.findViewById(R.id.contentDialog)
|
||
.getParent().getParent())
|
||
.setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
// 安全兜底
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public void showPermissionDialog(Drawable imgRes,boolean isIconShow, String title,
|
||
String content) {
|
||
View view = getLayoutInflater().inflate(R.layout.permisson_top_dialog, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_permission_icon);
|
||
TextView tvTitle = view.findViewById(R.id.tv_permission_title);
|
||
TextView tvContent = view.findViewById(R.id.tv_permission_content);
|
||
tvTitle.setText(title);
|
||
tvContent.setText(content);
|
||
if(isIconShow){
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if(imgRes != null ){
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
}else{
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.create();
|
||
// 获取 dialog 的 Window 对象
|
||
Window window = dialog.getWindow();
|
||
if (window != null) {
|
||
// 设置 dialog 在屏幕顶部显示
|
||
WindowManager.LayoutParams params = window.getAttributes();
|
||
params.gravity = Gravity.TOP; // 设置对话框顶部显示
|
||
window.setAttributes(params);
|
||
}
|
||
// 显示 AlertDialog
|
||
dialog.show();
|
||
// dialog = builder.show();
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 设置指定TextView的文本
|
||
*
|
||
* @param id
|
||
* @param value textView的值
|
||
*/
|
||
public void setTextView(int id, String value, View view) {
|
||
try {
|
||
if (!TextUtils.isEmpty(value)) {
|
||
value = value.replaceAll("null", "");
|
||
((TextView) view.findViewById(id)).setText(value);
|
||
((TextView) view.findViewById(id)).setVisibility(View.VISIBLE);
|
||
} else {
|
||
((TextView) view.findViewById(id)).setText("");
|
||
((TextView) view.findViewById(id)).setVisibility(View.GONE);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
|
||
public interface OnDialogClick {
|
||
public void onCancelClick();
|
||
|
||
public void onConfirmClick();
|
||
}
|
||
|
||
/**
|
||
* 初始化参数
|
||
* @param bundle 需要初始化的参数
|
||
*/
|
||
protected void initArgs(Bundle bundle) {
|
||
}
|
||
|
||
/**
|
||
* 初始化控件
|
||
*/
|
||
protected void initWidget(View root) {
|
||
}
|
||
/**
|
||
* 初始化views
|
||
*
|
||
* @param state
|
||
*/
|
||
public abstract void initData(Bundle state);
|
||
public abstract P createPresenter();
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
EventBus.getDefault().unregister(this);
|
||
if (mPresenter != null) {
|
||
mPresenter.onDestroy();//释放资源
|
||
}
|
||
this.mPresenter = null;
|
||
hideDialogLoading();
|
||
|
||
// 重置弹窗状态,让其他Fragment可以显示弹窗
|
||
isDialogShown.set(false);
|
||
|
||
super.onDestroy();
|
||
}
|
||
|
||
@Override public void onDestroyView() {
|
||
super.onDestroyView();
|
||
//此处try catch 是因为unbinder.unbind() 在清空状态下,会造成崩溃
|
||
//其他处不影响使用
|
||
try {
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onDetach() {
|
||
super.onDetach();
|
||
this.activity = null;
|
||
}
|
||
@Override
|
||
public void showLoading() {
|
||
loadingDialog.show();
|
||
}
|
||
@Override
|
||
public void hideLoading() {
|
||
loadingDialog.dismiss();
|
||
}
|
||
|
||
@Override
|
||
public void showMessage(String message) {
|
||
}
|
||
|
||
@Override
|
||
public void handleError(Exception e) {
|
||
hideLoading();
|
||
showMessage(e.getMessage());
|
||
e.printStackTrace();
|
||
}
|
||
|
||
/**
|
||
* 跳转
|
||
*
|
||
* @param data 携带数据
|
||
*/
|
||
protected void goTo(Context from, Class<? extends BaseActivity> to, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if(data!=null) i.putExtras(data);
|
||
from.startActivity(i);
|
||
}
|
||
/**
|
||
* 跳转返回
|
||
*
|
||
*
|
||
*/
|
||
protected void goToForResult(Context from, Class<? extends BaseActivity> to, int questCode, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if(data!=null) i.putExtras(data);
|
||
startActivityForResult(i,questCode);
|
||
}
|
||
|
||
|
||
/***
|
||
* 关闭等待页面
|
||
*/
|
||
public void hideDialogLoading() {
|
||
LoadingDialog dialog = mLoadingDialog;
|
||
try {
|
||
if (dialog != null && mContext != null) {
|
||
mLoadingDialog = null;
|
||
dialog.dismiss();
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
public void hidePermissionDialog(){
|
||
if(dialog != null && dialog.isShowing()){
|
||
dialog.dismiss();
|
||
}
|
||
}
|
||
|
||
|
||
public void clearLoginInfo(){
|
||
AppConfig.USER_LOGIN = false;
|
||
SharePreUtil.putString(ConstantUtil.SHARE_PRE_NAME,mContext,ConstantUtil.USER_INFO_PHONE_NUMBER,"");
|
||
SharePreUtil.putString(ConstantUtil.SHARE_PRE_NAME,mContext,ConstantUtil.USER_UPLOAD_COMPANY_NAME,"未登录");
|
||
}
|
||
|
||
|
||
/**
|
||
* 处理登录丢失的统一方法
|
||
* 子类可以直接调用此方法来处理401错误
|
||
*/
|
||
protected void handleLoginLost() {
|
||
Log.d("DINGDING", "BaseFragment.handleLoginLost() 被调用");
|
||
|
||
// 检查Fragment状态
|
||
if (isDetached() || isRemoving()) {
|
||
Log.d("DINGDING", "Fragment正在销毁,跳过处理");
|
||
return;
|
||
}
|
||
|
||
// 检查Activity状态
|
||
if (mContext == null || mContext.isFinishing() || mContext.isDestroyed()) {
|
||
Log.d("DINGDING", "Fragment所属Activity正在销毁,跳过处理");
|
||
return;
|
||
}
|
||
|
||
// 重置登录信息
|
||
resetLoginInfo();
|
||
|
||
// 显示401弹窗
|
||
show401Dialog();
|
||
}
|
||
|
||
/**
|
||
* 重置登录信息
|
||
*/
|
||
private void resetLoginInfo() {
|
||
AppConfig.USER_LOGIN = false;
|
||
AppConfig.MEMBER_ID = 0;
|
||
AppConfig.USER_TOKEN = "";
|
||
AppConfig.USER_ID = "";
|
||
SharePreUtil.putBoolean(ConstantUtil.SHARE_PRE_NAME, mContext, ConstantUtil.USER_LOGIN, AppConfig.USER_LOGIN);
|
||
Log.d("DINGDING", "登录信息已重置");
|
||
}
|
||
|
||
/**
|
||
* 显示401弹窗
|
||
*/
|
||
private void show401Dialog() {
|
||
if (mContext == null || mContext.isFinishing() || mContext.isDestroyed()) {
|
||
Log.d("DINGDING", "Activity状态异常,无法显示弹窗");
|
||
return;
|
||
}
|
||
|
||
mContext.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
// 再次检查状态
|
||
if (isDetached() || isRemoving() || mContext == null || mContext.isFinishing() || mContext.isDestroyed()) {
|
||
Log.d("DINGDING", "Fragment或Activity已销毁,取消弹窗");
|
||
return;
|
||
}
|
||
|
||
showDialogtextInterBackKey(null, false, false,
|
||
"提示",
|
||
"检测到您的账号在异地登录。若非本人操作,请立即修改密码并检查账号安全!",
|
||
"我知道了", "",
|
||
new OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
Log.d("DINGDING", "用户点击取消");
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
Log.d("DINGDING", "用户点击确定");
|
||
ActivityManager.removeAllActivityExceptMain();
|
||
goTo(mContext, LoginActivity.class, null);
|
||
}
|
||
}, true);
|
||
Log.d("DINGDING", "BaseFragment 401弹窗显示成功");
|
||
} catch (Exception e) {
|
||
Log.e("DINGDING", "BaseFragment 401弹窗显示失败", e);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
/**
|
||
* +
|
||
*
|
||
* @param title 标题
|
||
* @param hint 提示内容2
|
||
* @param content 内容
|
||
* @param touchOutsideCancel 弹窗类型
|
||
* @param cancelText 取消 文案
|
||
* @param confirmText 确定 文案
|
||
* @param click
|
||
*/
|
||
public void showEditDialog(String title,
|
||
String hint,
|
||
String content,
|
||
boolean touchOutsideCancel,
|
||
String cancelText,
|
||
String confirmText,
|
||
int inputType,
|
||
final BaseActivity.OnDialogClick click) {
|
||
|
||
View view = getLayoutInflater().inflate(
|
||
R.layout.public_dialog_input_text,
|
||
null
|
||
);
|
||
|
||
EditText editText = view.findViewById(R.id.et_input);
|
||
|
||
//设置输入类型
|
||
setEditInputType(editText,inputType);
|
||
|
||
if(TextUtils.isEmpty(content)){
|
||
editText.setHint(hint);
|
||
}else{
|
||
editText.setText(content);
|
||
editText.setSelection(content.length());
|
||
}
|
||
|
||
setTextView(R.id.tv_title,title,view);
|
||
|
||
if (!TextUtils.isEmpty(confirmText)
|
||
&& !TextUtils.isEmpty(cancelText)) {
|
||
|
||
((TextView)view.findViewById(R.id.btnLeft))
|
||
.setText(cancelText);
|
||
|
||
((TextView)view.findViewById(R.id.btnRight))
|
||
.setText(confirmText);
|
||
}
|
||
|
||
view.findViewById(R.id.btnLeft)
|
||
.setOnClickListener(v -> {
|
||
|
||
if(dialog!=null&&dialog.isShowing())
|
||
dialog.dismiss();
|
||
|
||
if(click!=null)
|
||
click.onCancelClick();
|
||
|
||
});
|
||
|
||
view.findViewById(R.id.btnRight)
|
||
.setOnClickListener(v -> {
|
||
|
||
String value=editText.getText()
|
||
.toString()
|
||
.trim();
|
||
|
||
if(TextUtils.isEmpty(value)){
|
||
ToastUtil.showShort(mContext,hint);
|
||
return;
|
||
}
|
||
|
||
if(dialog!=null&&dialog.isShowing())
|
||
dialog.dismiss();
|
||
|
||
if(click!=null)
|
||
click.onConfirmClisk(value);
|
||
|
||
});
|
||
|
||
AlertDialog.Builder builder;
|
||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(
|
||
mContext,
|
||
AlertDialog.THEME_HOLO_LIGHT
|
||
);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
|
||
dialog = builder.show();
|
||
|
||
editText.requestFocus();
|
||
editText.postDelayed(() -> {
|
||
if (editText != null) {
|
||
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||
if (imm != null) {
|
||
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
|
||
}
|
||
}
|
||
}, 200);
|
||
|
||
dialog.setCanceledOnTouchOutside(
|
||
touchOutsideCancel
|
||
);
|
||
|
||
try {
|
||
((View)dialog.getWindow()
|
||
.getDecorView()
|
||
.findViewById(R.id.contentDialog)
|
||
.getParent()
|
||
.getParent())
|
||
.setBackgroundColor(
|
||
getResources().getColor(
|
||
R.color.transparent
|
||
)
|
||
);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
}
|
||
|
||
private void setEditInputType(EditText editText,int type){
|
||
|
||
switch (type){
|
||
|
||
case INPUT_TYPE_NUMBER:
|
||
//纯数字键盘
|
||
editText.setInputType(
|
||
InputType.TYPE_CLASS_NUMBER
|
||
);
|
||
break;
|
||
|
||
case INPUT_TYPE_LETTER:
|
||
|
||
//只能输入字母
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[a-zA-Z]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
case INPUT_TYPE_CHINESE:
|
||
|
||
//只能输入中文
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[\\u4e00-\\u9fa5]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
case INPUT_TYPE_NUMBER_LETTER:
|
||
|
||
//数字+字母
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[a-zA-Z0-9]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
default:
|
||
editText.setInputType(
|
||
InputType.TYPE_CLASS_TEXT
|
||
);
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 权限请求结果回调
|
||
*/
|
||
@Override
|
||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||
// 将权限结果传递给EasyPermissions处理
|
||
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
||
}
|
||
|
||
/**
|
||
* 权限被授予时的回调
|
||
*/
|
||
@Override
|
||
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
|
||
// 这个方法会在子Fragment中重写,这里可以留空或者提供默认实现
|
||
}
|
||
|
||
/**
|
||
* 权限被拒绝时的回调
|
||
*/
|
||
@Override
|
||
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
|
||
// 这个方法会在子Fragment中重写,这里可以留空或者提供默认实现
|
||
}
|
||
|
||
|
||
}
|