113 lines
3.7 KiB
Java
113 lines
3.7 KiB
Java
package com.mogo.commons;
|
|
|
|
import android.app.Application;
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
|
|
import com.mogo.commons.crash.FinalizeCrashFixer;
|
|
import com.mogo.commons.debug.DebugConfig;
|
|
import com.mogo.commons.koom.KoomInitTask;
|
|
import com.mogo.commons.screen.ScreenHelper;
|
|
import com.mogo.commons.module.MogoServices;
|
|
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
|
|
import com.mogo.eagle.core.utilcode.mogo.toast.TipDrawable;
|
|
import com.mogo.eagle.core.utilcode.mogo.toast.TipToast;
|
|
import com.mogo.eagle.core.utilcode.util.AppStateManager;
|
|
import com.mogo.eagle.core.utilcode.util.ProcessUtils;
|
|
import com.mogo.eagle.core.utilcode.util.Utils;
|
|
|
|
import io.reactivex.plugins.RxJavaPlugins;
|
|
import me.jessyan.autosize.AutoSize;
|
|
|
|
/**
|
|
* @author congtaowang
|
|
* @since 2019-12-23
|
|
* <p>
|
|
* 必须继承并实现列面的方法,这里完成了鹰眼基础服务的初始化操作
|
|
*/
|
|
public abstract class AbsMogoApplication extends Application {
|
|
|
|
private static final String TAG = "AbsMogoApplication";
|
|
|
|
protected static Application sApp;
|
|
|
|
public static Application getApp() {
|
|
return sApp;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
AutoSize.checkAndInit(this);
|
|
ScreenHelper.setScreenConfig(this);
|
|
// 非主进程也需要初始化
|
|
if (DebugConfig.isDebug()) {
|
|
KoomInitTask.INSTANCE.init(AbsMogoApplication.getApp());
|
|
}
|
|
if (!shouldInit()) {
|
|
return;
|
|
}
|
|
AppStateManager.INSTANCE.init(this);
|
|
initRxJavaErrorHandler();
|
|
FinalizeCrashFixer.fix();
|
|
Utils.init(this);
|
|
MogoServices.getInstance().init(this);
|
|
}
|
|
|
|
private void initRxJavaErrorHandler() {
|
|
RxJavaPlugins.setErrorHandler(throwable -> {
|
|
CallerLogger.e("RxJava", "" + throwable);
|
|
});
|
|
}
|
|
|
|
protected boolean shouldInit() {
|
|
return ProcessUtils.isMainProcess(this);
|
|
}
|
|
|
|
/**
|
|
* 初始化 自定义样式e饿
|
|
*/
|
|
protected void initTipToast() {
|
|
TipToast.init(this, ((context, message, tipDrawable) -> {
|
|
if (TextUtils.isEmpty(message)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return generateToastView(context, message, tipDrawable);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* 初始化自定义 Toast View
|
|
*/
|
|
private View generateToastView(Context context, String message, TipDrawable tipDrawable) {
|
|
View contentView;
|
|
if (tipDrawable == null) {
|
|
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast, null);
|
|
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
|
|
txt.setText(message);
|
|
} else {
|
|
// 有图片,使用带图片的布局,当前只实现了左侧图片
|
|
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast_with_left_drawable, null);
|
|
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
|
|
ImageView img = contentView.findViewById(R.id.module_commons_toast_left_drawable);
|
|
img.setImageDrawable(tipDrawable.getDrawable());
|
|
ViewGroup.LayoutParams params = img.getLayoutParams();
|
|
params.width = tipDrawable.getWidth();
|
|
params.height = tipDrawable.getHeight();
|
|
txt.setText(message);
|
|
}
|
|
return contentView;
|
|
}
|
|
|
|
}
|