package com.hdl.photovoltaic.base;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.res.Configuration;
|
import android.os.Bundle;
|
import android.view.View;
|
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import com.hdl.photovoltaic.R;
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.listener.BaseView;
|
import com.hdl.photovoltaic.other.HdlThreadLogic;
|
import com.hdl.photovoltaic.utils.AppManagerUtils;
|
import com.hdl.photovoltaic.utils.LocalManageUtil;
|
import com.hdl.photovoltaic.widget.LoadingDialog;
|
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
public abstract class BaseActivity extends AppCompatActivity implements BaseView {
|
|
private LoadingDialog loadingDialog;
|
protected Activity _mActivity;
|
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
|
_mActivity = this;
|
Object content = getContentView();
|
//添加Activity到堆栈
|
AppManagerUtils.getAppManager().addActivity(this);
|
if (content instanceof Integer) {
|
setContentView((int) content);
|
} else if (content instanceof View) {
|
setContentView((View) content);
|
} else {
|
throw new RuntimeException("getContentView() should be a @LayoutRes or a View");
|
}
|
onBindView(savedInstanceState);
|
//注册EventBus
|
registerEventBus();
|
|
}
|
|
@Override
|
protected void attachBaseContext(Context newBase) {
|
//app中英文设置
|
LocalManageUtil.changeAppLanguage(UserConfigManage.getInstance().getCurrentAppLanguage(), newBase);
|
super.attachBaseContext(newBase);
|
|
|
}
|
|
/**
|
* 获取LoadingDialog
|
*
|
* @return LoadingDialog
|
*/
|
protected LoadingDialog getLoadingDialog() {
|
if (loadingDialog == null && _mActivity != null) {
|
loadingDialog = new LoadingDialog(_mActivity, R.style.Custom_Dialog);
|
}
|
return loadingDialog;
|
}
|
|
/**
|
* 开始Loading
|
*/
|
protected void showLoading() {
|
getLoadingDialog().start();
|
}
|
|
/**
|
* 开始Loading
|
*
|
* @param mes 自定义文本
|
*/
|
protected void showLoading(String mes) {
|
getLoadingDialog().start();
|
getLoadingDialog().setText(mes);
|
}
|
|
/**
|
* 停止隐藏Loading
|
*/
|
protected void hideLoading() {
|
HdlThreadLogic.runMainThread(new Runnable() {
|
@Override
|
public void run() {
|
if (loadingDialog != null && loadingDialog.isShowing()) {
|
loadingDialog.stop();
|
}
|
}
|
}, null, null);
|
}
|
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
//隐藏Loading
|
hideLoading();
|
//移除Activity
|
AppManagerUtils.getAppManager().removeActivity(this);
|
//注销EventBus
|
unregisterEventBus();
|
|
}
|
|
protected void unregisterEventBus() {
|
if (EventBus.getDefault().isRegistered(this)) {
|
EventBus.getDefault().unregister(this);
|
}
|
}
|
|
protected void registerEventBus() {
|
if (!EventBus.getDefault().isRegistered(this)) {
|
EventBus.getDefault().register(this);
|
}
|
}
|
|
@Subscribe(threadMode = ThreadMode.POSTING)
|
public void onEventMessage(BaseEventBus eventBus) {
|
}
|
|
|
/**
|
* 显示View
|
*
|
* @param view
|
*/
|
public void setViewVisible(View view) {
|
if (view.getVisibility() != View.VISIBLE) {
|
view.setVisibility(View.VISIBLE);
|
}
|
}
|
|
/**
|
* 隐藏View
|
*
|
* @param view
|
*/
|
protected void setViewGone(View view) {
|
if (view.getVisibility() != View.GONE) {
|
view.setVisibility(View.GONE);
|
}
|
}
|
|
/**
|
* 简单的跳转Activity
|
*
|
* @param clazz _
|
*/
|
protected void startActivity(Class<?> clazz) {
|
Intent intent = new Intent(this, clazz);
|
startActivity(intent);
|
|
}
|
|
/**
|
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
|
*/
|
public int dip2px(float dpValue) {
|
final float scale = getResources().getDisplayMetrics().density;
|
return (int) (dpValue * scale + 0.5f);
|
|
}
|
|
/**
|
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
|
*/
|
public int px2dip(float pxValue) {
|
final float scale = getResources().getDisplayMetrics().density;
|
return (int) (pxValue / scale + 0.5f);
|
}
|
|
|
}
|