修复Android 11 适配问题

This commit is contained in:
董宏宇
2021-08-06 20:55:06 +08:00
parent dbbf22b816
commit 9810e1844e
3 changed files with 88 additions and 80 deletions

View File

@@ -11,7 +11,7 @@ adb shell
// (新的HMI)使用命令行触发 V2X 预警场景
// 顶部弹窗场景,控制展示
adb shell am broadcast -a com.hmi.v2x.notification --ez v2xIsShow true --es tag "20008" --ei v2xType 20008 --es alertContent "测试外部传入数据" --es ttsContent "测试TTS"
adb shell am broadcast -a com.hmi.v2x.notification --ez v2xIsShow true --es tag "200011" --ei v2xType 20011 --es alertContent "测试外部传入数据" --es ttsContent "测试TTS"
// 关闭顶部弹窗
adb shell am broadcast -a com.hmi.v2x.notification --ez v2xIsShow false --es tag "20008"
@@ -32,3 +32,6 @@ adb shell am broadcast -a com.v2x.adas_data_broadcast --es ADASData right
// 调用应用内的模拟
adb shell am broadcast -a com.mogo.mock --ei oper 46
// 小智语音
adb shell am broadcast -a com.zhidao.auto.txz.receiver --es text "欢迎乘坐’蘑菇车联‘无人驾驶小巴车" --ei speakType 5

View File

@@ -70,79 +70,6 @@ internal class WarningFloatWindowHelper(
}
}
/**
* 设置浮窗的对齐方式,支持上下左右、居中、上中、下中、左中和右中,默认左上角
* 支持手动设置的偏移量
*/
@SuppressLint("RtlHardcoded")
private fun setGravity(view: View?) {
if (config.locationPair != Pair(0, 0) || view == null) return
val parentRect = Rect()
// 获取浮窗所在的矩形
windowManager.defaultDisplay.getRectSize(parentRect)
val location = IntArray(2)
// 获取在整个屏幕内的绝对坐标
view.getLocationOnScreen(location)
// 通过绝对高度和相对高度比较,判断包含顶部状态栏
val statusBarHeight =
if (location[1] > params.y) WindowUtils.getStatusBarHeight(view.context.applicationContext) else 0
val parentBottom =
WindowUtils.getScreenHeight(view.context.applicationContext) - statusBarHeight
when (config.gravity) {
// 右上
Gravity.END, Gravity.END or Gravity.TOP, Gravity.RIGHT, Gravity.RIGHT or Gravity.TOP ->
params.x = parentRect.right - view.width
// 左下
Gravity.START or Gravity.BOTTOM, Gravity.BOTTOM, Gravity.LEFT or Gravity.BOTTOM ->
params.y = parentBottom - view.height
// 右下
Gravity.END or Gravity.BOTTOM, Gravity.RIGHT or Gravity.BOTTOM -> {
params.x = parentRect.right - view.width
params.y = parentBottom - view.height
}
// 居中
Gravity.CENTER -> {
params.x = (parentRect.right - view.width).shr(1)
params.y = (parentBottom - view.height).shr(1)
}
// 上中
Gravity.CENTER_HORIZONTAL, Gravity.TOP or Gravity.CENTER_HORIZONTAL ->
params.x = (parentRect.right - view.width).shr(1)
// 下中
Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL -> {
params.x = (parentRect.right - view.width).shr(1)
params.y = parentBottom - view.height
}
// 左中
Gravity.CENTER_VERTICAL, Gravity.START or Gravity.CENTER_VERTICAL, Gravity.LEFT or Gravity.CENTER_VERTICAL ->
params.y = (parentBottom - view.height).shr(1)
// 右中
Gravity.END or Gravity.CENTER_VERTICAL, Gravity.RIGHT or Gravity.CENTER_VERTICAL -> {
params.x = parentRect.right - view.width
params.y = (parentBottom - view.height).shr(1)
}
// 其他情况,均视为左上
else -> {
}
}
// 设置偏移量
params.x += config.offsetPair.first
params.y += config.offsetPair.second
if (config.immersionStatusBar) {
if (config.showPattern != ShowPattern.CURRENT_ACTIVITY) {
params.y -= statusBarHeight
}
} else {
if (config.showPattern == ShowPattern.CURRENT_ACTIVITY) {
params.y += statusBarHeight
}
}
// 更新浮窗位置信息
windowManager.updateViewLayout(view, params)
}
/**
* 添加视图
*/
@@ -162,12 +89,14 @@ internal class WarningFloatWindowHelper(
frameLayout?.layoutListener = object : ParentFrameLayout.OnLayoutListener {
override fun onLayout() {
setGravity(frameLayout)
config.apply {
enterAnim(floatingView)
// 设置callbacks
layoutView = floatingView
callbacks?.createdResult(true, null, floatingView)
}
frameLayout?.postDelayed({
config.apply {
enterAnim(floatingView)
// 设置callbacks
layoutView = floatingView
callbacks?.createdResult(true, null, floatingView)
}
}, 100)
}
}
@@ -263,4 +192,79 @@ internal class WarningFloatWindowHelper(
Logger.e(TAG, "浮窗关闭出现异常:$e")
}
/**
* 设置浮窗的对齐方式,支持上下左右、居中、上中、下中、左中和右中,默认左上角
* 支持手动设置的偏移量
*/
@SuppressLint("RtlHardcoded")
private fun setGravity(view: View?) {
if (config.locationPair != Pair(0, 0) || view == null) return
val parentRect = Rect()
// 获取浮窗所在的矩形
windowManager.defaultDisplay.getRectSize(parentRect)
val location = IntArray(2)
// 获取在整个屏幕内的绝对坐标
view.getLocationOnScreen(location)
// 通过绝对高度和相对高度比较,判断包含顶部状态栏
val statusBarHeight =
if (location[1] > params.y) WindowUtils.getStatusBarHeight(view.context.applicationContext) else 0
val parentBottom =
WindowUtils.getScreenHeight(view.context.applicationContext) - statusBarHeight
when (config.gravity) {
// 右上
Gravity.END, Gravity.END or Gravity.TOP, Gravity.RIGHT, Gravity.RIGHT or Gravity.TOP ->
params.x = parentRect.right - view.width
// 左下
Gravity.START or Gravity.BOTTOM, Gravity.BOTTOM, Gravity.LEFT or Gravity.BOTTOM ->
params.y = parentBottom - view.height
// 右下
Gravity.END or Gravity.BOTTOM, Gravity.RIGHT or Gravity.BOTTOM -> {
params.x = parentRect.right - view.width
params.y = parentBottom - view.height
}
// 居中
Gravity.CENTER -> {
params.x = (parentRect.right - view.width).shr(1)
params.y = (parentBottom - view.height).shr(1)
}
// 上中
Gravity.CENTER_HORIZONTAL, Gravity.TOP or Gravity.CENTER_HORIZONTAL ->
params.x = (parentRect.right - view.width).shr(1)
// 下中
Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL -> {
params.x = (parentRect.right - view.width).shr(1)
params.y = parentBottom - view.height
}
// 左中
Gravity.CENTER_VERTICAL, Gravity.START or Gravity.CENTER_VERTICAL, Gravity.LEFT or Gravity.CENTER_VERTICAL ->
params.y = (parentBottom - view.height).shr(1)
// 右中
Gravity.END or Gravity.CENTER_VERTICAL, Gravity.RIGHT or Gravity.CENTER_VERTICAL -> {
params.x = parentRect.right - view.width
params.y = (parentBottom - view.height).shr(1)
}
// 其他情况,均视为左上
else -> {
}
}
// 设置偏移量
params.x += config.offsetPair.first
params.y += config.offsetPair.second
if (config.immersionStatusBar) {
if (config.showPattern != ShowPattern.CURRENT_ACTIVITY) {
params.y -= statusBarHeight
}
} else {
if (config.showPattern == ShowPattern.CURRENT_ACTIVITY) {
params.y += statusBarHeight
}
}
// 更新浮窗位置信息
windowManager.updateViewLayout(view, params)
}
}

View File

@@ -64,6 +64,7 @@ class MoGoWarningFragment : MvpFragment<MoGoWarningContract.View?, WaringPresent
.setLayout(notificationView)
.setSidePattern(SidePattern.TOP)
.setGravity(Gravity.CENTER_HORIZONTAL, offsetY = 110)
.setImmersionStatusBar(true)
.setAnimator(object : DefaultAnimator() {
override fun enterAnim(
view: View,