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 <utzcoz@outlook.com>
Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2382>
This commit is contained in:
utzcoz 2024-12-28 13:47:56 +08:00 committed by korejan
parent ab48d41724
commit 4f3c9bb2fe
2 changed files with 32 additions and 26 deletions

View file

@ -43,7 +43,8 @@ class DisplayOverOtherAppsStatusFragment : Fragment() {
private fun updateStatus(view: View?) {
displayOverOtherAppsEnabled = Settings.canDrawOverlays(requireContext())
val tv = view!!.findViewById<TextView>(R.id.textDisplayOverOtherAppsStatus)
with(requireNotNull(view)) {
val tv = this.findViewById<TextView>(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 =
@ -54,18 +55,21 @@ class DisplayOverOtherAppsStatusFragment : Fragment() {
)
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.
with(requireNotNull(context)) {
val intent =
Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + context!!.packageName),
Uri.parse("package:" + this.packageName),
)
startActivityForResult(intent, REQUEST_CODE_DISPLAY_OVER_OTHER_APPS)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// resultCode is always Activity.RESULT_CANCELED

View file

@ -22,7 +22,8 @@ import androidx.fragment.app.DialogFragment
class RestartRuntimeDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val message = arguments!!.getString(ARGS_KEY_MESSAGE)
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?,
@ -34,6 +35,7 @@ class RestartRuntimeDialogFragment : DialogFragment() {
}
return builder.create()
}
}
private fun delayRestart(delayMillis: Long) {
val intent = Intent(requireContext(), AboutActivity::class.java)