/**
* @ClassName: BaseActivity
* @Description: BaseActivity
* @CreateDate: 2021/7/19 8:42
* @UpdateUser: RyanLiu
*/
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;
import com.blankj.utilcode.util.ToastUtils;
import com.xql.loading.LoadingDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseActivity<B extends ViewDataBinding, VM extends BaseViewModel> extends AppCompatActivity {
protected final String TAG = "sansuiban";
//等待窗对象
private LoadingDialog loadingDialog;
public B mBinding;
public VM mViewModel;
//是否显示标题栏
private boolean isShowTitle = true;
//是否显示状态栏
private boolean isShowStatusBar = true;
//是否允许旋转屏幕
private boolean isAllowScreenRoate = true;
public Context context;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
//activityi管理
ActivityCollector.addActivity(this);
if (!isShowTitle) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
if (!isShowStatusBar) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//如果>=4.4才支持沉浸式
//显示上面的导航栏
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//显示下面的导航键
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//隐藏状态栏
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//显示状态栏
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// }
//设置屏幕是否可旋转
if (!isAllowScreenRoate) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//初始化布局
mBinding = DataBindingUtil.setContentView(this, layoutId());
mBinding.setLifecycleOwner(this);
initDialog();
//创建我们的ViewModel。
createViewModel();
//设置数据
initData();
}
/**
* 创建viewmoudel
*/
public void createViewModel() {
if (mViewModel == null) {
Class modelClass;
Type type = getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
Log.i(TAG, "createViewModel: " + modelClass.toString());
} else {
//如果没有指定泛型参数,则默认使用BaseViewModel
modelClass = BaseViewModel.class;
}
mViewModel = (VM) new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(modelClass);
}
}
/**
* 初始化布局
*
* @return 布局id
*/
protected abstract int layoutId();
/**
* 设置数据
*/
protected abstract void initData();
/**
* 初始化各种Dialog
*/
private void initDialog() {
if (loadingDialog == null) {
loadingDialog = new LoadingDialog(this);
}
}
/**
* 显示等待Dialog
*/
public void showLoading() {
if (loadingDialog != null && !loadingDialog.isShowing())
loadingDialog.show();
}
/**
* 显示等待Dialog,可自定义显示内容
*/
public LoadingDialog showLoading(String msg) {
if (loadingDialog != null && !loadingDialog.isShowing())
loadingDialog.setMessage(msg).show();
return loadingDialog;
}
/**
* 隐藏等待Dialog
*/
public void hideLoading() {
if (loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
}
/**
* 设置是否显示标题栏
*
* @param showTitle true or false
*/
public void setShowTitle(boolean showTitle) {
isShowTitle = showTitle;
}
/**
* 设置是否显示状态栏
*
* @param showStatusBar true or false
*/
public void setShowStatusBar(boolean showStatusBar) {
isShowStatusBar = showStatusBar;
}
/**
* 是否允许屏幕旋转
*
* @param allowScreenRoate true or false
*/
public void setAllowScreenRoate(boolean allowScreenRoate) {
isAllowScreenRoate = allowScreenRoate;
}
/**
* 保证同一按钮在1秒内只会响应一次点击事件
*/
public abstract class OnSingleClickListener implements View.OnClickListener {
//两次点击按钮之间的间隔,目前为1000ms
private static final int MIN_CLICK_DELAY_TIME = 1000;
private long lastClickTime;
public abstract void onSingleClick(View view);
@Override
public void onClick(View view) {
long curClickTime = System.currentTimeMillis();
if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
lastClickTime = curClickTime;
onSingleClick(view);
}
}
}
/**
* 同一按钮在短时间内可重复响应点击事件
*/
public abstract class OnMultiClickListener implements View.OnClickListener {
public abstract void onMultiClick(View view);
@Override
public void onClick(View v) {
onMultiClick(v);
}
}
//活动管理器
public static class ActivityCollector {
public static List<Activity> activitys = new ArrayList<Activity>();
/**
* 向List中添加一个活动
*
* @param activity 活动
*/
public static void addActivity(Activity activity) {
activitys.add(activity);
}
/**
* 从List中移除活动
*
* @param activity 活动
*/
public static void removeActivity(Activity activity) {
activitys.remove(activity);
}
/**
* 将List中存储的活动全部销毁掉
*/
public static void finishAll() {
for (Activity activity : activitys) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
AndPermission.with(this).runtime().permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE).onDenied(permissions -> {
ToastUtils.showLong("权限获取失败,将退出应用");
finish();
}).onGranted(permissions -> {
})
.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
//activity管理
ActivityCollector.removeActivity(this);
}
}
网友评论