refactor: 重构网络去重与锁屏状态管理
移除 ConnectManager,改由 RepeatRequestInterceptor 在请求前拦截重复请求并复用真实响应; MyAppList 改用缓存控制刷新;PushManager 去除短节流限制; ManagerService 引入本地 pwdUnlocked 标记,避免密码解锁直接改写服务端锁屏镜像, 并修复浮窗隐藏后状态未复位导致的 NPE。
This commit is contained in:
1 parent
d28f9ac288
commit
7e169eb462
7 files changed
+363
-268
No files matched your search
@@ -67,6 +67,13 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
private boolean screenlocked = false;
|
||||
private boolean timelocked = false;
|
||||
|
||||
/**
|
||||
* 本地"已用密码解除锁屏"标记。
|
||||
* 密码解锁不再直接改写服务端锁屏状态镜像(LOCK_STATE),改用本标记表达本地放行;
|
||||
* 服务端下发新状态(推送/轮询)或进程重启后该标记失效。
|
||||
*/
|
||||
private boolean pwdUnlocked = false;
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
|
||||
@@ -174,6 +181,8 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
if (code == 200) {
|
||||
JsonObject jsonObject = GsonUtils.getJsonObject(GsonUtils.toJSONString(baseResponse.data));
|
||||
int is_screen_lock = jsonObject.get("is_screen_lock").getAsInt();
|
||||
// 服务端状态已下发,本地"密码解锁"标记失效
|
||||
pwdUnlocked = false;
|
||||
setLockedState(is_screen_lock);
|
||||
} else {
|
||||
if (!timelocked) {
|
||||
@@ -214,6 +223,18 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
|
||||
public static final String LOCK_STATE = "SCRENN_LOOCKED_STATE";
|
||||
|
||||
/**
|
||||
* 读取本地当前应生效的服务端锁屏状态。
|
||||
* LOCK_STATE(SP) 是服务端下发状态的镜像,密码解锁只置内存标记 pwdUnlocked,
|
||||
* 在服务端下发新状态之前不再从镜像里恢复锁屏。
|
||||
*/
|
||||
private int getServerLockState() {
|
||||
if (pwdUnlocked) {
|
||||
return 0;
|
||||
}
|
||||
return (int) SPUtils.get(ManagerService.this, LOCK_STATE, 0);
|
||||
}
|
||||
|
||||
private void showFloatingWindow(String name) {
|
||||
// if (BuildConfig.DEBUG) {
|
||||
// Log.e(TAG, "showFloatingWindow: debug mode skip");
|
||||
@@ -352,7 +373,14 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
|
||||
private void hide() {
|
||||
hideFloatingWindow();
|
||||
SPUtils.put(ManagerService.this, ManagerService.LOCK_STATE, 0);
|
||||
// 视图已移除,内存状态必须同步复位:
|
||||
// 否则下一次 setFloatingWindow() 会走到 screenlocked==true 分支去访问已置空的 topView,
|
||||
// 抛出 NPE(release 包被全局异常捕获吞掉),表现为锁屏浮窗再也不出现。
|
||||
screenlocked = false;
|
||||
timelocked = false;
|
||||
// 密码解锁改用内存标记表达,不再在这里把 LOCK_STATE 直接清 0;
|
||||
// 该 key 交给"按服务端状态落地"的路径(setLockedState / 推送 / 轮询)统一维护。
|
||||
pwdUnlocked = true;
|
||||
NetInterfaceManager.getInstance().getUpdateLockScreenObservable()
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
@@ -419,11 +447,15 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
if (ACTION_LOCK.equals(action)) {
|
||||
// String name = intent.getStringExtra("name");
|
||||
String name = "屏幕已锁定";
|
||||
// 服务端状态已下发,本地"密码解锁"标记失效
|
||||
pwdUnlocked = false;
|
||||
if (!timelocked) {
|
||||
showFloatingWindow(name);
|
||||
}
|
||||
screenlocked = true;
|
||||
} else if (ACTION_UNLOCK.equals(action)) {
|
||||
// 服务端状态已下发,本地"密码解锁"标记失效
|
||||
pwdUnlocked = false;
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
@@ -455,6 +487,9 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
String action = intent.getAction();
|
||||
if (Intent.ACTION_DATE_CHANGED.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "data changed");
|
||||
// 跨天后"工作日/周末"规则集会切换,立即按新日期重算,
|
||||
// 避免浮窗/锁屏状态延迟到下一次 TIME_TICK 才生效
|
||||
setFloatingWindow();
|
||||
} else if (Intent.ACTION_TIME_CHANGED.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "time changed");
|
||||
} else if (Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
|
||||
@@ -480,22 +515,30 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
TimeUtils.ContralTime workingContralTime = TimeUtils.getWorkingDayContralTime(ManagerService.this);
|
||||
TimeUtils.ContralTime weekContralTime = TimeUtils.getWeekDayContralTime(ManagerService.this);
|
||||
// if (null != workingContralTime) {
|
||||
if (TimeUtils.inContralTime(workingContralTime, weekContralTime)) {
|
||||
if (!screenlocked) {
|
||||
showFloatingWindow("可用时间:\n" + TimeUtils.getNowTimeString(ManagerService.this));
|
||||
// 先算出本轮的时间管控状态并落到 timelocked,再处理 UI/锁屏状态:
|
||||
// setLockedState() 与 LockScreenReceiver 内部都要判断 timelocked,
|
||||
// 若在调用之后才赋值,会出现"服务端锁屏已下发但不显示"的空窗(最长到下一次 TIME_TICK)。
|
||||
boolean inTimeControl = TimeUtils.inContralTime(workingContralTime, weekContralTime);
|
||||
timelocked = inTimeControl;
|
||||
String timeText = "可用时间:\n" + TimeUtils.getNowTimeString(ManagerService.this);
|
||||
if (inTimeControl) {
|
||||
if (null == topView) {
|
||||
// topView 可能已被 hideFloatingWindow()(密码解锁、解锁广播)移除,
|
||||
// 此时不能再对 topView 取子控件,否则 NPE 会导致浮窗再也不显示。
|
||||
showFloatingWindow(timeText);
|
||||
} else {
|
||||
TextView textView = topView.findViewById(R.id.textView);
|
||||
textView.setText("可用时间:\n" + TimeUtils.getNowTimeString(ManagerService.this));
|
||||
if (null != textView) {
|
||||
textView.setText(timeText);
|
||||
}
|
||||
}
|
||||
timelocked = true;
|
||||
} else {
|
||||
// getScreenLockState();
|
||||
int is_screen_lock = (int) SPUtils.get(ManagerService.this, LOCK_STATE, 0);
|
||||
int is_screen_lock = getServerLockState();
|
||||
setLockedState(is_screen_lock);
|
||||
if (!screenlocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
timelocked = false;
|
||||
}
|
||||
// } else {
|
||||
// if (!screenlocked) {
|
||||
|
||||
Reference in new issue
Block a user