version:1.0
update:2021-10-13 18:52:13 fix:去除okgo,rxAndroid1,优化依赖 add:切换到奥乐云平台
This commit is contained in:
552
app/src/main/java/com/aoleyun/sn/base/BaseApplication.java
Normal file
552
app/src/main/java/com/aoleyun/sn/base/BaseApplication.java
Normal file
@@ -0,0 +1,552 @@
|
||||
package com.aoleyun.sn.base;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Process;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.multidex.MultiDexApplication;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.aoleyun.sn.BuildConfig;
|
||||
import com.aoleyun.sn.jpush.TagAliasOperatorHelper;
|
||||
import com.aoleyun.sn.manager.AmapManager;
|
||||
import com.aoleyun.sn.manager.FileManager;
|
||||
import com.aoleyun.sn.manager.NetInterfaceManager;
|
||||
import com.aoleyun.sn.network.HTTPInterface;
|
||||
import com.aoleyun.sn.network.URLAddress;
|
||||
import com.aoleyun.sn.statistics.AppInformation;
|
||||
import com.aoleyun.sn.statistics.StatisticsInfo;
|
||||
import com.aoleyun.sn.utils.ApkUtils;
|
||||
import com.aoleyun.sn.utils.JGYUtils;
|
||||
import com.aoleyun.sn.utils.Logutils;
|
||||
import com.aoleyun.sn.utils.SystemUtils;
|
||||
import com.aoleyun.sn.utils.ToastUtil;
|
||||
import com.aoleyun.sn.utils.Utils;
|
||||
import com.aoleyun.sn.utils.XAPKUtils;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class BaseApplication extends MultiDexApplication {
|
||||
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();
|
||||
context = getApplicationContext();
|
||||
if (SystemUtils.isMainProcessName(this, Process.myPid())) {
|
||||
//非主进程不初始化
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
String rootDir = MMKV.initialize(this);
|
||||
Logutils.e(TAG, "mmkv root: " + rootDir);
|
||||
ToastUtil.init(this);
|
||||
NetInterfaceManager.init(this);
|
||||
JGYUtils.init(this);
|
||||
XAPKUtils.init(this);
|
||||
FileManager.init(this);
|
||||
AmapManager.init(this);
|
||||
AmapManager.getInstance().initAmap();
|
||||
|
||||
JGYUtils.getInstance().hookWebView();
|
||||
|
||||
// 设置开启日志,发布时请关闭日志
|
||||
JPushInterface.setDebugMode(true);
|
||||
JPushInterface.requestPermission(this);
|
||||
//解锁设备不需要初始化
|
||||
// 初始化 JPush
|
||||
//JPushInterface.init(this);
|
||||
Aria.init(this);
|
||||
Aria.get(this).getDownloadConfig().setMaxTaskNum(1);
|
||||
Aria.get(this).getDownloadConfig().setConvertSpeed(true);
|
||||
|
||||
if (!BuildConfig.DEBUG) {
|
||||
catchException();
|
||||
}
|
||||
// Configuration config = getResources().getConfiguration();
|
||||
// int smallestScreenWidthDp = config.smallestScreenWidthDp;
|
||||
// Logutils.e("init", "smallestScreenWidthDp=" + smallestScreenWidthDp);
|
||||
// CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
|
||||
// .setDefaultFontPath("fonts/1234.ttf")
|
||||
// .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
|
||||
// .setFontAttrId(R.attr.fontPath)
|
||||
// .build()
|
||||
// );
|
||||
initAliasObservable();
|
||||
initTagObservable();
|
||||
}
|
||||
|
||||
private void catchException() {
|
||||
Thread.setDefaultUncaughtExceptionHandler(
|
||||
new Thread.UncaughtExceptionHandler() {
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
Logutils.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) {
|
||||
Logutils.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() {
|
||||
Logutils.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() {
|
||||
Logutils.e(TAG, "30s后重新设置tags");
|
||||
HTTPInterface.setJpushTags(context);
|
||||
}
|
||||
|
||||
|
||||
private static void initAliasObservable() {
|
||||
Logutils.e(TAG, "initAliasObservable: ");
|
||||
Observable.create(new ObservableOnSubscribe<Integer>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
|
||||
onAliasResult = new OnAliasResult() {
|
||||
@Override
|
||||
public void onResult(int code) {
|
||||
Logutils.e("initAliasObservable", "onResult: " + code);
|
||||
emitter.onNext(code);
|
||||
}
|
||||
};
|
||||
}
|
||||
}).throttleLast(1, TimeUnit.HOURS)
|
||||
.subscribe(new Observer<Integer>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Integer integer) {
|
||||
Logutils.e("initAliasObservable", "onNext: " + integer);
|
||||
String s = "alias:\t";
|
||||
switch (integer) {
|
||||
case 0:
|
||||
Logutils.e("jiguangInterface", s + "Alias绑定成功");
|
||||
break;
|
||||
case 6001:
|
||||
Logutils.e("jiguangInterface", s + "无效的设置");
|
||||
break;
|
||||
case 6011:
|
||||
Logutils.e("jiguangInterface", s + "短时间内操作过于频繁");
|
||||
break;
|
||||
case 6013:
|
||||
Logutils.e("jiguangInterface", s + "用户设备时间轴异常");
|
||||
ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + s + "code:6013");
|
||||
break;
|
||||
case 6022:
|
||||
Logutils.e("jiguangInterface", s + "alias 操作正在进行中");
|
||||
break;
|
||||
//需要重新设置
|
||||
case 6002:
|
||||
Logutils.e("jiguangInterface", s + "设置超时,请重试");
|
||||
setJpushAlias();
|
||||
break;
|
||||
case 6014:
|
||||
Logutils.e("jiguangInterface", s + "服务器繁忙,建议重试");
|
||||
setJpushAlias();
|
||||
break;
|
||||
case 6020:
|
||||
Logutils.e("jiguangInterface", s + "建议过一段时间再设置");
|
||||
setJpushAlias();
|
||||
break;
|
||||
case 6024:
|
||||
Logutils.e("jiguangInterface", s + "服务器内部错误");
|
||||
setJpushAlias();
|
||||
break;
|
||||
case 6017:
|
||||
case 6027:
|
||||
Logutils.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成功");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void initTagObservable() {
|
||||
Logutils.e(TAG, "initTagObservable: ");
|
||||
Observable.create(new ObservableOnSubscribe<Integer>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<Integer> emitter) {
|
||||
onTagResult = new OnTagResult() {
|
||||
@Override
|
||||
public void onResult(int code) {
|
||||
Logutils.e("initTagObservable", "onResult: " + code);
|
||||
emitter.onNext(code);
|
||||
}
|
||||
};
|
||||
}
|
||||
}).throttleLast(1, TimeUnit.HOURS)
|
||||
.subscribe(new Observer<Integer>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Integer integer) {
|
||||
Logutils.e("initTagObservable", "onNext: " + integer);
|
||||
String s = "tags:\t";
|
||||
switch (integer) {
|
||||
case 0:
|
||||
Logutils.e("jiguangInterface", s + "Tag绑定成功");
|
||||
break;
|
||||
case 6001:
|
||||
Logutils.e("jiguangInterface", s + "无效的设置");
|
||||
break;
|
||||
case 6005:
|
||||
Logutils.e("jiguangInterface", s + "某一个 tag 字符串不合法");
|
||||
ToastUtil.show("设备标签不合法,联系管理员修改\t" + "code:6005");
|
||||
break;
|
||||
case 6006:
|
||||
Logutils.e("jiguangInterface", s + "某一个 tag 超长");
|
||||
ToastUtil.show("设备标签过长,联系管理员修改\t" + "code:6006");
|
||||
break;
|
||||
case 6007:
|
||||
Logutils.e("jiguangInterface", s + "tags 数量超出限制");
|
||||
ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6007");
|
||||
break;
|
||||
case 6008:
|
||||
Logutils.e("jiguangInterface", s + "tag 超出总长度限制");
|
||||
ToastUtil.show("设备标签超出总长度限制,联系管理员修改\t" + "code:6008");
|
||||
break;
|
||||
case 6011:
|
||||
Logutils.e("jiguangInterface", s + "短时间内操作过于频繁");
|
||||
break;
|
||||
case 6013:
|
||||
Logutils.e("jiguangInterface", s + "用户设备时间轴异常");
|
||||
ToastUtil.show("用户设备时间轴异常,修改后重新登陆\t" + "code:6013");
|
||||
break;
|
||||
case 6018:
|
||||
Logutils.e("jiguangInterface", s + "Tags 过多");
|
||||
cleanJpushTag();
|
||||
ToastUtil.show("设备标签数量超出限制,联系管理员修改\t" + "code:6018");
|
||||
break;
|
||||
case 6021:
|
||||
Logutils.e("jiguangInterface", s + "tags 操作正在进行中");
|
||||
break;
|
||||
//需要重新设置
|
||||
case 6002:
|
||||
Logutils.e("jiguangInterface", s + "设置超时,请重试");
|
||||
setJpushTags();
|
||||
break;
|
||||
case 6014:
|
||||
Logutils.e("jiguangInterface", s + "服务器繁忙,建议重试");
|
||||
setJpushTags();
|
||||
break;
|
||||
case 6020:
|
||||
Logutils.e("jiguangInterface", s + "建议过一段时间再设置");
|
||||
setJpushTags();
|
||||
break;
|
||||
case 6024:
|
||||
Logutils.e("jiguangInterface", s + "服务器内部错误");
|
||||
setJpushTags();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static OnAliasResult onAliasResult;
|
||||
|
||||
interface OnAliasResult {
|
||||
void onResult(int code);
|
||||
}
|
||||
|
||||
private static OnTagResult onTagResult;
|
||||
|
||||
interface OnTagResult {
|
||||
void onResult(int code);
|
||||
}
|
||||
|
||||
private boolean finished = false;
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void setFinished(boolean b) {
|
||||
this.finished = b;
|
||||
}
|
||||
|
||||
public static void sendAppUsedTime(String random, String type) {
|
||||
StatisticsInfo statisticsInfo = null;
|
||||
if (type.equals("0")) {
|
||||
statisticsInfo = new StatisticsInfo(getAppContext());
|
||||
} else if (type.equals("1")) {
|
||||
statisticsInfo = new StatisticsInfo(getAppContext(), StatisticsInfo.WEEK);
|
||||
}
|
||||
List<AppInformation> list = null;
|
||||
List<AppInformation> localAppList = new ArrayList<>();
|
||||
|
||||
if (statisticsInfo != null) {
|
||||
list = statisticsInfo.getShowList();
|
||||
if (list != null && list.size() > 0) {
|
||||
for (AppInformation appInformation : list) {
|
||||
if (!ApkUtils.isSystemApp(getAppContext(), appInformation.getPackageName())) {
|
||||
localAppList.add(appInformation);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Logutils.e("fht", "今日没有打开的应用");
|
||||
}
|
||||
} else {
|
||||
Logutils.e("fht", "获取信息失败");
|
||||
}
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
JSONArray appinfo = new JSONArray();
|
||||
try {
|
||||
if (localAppList.size() > 0) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
AppInformation information = localAppList.get(i);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("package", information.getPackageName());
|
||||
jsonObject.put("use_time", (int) information.getUsedTimebyDay() / 1000);
|
||||
appinfo.add(jsonObject);
|
||||
}
|
||||
data.put("data", appinfo);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Logutils.e("sendAppUsedTime", e.getMessage());
|
||||
}
|
||||
|
||||
NetInterfaceManager.getInstance()
|
||||
.getAppLogApi()
|
||||
.getAppLog(Utils.getSerial(), random, data.toJSONString())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("sendAppUsedTime", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(BaseResponse baseResponse) {
|
||||
Log.e("sendAppUsedTime", "onNext: ");
|
||||
int code = baseResponse.code;
|
||||
String msg = baseResponse.msg;
|
||||
Logutils.e("sendAppUsedTime", "onSubscribe:" + msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("sendAppUsedTime", "onError: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("sendAppUsedTime", "onComplete: ");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static long totalTime;
|
||||
private static int totalTimes;
|
||||
|
||||
synchronized public static void setAPPUsage() {
|
||||
StatisticsInfo statisticsInfo = new StatisticsInfo(getAppContext());
|
||||
totalTime = statisticsInfo.getTotalTime();//全部时间
|
||||
totalTimes = statisticsInfo.getTotalTimes();//全部次数
|
||||
}
|
||||
|
||||
|
||||
synchronized public static void setAPPUsage(List<String> lists) {
|
||||
StatisticsInfo statisticsInfo = new StatisticsInfo(getAppContext());
|
||||
List<AppInformation> list = null;
|
||||
List<AppInformation> localAppList = new ArrayList<>();
|
||||
|
||||
totalTime = statisticsInfo.getTotalTime();//全部时间
|
||||
totalTimes = statisticsInfo.getTotalTimes();//全部次数
|
||||
|
||||
list = statisticsInfo.getShowList();
|
||||
if (list != null && list.size() > 0) {
|
||||
for (AppInformation appInformation : list) {
|
||||
if (lists.indexOf(appInformation.getPackageName()) != -1) {
|
||||
localAppList.add(appInformation);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
boolean isForecDownload = false;
|
||||
|
||||
public boolean isDownloading() {
|
||||
return isForecDownload;
|
||||
}
|
||||
|
||||
public void setDownloadState(boolean state) {
|
||||
isForecDownload = state;
|
||||
}
|
||||
|
||||
public void checkIsDownloading() {
|
||||
List<DownloadEntity> list = Aria.download(this).getDRunningTask();
|
||||
if (list == null || list.size() == 0) {
|
||||
BaseApplication.getInstance().setDownloadState(false);
|
||||
} else {
|
||||
BaseApplication.getInstance().setDownloadState(true);
|
||||
}
|
||||
Logutils.e(TAG, "isDownloading=" + BaseApplication.getInstance().isDownloading());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user