Files
CubeAoleyunSN/app/src/main/java/com/aoleyun/sn/base/BaseApplication.java
Godfather 11bad35331 version:4.5beta
fix:
update:增加获取手机号码,获取运营商类型,获取wifi网络运营商,获取手机号码
2022-04-21 11:36:17 +08:00

541 lines
22 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.text.TextUtils;
import android.util.Log;
import androidx.multidex.MultiDexApplication;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aoleyun.sn.BuildConfig;
import com.aoleyun.sn.bean.BaseResponse;
import com.aoleyun.sn.manager.AmapManager;
import com.aoleyun.sn.manager.ConnectManager;
import com.aoleyun.sn.manager.FileManager;
import com.aoleyun.sn.network.NetInterfaceManager;
import com.aoleyun.sn.network.UrlAddress;
import com.aoleyun.sn.rlog.LogDBManager;
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.SystemUtils;
import com.aoleyun.sn.utils.ToastUtil;
import com.aoleyun.sn.utils.Utils;
import com.aoleyun.sn.utils.WiFiUtils;
import com.aoleyun.sn.utils.XAPKUtils;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
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 io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.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;
public static Context getAppContext() {
return context;
}
@Override
public void onCreate() {
super.onCreate();
context = this;
if (SystemUtils.isMainProcessName(this, Process.myPid())) {
//非主进程不初始化
init();
}
}
private void init() {
String rootDir = MMKV.initialize(this);
Log.e(TAG, "mmkv root: " + rootDir);
tPushInit();
ToastUtil.init(this);
NetInterfaceManager.init(this);
JGYUtils.init(this);
WiFiUtils.init(this);
XAPKUtils.init(this);
LogDBManager.init(this);
FileManager.init(this);
ConnectManager.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;
// Log.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) {
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();
}
}
}
});
}
private void tPushInit() {
XGPushConfig.enableDebug(this, true);
XGPushManager.registerPush(this, new XGIOperateCallback() {
@Override
public void onSuccess(Object data, int flag) {
//token在设备卸载重装的时候有可能会变
Log.e("TPush", "注册成功设备token为" + data);
MMKV.defaultMMKV().encode("XGPushtoken", data.toString());
List<XGPushManager.AccountInfo> accountInfoList = new ArrayList<>();
accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.CUSTOM.getValue(), Utils.getSerial(getApplicationContext())));
if (Utils.NOSN.equalsIgnoreCase(Utils.getSerial())) {
accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.CUSTOM.getValue(), Utils.getIMEI(getAppContext(), 0)));
accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.CUSTOM.getValue(), Utils.getIMEI(getAppContext(), 1)));
}
XGPushManager.upsertAccounts(getAppContext(), accountInfoList, new XGIOperateCallback() {
@Override
public void onSuccess(Object data, int flag) {
Log.e("TPush", "onSuccess, data:" + data + ", flag:" + flag);
}
@Override
public void onFail(Object data, int errCode, String msg) {
Log.e("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);
}
});
}
@Override
public void onFail(Object data, int errCode, String msg) {
Log.e("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg);
}
});
}
private static void initAliasObservable() {
Log.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) {
Log.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) {
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() {
}
});
}
// 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());
}
private static void initTagObservable() {
Log.e(TAG, "initTagObservable: ");
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) {
onTagResult = new OnTagResult() {
@Override
public void onResult(int code) {
Log.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) {
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() {
}
});
}
public static void setJpushTags() {
Log.e(TAG, "30s后重新设置tags");
}
private static OnAliasResult onAliasResult;
interface OnAliasResult {
void onResult(int code);
}
private static OnTagResult onTagResult;
interface OnTagResult {
void onResult(int code);
}
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(getAppContext()))
.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(getAppContext()))
.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成功");
}
});
}
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 {
Log.e("fht", "今日没有打开的应用");
}
} else {
Log.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) {
Log.e("sendAppUsedTime", e.getMessage());
}
NetInterfaceManager.getInstance()
.getAppLogApi()
.getAppLog(Utils.getSerial(getAppContext()), 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;
Log.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 {
}
}
}