feat(push): 升级下载与启动请求错峰,优化网络去重
This commit is contained in:
1 parent
7e169eb462
commit
14ff2c42ae
13 files changed
+573
-488
No files matched your search
@@ -32,7 +32,6 @@ import com.fuying.sn.push.PushManager;
|
||||
import com.fuying.sn.receiver.APKinstallReceiver;
|
||||
import com.fuying.sn.receiver.BootReceiver;
|
||||
import com.fuying.sn.utils.SPUtils;
|
||||
import com.fuying.sn.utils.TimeUtils;
|
||||
import com.fuying.sn.utils.Utils;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
@@ -60,12 +59,9 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
|
||||
public static final String ACTION_LOCK = "LockScreenReceiver_lockscreen";
|
||||
public static final String ACTION_UNLOCK = "LockScreenReceiver_unlockscreen";
|
||||
public static final String ACTION_UPDATE = "TimeChangedReceiver_update";
|
||||
private static final String DEFAULT_PASSWORD = "0000";
|
||||
private WindowManager windowManager;
|
||||
private View topView;
|
||||
private boolean screenlocked = false;
|
||||
private boolean timelocked = false;
|
||||
|
||||
/**
|
||||
* 本地"已用密码解除锁屏"标记。
|
||||
@@ -185,10 +181,8 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
pwdUnlocked = false;
|
||||
setLockedState(is_screen_lock);
|
||||
} else {
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
// 服务端明确返回非锁定(未绑定/未登录等):直接解锁
|
||||
hideFloatingWindow();
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
|
||||
}
|
||||
}
|
||||
@@ -207,16 +201,10 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
|
||||
private void setLockedState(int lockedState) {
|
||||
if (lockedState == 1) {
|
||||
if (!timelocked) {
|
||||
showFloatingWindow("屏幕已锁定");
|
||||
}
|
||||
screenlocked = true;
|
||||
showFloatingWindow("屏幕已锁定");
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 1);
|
||||
} else {
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
hideFloatingWindow();
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
|
||||
}
|
||||
}
|
||||
@@ -240,63 +228,74 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
// Log.e(TAG, "showFloatingWindow: debug mode skip");
|
||||
// return;
|
||||
// }
|
||||
if (Settings.canDrawOverlays(this)) {
|
||||
// 获取WindowManager服务
|
||||
if (null == windowManager) {
|
||||
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
||||
}
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
windowManager.getDefaultDisplay().getRealMetrics(dm);
|
||||
int width = dm.widthPixels; // 屏幕宽度(像素)
|
||||
int height = dm.heightPixels; // 屏幕高度(像素)
|
||||
// 新建悬浮窗控件
|
||||
final Button button = new Button(getApplicationContext());
|
||||
button.setText("霸屏测试");
|
||||
button.setAlpha(0.9f);
|
||||
button.setBackgroundColor(Color.BLACK);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (!Settings.canDrawOverlays(this)) {
|
||||
// 没有悬浮窗权限时原来会静默返回,出问题时完全无迹可查
|
||||
Log.e(TAG, "showFloatingWindow: no overlay permission, skip. name=" + name);
|
||||
return;
|
||||
}
|
||||
// 获取WindowManager服务
|
||||
if (null == windowManager) {
|
||||
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
||||
}
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
windowManager.getDefaultDisplay().getRealMetrics(dm);
|
||||
int width = dm.widthPixels; // 屏幕宽度(像素)
|
||||
int height = dm.heightPixels; // 屏幕高度(像素)
|
||||
// 新建悬浮窗控件
|
||||
final Button button = new Button(getApplicationContext());
|
||||
button.setText("霸屏测试");
|
||||
button.setAlpha(0.9f);
|
||||
button.setBackgroundColor(Color.BLACK);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// windowManager.removeView(button);
|
||||
}
|
||||
});
|
||||
if (null == topView) {
|
||||
topView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_screen_lock, null);
|
||||
}
|
||||
});
|
||||
if (null != topView) {
|
||||
if (topView.isAttachedToWindow()) {
|
||||
// 已在屏:只刷新文案,避免重复 addView
|
||||
initTopView(topView, name);
|
||||
} else {
|
||||
if ("added".equals(topView.getTag())) {
|
||||
initTopView(topView, name);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// topView.setAlpha(0.8f);
|
||||
// 设置LayoutParam
|
||||
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
|
||||
//TYPE_SYSTEM_OVERLAY可以下滑通知栏
|
||||
// 引用还在但窗口已被系统移除(被其他窗口顶掉/系统回收):
|
||||
// 原实现会一直复用这个已不在屏上的 View,导致锁屏"看起来失效"
|
||||
Log.e(TAG, "showFloatingWindow: topView detached, rebuild it.");
|
||||
removeTopViewSafely();
|
||||
}
|
||||
topView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_screen_lock, null);
|
||||
initTopView(topView, name);
|
||||
// topView.setAlpha(0.8f);
|
||||
// 设置LayoutParam
|
||||
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
|
||||
//TYPE_SYSTEM_OVERLAY可以下滑通知栏
|
||||
// layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
|
||||
} else {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
|
||||
}
|
||||
layoutParams.flags |= WindowManager.LayoutParams.FLAG_BLUR_BEHIND
|
||||
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
|
||||
layoutParams.format = PixelFormat.RGBA_8888;
|
||||
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
//systemUiVisibility 关闭通知栏和导航栏
|
||||
layoutParams.systemUiVisibility =
|
||||
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
| View.SYSTEM_UI_FLAG_IMMERSIVE
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
||||
layoutParams.x = 0;
|
||||
layoutParams.y = 0;
|
||||
// 将悬浮窗控件添加到WindowManager
|
||||
} else {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
|
||||
}
|
||||
layoutParams.flags |= WindowManager.LayoutParams.FLAG_BLUR_BEHIND
|
||||
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
|
||||
layoutParams.format = PixelFormat.RGBA_8888;
|
||||
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
//systemUiVisibility 关闭通知栏和导航栏
|
||||
layoutParams.systemUiVisibility =
|
||||
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
| View.SYSTEM_UI_FLAG_IMMERSIVE
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
||||
layoutParams.x = 0;
|
||||
layoutParams.y = 0;
|
||||
// 将悬浮窗控件添加到WindowManager
|
||||
try {
|
||||
windowManager.addView(topView, layoutParams);
|
||||
topView.setTag("added");
|
||||
Log.e(TAG, "showFloatingWindow: added. name=" + name);
|
||||
} catch (Exception e) {
|
||||
// 添加失败时不能留下脏引用,否则下一分钟 tick 会一直复用它
|
||||
Log.e(TAG, "showFloatingWindow: addView failed: " + e.getMessage());
|
||||
topView = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,11 +372,6 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
|
||||
private void hide() {
|
||||
hideFloatingWindow();
|
||||
// 视图已移除,内存状态必须同步复位:
|
||||
// 否则下一次 setFloatingWindow() 会走到 screenlocked==true 分支去访问已置空的 topView,
|
||||
// 抛出 NPE(release 包被全局异常捕获吞掉),表现为锁屏浮窗再也不出现。
|
||||
screenlocked = false;
|
||||
timelocked = false;
|
||||
// 密码解锁改用内存标记表达,不再在这里把 LOCK_STATE 直接清 0;
|
||||
// 该 key 交给"按服务端状态落地"的路径(setLockedState / 推送 / 轮询)统一维护。
|
||||
pwdUnlocked = true;
|
||||
@@ -413,11 +407,28 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
}
|
||||
|
||||
private void hideFloatingWindow() {
|
||||
if (null == windowManager) {
|
||||
removeTopViewSafely();
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全移除锁屏浮窗。
|
||||
* removeView 在"View 已被系统移除"时会抛 IllegalArgumentException,
|
||||
* 这里用 try/catch 兜住,并在 finally 里把 topView 置空,
|
||||
* 否则会留下脏引用,后续再也弹不出锁屏。
|
||||
* 注意:不能用 isAttachedToWindow() 做前置判断 —— addView 之后、第一次 traversal 之前
|
||||
* 它仍为 false,会漏掉真正的 removeView,导致浮窗泄漏且永远关不掉。
|
||||
*/
|
||||
private void removeTopViewSafely() {
|
||||
if (null == topView) {
|
||||
return;
|
||||
}
|
||||
if (null != topView) {
|
||||
windowManager.removeView(topView);
|
||||
try {
|
||||
if (null != windowManager) {
|
||||
windowManager.removeView(topView);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "removeTopViewSafely: " + e.getMessage());
|
||||
} finally {
|
||||
topView = null;
|
||||
}
|
||||
}
|
||||
@@ -449,17 +460,11 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
String name = "屏幕已锁定";
|
||||
// 服务端状态已下发,本地"密码解锁"标记失效
|
||||
pwdUnlocked = false;
|
||||
if (!timelocked) {
|
||||
showFloatingWindow(name);
|
||||
}
|
||||
screenlocked = true;
|
||||
showFloatingWindow(name);
|
||||
} else if (ACTION_UNLOCK.equals(action)) {
|
||||
// 服务端状态已下发,本地"密码解锁"标记失效
|
||||
pwdUnlocked = false;
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
hideFloatingWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -467,7 +472,7 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
private TimeChangedReceiver mTimeChangedReceiver;
|
||||
|
||||
/**
|
||||
* 监听时间和日期变化
|
||||
* 监听时间和日期变化,作为锁屏状态丢失(推送未到、进程被回收)时的兜底刷新
|
||||
*/
|
||||
private void registerTimeReceiver() {
|
||||
mTimeChangedReceiver = new TimeChangedReceiver();
|
||||
@@ -477,7 +482,6 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIME_TICK);
|
||||
filter.addAction(ACTION_UPDATE);
|
||||
registerReceiver(mTimeChangedReceiver, filter);
|
||||
}
|
||||
|
||||
@@ -487,8 +491,7 @@ 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");
|
||||
@@ -497,9 +500,6 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
} else if (Intent.ACTION_TIME_TICK.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "time tick");
|
||||
setFloatingWindow();
|
||||
} else if (ACTION_UPDATE.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "date update");
|
||||
setFloatingWindow();
|
||||
}
|
||||
|
||||
|
||||
@@ -511,42 +511,15 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按服务端锁屏状态刷新锁屏浮窗(唯一的锁屏来源)。
|
||||
* 由 TIME_TICK / DATE_CHANGED 周期驱动,用于兜底:
|
||||
* 推送丢失、进程被回收重启后,只要 SP 里还留着 LOCK_STATE 就能自动恢复锁屏。
|
||||
*/
|
||||
private void setFloatingWindow() {
|
||||
TimeUtils.ContralTime workingContralTime = TimeUtils.getWorkingDayContralTime(ManagerService.this);
|
||||
TimeUtils.ContralTime weekContralTime = TimeUtils.getWeekDayContralTime(ManagerService.this);
|
||||
// if (null != workingContralTime) {
|
||||
// 先算出本轮的时间管控状态并落到 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);
|
||||
if (null != textView) {
|
||||
textView.setText(timeText);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// getScreenLockState();
|
||||
int is_screen_lock = getServerLockState();
|
||||
setLockedState(is_screen_lock);
|
||||
if (!screenlocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
}
|
||||
// } else {
|
||||
// if (!screenlocked) {
|
||||
// hideFloatingWindow()
|
||||
// }
|
||||
// getScreenLockState()
|
||||
// timelocked = false
|
||||
// }
|
||||
// 需要以服务端为准时,可改成先 getScreenLockState() 拉一次再落地
|
||||
int is_screen_lock = getServerLockState();
|
||||
setLockedState(is_screen_lock);
|
||||
}
|
||||
|
||||
private ScreenLockReceiver screenLockReceiver;
|
||||
|
||||
@@ -63,6 +63,32 @@ import io.reactivex.rxjava3.disposables.Disposable;
|
||||
public class MainService extends LifecycleService implements ViewModelStoreOwner, NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private static final String TAG = "MainService";
|
||||
|
||||
/**
|
||||
* 启动/重启后首次拉取配置的基础延时(毫秒)
|
||||
*/
|
||||
private static final long STARTUP_DELAY_BASE = 20 * 1000L;
|
||||
|
||||
/**
|
||||
* 网络重连后首次拉取配置的基础延时(毫秒)
|
||||
*/
|
||||
private static final long STARTUP_DELAY_NETWORK = 15 * 1000L;
|
||||
|
||||
/**
|
||||
* 启动后首次请求的随机错峰上限(毫秒)。
|
||||
* <p>
|
||||
* 静默升级是全量下发的:整批设备几乎同时下载、同时安装、同时重启,
|
||||
* 若重启后都在固定时刻集中请求接口,服务端会被瞬时打挂。
|
||||
* 因此在基础延时上再叠加一段随机时间,把整批设备的请求摊开到 10 分钟内。
|
||||
*/
|
||||
private static final long STARTUP_DELAY_SPREAD = 10 * 60 * 1000L;
|
||||
|
||||
/**
|
||||
* 计算首次拉取接口的错峰延时:基础延时 + [0, {@link #STARTUP_DELAY_SPREAD}) 随机值
|
||||
*/
|
||||
private static long nextStartupDelay(long baseDelay) {
|
||||
return TimeUtils.randomDelay(baseDelay, baseDelay + STARTUP_DELAY_SPREAD);
|
||||
}
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
private MainServiceModel mViewModel;
|
||||
@@ -89,7 +115,8 @@ public class MainService extends LifecycleService implements ViewModelStoreOwner
|
||||
// if (reboot) {
|
||||
//第一次开机联网后直接连接
|
||||
// TODO: 2022/11/21 有些设备联网了但是立刻请求还是失败
|
||||
Log.e(TAG, "onConnected run: 15秒后请求网络");
|
||||
long connectedDelay = nextStartupDelay(STARTUP_DELAY_NETWORK);
|
||||
Log.e(TAG, "onConnected run: " + (connectedDelay / 1000) + "秒后请求网络");
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -108,7 +135,7 @@ public class MainService extends LifecycleService implements ViewModelStoreOwner
|
||||
SPUtils.put(MainService.this, CommonConfig.JGY_FIRST_BOOT, 1);
|
||||
}
|
||||
}
|
||||
}, 15000);
|
||||
}, connectedDelay);
|
||||
mMMKV.encode(CommonConfig.DEVICES_REBOOT, false);
|
||||
// }
|
||||
// NetInterfaceManager.getInstance().updateAdminInfo();
|
||||
@@ -176,7 +203,9 @@ public class MainService extends LifecycleService implements ViewModelStoreOwner
|
||||
boolean networkConnected = JgyUtils.getInstance().isNetworkConnected();
|
||||
Log.e(TAG, "onCreate: networkConnected = " + networkConnected);
|
||||
// if (networkConnected && reboot) {
|
||||
Log.e(TAG, "onCreate run: 20秒后请求网络");
|
||||
//升级重启后所有设备都在这里发起首批请求,加随机错峰避免集中打挂服务端
|
||||
long startupDelay = nextStartupDelay(STARTUP_DELAY_BASE);
|
||||
Log.e(TAG, "onCreate run: " + (startupDelay / 1000) + "秒后请求网络");
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -191,7 +220,7 @@ public class MainService extends LifecycleService implements ViewModelStoreOwner
|
||||
NetInterfaceManager.getInstance().SendAppInstallInfo();
|
||||
}
|
||||
}
|
||||
}, 20000);
|
||||
}, startupDelay);
|
||||
mMMKV.encode(CommonConfig.DEVICES_REBOOT, false);
|
||||
// } else {
|
||||
// NetInterfaceManager.getInstance().updateAdminInfo();
|
||||
|
||||
Reference in new issue
Block a user