锁屏权限引导:设置页新增锁屏显示权限入口一键跳应用启动管理,版本 1.0
This commit is contained in:
@@ -94,6 +94,9 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
toggle(getString(R.string.vibrate), Prefs.vibrateOn) { Prefs.vibrateOn = it },
|
||||
toggle(getString(R.string.keep_screen_on), Prefs.keepScreenOn) { Prefs.keepScreenOn = it },
|
||||
toggle(getString(R.string.lock_fullscreen_title), Prefs.lockFullscreen) { Prefs.lockFullscreen = it },
|
||||
// 华为/鸿蒙默认拦截后台拉起页面,锁屏倒计时需要手动开两个权限;
|
||||
// 放在开关正下方,用户开了开关不生效时第一眼就能看到入口
|
||||
action(getString(R.string.lock_perm_title), getString(R.string.lock_perm_value)) { openLockPerm() },
|
||||
)
|
||||
|
||||
section(R.string.section_goal)
|
||||
@@ -316,6 +319,60 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
/** 锁屏显示权限:弹说明后直达华为「应用启动管理」,非华为/直达失败退回应用详情页 */
|
||||
private fun openLockPerm() {
|
||||
val ctx = requireContext()
|
||||
val dialog = android.app.Dialog(ctx)
|
||||
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
|
||||
|
||||
val panel = LinearLayout(ctx).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f)
|
||||
setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f))
|
||||
}
|
||||
panel.addView(TextView(ctx).apply {
|
||||
text = getString(R.string.lock_perm_title)
|
||||
textSize = 16f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
setTextColor(col(R.color.text))
|
||||
})
|
||||
panel.addView(TextView(ctx).apply {
|
||||
text = getString(R.string.lock_perm_hint)
|
||||
textSize = 14f
|
||||
setTextColor(col(R.color.text_secondary))
|
||||
setPadding(0, dpi(10f), 0, dpi(6f))
|
||||
})
|
||||
|
||||
val btnRow = LinearLayout(ctx).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.END
|
||||
setPadding(0, dpi(12f), 0, 0)
|
||||
}
|
||||
fun btn(labelRes: Int, onClick: () -> Unit) = TextView(ctx).apply {
|
||||
text = getString(labelRes)
|
||||
textSize = 14f
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
gravity = Gravity.CENTER
|
||||
setTextColor(col(R.color.brand))
|
||||
background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f)
|
||||
setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f))
|
||||
setOnClickListener { onClick() }
|
||||
}
|
||||
btnRow.addView(btn(R.string.cancel) { dialog.dismiss() })
|
||||
btnRow.addView(btn(R.string.go_set) {
|
||||
dialog.dismiss()
|
||||
if (!com.focusbox.app.util.HuaweiPerms.openStartupManager(ctx)) {
|
||||
com.focusbox.app.util.HuaweiPerms.openAppDetails(ctx)
|
||||
}
|
||||
}, LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
|
||||
marginStart = dpi(10f)
|
||||
})
|
||||
panel.addView(btnRow)
|
||||
|
||||
dialog.setContentView(panel)
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun confirmClear() {
|
||||
val ctx = requireContext()
|
||||
val dialog = android.app.Dialog(ctx)
|
||||
@@ -374,6 +431,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val VERSION = "0.17"
|
||||
const val VERSION = "1.0"
|
||||
}
|
||||
}
|
||||
|
||||
60
src/main/java/com/focusbox/app/util/HuaweiPerms.kt
Normal file
60
src/main/java/com/focusbox/app/util/HuaweiPerms.kt
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
|
||||
/**
|
||||
* 华为/鸿蒙权限引导。
|
||||
*
|
||||
* 锁屏全屏倒计时依赖前台服务直接 startActivity 拉起 LockScreenActivity,
|
||||
* 而 EMUI/鸿蒙默认拦截"后台弹出界面"(拦截是静默的,runCatching 不报错,
|
||||
* 只剩全屏意图通知降级成横幅)。这两个开关藏在
|
||||
* 设置→应用→应用启动管理→(关闭自动管理)里,没有公开 API 可查询/授予权限,
|
||||
* 只能引导用户手动去开。
|
||||
*/
|
||||
object HuaweiPerms {
|
||||
|
||||
/**
|
||||
* 尝试直达华为手机管家的「应用启动管理」页。
|
||||
* 不同鸿蒙版本类名有差异,按候选顺序试,全部失败返回 false。
|
||||
*/
|
||||
fun openStartupManager(context: Context): Boolean {
|
||||
val pm = context.packageManager
|
||||
val candidates = listOf(
|
||||
ComponentName(
|
||||
"com.huawei.systemmanager",
|
||||
"com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity",
|
||||
),
|
||||
ComponentName(
|
||||
"com.huawei.systemmanager",
|
||||
"com.huawei.systemmanager.startupmgr.ui.StartupAppControlActivity",
|
||||
),
|
||||
)
|
||||
for (cn in candidates) {
|
||||
runCatching {
|
||||
pm.getActivityInfo(cn, 0)
|
||||
context.startActivity(
|
||||
Intent().setComponent(cn)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** 兜底:系统应用详情页(里面有"应用启动管理"入口),必成功 */
|
||||
fun openAppDetails(context: Context) {
|
||||
runCatching {
|
||||
context.startActivity(
|
||||
Intent(
|
||||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||
Uri.fromParts("package", context.packageName, null),
|
||||
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,6 +188,10 @@
|
||||
<string name="vibrate">振动</string>
|
||||
<string name="keep_screen_on">计时时保持屏幕常亮</string>
|
||||
<string name="lock_fullscreen_title">锁屏全屏倒计时</string>
|
||||
<string name="lock_perm_title">锁屏显示权限</string>
|
||||
<string name="lock_perm_value">去开启</string>
|
||||
<string name="go_set">去设置</string>
|
||||
<string name="lock_perm_hint">华为/鸿蒙默认拦截后台拉起页面,锁屏倒计时需要手动开启。路径:设置 → 应用 → 应用启动管理 → 时间简史 → 关闭「自动管理」,并允许「后台弹出界面」和「锁屏显示」。点「去设置」可直接打开对应页面。</string>
|
||||
<string name="lock_mode_focus">工作中</string>
|
||||
<string name="lock_mode_break">休息中</string>
|
||||
<string name="daily_goal">每日目标</string>
|
||||
|
||||
Reference in New Issue
Block a user