refactor(push): 推送字段解析改用 optInt/optString 并校验缺失字段

This commit is contained in:
tongtong committed 2026-11-18 18:13:43 +08:00
1 parent d711d060df
commit cac9c50948
1 file changed
+208 -52
@@ -549,7 +549,11 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: USB连接模式管控");
if (!TextUtils.isEmpty(jsonString)) {
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
String setting_usb = jsonObject.get("setting_usb").getAsString();
String setting_usb = optString(jsonObject, "setting_usb");
if (TextUtils.isEmpty(setting_usb)) {
ToastUtil.debugShow("setUsbStatus 缺少 setting_usb");
return;
}
ControlManager.getInstance().setUSBstate(setting_usb);
} else {
ToastUtil.debugShow("setUsbState jsonString is NULL");
@@ -560,7 +564,11 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: TF卡管控");
if (!TextUtils.isEmpty(jsonString)) {
JsonObject extra = GsonUtils.getJsonObject(jsonString);
int is_tf = extra.get("setting_memory").getAsInt();
Integer is_tf = optInt(extra, "setting_memory");
if (is_tf == null) {
ToastUtil.debugShow("setTfcardState 缺少 setting_memory");
return;
}
boolean aole_sdcard_forbid_on = ControlManager.getInstance().setTfcardState(changeNum(is_tf));
if (!aole_sdcard_forbid_on) {
ToastUtil.debugShow("setTfcardState failed,state:" + is_tf);
@@ -574,7 +582,12 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: 蓝牙管控");
if (!TextUtils.isEmpty(jsonString)) {
JsonObject extra = GsonUtils.getJsonObject(jsonString);
int is_bluetooth = changeNum(extra.get("setting_bht").getAsInt());
Integer setting_bht = optInt(extra, "setting_bht");
if (setting_bht == null) {
ToastUtil.debugShow("setBluetoothState 缺少 setting_bht");
return;
}
int is_bluetooth = changeNum(setting_bht);
// 写入 aole_bht_forbid_on 并按需关闭蓝牙,key 归 ControlManager 维护
ControlManager.getInstance().setBluetooth(is_bluetooth);
} else {
@@ -609,8 +622,12 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: 应用锁管控");
if (!TextUtils.isEmpty(jsonString)) {
JsonObject extra = GsonUtils.getJsonObject(jsonString);
String packageName = extra.get("package").getAsString();
int is_lock = extra.get("is_lock").getAsInt();
String packageName = optString(extra, "package");
Integer is_lock = optInt(extra, "is_lock");
if (TextUtils.isEmpty(packageName) || is_lock == null) {
ToastUtil.debugShow("setAppLockedstate 缺少 package 或 is_lock");
return;
}
ToastUtil.debugShow("收到应用锁管控消息:包名" + packageName + "is_lock_state:" + is_lock);
PackageManager pm = mContext.getPackageManager();
//后台为0可能传过来null
@@ -632,11 +649,15 @@ public class PushManager {
private void unintallApk(String jsonString) {
ToastUtil.debugShow("收到推送消息: 强制卸载应用");
JsonObject object = GsonUtils.getJsonObject(jsonString);
String packageName = object.get("app_package").getAsString();
String packageName = optString(object, "app_package");
if (TextUtils.isEmpty(packageName)) {
ToastUtil.debugShow("unintallApk 缺少 app_package");
return;
}
// 台电设备不删除无法静默卸载
AppPolicySync.getInstance().removeForceApp(packageName);
ToastUtil.debugShow("收到应用卸载消息:包名" + packageName);
if (!"".equals(packageName) && !packageName.equals(mContext.getApplicationContext().getPackageName())) {
if (!packageName.equals(mContext.getApplicationContext().getPackageName())) {
if (!ApkUtils.isAvailable(mContext.getApplicationContext(), packageName)) {
} else {
@@ -649,9 +670,13 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: 绑定设备");
ToastUtil.debugShow("收到绑定设备请求");
JsonObject object = GsonUtils.getJsonObject(jsonString);
String userName = object.get("member_name").getAsString();
final String id = object.get("id").getAsString();
String phoneNum = object.get("member_phone").getAsString();
String userName = optString(object, "member_name", "");
final String id = optString(object, "id");
String phoneNum = optString(object, "member_phone", "");
if (TextUtils.isEmpty(id)) {
Log.e(TAG, "bindService: 缺少 id,忽略本次绑定请求");
return;
}
dialog = new CustomDialog(mContext);
subscribe = Observable.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
@@ -717,8 +742,12 @@ public class PushManager {
private void setAppUpdate(String jsonString) {
ToastUtil.debugShow("收到推送消息: 禁止升级");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
String is_upgrade = jsonObject.get("is_upgrade").getAsString();
String packageName = jsonObject.get("package").getAsString();
String is_upgrade = optString(jsonObject, "is_upgrade");
String packageName = optString(jsonObject, "package");
if (TextUtils.isEmpty(packageName) || is_upgrade == null) {
ToastUtil.debugShow("setAppUpdate 缺少 package 或 is_upgrade");
return;
}
// aole_app_forbid 名单的增删由 AppWhitelistUtils 统一维护
boolean written = AppWhitelistUtils.setAppUpgradeForbid(mContext, packageName, "1".equals(is_upgrade));
Log.e(TAG, "setAppUpdate: " + packageName + ", written = " + written);
@@ -727,7 +756,11 @@ public class PushManager {
private void setAppInsideNetwork(String extras) {
ToastUtil.debugShow("收到推送消息: app内部网址管控");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int is_ground = jsonObject.get("is_ground").getAsInt();
Integer is_ground = optInt(jsonObject, "is_ground");
if (is_ground == null) {
ToastUtil.debugShow("setAppInsideNetwork 缺少 is_ground");
return;
}
if (is_ground == 0) {
// app_web_black_list 由 AppPolicySync 统一读写
AppPolicySync.getInstance().clearAppJumpBlackList();
@@ -783,7 +816,11 @@ public class PushManager {
private void forceKillApp(String extras) {
ToastUtil.debugShow("收到推送消息: 强制停止应用");
JsonObject killJSONObject = GsonUtils.getJsonObject(extras);
String packages = killJSONObject.get("app_package").getAsString();
String packages = optString(killJSONObject, "app_package");
if (TextUtils.isEmpty(packages)) {
ToastUtil.debugShow("forceKillApp 缺少 app_package");
return;
}
JgyUtils.getInstance().killPackage(packages);
JgyUtils.getInstance().gotoLauncher();
Log.e(TAG, extras);
@@ -811,22 +848,34 @@ public class PushManager {
private void setHotspot(String jsonString) {
ToastUtil.debugShow("收到推送消息: 热点管控");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int setting_hotspot = changeNum(jsonObject.get("setting_hotspot").getAsInt());//热点
ControlManager.getInstance().setHotspotForbid(setting_hotspot);
Integer setting_hotspot = optInt(jsonObject, "setting_hotspot");//热点
if (setting_hotspot == null) {
ToastUtil.debugShow("setHotspot 缺少 setting_hotspot");
return;
}
ControlManager.getInstance().setHotspotForbid(changeNum(setting_hotspot));
}
private void setRestore(String jsonString) {
ToastUtil.debugShow("收到推送消息: 恢复出厂设置管控");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int mode = jsonObject.get("qch_restore").getAsInt();
Integer mode = optInt(jsonObject, "qch_restore");
if (mode == null) {
ToastUtil.debugShow("setRestore 缺少 qch_restore");
return;
}
ControlManager.getInstance().setRestoreForbid(changeNum(mode));
}
private void setBrowserInput(String jsonString) {
ToastUtil.debugShow("收到推送消息: 浏览器禁止输入");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int setting_browserInput = changeNum(jsonObject.get("setting_browserInput").getAsInt());
ControlManager.getInstance().setBrowserInputForbid(setting_browserInput);
Integer setting_browserInput = optInt(jsonObject, "setting_browserInput");
if (setting_browserInput == null) {
ToastUtil.debugShow("setBrowserInput 缺少 setting_browserInput");
return;
}
ControlManager.getInstance().setBrowserInputForbid(changeNum(setting_browserInput));
}
private void setDeveloperControl(String extras) {
@@ -837,7 +886,7 @@ public class PushManager {
private void cleanAppCache(String jsonString) {
ToastUtil.debugShow("收到推送消息: 清除应用缓存");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
String pkg = jsonObject.get("package").getAsString();
String pkg = optString(jsonObject, "package");
if (TextUtils.isEmpty(pkg)) {
Log.e(TAG, "cleanAppCache: pkg is null");
return;
@@ -854,7 +903,11 @@ public class PushManager {
ToastUtil.debugShow("收到推送消息: 搜题开关");
Log.e(TAG, "searchTopic: " + jsonString);
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int search_topic = jsonObject.get("search_topic").getAsInt();
Integer search_topic = optInt(jsonObject, "search_topic");
if (search_topic == null) {
ToastUtil.debugShow("searchTopic 缺少 search_topic");
return;
}
KVStore.encode("search_topic", search_topic);
Log.e(TAG, "searchTopic: put = " + search_topic);
JgyUtils.getInstance().updateForbidList();
@@ -901,7 +954,10 @@ public class PushManager {
private void setZenMode(String extras) {
ToastUtil.debugShow("收到推送消息: 设置勿扰模式");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int setting_disturbance = jsonObject.get("setting_disturbance").getAsInt();
Integer setting_disturbance = optInt(jsonObject, "setting_disturbance");
if (setting_disturbance == null) {
ToastUtil.debugShow("setZenMode 缺少 setting_disturbance");
}
}
private void setLocation() {
@@ -918,21 +974,33 @@ public class PushManager {
private void setScreenBrightness(String extras) {
ToastUtil.debugShow("收到推送消息: 设置屏幕亮度");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int setting_luminance = jsonObject.get("setting_luminance").getAsInt();
Integer setting_luminance = optInt(jsonObject, "setting_luminance");
if (setting_luminance == null) {
ToastUtil.debugShow("setScreenBrightness 缺少 setting_luminance");
return;
}
ControlManager.getInstance().setScreenBrightness(setting_luminance);
}
private void setFontSize(String extras) {
ToastUtil.debugShow("收到推送消息: 设置字体大小");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
String setting_typeface = jsonObject.get("setting_typeface").getAsString();
String setting_typeface = optString(jsonObject, "setting_typeface");
if (TextUtils.isEmpty(setting_typeface)) {
ToastUtil.debugShow("setFontSize 缺少 setting_typeface");
return;
}
ControlManager.getInstance().setFontSize(setting_typeface);
}
private void setRotationLock(String extras) {
ToastUtil.debugShow("收到推送消息: 设置自动旋转");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int setting_rotation = jsonObject.get("setting_rotation").getAsInt();
Integer setting_rotation = optInt(jsonObject, "setting_rotation");
if (setting_rotation == null) {
ToastUtil.debugShow("setRotationLock 缺少 setting_rotation");
return;
}
RotationPolicy.setRotationLock(mContext, setting_rotation == 0);
}
@@ -958,20 +1026,32 @@ public class PushManager {
private void setSOSPhoneNumber(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
String setting_sos = jsonObject.get("setting_sos").getAsString();
String setting_sos = optString(jsonObject, "setting_sos");
if (setting_sos == null) {
ToastUtil.debugShow("setSOSPhoneNumber 缺少 setting_sos");
return;
}
ControlManager.getInstance().setSOSNumber(setting_sos);
}
private void setAdminApp(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int setting_admin_app = jsonObject.get("setting_admin_app").getAsInt();
Integer setting_admin_app = optInt(jsonObject, "setting_admin_app");
if (setting_admin_app == null) {
ToastUtil.debugShow("setAdminApp 缺少 setting_admin_app");
return;
}
ControlManager.getInstance().setAppstoreAdmin(setting_admin_app);
}
private void setScreenShared(String extras) {
ToastUtil.debugShow("收到推送消息: 投屏开关");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int projection_screen = jsonObject.get("projection_screen").getAsInt();
Integer projection_screen = optInt(jsonObject, "projection_screen");
if (projection_screen == null) {
ToastUtil.debugShow("setScreenShared 缺少 projection_screen");
return;
}
ControlManager.getInstance().setScreenShared(changeNum(projection_screen));
}
@@ -981,26 +1061,26 @@ public class PushManager {
private void setHotPoint(String jsonString) {
ToastUtil.debugShow("收到推送消息: 热点开关");
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int setting_hotspot = changeNum(jsonObject.get("hot_point").getAsInt());//热点
ControlManager.getInstance().setHotPointForbid(setting_hotspot);
Integer setting_hotspot = optInt(jsonObject, "hot_point");//热点
if (setting_hotspot == null) {
ToastUtil.debugShow("setHotPoint 缺少 hot_point");
return;
}
ControlManager.getInstance().setHotPointForbid(changeNum(setting_hotspot));
}
private void setDefaultDesktop(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
JsonElement jsonElement = jsonObject.get("desktop_app");
if (jsonElement != null) {
String packeges = jsonElement.getAsString();
String packeges = optString(jsonObject, "desktop_app");
if (packeges != null) {
JgyUtils.getInstance().setDefaultDesktop(packeges);
} else {
}
}
private void setDefaultBrowser(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
JsonElement jsonElement = jsonObject.get("browser_app");
if (jsonElement != null) {
String packeges = jsonElement.getAsString();
String packeges = optString(jsonObject, "browser_app");
if (packeges != null) {
JgyUtils.getInstance().setDefaultBrowser(packeges);
} else {
Log.e(TAG, "setDefaultBrowser: packeges is NULL");
@@ -1009,7 +1089,11 @@ public class PushManager {
private void setDefaultInputMethod(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
String packeges = jsonObject.get("typewriting_app").getAsString();
String packeges = optString(jsonObject, "typewriting_app");
if (packeges == null) {
ToastUtil.debugShow("setDefaultInputMethod 缺少 typewriting_app");
return;
}
JgyUtils.getInstance().setDefaultInputMethod(packeges);
}
@@ -1043,7 +1127,11 @@ public class PushManager {
private void oneKeyNetwork(String extras) {
ToastUtil.debugShow("收到推送消息: 一键断网");
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
int is_network = jsonObject.get("is_network").getAsInt();
Integer is_network = optInt(jsonObject, "is_network");
if (is_network == null) {
ToastUtil.debugShow("oneKeyNetwork 缺少 is_network");
return;
}
if (is_network == 1) {
JgyUtils.getInstance().oneKeyDisconnection();
} else {
@@ -1053,14 +1141,10 @@ public class PushManager {
private void addTag(String extras) {
JsonObject jsonObject = GsonUtils.getJsonObject(extras);
try {
String tag_name = jsonObject.get("tag_name").getAsString();
Log.e(TAG, "addTag: " + tag_name);
if (!TextUtils.isEmpty(tag_name)) {
PushTagSync.getInstance().setPushTags(tag_name);
}
} catch (Exception e) {
Log.e(TAG, "addTag: " + e.getMessage());
String tag_name = optString(jsonObject, "tag_name", "");
Log.e(TAG, "addTag: " + tag_name);
if (!TextUtils.isEmpty(tag_name)) {
PushTagSync.getInstance().setPushTags(tag_name);
}
}
@@ -1168,6 +1252,71 @@ public class PushManager {
return paramInt == 1 ? 0 : 1;
}
/**
* 读取推送 JSON 的整型字段,字段缺失、为 null 或类型不符时返回 defaultValue。
* 后台协议可能漏发字段,直接 {@code get(key).getAsInt()} 会抛空指针。
*/
private static int optInt(JsonObject jsonObject, String key, int defaultValue) {
Integer value = optInt(jsonObject, key);
return value == null ? defaultValue : value;
}
/**
* 读取推送 JSON 的整型字段,字段缺失时返回 null,由调用方决定是否跳过本次处理。
*/
private static Integer optInt(JsonObject jsonObject, String key) {
JsonElement element = optElement(jsonObject, key);
if (element == null) {
return null;
}
try {
return element.getAsInt();
} catch (Exception e) {
Log.e(TAG, "optInt: " + key + ", " + e.getMessage());
return null;
}
}
/**
* 读取推送 JSON 的字符串字段,字段缺失时返回 defaultValue。
*/
private static String optString(JsonObject jsonObject, String key, String defaultValue) {
String value = optString(jsonObject, key);
return value == null ? defaultValue : value;
}
/**
* 读取推送 JSON 的字符串字段,字段缺失时返回 null。
*/
private static String optString(JsonObject jsonObject, String key) {
JsonElement element = optElement(jsonObject, key);
if (element == null) {
return null;
}
try {
return element.getAsString();
} catch (Exception e) {
Log.e(TAG, "optString: " + key + ", " + e.getMessage());
return null;
}
}
/**
* 取字段对应的 JsonElement,统一挡掉 jsonObject 为 null、字段不存在、值为 JsonNull 三种情况。
*/
private static JsonElement optElement(JsonObject jsonObject, String key) {
if (jsonObject == null) {
Log.e(TAG, "optElement: jsonObject is NULL, key = " + key);
return null;
}
JsonElement element = jsonObject.get(key);
if (element == null || element.isJsonNull()) {
Log.e(TAG, "optElement: 字段缺失 " + key);
return null;
}
return element;
}
private void sendStartTime(String jsonString) {
DeviceProfileSync.getInstance().updateAdminInfo();
AppInventorySync.getInstance().SendAppInstallInfo();
@@ -1221,8 +1370,12 @@ public class PushManager {
private void setCameta(String jsonString) {
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
//摄像头开关
int setting_camera = changeNum(jsonObject.get("setting_camera").getAsInt());
ControlManager.getInstance().setCamera(setting_camera);
Integer setting_camera = optInt(jsonObject, "setting_camera");
if (setting_camera == null) {
ToastUtil.debugShow("setCameta 缺少 setting_camera");
return;
}
ControlManager.getInstance().setCamera(changeNum(setting_camera));
}
private void setPhone(String jsonString) {
@@ -1231,9 +1384,12 @@ public class PushManager {
}
private void setLock_screen(String jsonString) {
int type = 0;
JsonObject jSONObject = GsonUtils.getJsonObject(jsonString);
type = jSONObject.get("type").getAsInt();
Integer type = optInt(jSONObject, "type");
if (type == null) {
ToastUtil.debugShow("setLock_screen 缺少 type");
return;
}
if (!SystemUtils.isServiceAlive(mContext, ManagerService.class.getName())) {
mContext.startService(new Intent(mContext, ManagerService.class));