package com.info.sn.base; import android.annotation.SuppressLint; import android.app.Application; import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.Process; import android.util.Log; import com.arialyy.aria.core.Aria; import com.blankj.utilcode.util.ProcessUtils; import com.info.sn.BuildConfig; import com.info.sn.jpush.TagAliasOperatorHelper; import com.info.sn.manager.AmapManager; import com.info.sn.manager.NetInterfaceManager; import com.info.sn.manager.ControlManager; import com.info.sn.manager.DeviceManager; import com.info.sn.network.HTTPInterface; import com.info.sn.network.UrlAddress; import com.info.sn.utils.JGYUtils; import com.info.sn.utils.SystemUtils; import com.info.sn.utils.ToastUtil; import com.info.sn.utils.Utils; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.concurrent.TimeUnit; import cn.jpush.android.api.JPushInterface; import cn.jpush.android.api.JPushMessage; import io.reactivex.Observable; import io.reactivex.ObservableEmitter; import io.reactivex.ObservableOnSubscribe; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class BaseApplication extends Application { private static final String TAG = BaseApplication.class.getSimpleName(); @SuppressLint("StaticFieldLeak") public static Context context; @SuppressLint("StaticFieldLeak") private static BaseApplication instance; public static Context getAppContext() { return context; } // 单例模式中获取唯一的ExitApplication实例 public static BaseApplication getInstance() { if (null == instance) { instance = new BaseApplication(); } return instance; } @Override public void onCreate() { super.onCreate(); // if (!getPackageName().equals(ProcessUtils.getCurrentProcessName())) { // return; // } context = this; instance = this; // if (SystemUtils.isMainProcessName(this, Process.myPid())) { //非主进程不初始化 utilsInint(); // } } private void utilsInint() { if (!BuildConfig.DEBUG) { catchException(); } // 设置开启日志,发布时请关闭日志 JPushInterface.setDebugMode(true); JPushInterface.requestPermission(this); Aria.init(this); Aria.download(this).resumeAllTask(); JGYUtils.init(this); ControlManager.init(this); DeviceManager.init(this); AmapManager.init(this); AmapManager.getInstance().initAmap(); NetInterfaceManager.init(this); JGYUtils.hookWebView(); initAliasObservable(); initTagObservable(); } private void catchException() { Thread.setDefaultUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { Log.e("捕获异常子线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName()); } } ); //下面是新增方法! new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { while (true) { try { Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法! } catch (Exception e) { Log.e("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName()); e.printStackTrace(); } } } }); } public void onAliasOperatorResult(JPushMessage jPushMessage) { if (jPushMessage == null) { return; } int errorCode = jPushMessage.getErrorCode(); onAliasResult.onResult(errorCode); } public static void setJpushAlias() { Log.e("jiguangInterface", "30s后重新设置alias"); JPushInterface.setAlias(context, TagAliasOperatorHelper.sequence++, Utils.getSerial()); } public void onTagOperatorResult(JPushMessage jPushMessage) { if (jPushMessage == null) { return; } int errorCode = jPushMessage.getErrorCode(); onTagResult.onResult(errorCode); } public static void setJpushTags() { Log.e(TAG, "30s后重新设置tags"); HTTPInterface.setJpushTags(context); } private static OnAliasResult onAliasResult; interface OnAliasResult { void onResult(int code); } private static OnTagResult onTagResult; interface OnTagResult { void onResult(int code); } private static void initTagObservable() { Log.e(TAG, "initTagObservable: "); Observable.create(new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter emitter) { onTagResult = new OnTagResult() { @Override public void onResult(int code) { Log.e("initTagObservable", "onResult: " + code); emitter.onNext(code); } }; } }).throttleLast(1, TimeUnit.MINUTES) .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Integer integer) { Log.e("initTagObservable", "onNext: " + integer); String s = "tags:\t"; switch (integer) { case 0: Log.e("jiguangInterface", s + "Tag绑定成功"); break; case 6001: Log.e("jiguangInterface", s + "无效的设置"); break; case 6005: Log.e("jiguangInterface", s + "某一个 tag 字符串不合法"); ToastUtil.show("设备标签不合法,联系管理员修改\t" + "code:6005"); break; case 6006: Log.e("jiguangInterface", s + "某一个 tag 超长"); ToastUtil.show("设备标签过长,联系管理员修改\t" + "code:6006"); break; case 6007: Log.e("jiguangInterface", s + "tags 数量超出限制"); ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6007"); break; case 6008: Log.e("jiguangInterface", s + "tag 超出总长度限制"); ToastUtil.show("设备标签超出总长度限制,联系管理员修改\t" + "code:6008"); break; case 6011: Log.e("jiguangInterface", s + "短时间内操作过于频繁"); break; case 6013: Log.e("jiguangInterface", s + "用户设备时间轴异常"); ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + "code:6013"); break; case 6018: Log.e("jiguangInterface", s + "Tags 过多"); cleanJpushTag(); ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6018"); break; case 6021: Log.e("jiguangInterface", s + "tags 操作正在进行中"); break; //需要重新设置 case 6002: Log.e("jiguangInterface", s + "设置超时,请重试"); setJpushTags(); break; case 6014: Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); setJpushTags(); break; case 6020: Log.e("jiguangInterface", s + "建议过一段时间再设置"); setJpushTags(); break; case 6024: Log.e("jiguangInterface", s + "服务器内部错误"); setJpushTags(); break; default: break; } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } private static void initAliasObservable() { Log.e(TAG, "initAliasObservable: "); Observable.create(new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter emitter) throws Exception { onAliasResult = new OnAliasResult() { @Override public void onResult(int code) { Log.e("initAliasObservable", "onResult: " + code); emitter.onNext(code); } }; } }).throttleLast(1, TimeUnit.MINUTES) .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Integer integer) { Log.e("initAliasObservable", "onNext: " + integer); String s = "alias:\t"; switch (integer) { case 0: Log.e("jiguangInterface", s + "Alias绑定成功"); break; case 6001: Log.e("jiguangInterface", s + "无效的设置"); break; case 6011: Log.e("jiguangInterface", s + "短时间内操作过于频繁"); break; case 6013: Log.e("jiguangInterface", s + "用户设备时间轴异常"); ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + s + "code:6013"); break; case 6022: Log.e("jiguangInterface", s + "alias 操作正在进行中"); break; //需要重新设置 case 6002: Log.e("jiguangInterface", s + "设置超时,请重试"); setJpushAlias(); break; case 6014: Log.e("jiguangInterface", s + "服务器繁忙,建议重试"); setJpushAlias(); break; case 6020: Log.e("jiguangInterface", s + "建议过一段时间再设置"); setJpushAlias(); break; case 6024: Log.e("jiguangInterface", s + "服务器内部错误"); setJpushAlias(); break; case 6017: case 6027: Log.e("jiguangInterface", s + "别名绑定的设备数超过限制"); cleanJpushAlias(); setJpushAlias(); break; default: break; } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } synchronized public static void cleanJpushAlias() { //alias的绑定的设备超过10个,但是alias应该是一个设备对应一个,在重置设备后jpush的regid会变动,所以需要清除 //https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_5 OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(UrlAddress.DELETE_JPUSH_ALIAS + Utils.getSerial()) .header("Authorization", JGYUtils.getAuthorization()) .delete() .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { Log.e("cleanJpushAlias", "onFailure: " + e.getMessage()); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { Log.e("cleanJpushAlias", "onResponse: " + response.toString()); Log.e(TAG, "onResponse: " + "清除Alias成功"); } }); } synchronized public static void cleanJpushTag() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(UrlAddress.DELETE_JPUSH_TAG + Utils.getSerial()) .header("Authorization", JGYUtils.getAuthorization()) .delete() .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { Log.e("cleanJpushTag", "onFailure: " + e.getMessage()); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { Log.e("cleanJpushTag", "onResponse: " + response.toString()); Log.e(TAG, "onResponse: " + "清除Tag成功"); } }); } }