优化:进入通用设置再次检查是否已授权已开启功能必需权限

This commit is contained in:
pppscn 2024-03-14 11:36:29 +08:00
parent d3899d9404
commit 22474b2295
8 changed files with 96 additions and 80 deletions

View File

@ -349,7 +349,6 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//转发短信
@SuppressLint("UseSwitchCompatOrMaterialCode")
private fun switchEnableSms(sbEnableSms: SwitchButton) {
sbEnableSms.isChecked = SettingUtils.enableSms
sbEnableSms.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
SettingUtils.enableSms = isChecked
if (isChecked) {
@ -364,22 +363,23 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
// 发送短信
//.permission(Permission.SEND_SMS)
// 读取短信
.permission(Permission.READ_SMS).request(object : OnPermissionCallback {
.permission(Permission.READ_SMS)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
if (all) {
XToastUtils.info(R.string.toast_granted_all)
} else {
XToastUtils.info(R.string.toast_granted_part)
Log.d(TAG, "onGranted: permissions=$permissions, all=$all")
if (!all) {
XToastUtils.warning(getString(R.string.forward_sms) + ": " + getString(R.string.toast_granted_part))
}
}
override fun onDenied(permissions: List<String>, never: Boolean) {
Log.e(TAG, "onDenied: permissions=$permissions, never=$never")
if (never) {
XToastUtils.info(R.string.toast_denied_never)
XToastUtils.error(getString(R.string.forward_sms) + ": " + getString(R.string.toast_denied_never))
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.info(R.string.toast_denied)
XToastUtils.error(getString(R.string.forward_sms) + ": " + getString(R.string.toast_denied))
}
SettingUtils.enableSms = false
sbEnableSms.isChecked = false
@ -387,12 +387,12 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
})
}
}
sbEnableSms.isChecked = SettingUtils.enableSms
}
//转发通话
@SuppressLint("UseSwitchCompatOrMaterialCode")
private fun switchEnablePhone(sbEnablePhone: SwitchButton, scbCallType1: SmoothCheckBox, scbCallType2: SmoothCheckBox, scbCallType3: SmoothCheckBox, scbCallType4: SmoothCheckBox, scbCallType5: SmoothCheckBox, scbCallType6: SmoothCheckBox) {
sbEnablePhone.isChecked = SettingUtils.enablePhone
scbCallType1.isChecked = SettingUtils.enableCallType1
scbCallType2.isChecked = SettingUtils.enableCallType2
scbCallType3.isChecked = SettingUtils.enableCallType3
@ -417,22 +417,23 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
// 读取通话记录
.permission(Permission.READ_CALL_LOG)
// 读取联系人
.permission(Permission.READ_CONTACTS).request(object : OnPermissionCallback {
.permission(Permission.READ_CONTACTS)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
if (all) {
XToastUtils.info(R.string.toast_granted_all)
} else {
XToastUtils.info(R.string.toast_granted_part)
Log.d(TAG, "onGranted: permissions=$permissions, all=$all")
if (!all) {
XToastUtils.warning(getString(R.string.forward_calls) + ": " + getString(R.string.toast_granted_part))
}
}
override fun onDenied(permissions: List<String>, never: Boolean) {
Log.e(TAG, "onDenied: permissions=$permissions, never=$never")
if (never) {
XToastUtils.info(R.string.toast_denied_never)
XToastUtils.error(getString(R.string.forward_calls) + ": " + getString(R.string.toast_denied_never))
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.info(R.string.toast_denied)
XToastUtils.error(getString(R.string.forward_calls) + ": " + getString(R.string.toast_denied))
}
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false
@ -440,6 +441,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
})
}
}
sbEnablePhone.isChecked = SettingUtils.enablePhone
scbCallType1.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCallType1 = isChecked
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4 && !SettingUtils.enableCallType5 && !SettingUtils.enableCallType6) {
@ -493,34 +495,32 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//转发应用通知
@SuppressLint("UseSwitchCompatOrMaterialCode")
private fun switchEnableAppNotify(sbEnableAppNotify: SwitchButton, scbCancelAppNotify: SmoothCheckBox, scbNotUserPresent: SmoothCheckBox) {
val isEnable: Boolean = SettingUtils.enableAppNotify
sbEnableAppNotify.isChecked = isEnable
val layoutOptionalAction: LinearLayout = binding!!.layoutOptionalAction
layoutOptionalAction.visibility = if (isEnable) View.VISIBLE else View.GONE
//val layoutAppList: LinearLayout = binding!!.layoutAppList
//layoutAppList.visibility = if (isEnable) View.VISIBLE else View.GONE
sbEnableAppNotify.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
layoutOptionalAction.visibility = if (isChecked) View.VISIBLE else View.GONE
//layoutAppList.visibility = if (isChecked) View.VISIBLE else View.GONE
binding!!.layoutOptionalAction.visibility = if (isChecked) View.VISIBLE else View.GONE
SettingUtils.enableAppNotify = isChecked
if (isChecked) {
//检查权限是否获取
XXPermissions.with(this).permission(Permission.BIND_NOTIFICATION_LISTENER_SERVICE).request(OnPermissionCallback { _, allGranted ->
if (!allGranted) {
SettingUtils.enableAppNotify = false
sbEnableAppNotify.isChecked = false
XToastUtils.error(R.string.tips_notification_listener)
return@OnPermissionCallback
}
XXPermissions.with(this)
.permission(Permission.BIND_NOTIFICATION_LISTENER_SERVICE)
.request(OnPermissionCallback { permissions, allGranted ->
if (!allGranted) {
Log.e(TAG, "onGranted: permissions=$permissions, allGranted=false")
SettingUtils.enableAppNotify = false
sbEnableAppNotify.isChecked = false
XToastUtils.error(R.string.tips_notification_listener)
return@OnPermissionCallback
}
SettingUtils.enableAppNotify = true
sbEnableAppNotify.isChecked = true
CommonUtils.toggleNotificationListenerService(requireContext())
})
SettingUtils.enableAppNotify = true
sbEnableAppNotify.isChecked = true
CommonUtils.toggleNotificationListenerService(requireContext())
})
}
}
val isEnable = SettingUtils.enableAppNotify
sbEnableAppNotify.isChecked = isEnable
binding!!.layoutOptionalAction.visibility = if (isEnable) View.VISIBLE else View.GONE
scbCancelAppNotify.isChecked = SettingUtils.enableCancelAppNotify
scbCancelAppNotify.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCancelAppNotify = isChecked
@ -533,35 +533,44 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//启用定位功能
private fun switchEnableLocation(@SuppressLint("UseSwitchCompatOrMaterialCode") sbEnableLocation: SwitchButton, layoutLocationSetting: LinearLayout, rgAccuracy: RadioGroup, rgPowerRequirement: RadioGroup, xsbMinInterval: XSeekBar, xsbMinDistance: XSeekBar) {
//是否启用定位功能
sbEnableLocation.isChecked = SettingUtils.enableLocation
layoutLocationSetting.visibility = if (SettingUtils.enableLocation) View.VISIBLE else View.GONE
sbEnableLocation.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
SettingUtils.enableLocation = isChecked
layoutLocationSetting.visibility = if (isChecked) View.VISIBLE else View.GONE
if (isChecked) {
XXPermissions.with(this).permission(Permission.ACCESS_COARSE_LOCATION).permission(Permission.ACCESS_FINE_LOCATION).permission(Permission.ACCESS_BACKGROUND_LOCATION).request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
restartLocationService("START")
}
override fun onDenied(permissions: List<String>, never: Boolean) {
if (never) {
XToastUtils.error(R.string.toast_denied_never)
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.error(R.string.toast_denied)
XXPermissions.with(this)
.permission(Permission.ACCESS_COARSE_LOCATION)
.permission(Permission.ACCESS_FINE_LOCATION)
.permission(Permission.ACCESS_BACKGROUND_LOCATION)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
Log.d(TAG, "onGranted: permissions=$permissions, all=$all")
if (!all) {
XToastUtils.warning(getString(R.string.enable_location) + ": " + getString(R.string.toast_granted_part))
}
restartLocationService("START")
}
SettingUtils.enableLocation = false
sbEnableLocation.isChecked = false
restartLocationService("STOP")
}
})
override fun onDenied(permissions: List<String>, never: Boolean) {
Log.e(TAG, "onDenied: permissions=$permissions, never=$never")
if (never) {
XToastUtils.error(getString(R.string.enable_location) + ": " + getString(R.string.toast_denied_never))
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.error(getString(R.string.enable_location) + ": " + getString(R.string.toast_denied))
}
SettingUtils.enableLocation = false
sbEnableLocation.isChecked = false
restartLocationService("STOP")
}
})
} else {
restartLocationService("STOP")
}
}
val isEnable = SettingUtils.enableLocation
sbEnableLocation.isChecked = isEnable
layoutLocationSetting.visibility = if (isEnable) View.VISIBLE else View.GONE
//设置位置精度:高精度(默认)
rgAccuracy.check(
@ -643,9 +652,6 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//接受短信指令
@SuppressLint("UseSwitchCompatOrMaterialCode")
private fun switchEnableSmsCommand(sbEnableSmsCommand: SwitchButton, etSafePhone: EditText) {
sbEnableSmsCommand.isChecked = SettingUtils.enableSmsCommand
etSafePhone.visibility = if (SettingUtils.enableSmsCommand) View.VISIBLE else View.GONE
sbEnableSmsCommand.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
SettingUtils.enableSmsCommand = isChecked
etSafePhone.visibility = if (isChecked) View.VISIBLE else View.GONE
@ -659,22 +665,21 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
// 发送短信
.permission(Permission.SEND_SMS)
// 读取短信
.permission(Permission.READ_SMS).request(object : OnPermissionCallback {
.permission(Permission.READ_SMS)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
if (all) {
XToastUtils.info(R.string.toast_granted_all)
} else {
XToastUtils.info(R.string.toast_granted_part)
if (!all) {
XToastUtils.warning(getString(R.string.sms_command) + ": " + getString(R.string.toast_denied_never))
}
}
override fun onDenied(permissions: List<String>, never: Boolean) {
if (never) {
XToastUtils.info(R.string.toast_denied_never)
XToastUtils.error(getString(R.string.sms_command) + ": " + getString(R.string.toast_denied_never))
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.info(R.string.toast_denied)
XToastUtils.error(getString(R.string.sms_command) + ": " + getString(R.string.toast_denied))
}
SettingUtils.enableSmsCommand = false
sbEnableSmsCommand.isChecked = false
@ -682,6 +687,9 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
})
}
}
val isEnable = SettingUtils.enableSmsCommand
sbEnableSmsCommand.isChecked = isEnable
etSafePhone.visibility = if (isEnable) View.VISIBLE else View.GONE
etSafePhone.setText(SettingUtils.smsCommandSafePhone)
etSafePhone.addTextChangedListener(object : TextWatcher {

View File

@ -430,7 +430,7 @@ class SettingsFragment : BaseFragment<FragmentTasksActionSettingsBinding?>(), Vi
if (enableSms) enableList.add(getString(R.string.forward_sms)) else disableList.add(getString(R.string.forward_sms))
val enablePhone = binding!!.sbEnablePhone.isChecked
if (enablePhone) enableList.add(getString(R.string.forward_missed_calls)) else disableList.add(getString(R.string.forward_missed_calls))
if (enablePhone) enableList.add(getString(R.string.forward_calls)) else disableList.add(getString(R.string.forward_calls))
val enableCallType1 = binding!!.scbCallType1.isChecked
val enableCallType2 = binding!!.scbCallType2.isChecked
val enableCallType3 = binding!!.scbCallType3.isChecked

View File

@ -86,13 +86,13 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/forward_missed_calls"
android:text="@string/forward_calls"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/forward_missed_calls_tips"
android:text="@string/forward_calls_tips"
android:textSize="@dimen/text_size_mini"
tools:ignore="SmallSp" />

View File

@ -96,13 +96,13 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/forward_missed_calls"
android:text="@string/forward_calls"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/forward_missed_calls_tips"
android:text="@string/forward_calls_tips"
android:textSize="@dimen/text_size_mini"
tools:ignore="SmallSp" />

View File

@ -386,8 +386,8 @@
<string name="sms_command_tips">Open the HttpServer or FRPC by the SMS command</string>
<string name="safe_phone">Safe Phone</string>
<string name="safe_phone_tips">Only handle requests from specified phones</string>
<string name="forward_missed_calls">Forward Calls Log</string>
<string name="forward_missed_calls_tips">Main switch, requires permissions to read call log and contacts.</string>
<string name="forward_calls">Forward Calls Log</string>
<string name="forward_calls_tips">Main switch, requires permissions to read call log and contacts.</string>
<string name="forward_app_notify">Forward App Notify</string>
<string name="forward_app_notify_tips">Main switch, requires permission to read notification.</string>
<string name="cancel_app_notify">Auto close Ntf.</string>
@ -1206,6 +1206,8 @@
<string name="task_app">Notification</string>
<string name="task_app_tips">Triggered upon receiving app notification</string>
<string name="task_app_when">received app notification</string>
<string name="task_bluetooth">Bluetooth Device</string>
<string name="task_bluetooth_tips">Triggered upon changes in Bluetooth status</string>
<string name="task_sendsms">Send Sms</string>
<string name="task_notification">Notify</string>
<string name="task_frpc">Frpc On/Off</string>

View File

@ -387,8 +387,8 @@
<string name="sms_command_tips">根据短信指令开关对应功能指令格式smsf#功能名#动作名</string>
<string name="safe_phone">安全手机</string>
<string name="safe_phone_tips">仅处理指定手机请求,多个手机以逗号分隔</string>
<string name="forward_missed_calls">转发通话记录</string>
<string name="forward_missed_calls_tips">请授予读取通话记录、联系人等权限,并选择转发类型,再开启</string>
<string name="forward_calls">转发通话记录</string>
<string name="forward_calls_tips">请授予读取通话记录、联系人等权限,并选择转发类型,再开启</string>
<string name="forward_app_notify">转发应用通知</string>
<string name="forward_app_notify_tips">请授予通知使用权再开启;开启“自动消除”可避免多条通知堆叠</string>
<string name="cancel_app_notify">自动消除通知</string>
@ -1207,6 +1207,8 @@
<string name="task_app">APP通知</string>
<string name="task_app_tips">在接收到APP通知时触发</string>
<string name="task_app_when">接收到APP通知</string>
<string name="task_bluetooth">蓝牙设备</string>
<string name="task_bluetooth_tips">在蓝牙状态发生改变时触发</string>
<string name="task_sendsms">发送短信</string>
<string name="task_notification">推送通知</string>
<string name="task_frpc">启停Frpc</string>

View File

@ -387,8 +387,8 @@
<string name="sms_command_tips">根據簡訊指令開關對應功能指令格式smsf#功能名#動作名</string>
<string name="safe_phone">安全手機</string>
<string name="safe_phone_tips">僅處理指定手機請求,多個手機以逗號分隔</string>
<string name="forward_missed_calls">轉發通話記錄</string>
<string name="forward_missed_calls_tips">請授予讀取通話記錄、聯繫人等權限,並選擇轉發類型,再開啟</string>
<string name="forward_calls">轉發通話記錄</string>
<string name="forward_calls_tips">請授予讀取通話記錄、聯繫人等權限,並選擇轉發類型,再開啟</string>
<string name="forward_app_notify">轉發應用通知</string>
<string name="forward_app_notify_tips">請授予通知使用權再開啟;開啟“自動消除”可避免多條通知堆疊</string>
<string name="cancel_app_notify">自動消除通知</string>
@ -1207,6 +1207,8 @@
<string name="task_app">APP通知</string>
<string name="task_app_tips">在接收到APP通知時觸發</string>
<string name="task_app_when">接收到APP通知</string>
<string name="task_bluetooth">蓝牙设备</string>
<string name="task_bluetooth_tips">在蓝牙状态发生改变时触发</string>
<string name="task_sendsms">發送簡訊</string>
<string name="task_notification">推送通知</string>
<string name="task_frpc">啟停Frpc</string>

View File

@ -387,8 +387,8 @@
<string name="sms_command_tips">根据短信指令开关对应功能指令格式smsf#功能名#动作名</string>
<string name="safe_phone">安全手机</string>
<string name="safe_phone_tips">仅处理指定手机请求,多个手机以逗号分隔</string>
<string name="forward_missed_calls">转发通话记录</string>
<string name="forward_missed_calls_tips">请授予读取通话记录、联系人等权限,并选择转发类型,再开启</string>
<string name="forward_calls">转发通话记录</string>
<string name="forward_calls_tips">请授予读取通话记录、联系人等权限,并选择转发类型,再开启</string>
<string name="forward_app_notify">转发应用通知</string>
<string name="forward_app_notify_tips">请授予通知使用权再开启;开启“自动消除”可避免多条通知堆叠</string>
<string name="cancel_app_notify">自动消除通知</string>
@ -1207,6 +1207,8 @@
<string name="task_app">APP通知</string>
<string name="task_app_tips">在接收到APP通知时触发</string>
<string name="task_app_when">接收到APP通知</string>
<string name="task_bluetooth">蓝牙设备</string>
<string name="task_bluetooth_tips">在蓝牙状态发生改变时触发</string>
<string name="task_sendsms">发送短信</string>
<string name="task_notification">推送通知</string>
<string name="task_frpc">启停Frpc</string>