class DYFeedCastPluginLoadingDialog( context: Context, private val controller:…
- User
class DYFeedCastPluginLoadingDialog( context: Context, private val controller: CastSourceUIPluginController?, private val depend: ICastSourceUIDepend, private val screenMode: ScreenMode? ) : AbsCastSourcePluginLoadingDialog(context, controller, depend) {
companion object { private const val TAG = "LVCastSourcePluginLoadingDialog" } private var hasLoggedPageShow: Boolean = false // 是否上报过page_show埋点 override fun getLayout(): Int { return if (screenMode == ScreenMode.PORTRAIT) { R.layout.sourceui_activity_plugin_loading } else { R.layout.sourceui_activity_plugin_landscape_loading } } override fun getContentFl(): FrameLayout = findViewById(R.id.content_fl) override fun getPluginLoadingView(): AbsCastSourcePluginLoadingView { return DYFeedCastPluginLoadingView(context, depend = depend, screenMode = screenMode) } override fun showSearchView() { val searchView = if (screenMode == ScreenMode.PORTRAIT) { controller?.getCastSearchView(context, object: CastSourceUIDependWrapper(depend) { override fun onSearchPageClose(context: Context?, isLandscape: Boolean, action: Int) { try { if (isShowing) dismiss() } catch (e: Exception) { CastSourceUILog.e(TAG, "dismiss dialog error: $e") } super.onSearchPageClose(context, isLandscape, action) } }) } else { controller?.getCastLandscapeSearchView(context, object: CastSourceUIDependWrapper(depend) { override fun onSearchPageClose(context: Context?, isLandscape: Boolean, action: Int) { try { if (isShowing) dismiss() } catch (e: Exception) { CastSourceUILog.e(TAG, "dismiss dialog error: $e") } super.onSearchPageClose(context, isLandscape, action) } }) } showSearchView(searchView) } override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) if (hasFocus) { if (screenMode == ScreenMode.LANDSCAPE) { window?.setGravity(Gravity.RIGHT) window?.setLayout( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT ) } } } override fun onStart() { super.onStart() window?.let { it.setBackgroundDrawable(ColorDrawable(0x00000000)) it.setDimAmount(0F) // 去掉蒙层 if (screenMode == ScreenMode.LANDSCAPE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { it.setFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) } it.setWindowAnimations(R.style.sourceui_right_dialog_anim) it.setGravity(Gravity.RIGHT) it.setLayout( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT ) } else { it.setWindowAnimations(R.style.sourceui_bottom_dialog_anim) val height = depend?.getSearchViewTargetHeight() ?: context.dip2Px(528F).toInt() it.setGravity(Gravity.BOTTOM) it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height) } } if (!hasLoggedPageShow) { hasLoggedPageShow = true CastSourceUIApiAppLogEvent.sendPluginPageShow(depend, screenMode == ScreenMode.LANDSCAPE) } } - Assistant
上面的代码是一个自定义的对话框类(DYFeedCastPluginLoadingDialog),它继承了AbsCastSourcePluginLoadingDialog。这个对话框主要用于展示搜索界面,包含了两种屏幕模式:竖屏和横屏。
-
首先,定义了一个伴生对象,里面包含一个TAG常量,用于日志输出。
-
定义了一些私有属性,如controller、depend、screenMode等,这些属性主要用于设置和控制界面的显示。
-
重写了getLayout方法,用于获取布局资源。根据屏幕模式不同,返回不同的布局资源。
-
getContentFl方法用于获取对话框的内容容器。
-
getPluginLoadingView方法用于获取加载视图,创建了一个DYFeedCastPluginLoadingView对象。
-
showSearchView方法会调用controller的getCastSearchView或getCastLandscapeSearchView方法,获取搜索视图,并调用showSearchView方法显示。
-
重写了onWindowFocusChanged方法,如果窗口获取到焦点且屏幕模式为横屏,设置窗口的位置和大小。
-
重写了onStart方法,对窗口进行一些定制化设置,例如设置背景、动画、位置和大小等。此外,还发送了一个插件页面展示的日志事件。
代码中存在的问题分析:
-
这个类的构造方法中,controller类型为CastSourceUIPluginController?,表示controller属性可以为null。但在整个类中,并没有判断这个属性是否为null,如果传入的controller是null,那么在调用controller的方法时,会出现空指针异常。
-
这个类执行了日志打点,但没有判断数据是否正常。例如在onStart方法中发送插件页面展示的日志事件,需要使用到depend属性,但需要先判断depend是否为null或者依赖的数据是否正确,否则上报的日志数据将可能出现异常。
-
showSearchView方法中传入的参数应该进行非空判断,因为在调用controller的getCastSearchView或getCastLandscapeSearchView方法时,都可能返回null。若不做非空判断,showSearchView方法内部可能会引发空指针异常。
-