Skip to content

Commit 6ea8e21

Browse files
6.6.2 - Alpha7 - 新增 ui.statusBarHeight 属性 (getter) (issue #357)
1 parent 43d9fa9 commit 6ea8e21

File tree

8 files changed

+46
-39
lines changed

8 files changed

+46
-39
lines changed

.changelog/lang_zh-Hans.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"released_date": "2025/04/16",
55
"feature": [
66
"ui.statusBarAppearanceLight/statusBarAppearanceLightBy/navigationBarColor 等方法",
7+
"ui.statusBarHeight 属性 (getter), 用于获取状态栏高度 _[`issue #357`](http://issues.autojs6.com/357)_",
78
"images.flip 方法, 用于图像翻转 _[`issue #349`](http://issues.autojs6.com/349)_",
89
"设置页面增加 \"文件扩展名\" 设置选项",
910
"主题色设置页面增加新布局支持 (分组/定位/搜索/历史记录/调色盘增强等)"
@@ -148,7 +149,7 @@
148149
"text/button/input 元素的文本内容包含半角双引号时无法正常解析的问题",
149150
"text/textswitcher 元素的 autoLink 属性功能失效的问题",
150151
"不同脚本可能错误地共享同一个 ScriptRuntime 对象的问题",
151-
"全局变量 HEIGHT 及 WIDTH 丢失 Getter 动态属性的问题",
152+
"全局变量 HEIGHT 及 WIDTH 丢失 getter 动态属性的问题",
152153
"脚本启动时 RootShell 随即加载可能导致启动高延迟的问题",
153154
"控制台浮动窗口设置背景颜色导致矩形圆角样式丢失的问题",
154155
"无障碍服务自动启动可能出现的服务异常问题 (试修)",

app/src/main/java/org/autojs/autojs/execution/ScriptExecuteActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import android.view.MenuItem
1414
import android.view.MotionEvent
1515
import android.view.WindowManager
1616
import androidx.appcompat.app.AppCompatActivity
17+
import androidx.core.graphics.drawable.toDrawable
18+
import androidx.core.view.isNotEmpty
1719
import org.autojs.autojs.AbstractAutoJs.Companion.isInrt
1820
import org.autojs.autojs.annotation.ScriptInterface
1921
import org.autojs.autojs.core.eventloop.EventEmitter
@@ -65,7 +67,7 @@ class ScriptExecuteActivity : AppCompatActivity() {
6567
super.onCreate(savedInstanceState)
6668

6769
val windowBackground: Drawable = window.decorView.background
68-
?: ColorDrawable(getColor(R.color.md_gray_50)).also { window.setBackgroundDrawable(it) }
70+
?: getColor(R.color.md_gray_50).toDrawable().also { window.setBackgroundDrawable(it) }
6971

7072
if (windowBackground is ColorDrawable) windowBackground.color.let { backgroundColor ->
7173
val isLightColor = ViewUtils.isLuminanceDark(backgroundColor)
@@ -220,7 +222,7 @@ class ScriptExecuteActivity : AppCompatActivity() {
220222

221223
override fun onCreateOptionsMenu(menu: Menu): Boolean {
222224
emit("create_options_menu", menu)
223-
return menu.size() > 0
225+
return menu.isNotEmpty()
224226
}
225227

226228
override fun onOptionsItemSelected(item: MenuItem): Boolean {

app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.autojs.autojs.runtime.api.augment.ui
22

3-
import android.graphics.drawable.ColorDrawable
43
import android.os.Build
54
import android.view.ContextThemeWrapper
65
import android.view.View
76
import android.view.ViewGroup
7+
import androidx.core.graphics.drawable.toDrawable
88
import org.autojs.autojs.annotation.RhinoFunctionBody
99
import org.autojs.autojs.annotation.RhinoRuntimeFunctionInterface
1010
import org.autojs.autojs.core.ui.JsViewHelper
@@ -131,6 +131,9 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt
131131
val activity = getActivity(scriptRuntime)
132132
(activity as? ScriptExecuteActivity)?.eventEmitter
133133
},
134+
"statusBarHeight" to Supplier {
135+
ViewUtils.getStatusBarHeightByDimen(globalContext)
136+
}
134137
)
135138

136139
init {
@@ -555,7 +558,7 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt
555558
ensureActivity(scriptRuntime) { activity ->
556559
runRhinoRuntime(scriptRuntime, newBaseFunction("action", {
557560
Colors.toIntRhino(Colors.setAlphaRhino(color, 1.0)).also {
558-
activity.window.setBackgroundDrawable(ColorDrawable(it))
561+
activity.window.setBackgroundDrawable(it.toDrawable())
559562
}
560563
}, NOT_CONSTRUCTABLE))
561564
}

app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutBoundsView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class LayoutBoundsView : View {
5151

5252
init {
5353
this.setWillNotDraw(false)
54-
statusBarHeight = ViewUtils.getStatusBarHeight(context)
54+
statusBarHeight = ViewUtils.getStatusBarHeightByWindow(context)
5555
}
5656

5757
fun setOnNodeInfoSelectListener(onNodeInfoSelectListener: OnNodeInfoSelectListener?) {

app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutHierarchyView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private void initPaint() {
155155
mPaint.setStyle(Paint.Style.STROKE);
156156
mPaint.setAntiAlias(true);
157157
mPaint.setStrokeWidth(3);
158-
mStatusBarHeight = ViewUtils.getStatusBarHeight(getContext());
158+
mStatusBarHeight = ViewUtils.getStatusBarHeightByWindow(getContext());
159159
}
160160

161161
public Paint getBoundsPaint() {

app/src/main/java/org/autojs/autojs/ui/main/scripts/ExplorerFragment.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import android.view.LayoutInflater
99
import android.view.View
1010
import android.view.ViewGroup
1111
import androidx.coordinatorlayout.widget.CoordinatorLayout
12-
import androidx.core.view.ViewCompat
13-
import androidx.core.view.WindowInsetsCompat
1412
import androidx.core.view.marginBottom
1513
import com.google.android.material.floatingactionbutton.FloatingActionButton
1614
import com.google.android.material.tabs.TabLayout
@@ -50,8 +48,6 @@ class ExplorerFragment : ViewPagerFragment(0), OnFloatingActionButtonClickListen
5048
private var mFloatingActionMenu: FloatingActionMenu? = null
5149
private var mIsCurrentPageFiles = false
5250

53-
private var mBottomInset: Int = 0
54-
5551
override fun onCreate(savedInstanceState: Bundle?) {
5652
super.onCreate(savedInstanceState)
5753
EventBus.getDefault().register(this)
@@ -63,10 +59,6 @@ class ExplorerFragment : ViewPagerFragment(0), OnFloatingActionButtonClickListen
6359

6460
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
6561
super.onViewCreated(view, savedInstanceState)
66-
ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
67-
mBottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom
68-
insets
69-
}
7062
mExplorerView = binding.itemList.also { explorerView ->
7163
explorerView.setOnItemClickListener(object : ExplorerView.OnItemClickListener {
7264
override fun onItemClick(view: View?, item: ExplorerItem) {

app/src/main/java/org/autojs/autojs/util/ViewUtils.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import org.autojs.autojs.theme.ThemeColorManager
4848
import org.autojs.autojs.util.StringUtils.key
4949
import org.autojs.autojs6.R
5050
import kotlin.math.roundToInt
51+
import androidx.core.view.size
52+
import androidx.core.view.get
5153

5254
/**
5355
* Created by Stardust on Jan 24, 2017.
@@ -100,29 +102,40 @@ object ViewUtils {
100102
// ! 在一些设备上无法正常获取结果.
101103
// ! 参阅: https://github.com/hyb1996/Auto.js/issues/268
102104
@SuppressLint("InternalInsetResource", "DiscouragedApi")
103-
fun getStatusBarHeightLegacy(context: Context): Int {
104-
return context.resources.getIdentifier("status_bar_height", "dimen", "android").let { resourceId ->
105-
when (resourceId > 0) {
106-
true -> context.resources.getDimensionPixelSize(resourceId)
107-
else -> 0
108-
}
105+
@JvmStatic
106+
@JvmOverloads
107+
fun getStatusBarHeightByDimen(context: Context, withFallback: Boolean = true): Int {
108+
val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
109+
val dimenStatusBarHeight = when (resourceId > 0) {
110+
true -> context.resources.getDimensionPixelSize(resourceId)
111+
else -> 0
112+
}
113+
return when {
114+
dimenStatusBarHeight > 0 -> dimenStatusBarHeight
115+
withFallback -> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt()
116+
else -> 0
109117
}
110118
}
111119

112120
@JvmStatic
113-
fun getStatusBarHeight(context: Context): Int {
114-
return getWindowForContext(context)?.let { window ->
115-
Rect().let { rect ->
116-
window.decorView.getWindowVisibleDisplayFrame(rect)
117-
rect.top.takeIf { it >= 0 }
118-
}
119-
} ?: TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt()
121+
@JvmOverloads
122+
fun getStatusBarHeightByWindow(context: Context, withFallback: Boolean = true): Int {
123+
val windowStatusBarHeight = getWindowForContext(context)?.decorView?.let { decorView ->
124+
Rect().also { rect ->
125+
decorView.getWindowVisibleDisplayFrame(rect)
126+
}.top
127+
} ?: 0
128+
return when {
129+
windowStatusBarHeight > 0 -> windowStatusBarHeight
130+
withFallback -> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt()
131+
else -> 0
132+
}
120133
}
121134

122135
fun getTitleBarHeight(context: Context): Int {
123136
val window = getWindowForContext(context)
124137
val contentViewTop = window?.findViewById<View>(Window.ID_ANDROID_CONTENT)?.top ?: 0
125-
return contentViewTop - getStatusBarHeight(context)
138+
return contentViewTop - getStatusBarHeightByWindow(context)
126139
}
127140

128141
fun getScreenHeight(activity: Activity) = DisplayMetrics().let { metrics ->
@@ -415,8 +428,8 @@ object ViewUtils {
415428
}
416429

417430
fun Menu.setItemsColor(color: Int) {
418-
for (i in 0 until size()) {
419-
val menuItem = getItem(i)
431+
for (i in 0 until size) {
432+
val menuItem = this[i]
420433
menuItem.icon = menuItem.icon?.applyColorFilterWith(color)
421434
menuItem.subMenu?.setItemsColor(color)
422435
}
@@ -471,11 +484,7 @@ object ViewUtils {
471484
val view = this
472485
val viewTreeObserver = view.viewTreeObserver
473486
if (viewTreeObserver.isAlive) {
474-
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
475-
override fun onGlobalLayout() {
476-
listener.invoke()
477-
}
478-
})
487+
viewTreeObserver.addOnGlobalLayoutListener { listener.invoke() }
479488
}
480489
}
481490

@@ -508,7 +517,7 @@ object ViewUtils {
508517
val fullColor = context.getColor(if (isAimColorLight) R.color.day_full else R.color.night_full)
509518
val hintColor = context.getColor(if (isAimColorLight) R.color.day_alpha_70 else R.color.night_alpha_70)
510519

511-
findViewById<EditText?>(androidx.appcompat.R.id.search_src_text).apply {
520+
findViewById<EditText?>(androidx.appcompat.R.id.search_src_text)?.apply {
512521
setTextColor(fullColor)
513522
setHintTextColor(hintColor)
514523
setLinkTextColor(fullColor)

version.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#Wed Apr 16 14:34:09 CST 2025
2-
BUILD_TIME=1744785249350
1+
#Wed Apr 16 16:02:18 CST 2025
2+
BUILD_TIME=1744790538995
33
COMPILE_SDK_VERSION=35
44
JAVA_VERSION=23
55
JAVA_VERSION_MIN_RADICAL=0

0 commit comments

Comments
 (0)