修复接口返回 200 但 data 为空导致的空指针,并加固管控配置的并发读写
This commit is contained in:
1 parent
d0d8ed583a
commit
eea385cc86
11 files changed
+68
-23
No files matched your search
@@ -370,8 +370,11 @@ public class UsageFragment extends BaseMvvmFragment<UsageViewModel, FragmentUsag
|
||||
}
|
||||
|
||||
private void onAppUseTime(BaseResponse<AppUseTimeData> response) {
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
AppUseTimeData resp = response.data;
|
||||
if (resp == null || resp.getApp_list().isEmpty()) {
|
||||
if (resp == null || resp.getApp_list() == null || resp.getApp_list().isEmpty()) {
|
||||
// 数据为空时清空图表已有内容
|
||||
mBarChart.clear();
|
||||
// 强制刷新图表(触发重绘,使清空生效)
|
||||
|
||||
@@ -243,6 +243,7 @@ public class UsageViewModel extends BaseViewModel {
|
||||
appUseTime.setValue(resp);
|
||||
loading.setValue(false);
|
||||
boolean success = resp.code == 200 && resp.data != null
|
||||
&& resp.data.getApp_list() != null
|
||||
&& !resp.data.getApp_list().isEmpty();
|
||||
onRequestFinished(REQ_USE_TIME, success);
|
||||
},
|
||||
|
||||
@@ -84,7 +84,7 @@ public class UsageDetailActivity extends BaseMvvmActivity<UsageDetailViewModel,
|
||||
}
|
||||
|
||||
private void onAppUseTime(AppUseTimeData resp) {
|
||||
if (resp == null || resp.getApp_list().isEmpty()) {
|
||||
if (resp == null || resp.getApp_list() == null || resp.getApp_list().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@@ -109,9 +110,10 @@ public class TimeManager {
|
||||
|
||||
|
||||
/**
|
||||
* 保存所有应用的管控配置(Key 为包名)
|
||||
* 保存所有应用的管控配置(Key 为包名)。
|
||||
* 主线程整体重建后发布,AIDL 查询线程只读,用 volatile 保证可见性。
|
||||
*/
|
||||
private Map<String, AppTimeControlInfo> mAppTimeControlMap = new HashMap<>();
|
||||
private volatile Map<String, AppTimeControlInfo> mAppTimeControlMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 记录整机(SN)当前剩余可用时长(秒)
|
||||
@@ -119,9 +121,10 @@ public class TimeManager {
|
||||
private volatile long mGlobalRemainingTime;
|
||||
|
||||
/**
|
||||
* 记录每个受限应用的当前剩余可用时长(秒)
|
||||
* 记录每个受限应用的当前剩余可用时长(秒)。
|
||||
* 每秒在主线程扣减,同时可能被 AIDL 查询线程读取,使用并发容器避免并发读写。
|
||||
*/
|
||||
private Map<String, Long> mRemainingTimeMap = new HashMap<>();
|
||||
private volatile Map<String, Long> mRemainingTimeMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 记录上一次已处理过的日期,用于检测跨天(0点)后刷新配置
|
||||
@@ -376,14 +379,18 @@ public class TimeManager {
|
||||
* @param list App 管控配置列表(可为空)
|
||||
*/
|
||||
private void rebuildAppMaps(List<AppTimeControlInfo> list) {
|
||||
mAppTimeControlMap = new HashMap<>();
|
||||
mRemainingTimeMap = new HashMap<>();
|
||||
if (list == null || list.isEmpty()) return;
|
||||
for (AppTimeControlInfo info : list) {
|
||||
if (info == null || TextUtils.isEmpty(info.getApp_package())) continue; // 防御空包名
|
||||
mAppTimeControlMap.put(info.getApp_package(), info);
|
||||
mRemainingTimeMap.put(info.getApp_package(), info.getToday_time());
|
||||
// 先在局部变量中构建完整配置,再整体发布,避免查询线程看到半成品数据
|
||||
Map<String, AppTimeControlInfo> controlMap = new HashMap<>();
|
||||
Map<String, Long> remainingMap = new ConcurrentHashMap<>();
|
||||
if (list != null && !list.isEmpty()) {
|
||||
for (AppTimeControlInfo info : list) {
|
||||
if (info == null || TextUtils.isEmpty(info.getApp_package())) continue; // 防御空包名
|
||||
controlMap.put(info.getApp_package(), info);
|
||||
remainingMap.put(info.getApp_package(), info.getToday_time());
|
||||
}
|
||||
}
|
||||
mAppTimeControlMap = controlMap;
|
||||
mRemainingTimeMap = remainingMap;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,11 +398,12 @@ public class TimeManager {
|
||||
*/
|
||||
private void resetRemainingToQuota() {
|
||||
if (mAppTimeControlMap == null || mAppTimeControlMap.isEmpty()) return;
|
||||
mRemainingTimeMap = new HashMap<>();
|
||||
Map<String, Long> remainingMap = new ConcurrentHashMap<>();
|
||||
for (AppTimeControlInfo info : mAppTimeControlMap.values()) {
|
||||
if (info == null || TextUtils.isEmpty(info.getApp_package())) continue;
|
||||
mRemainingTimeMap.put(info.getApp_package(), info.getToday_time());
|
||||
remainingMap.put(info.getApp_package(), info.getToday_time());
|
||||
}
|
||||
mRemainingTimeMap = remainingMap;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -131,7 +131,12 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
super.onStartCommand(intent, flags, startId);
|
||||
Log.e(TAG, "onStartCommand: " + intent.getAction());
|
||||
// 进程被杀后系统重启 START_STICKY 服务时 intent 可能为空,不能直接解引用
|
||||
String action = null;
|
||||
if (intent != null) {
|
||||
action = intent.getAction();
|
||||
}
|
||||
Log.e(TAG, "onStartCommand: " + action);
|
||||
startEvent.onNext(System.currentTimeMillis());
|
||||
return START_STICKY;
|
||||
}
|
||||
@@ -146,6 +151,10 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
|
||||
Log.e(TAG, "getScreenLockState: onNext");
|
||||
int code = baseResponse.code;
|
||||
if (code == 200) {
|
||||
if (baseResponse.data == null) {
|
||||
Log.e(TAG, "getScreenLockState: 响应缺少 data");
|
||||
return;
|
||||
}
|
||||
JsonObject jsonObject = GsonUtils.getJsonObject(GsonUtils.toJSONString(baseResponse.data));
|
||||
Integer screenLock = GsonUtils.optInt(jsonObject, "is_screen_lock");
|
||||
if (screenLock == null) {
|
||||
|
||||
@@ -139,11 +139,12 @@ public class MainServiceModel extends BaseAndroidViewModel {
|
||||
@Override
|
||||
public void onNext(@NonNull BaseResponse<SnBindState> snBindStateBaseResponse) {
|
||||
Log.e(TAG, "getSnBind: onNext: " + snBindStateBaseResponse);
|
||||
if (snBindStateBaseResponse.code == 200) {
|
||||
if (snBindStateBaseResponse.code == 200 && snBindStateBaseResponse.data != null) {
|
||||
SnBindState snBindState = snBindStateBaseResponse.data;
|
||||
int is_bind = snBindState.getIs_bind();
|
||||
KVStore.encode(CommonConfig.DEVICES_IS_BIND_KEY, is_bind);
|
||||
} else {
|
||||
} else if (snBindStateBaseResponse.code != 200) {
|
||||
// 200 但缺少 data 时保留上一次的绑定状态,避免误判为未绑定
|
||||
KVStore.encode(CommonConfig.DEVICES_IS_BIND_KEY, 0);
|
||||
}
|
||||
}
|
||||
@@ -273,6 +274,10 @@ public class MainServiceModel extends BaseAndroidViewModel {
|
||||
Log.e(TAG, "getScreenLock: onNext: " + baseResponse);
|
||||
int code = baseResponse.code;
|
||||
if (code == 200) {
|
||||
if (baseResponse.data == null) {
|
||||
Log.e(TAG, "getScreenLock: 响应缺少 data");
|
||||
return;
|
||||
}
|
||||
JsonObject jsonObject = GsonUtils.getJsonObject(GsonUtils.toJSONString(baseResponse.data));
|
||||
Integer screenLock = GsonUtils.optInt(jsonObject, "is_screen_lock");
|
||||
if (screenLock == null) {
|
||||
|
||||
@@ -426,7 +426,7 @@ public final class AppPolicySync {
|
||||
blackListUrls.addAll(urls);
|
||||
}
|
||||
}
|
||||
if (blacklistBeanBaseResponse.code == 200) {
|
||||
if (blacklistBeanBaseResponse.code == 200 && blacklistBeanBaseResponse.data != null) {
|
||||
BlacklistBean blacklistBean = blacklistBeanBaseResponse.data;
|
||||
List<String> stringList = blacklistBean.getBlacklist();
|
||||
if (stringList != null && !stringList.isEmpty()) {
|
||||
|
||||
@@ -64,13 +64,13 @@ public final class BrowserPolicySync {
|
||||
public HashSet<String> apply(BaseResponse<Label> baseResponse, BaseResponse<BrowserApiData> baseResponse2) throws Exception {
|
||||
HashSet<String> hashSet = new HashSet<>();
|
||||
setLabelResponse(baseResponse);
|
||||
if (baseResponse.code == 200) {
|
||||
if (baseResponse.code == 200 && baseResponse.data != null) {
|
||||
if (!TextUtils.isEmpty(baseResponse.data.getHome_page())) {
|
||||
hashSet.add(baseResponse.data.getHome_page());
|
||||
}
|
||||
}
|
||||
setBrowserResponse(baseResponse2);
|
||||
if (baseResponse2.code == 200) {
|
||||
if (baseResponse2.code == 200 && baseResponse2.data != null) {
|
||||
List<BrowserBean> beanList = baseResponse2.data.getWhiteList();
|
||||
if (beanList != null && beanList.size() != 0) {
|
||||
for (BrowserBean browserBean : beanList) {
|
||||
@@ -152,6 +152,11 @@ public final class BrowserPolicySync {
|
||||
|
||||
|
||||
private void setBrowserResponse(BaseResponse<BrowserApiData> response) {
|
||||
if (response.code == 200 && response.data == null) {
|
||||
// 200 但没有数据说明服务端响应异常,保留现有黑白名单
|
||||
Log.e(TAG, "setBrowserResponse: 响应缺少 data,保留现有配置");
|
||||
return;
|
||||
}
|
||||
if (response.code == 200) {
|
||||
//白名单
|
||||
List<BrowserBean> white = response.data.getWhiteList();
|
||||
@@ -218,6 +223,11 @@ public final class BrowserPolicySync {
|
||||
}
|
||||
|
||||
private void setLabelResponse(BaseResponse<Label> response) {
|
||||
if (response.code == 200 && response.data == null) {
|
||||
// 200 但没有数据说明服务端响应异常,保留现有主页与书签
|
||||
Log.e(TAG, "setLabelResponse: 响应缺少 data,保留现有配置");
|
||||
return;
|
||||
}
|
||||
if (response.code == 200) {
|
||||
String home_page = response.data.getHome_page();
|
||||
AndroidSettingsWriter.putString(mContext, AndroidSettingsWriter.NAMESPACE_SYSTEM, "homepagURL", home_page);
|
||||
|
||||
@@ -104,7 +104,7 @@ public final class DeviceSettingsSync {
|
||||
@Override
|
||||
public void onNext(BaseResponse<AppletQRCode> baseResponse) {
|
||||
Log.e(TAG, "getQRCodeURL: onNext: " + baseResponse);
|
||||
if (baseResponse.code == 200) {
|
||||
if (baseResponse.code == 200 && baseResponse.data != null) {
|
||||
AppletQRCode appletQRCode = baseResponse.data;
|
||||
mCacheHelper.put(UrlAddress.GET_APPLET_QRCODE, GsonUtils.toJSONString(appletQRCode));
|
||||
String url = appletQRCode.getFile();
|
||||
@@ -157,6 +157,11 @@ public final class DeviceSettingsSync {
|
||||
|
||||
int code = systemSettingsBaseResponse.code;
|
||||
if (code == 200) {
|
||||
if (systemSettingsBaseResponse.data == null || systemSettingsBaseResponse.data.getUser() == null) {
|
||||
// 200 但缺少数据时忽略本次配置,避免把已下发的管控设置清空
|
||||
Log.e(TAG, "getSystemSettings: 响应缺少 data/user,忽略本次配置");
|
||||
return;
|
||||
}
|
||||
String data = new Gson().toJson(systemSettingsBaseResponse.data.getUser());
|
||||
ControlManager.getInstance().setSystemSetting(data);
|
||||
} else {
|
||||
|
||||
@@ -133,7 +133,7 @@ public final class PushTagSync {
|
||||
@Override
|
||||
public void onNext(@NonNull BaseResponse<SnTag> baseResponse) {
|
||||
Log.e(TAG, "getTag: onNext: " + baseResponse);
|
||||
if (baseResponse.code == 200) {
|
||||
if (baseResponse.code == 200 && baseResponse.data != null) {
|
||||
SnTag snTag = baseResponse.data;
|
||||
callback.setTag(snTag.getTagName());
|
||||
} else {
|
||||
|
||||
@@ -199,6 +199,10 @@ public final class RemoteDiagnostics {
|
||||
Log.e(TAG, "getRunLog: onNext: " + snRunLogBaseResponse);
|
||||
if (snRunLogBaseResponse.code == 200) {
|
||||
SnRunLog snRunLog = snRunLogBaseResponse.data;
|
||||
if (snRunLog == null) {
|
||||
Log.e(TAG, "getRunLog: 响应缺少 data,忽略本次日志采集配置");
|
||||
return;
|
||||
}
|
||||
LogcatService.setStart_Time(snRunLog.getStart_time());
|
||||
LogcatService.setDuration(snRunLog.getDuration());
|
||||
LogcatService.setFileMaxSize(snRunLog.getFile_max_size());
|
||||
|
||||
Reference in new issue
Block a user