Files
FLYSN/app/src/main/java/com/info/sn/MyApplication.java
Administrator 49b19777b9 update:2020.10.19
fix:设备号获取问题,兼容Android10,报错修复
add:
2020-10-19 18:41:47 +08:00

390 lines
13 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.info.sn;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Application;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.task.DownloadTask;
import com.blankj.utilcode.util.ToastUtils;
import com.info.sn.jpush.LocalBroadcastManager;
import com.info.sn.jpush.TagAliasOperatorHelper;
import com.info.sn.network.UrlPath;
import com.info.sn.network.api.HTTPInterface;
import com.info.sn.service.MyDownloadService;
import com.info.sn.utils.ApkUtils;
import com.info.sn.utils.LogUtils;
import com.info.sn.utils.SPUtils;
import com.info.sn.utils.ToastUtil;
import com.info.sn.utils.Utils;
import com.info.sn.view.CustomDialog;
import com.info.sn.view.InfoDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.net.URI;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import rx.Observable;
import rx.functions.Action1;
//import com.blankj.utilcode.util.NetworkUtils;
//public class MyApplication extends Application implements NetworkUtils.OnNetworkStatusChangedListener {
public class MyApplication extends Application {
public static Context context;
private static MyApplication app;
@Override
public void onCreate() {
super.onCreate();
app = this;
ToastUtil.init(this);
context = getApplicationContext();
ToastUtil.init(this);
OkGo.getInstance().init(this);
// NetworkUtils.registerNetworkStatusChangedListener(this);
Aria.init(this);
Aria.download(this).resumeAllTask();
if (!BuildConfig.DEBUG) {
catchException();
}
}
private void catchException() {
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
Log.d("捕获异常子线程:", 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.d("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName());
e.printStackTrace();
}
}
}
});
}
// protected void attachBaseContext(Context newBase) {
// super.attachBaseContext(newBase);
// MultiDex.install(this);
// }
@Override
public void onTerminate() {
super.onTerminate();
// NetworkUtils.unregisterNetworkStatusChangedListener(this);
}
public static MyApplication getInstance() {
return app;
}
public static Context getAppContext() {
if (context == null) {
context = getAppContext();
}
return context;
}
// public void onTagOperatorResult(JPushMessage jPushMessage) {
// if (jPushMessage == null) {
// return;
// }
// String s = "tags:\t";
// int errorCode = jPushMessage.getErrorCode();
// switch (errorCode) {
// 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 过多");
// 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;
// }
// }
// public static void setJpushTags() {
// Log.e("jiguangInterface", "10s后重新设置tags");
// Observable.timer(10000, TimeUnit.MILLISECONDS)
// .observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
// .subscribe(new Action1<Long>() {
// @Override
// public void call(Long aLong) {
// HTTPInterface.setJpushTags(context);
// }
// });
// }
public void onAliasOperatorResult(JPushMessage jPushMessage) {
if (jPushMessage == null) {
return;
}
String s = "alias:\t";
int errorCode = jPushMessage.getErrorCode();
switch (errorCode) {
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 + "别名绑定的设备数超过限制");
clean();
setJpushAlias();
break;
}
}
public static void setJpushAlias() {
Log.e("jiguangInterface", "10s后重新设置alias");
Observable.timer(10000, TimeUnit.MILLISECONDS)
.observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
JPushInterface.setAlias(context, TagAliasOperatorHelper.sequence++, Utils.getSerial());
}
});
}
public void clean() {
//alias的绑定的设备超过10个但是alias应该是一个设备对应一个在重置设备后jpush的regid会变动所以需要清除
//https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_5
// HTTPInterface.cleanJpushAlias(Utils.getSerial());
}
synchronized public void manageCustomMessage(CustomMessage customMessage) {
String sn_id = (String) SPUtils.get(context, "sn_id", "-1");
String member_id = (String) SPUtils.get(context, "member_id", "-1");
if (customMessage == null) {
LogUtils.e("jiguang", "customMessage is NULL");
} else {
String MESSAGE = customMessage.message;
//MESSAGE用作判断
String TITLE = customMessage.title;
String CONTENT_TYPE = customMessage.contentType;
String EXTRA = customMessage.extra;
LogUtils.e("EXTRA", EXTRA);
// switch (MESSAGE) {
// case JIGUANG_GET_DRIVELINE:
// HTTPInterface.getDriveState(member_id, sn_id);
// break;
// case JIGUANG_GET_STARTTIME:
// sendStartTime(EXTRA);
// break;
// case JIGUANG_USB_STATE:
// setUsbState(EXTRA);
// break;
// case JIGUANG_TFCARD_STATE:
// setTfcardState(EXTRA);
// break;
// case JIGUANG_BLUETOOTH_STATE:
// setBluetoothState(EXTRA);
// break;
// case JIGUANG_BROWSER_URLPATH:
// setBrowserUrlpath(EXTRA);
// break;
// case JIGUANG_APP_NETWORKSTATE:
// setAppNetworkstate(EXTRA);
// break;
// case JIGUANG_APP_LOCKEDSTATE:
// setAppLockedstate(EXTRA);
// break;
// case JIGUANG_FORCE_INSTALLAPK:
// intallApk(EXTRA);
// break;
// case JIGUANG_FORCE_UNINSTALLAPK:
// unintallApk(EXTRA);
// break;
// case JIGUANG_BIND_DEVIVES:
// bindService(EXTRA);
// break;
// case JIGUANG_TFMEDIA:
//
// break;
// case JIGUANG_CAMRERA:
// break;
// case JIGUANG_PHONE:
// break;
// }
}
}
synchronized private void defaults(String jsonArray) {
if (jsonArray.length() > 0) {
try {
JSONObject extra = new JSONObject(jsonArray);
String packageName = extra.getString("package");
int is_network = extra.getInt("is_network");
} catch (JSONException e) {
e.printStackTrace();
LogUtils.e("defaults", e.getMessage());
}
} else {
ToastUtil.debugShow("defaults jsonArray is NULL");
}
}
//USB数据功能管控
//仅充电usb_charge
//MTP模式usb_mtp
//Midi模式usb_midi
// @Override
// public void onDisconnected() {
// LogUtils.e("onDisconnected", "网络断开");
// }
//
// @Override
// public void onConnected(NetworkUtils.NetworkType networkType) {
// Aria.download(this).resumeAllTask();
// LogUtils.e("onConnected", "网络连接");
//
// }
}