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,28 +43,32 @@ class DisplayOverOtherAppsStatusFragment : Fragment() {
private fun updateStatus(view: View?) {
displayOverOtherAppsEnabled = Settings.canDrawOverlays(requireContext())
val tv = view!!.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 =
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<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 =
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?) {

View file

@ -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) {