fix(MainActivity): 优化定位权限校验逻辑与空转状态UI刷新

1. 服务开关开启前增加定位权限前置校验,无权限时阻止开启并提示
2. 空转关闭时自动检测权限,无权限则强制停止GPS服务
3. 权限被拒绝时强制关闭服务开关并禁用管理按钮
4. 新增Toolbar副标题显示空转运行状态提示
5. 新增管理按钮状态刷新,空转中强制可点击并追加提示文字
6. 移除冗余注释,精简接口定义
This commit is contained in:
2026-05-03 15:22:18 +08:00
parent 389300433c
commit 2cfc29845e
2 changed files with 52 additions and 51 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Sun May 03 05:39:16 GMT 2026 #Sun May 03 15:20:10 CST 2026
stageCount=20 stageCount=20
libraryProject= libraryProject=
baseVersion=15.12 baseVersion=15.12
publishVersion=15.12.19 publishVersion=15.12.19
buildCount=7 buildCount=11
baseBetaVersion=15.12.20 baseBetaVersion=15.12.20

View File

@@ -46,7 +46,7 @@ import cc.winboll.studio.positions.R;
* 4. 全局权限申请与权限结果回调处理 * 4. 全局权限申请与权限结果回调处理
* @Author 豆包&ZhanGSKen<zhangsken@qq.com> * @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @CreateTime 2026/05/03 12:23:00 * @CreateTime 2026/05/03 12:23:00
* @EditTime 2026/05/03 15:26:13 * @EditTime 2026/05/03 15:42:15
*/ */
public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity { public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
@@ -71,16 +71,7 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
* 应用空转状态回调内部接口 * 应用空转状态回调内部接口
*/ */
public interface OnAppIdleRunningListener { public interface OnAppIdleRunningListener {
/**
* 接收空转开关状态变更
* @param isRunning 空转是否开启
*/
void onIdleStatusChange(boolean isRunning); void onIdleStatusChange(boolean isRunning);
/**
* 接收空转日志消息
* @param log 日志文本
*/
void onIdleLogReceive(String log); void onIdleLogReceive(String log);
} }
@@ -112,7 +103,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
mADsBannerView = findViewById(R.id.adsbanner); mADsBannerView = findViewById(R.id.adsbanner);
initAppIdleHandler(); initAppIdleHandler();
// 初始化自动根据空转状态刷新日志面板
refreshIdleLogLayout(); refreshIdleLogLayout();
} }
@@ -136,9 +126,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
} }
// ===================== 初始化相关方法 ===================== // ===================== 初始化相关方法 =====================
/**
* 初始化顶部导航栏
*/
private void initToolbar() { private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar); setSupportActionBar(mToolbar);
@@ -148,9 +135,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
LogUtils.d(TAG, "initToolbar -> 顶部工具栏初始化完毕"); LogUtils.d(TAG, "initToolbar -> 顶部工具栏初始化完毕");
} }
/**
* 初始化全部UI控件与绑定事件
*/
private void initViews() { private void initViews() {
mTvIdleLog = (TextView) findViewById(R.id.tv_idle_log); mTvIdleLog = (TextView) findViewById(R.id.tv_idle_log);
mScrollIdleLog = (ScrollView) findViewById(R.id.scroll_idle_log); mScrollIdleLog = (ScrollView) findViewById(R.id.scroll_idle_log);
@@ -164,31 +148,31 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
mServiceSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { mServiceSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//切换开关时先校验权限,无权限直接强制关闭开关
if(isChecked && !checkLocationPermissions()){
mServiceSwitch.setChecked(false);
Toast.makeText(MainActivity.this,"未获取定位权限无法开启GPS服务",Toast.LENGTH_SHORT).show();
return;
}
LogUtils.d(TAG, "onCheckedChanged -> 服务开关状态变更isChecked = " + isChecked); LogUtils.d(TAG, "onCheckedChanged -> 服务开关状态变更isChecked = " + isChecked);
if (isChecked && !checkLocationPermissions()) {
requestLocationPermissions();
return;
}
if (isChecked) { if (isChecked) {
ServiceUtil.startAutoService(MainActivity.this); ServiceUtil.startAutoService(MainActivity.this);
} else { } else {
ServiceUtil.stopAutoService(MainActivity.this); ServiceUtil.stopAutoService(MainActivity.this);
} }
mManagePositionsButton.setEnabled(isChecked); mManagePositionsButton.setEnabled(isChecked);
refreshManageButtonState();
} }
}); });
LogUtils.i(TAG, "initViews -> 全部UI控件初始化绑定完成"); LogUtils.i(TAG, "initViews -> 全部UI控件初始化绑定完成");
} }
/**
* 初始化空转处理器并绑定监听回调
*/
private void initAppIdleHandler() { private void initAppIdleHandler() {
AppIdleRunningModeHandler.init(MainActivity.this); AppIdleRunningModeHandler.init(MainActivity.this);
setOnAppIdleRunningListener(new OnAppIdleRunningListener() { setOnAppIdleRunningListener(new OnAppIdleRunningListener() {
@Override @Override
public void onIdleStatusChange(boolean isRunning) { public void onIdleStatusChange(boolean isRunning) {
// 状态变更立刻刷新边框与日志
refreshIdleLogLayout(); refreshIdleLogLayout();
appendIdleLog("IdleRunning Status : " + isRunning); appendIdleLog("IdleRunning Status : " + isRunning);
} }
@@ -201,9 +185,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
LogUtils.i(TAG, "initAppIdleHandler -> 空转处理器初始化与监听绑定完成"); LogUtils.i(TAG, "initAppIdleHandler -> 空转处理器初始化与监听绑定完成");
} }
/**
* 设置主布局主题背景色
*/
private void setLLMainBackgroundColor() { private void setLLMainBackgroundColor() {
TypedArray typedArray = getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent}); TypedArray typedArray = getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
int colorAccent = typedArray.getColor(0, Color.GRAY); int colorAccent = typedArray.getColor(0, Color.GRAY);
@@ -213,10 +194,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
} }
// ===================== 空转日志输出工具 ===================== // ===================== 空转日志输出工具 =====================
/**
* 追加空转日志并自动滚动至底部
* @param logText 需要追加的日志文本
*/
private void appendIdleLog(final String logText) { private void appendIdleLog(final String logText) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@@ -233,11 +210,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
}); });
} }
/**
* 日志面板样式自动控制
* 非空转 = 清空日志 + 去除边框
* 空转开启 = 恢复边框
*/
private void refreshIdleLogLayout() { private void refreshIdleLogLayout() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@@ -245,19 +217,52 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
if (App.isAppIdleRunning()) { if (App.isAppIdleRunning()) {
mScrollIdleLog.setBackgroundResource(R.drawable.shape_log_border); mScrollIdleLog.setBackgroundResource(R.drawable.shape_log_border);
} else { } else {
//关闭空转时检测权限无权限强制关闭GPS开关
if(!checkLocationPermissions()){
mServiceSwitch.setChecked(false);
ServiceUtil.stopAutoService(MainActivity.this);
}
mTvIdleLog.setText(""); mTvIdleLog.setText("");
mScrollIdleLog.setBackgroundColor(Color.TRANSPARENT); mScrollIdleLog.setBackgroundColor(Color.TRANSPARENT);
LogUtils.d(TAG, "refreshIdleLogLayout -> 非空转状态:日志清空、边框已移除"); LogUtils.d(TAG, "refreshIdleLogLayout -> 非空转状态:日志清空、边框已移除");
} }
refreshToolbarSubTitle();
refreshManageButtonState();
}
});
}
private void refreshToolbarSubTitle() {
if(getSupportActionBar() == null){
return;
}
if(App.isAppIdleRunning()){
getSupportActionBar().setSubtitle("当前处于空转运行状态");
}else{
getSupportActionBar().setSubtitle("");
}
}
/**
* 空转状态专属:按钮强制可点击 + 文字追加提示
*/
private void refreshManageButtonState() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (App.isAppIdleRunning()) {
mManagePositionsButton.setText("位置与任务管理【当前空转中】");
mManagePositionsButton.setEnabled(true);
} else {
mManagePositionsButton.setText("位置与任务管理");
boolean serviceEnable = AppConfigsUtil.getInstance(MainActivity.this).isEnableMainService(true);
mManagePositionsButton.setEnabled(serviceEnable);
}
} }
}); });
} }
// ===================== 权限处理相关 ===================== // ===================== 权限处理相关 =====================
/**
* 检查前台+后台定位权限
* @return 权限是否全部通过
*/
private boolean checkLocationPermissions() { private boolean checkLocationPermissions() {
int foregroundPerm = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION); int foregroundPerm = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
boolean hasForegroundPerm = (foregroundPerm == PackageManager.PERMISSION_GRANTED); boolean hasForegroundPerm = (foregroundPerm == PackageManager.PERMISSION_GRANTED);
@@ -270,9 +275,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
return hasForegroundPerm && hasBackgroundPerm; return hasForegroundPerm && hasBackgroundPerm;
} }
/**
* 发起定位权限动态申请
*/
private void requestLocationPermissions() { private void requestLocationPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) {
@@ -301,6 +303,10 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
requestLocationPermissions(); requestLocationPermissions();
} else { } else {
Toast.makeText(this, "需要前台定位权限才能使用该功能", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "需要前台定位权限才能使用该功能", Toast.LENGTH_SHORT).show();
//权限被拒绝,强制关闭开关
mServiceSwitch.setChecked(false);
ServiceUtil.stopAutoService(MainActivity.this);
mManagePositionsButton.setEnabled(false);
} }
} else if (requestCode == REQUEST_BACKGROUND_LOCATION_PERMISSION) { } else if (requestCode == REQUEST_BACKGROUND_LOCATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
@@ -332,14 +338,12 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
} else if (DevelopUtils.onDevelopItemSelected(this, item)) { } else if (DevelopUtils.onDevelopItemSelected(this, item)) {
LogUtils.d(TAG, "onOptionsItemSelected -> 进入开发工具菜单"); LogUtils.d(TAG, "onOptionsItemSelected -> 进入开发工具菜单");
} else if (menuItemId == R.id.item_idle_switch) { } else if (menuItemId == R.id.item_idle_switch) {
// 菜单切换应用空转状态
boolean idleNow = App.isAppIdleRunning(); boolean idleNow = App.isAppIdleRunning();
boolean idleNew = !idleNow; boolean idleNew = !idleNow;
App.setAppIdleRunning(idleNew); App.setAppIdleRunning(idleNew);
AppIdleRunningModeHandler.sendIdleSwitch(idleNew); AppIdleRunningModeHandler.sendIdleSwitch(idleNew);
AppIdleRunningModeHandler.sendIdleLog("菜单手动切换空转状态:" + idleNew); AppIdleRunningModeHandler.sendIdleLog("菜单手动切换空转状态:" + idleNew);
LogUtils.d(TAG, "onOptionsItemSelected -> 空转状态已切换,当前:" + idleNew); LogUtils.d(TAG, "onOptionsItemSelected -> 空转状态已切换,当前:" + idleNew);
// 切换立刻刷新UI样式
refreshIdleLogLayout(); refreshIdleLogLayout();
} else if (menuItemId == R.id.item_settings) { } else if (menuItemId == R.id.item_settings) {
Intent intent = new Intent(); Intent intent = new Intent();
@@ -355,9 +359,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
return true; return true;
} }
/**
* 跳转位置管理页面
*/
public void onPositions(View view) { public void onPositions(View view) {
LogUtils.d(TAG, "onPositions -> 跳转位置任务管理页面"); LogUtils.d(TAG, "onPositions -> 跳转位置任务管理页面");
startActivity(new Intent(MainActivity.this, LocationActivity.class)); startActivity(new Intent(MainActivity.this, LocationActivity.class));