diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index 990ee696..3c743215 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -4,6 +4,7 @@ "released_date": "2025/04/16", "feature": [ "ui.statusBarAppearanceLight/statusBarAppearanceLightBy/navigationBarColor 等方法", + "ui.statusBarHeight 属性 (getter), 用于获取状态栏高度 _[`issue #357`](http://issues.autojs6.com/357)_", "images.flip 方法, 用于图像翻转 _[`issue #349`](http://issues.autojs6.com/349)_", "设置页面增加 \"文件扩展名\" 设置选项", "主题色设置页面增加新布局支持 (分组/定位/搜索/历史记录/调色盘增强等)" @@ -148,7 +149,7 @@ "text/button/input 元素的文本内容包含半角双引号时无法正常解析的问题", "text/textswitcher 元素的 autoLink 属性功能失效的问题", "不同脚本可能错误地共享同一个 ScriptRuntime 对象的问题", - "全局变量 HEIGHT 及 WIDTH 丢失 Getter 动态属性的问题", + "全局变量 HEIGHT 及 WIDTH 丢失 getter 动态属性的问题", "脚本启动时 RootShell 随即加载可能导致启动高延迟的问题", "控制台浮动窗口设置背景颜色导致矩形圆角样式丢失的问题", "无障碍服务自动启动可能出现的服务异常问题 (试修)", diff --git a/app/src/main/java/org/autojs/autojs/execution/ScriptExecuteActivity.kt b/app/src/main/java/org/autojs/autojs/execution/ScriptExecuteActivity.kt index 9fa02dbb..d915222d 100644 --- a/app/src/main/java/org/autojs/autojs/execution/ScriptExecuteActivity.kt +++ b/app/src/main/java/org/autojs/autojs/execution/ScriptExecuteActivity.kt @@ -14,6 +14,8 @@ import android.view.MenuItem import android.view.MotionEvent import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity +import androidx.core.graphics.drawable.toDrawable +import androidx.core.view.isNotEmpty import org.autojs.autojs.AbstractAutoJs.Companion.isInrt import org.autojs.autojs.annotation.ScriptInterface import org.autojs.autojs.core.eventloop.EventEmitter @@ -65,7 +67,7 @@ class ScriptExecuteActivity : AppCompatActivity() { super.onCreate(savedInstanceState) val windowBackground: Drawable = window.decorView.background - ?: ColorDrawable(getColor(R.color.md_gray_50)).also { window.setBackgroundDrawable(it) } + ?: getColor(R.color.md_gray_50).toDrawable().also { window.setBackgroundDrawable(it) } if (windowBackground is ColorDrawable) windowBackground.color.let { backgroundColor -> val isLightColor = ViewUtils.isLuminanceDark(backgroundColor) @@ -220,7 +222,7 @@ class ScriptExecuteActivity : AppCompatActivity() { override fun onCreateOptionsMenu(menu: Menu): Boolean { emit("create_options_menu", menu) - return menu.size() > 0 + return menu.isNotEmpty() } override fun onOptionsItemSelected(item: MenuItem): Boolean { diff --git a/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt b/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt index f9162d8c..f10534d8 100644 --- a/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt +++ b/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt @@ -1,10 +1,10 @@ package org.autojs.autojs.runtime.api.augment.ui -import android.graphics.drawable.ColorDrawable import android.os.Build import android.view.ContextThemeWrapper import android.view.View import android.view.ViewGroup +import androidx.core.graphics.drawable.toDrawable import org.autojs.autojs.annotation.RhinoFunctionBody import org.autojs.autojs.annotation.RhinoRuntimeFunctionInterface import org.autojs.autojs.core.ui.JsViewHelper @@ -131,6 +131,9 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt val activity = getActivity(scriptRuntime) (activity as? ScriptExecuteActivity)?.eventEmitter }, + "statusBarHeight" to Supplier { + ViewUtils.getStatusBarHeightByDimen(globalContext) + } ) init { @@ -555,7 +558,7 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt ensureActivity(scriptRuntime) { activity -> runRhinoRuntime(scriptRuntime, newBaseFunction("action", { Colors.toIntRhino(Colors.setAlphaRhino(color, 1.0)).also { - activity.window.setBackgroundDrawable(ColorDrawable(it)) + activity.window.setBackgroundDrawable(it.toDrawable()) } }, NOT_CONSTRUCTABLE)) } diff --git a/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutBoundsView.kt b/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutBoundsView.kt index 2e882c45..a474838a 100644 --- a/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutBoundsView.kt +++ b/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutBoundsView.kt @@ -51,7 +51,7 @@ open class LayoutBoundsView : View { init { this.setWillNotDraw(false) - statusBarHeight = ViewUtils.getStatusBarHeight(context) + statusBarHeight = ViewUtils.getStatusBarHeightByWindow(context) } fun setOnNodeInfoSelectListener(onNodeInfoSelectListener: OnNodeInfoSelectListener?) { diff --git a/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutHierarchyView.java b/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutHierarchyView.java index d90329ce..fddbe645 100644 --- a/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutHierarchyView.java +++ b/app/src/main/java/org/autojs/autojs/ui/floating/layoutinspector/LayoutHierarchyView.java @@ -155,7 +155,7 @@ public class LayoutHierarchyView extends MultiLevelListView { mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(3); - mStatusBarHeight = ViewUtils.getStatusBarHeight(getContext()); + mStatusBarHeight = ViewUtils.getStatusBarHeightByWindow(getContext()); } public Paint getBoundsPaint() { diff --git a/app/src/main/java/org/autojs/autojs/ui/main/scripts/ExplorerFragment.kt b/app/src/main/java/org/autojs/autojs/ui/main/scripts/ExplorerFragment.kt index 14471ced..ba2ba377 100644 --- a/app/src/main/java/org/autojs/autojs/ui/main/scripts/ExplorerFragment.kt +++ b/app/src/main/java/org/autojs/autojs/ui/main/scripts/ExplorerFragment.kt @@ -9,8 +9,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.coordinatorlayout.widget.CoordinatorLayout -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat import androidx.core.view.marginBottom import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.tabs.TabLayout @@ -50,8 +48,6 @@ class ExplorerFragment : ViewPagerFragment(0), OnFloatingActionButtonClickListen private var mFloatingActionMenu: FloatingActionMenu? = null private var mIsCurrentPageFiles = false - private var mBottomInset: Int = 0 - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) EventBus.getDefault().register(this) @@ -63,10 +59,6 @@ class ExplorerFragment : ViewPagerFragment(0), OnFloatingActionButtonClickListen override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets -> - mBottomInset = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom - insets - } mExplorerView = binding.itemList.also { explorerView -> explorerView.setOnItemClickListener(object : ExplorerView.OnItemClickListener { override fun onItemClick(view: View?, item: ExplorerItem) { diff --git a/app/src/main/java/org/autojs/autojs/util/ViewUtils.kt b/app/src/main/java/org/autojs/autojs/util/ViewUtils.kt index e2e1a8d5..32564bde 100644 --- a/app/src/main/java/org/autojs/autojs/util/ViewUtils.kt +++ b/app/src/main/java/org/autojs/autojs/util/ViewUtils.kt @@ -48,6 +48,8 @@ import org.autojs.autojs.theme.ThemeColorManager import org.autojs.autojs.util.StringUtils.key import org.autojs.autojs6.R import kotlin.math.roundToInt +import androidx.core.view.size +import androidx.core.view.get /** * Created by Stardust on Jan 24, 2017. @@ -100,29 +102,40 @@ object ViewUtils { // ! 在一些设备上无法正常获取结果. // ! 参阅: https://github.com/hyb1996/Auto.js/issues/268 @SuppressLint("InternalInsetResource", "DiscouragedApi") - fun getStatusBarHeightLegacy(context: Context): Int { - return context.resources.getIdentifier("status_bar_height", "dimen", "android").let { resourceId -> - when (resourceId > 0) { - true -> context.resources.getDimensionPixelSize(resourceId) - else -> 0 - } + @JvmStatic + @JvmOverloads + fun getStatusBarHeightByDimen(context: Context, withFallback: Boolean = true): Int { + val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android") + val dimenStatusBarHeight = when (resourceId > 0) { + true -> context.resources.getDimensionPixelSize(resourceId) + else -> 0 + } + return when { + dimenStatusBarHeight > 0 -> dimenStatusBarHeight + withFallback -> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt() + else -> 0 } } @JvmStatic - fun getStatusBarHeight(context: Context): Int { - return getWindowForContext(context)?.let { window -> - Rect().let { rect -> - window.decorView.getWindowVisibleDisplayFrame(rect) - rect.top.takeIf { it >= 0 } - } - } ?: TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt() + @JvmOverloads + fun getStatusBarHeightByWindow(context: Context, withFallback: Boolean = true): Int { + val windowStatusBarHeight = getWindowForContext(context)?.decorView?.let { decorView -> + Rect().also { rect -> + decorView.getWindowVisibleDisplayFrame(rect) + }.top + } ?: 0 + return when { + windowStatusBarHeight > 0 -> windowStatusBarHeight + withFallback -> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24f, context.resources.displayMetrics).toInt() + else -> 0 + } } fun getTitleBarHeight(context: Context): Int { val window = getWindowForContext(context) val contentViewTop = window?.findViewById(Window.ID_ANDROID_CONTENT)?.top ?: 0 - return contentViewTop - getStatusBarHeight(context) + return contentViewTop - getStatusBarHeightByWindow(context) } fun getScreenHeight(activity: Activity) = DisplayMetrics().let { metrics -> @@ -415,8 +428,8 @@ object ViewUtils { } fun Menu.setItemsColor(color: Int) { - for (i in 0 until size()) { - val menuItem = getItem(i) + for (i in 0 until size) { + val menuItem = this[i] menuItem.icon = menuItem.icon?.applyColorFilterWith(color) menuItem.subMenu?.setItemsColor(color) } @@ -471,11 +484,7 @@ object ViewUtils { val view = this val viewTreeObserver = view.viewTreeObserver if (viewTreeObserver.isAlive) { - viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { - override fun onGlobalLayout() { - listener.invoke() - } - }) + viewTreeObserver.addOnGlobalLayoutListener { listener.invoke() } } } @@ -508,7 +517,7 @@ object ViewUtils { val fullColor = context.getColor(if (isAimColorLight) R.color.day_full else R.color.night_full) val hintColor = context.getColor(if (isAimColorLight) R.color.day_alpha_70 else R.color.night_alpha_70) - findViewById(androidx.appcompat.R.id.search_src_text).apply { + findViewById(androidx.appcompat.R.id.search_src_text)?.apply { setTextColor(fullColor) setHintTextColor(hintColor) setLinkTextColor(fullColor) diff --git a/version.properties b/version.properties index 2c124f91..3f25de79 100644 --- a/version.properties +++ b/version.properties @@ -1,5 +1,5 @@ -#Wed Apr 16 14:34:09 CST 2025 -BUILD_TIME=1744785249350 +#Wed Apr 16 16:02:18 CST 2025 +BUILD_TIME=1744790538995 COMPILE_SDK_VERSION=35 JAVA_VERSION=23 JAVA_VERSION_MIN_RADICAL=0