From 4f3c9bb2fe2427600f4915eab99d1a72bc15d67f Mon Sep 17 00:00:00 2001 From: utzcoz Date: Sat, 28 Dec 2024 13:47:56 +0800 Subject: [PATCH] t/android_common: Fix lint errors for !! usages The lint doesn't recommend us to use someVariable!!, and we can use built-in Kotlin's requireNotNull and with scope function to use variables without errors. Signed-off-by: utzcoz Part-of: --- .../DisplayOverOtherAppsStatusFragment.kt | 36 ++++++++++--------- .../RestartRuntimeDialogFragment.kt | 22 ++++++------ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/DisplayOverOtherAppsStatusFragment.kt b/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/DisplayOverOtherAppsStatusFragment.kt index 93d410cd9..4259816eb 100644 --- a/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/DisplayOverOtherAppsStatusFragment.kt +++ b/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/DisplayOverOtherAppsStatusFragment.kt @@ -43,28 +43,32 @@ class DisplayOverOtherAppsStatusFragment : Fragment() { private fun updateStatus(view: View?) { displayOverOtherAppsEnabled = Settings.canDrawOverlays(requireContext()) - val tv = view!!.findViewById(R.id.textDisplayOverOtherAppsStatus) - // Combining format with html style tag might have problem. See - // https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithHTML - val msg = - getString( - R.string.msg_display_over_other_apps, - if (displayOverOtherAppsEnabled) getString(R.string.enabled) - else getString(R.string.disabled), - ) - tv.text = Html.fromHtml(msg, Html.FROM_HTML_MODE_LEGACY) + with(requireNotNull(view)) { + val tv = this.findViewById(R.id.textDisplayOverOtherAppsStatus) + // Combining format with html style tag might have problem. See + // https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithHTML + val msg = + getString( + R.string.msg_display_over_other_apps, + if (displayOverOtherAppsEnabled) getString(R.string.enabled) + else getString(R.string.disabled), + ) + tv.text = Html.fromHtml(msg, Html.FROM_HTML_MODE_LEGACY) + } } private fun launchDisplayOverOtherAppsSettings() { // Since Android 11, framework ignores the uri and takes user to the top-level settings. // See https://developer.android.com/about/versions/11/privacy/permissions#system-alert // for detail. - val intent = - Intent( - Settings.ACTION_MANAGE_OVERLAY_PERMISSION, - Uri.parse("package:" + context!!.packageName), - ) - startActivityForResult(intent, REQUEST_CODE_DISPLAY_OVER_OTHER_APPS) + with(requireNotNull(context)) { + val intent = + Intent( + Settings.ACTION_MANAGE_OVERLAY_PERMISSION, + Uri.parse("package:" + this.packageName), + ) + startActivityForResult(intent, REQUEST_CODE_DISPLAY_OVER_OTHER_APPS) + } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/RestartRuntimeDialogFragment.kt b/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/RestartRuntimeDialogFragment.kt index 313499ea8..1f671749d 100644 --- a/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/RestartRuntimeDialogFragment.kt +++ b/src/xrt/targets/android_common/src/main/java/org/freedesktop/monado/android_common/RestartRuntimeDialogFragment.kt @@ -22,17 +22,19 @@ import androidx.fragment.app.DialogFragment class RestartRuntimeDialogFragment : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val message = arguments!!.getString(ARGS_KEY_MESSAGE) - val builder = AlertDialog.Builder(requireActivity()) - builder.setMessage(message).setCancelable(false).setPositiveButton(R.string.restart) { - _: DialogInterface?, - _: Int -> - delayRestart(DELAY_RESTART_DURATION) - // ! @todo elegant way to stop service? A bounded service might be restarted by - // framework automatically. - Process.killProcess(Process.myPid()) + with(requireNotNull(arguments)) { + val message = this.getString(ARGS_KEY_MESSAGE) + val builder = AlertDialog.Builder(requireActivity()) + builder.setMessage(message).setCancelable(false).setPositiveButton(R.string.restart) { + _: DialogInterface?, + _: Int -> + delayRestart(DELAY_RESTART_DURATION) + // ! @todo elegant way to stop service? A bounded service might be restarted by + // framework automatically. + Process.killProcess(Process.myPid()) + } + return builder.create() } - return builder.create() } private fun delayRestart(delayMillis: Long) {