Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality: use compat functions for Android 13 type extras #4501

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.Ndef
import android.nfc.tech.NdefFormatable
import android.os.Build
import androidx.core.content.IntentCompat
import io.homeassistant.companion.android.BuildConfig
import java.io.IOException

Expand All @@ -21,12 +21,7 @@ object NFCUtil {
return null
}

val rawMessages = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, NdefMessage::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
}
val rawMessages = IntentCompat.getParcelableArrayExtra(intent, NfcAdapter.EXTRA_NDEF_MESSAGES, NdefMessage::class.java)
val ndefMessage = rawMessages?.get(0) as NdefMessage?
return ndefMessage?.records?.get(0)?.toUri()
}
Expand All @@ -41,12 +36,7 @@ object NFCUtil {
val nfcMessage = NdefMessage(arrayOf(nfcRecord) + applicationRecords)
val nfcFallbackMessage = NdefMessage(arrayOf(nfcRecord))
intent?.let {
val tag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
it.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
} else {
@Suppress("DEPRECATION")
it.getParcelableExtra(NfcAdapter.EXTRA_TAG)
}
val tag = IntentCompat.getParcelableExtra(it, NfcAdapter.EXTRA_TAG, Tag::class.java)
return writeMessageToTag(nfcMessage, nfcFallbackMessage, tag)
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package io.homeassistant.companion.android.notifications
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.RemoteInput
import androidx.core.content.IntentCompat
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.servers.ServerManager
Expand Down Expand Up @@ -45,12 +45,7 @@ class NotificationActionReceiver : BroadcastReceiver() {
lateinit var notificationDao: NotificationDao

override fun onReceive(context: Context, intent: Intent) {
val notificationAction = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(EXTRA_NOTIFICATION_ACTION, NotificationAction::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableExtra(EXTRA_NOTIFICATION_ACTION)
}
val notificationAction = IntentCompat.getParcelableExtra(intent, EXTRA_NOTIFICATION_ACTION, NotificationAction::class.java)

if (notificationAction == null) {
Log.e(TAG, "Failed to get notification action.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.homeassistant.companion.android.settings.notification

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.BundleCompat
import androidx.core.view.MenuHost
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
Expand Down Expand Up @@ -35,11 +35,8 @@ class NotificationDetailFragment : Fragment() {
private lateinit var notification: NotificationItem

override fun onCreate(savedInstanceState: Bundle?) {
notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
arguments?.getSerializable(ARG_NOTIF, NotificationItem::class.java)
} else {
@Suppress("DEPRECATION")
arguments?.get(ARG_NOTIF) as? NotificationItem
notification = arguments?.let {
BundleCompat.getSerializable(it, ARG_NOTIF, NotificationItem::class.java)
} ?: return
super.onCreate(savedInstanceState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand All @@ -19,6 +18,7 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.toColorInt
import androidx.core.os.BundleCompat
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.android.material.color.DynamicColors
Expand Down Expand Up @@ -363,12 +363,8 @@ class ButtonWidget : AppWidgetProvider() {
val label: String? = extras.getString(EXTRA_LABEL)
val requireAuthentication: Boolean = extras.getBoolean(EXTRA_REQUIRE_AUTHENTICATION)
val icon: String = extras.getString(EXTRA_ICON_NAME) ?: "mdi:flash"
val backgroundType: WidgetBackgroundType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
extras.getSerializable(EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
} else {
@Suppress("DEPRECATION")
extras.getSerializable(EXTRA_BACKGROUND_TYPE) as? WidgetBackgroundType
} ?: WidgetBackgroundType.DAYNIGHT
val backgroundType = BundleCompat.getSerializable(extras, EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
?: WidgetBackgroundType.DAYNIGHT
val textColor: String? = extras.getString(EXTRA_TEXT_COLOR)

if (serverId == null || domain == null || service == null || serviceData == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.RemoteViews
import androidx.core.os.BundleCompat
import com.squareup.picasso.Picasso
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BuildConfig
Expand All @@ -24,7 +24,6 @@ import io.homeassistant.companion.android.database.widget.CameraWidgetEntity
import io.homeassistant.companion.android.database.widget.WidgetTapAction
import io.homeassistant.companion.android.util.hasActiveConnection
import io.homeassistant.companion.android.webview.WebViewActivity
import java.io.Serializable
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -221,7 +220,8 @@ class CameraWidget : AppWidgetProvider() {

val serverSelection = if (extras.containsKey(EXTRA_SERVER_ID)) extras.getInt(EXTRA_SERVER_ID) else null
val entitySelection: String? = extras.getString(EXTRA_ENTITY_ID)
val tapActionSelection: WidgetTapAction = extras.getSerializableCompat(EXTRA_TAP_ACTION) ?: WidgetTapAction.REFRESH
val tapActionSelection = BundleCompat.getSerializable(extras, EXTRA_TAP_ACTION, WidgetTapAction::class.java)
?: WidgetTapAction.REFRESH

if (serverSelection == null || entitySelection == null) {
Log.e(TAG, "Did not receive complete configuration data")
Expand Down Expand Up @@ -266,12 +266,3 @@ class CameraWidget : AppWidgetProvider() {
return Resources.getSystem().displayMetrics.widthPixels
}
}

private inline fun <reified T : Serializable> Bundle.getSerializableCompat(key: String): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getSerializable(key, T::class.java)
} else {
@Suppress("DEPRECATION")
getSerializable(key) as? T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
Expand All @@ -15,6 +14,7 @@ import android.widget.RemoteViews
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.graphics.toColorInt
import androidx.core.os.BundleCompat
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
Expand Down Expand Up @@ -209,18 +209,10 @@ class EntityWidget : BaseWidgetProvider() {
val textSizeSelection: String? = extras.getString(EXTRA_TEXT_SIZE)
val stateSeparatorSelection: String? = extras.getString(EXTRA_STATE_SEPARATOR)
val attributeSeparatorSelection: String? = extras.getString(EXTRA_ATTRIBUTE_SEPARATOR)
val tapActionSelection: WidgetTapAction = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
extras.getSerializable(EXTRA_TAP_ACTION, WidgetTapAction::class.java)
} else {
@Suppress("DEPRECATION")
extras.getSerializable(EXTRA_TAP_ACTION) as? WidgetTapAction
} ?: WidgetTapAction.REFRESH
val backgroundTypeSelection: WidgetBackgroundType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
extras.getSerializable(EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
} else {
@Suppress("DEPRECATION")
extras.getSerializable(EXTRA_BACKGROUND_TYPE) as? WidgetBackgroundType
} ?: WidgetBackgroundType.DAYNIGHT
val tapActionSelection = BundleCompat.getSerializable(extras, EXTRA_TAP_ACTION, WidgetTapAction::class.java)
?: WidgetTapAction.REFRESH
val backgroundTypeSelection = BundleCompat.getSerializable(extras, EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
?: WidgetBackgroundType.DAYNIGHT
val textColorSelection: String? = extras.getString(EXTRA_TEXT_COLOR)

if (serverId == null || entitySelection == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.RemoteViews
import android.widget.Toast
import androidx.core.os.BundleCompat
import com.google.android.material.color.DynamicColors
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
Expand Down Expand Up @@ -473,12 +473,8 @@ class MediaPlayerControlsWidget : BaseWidgetProvider() {
val showSeek: Boolean = extras.getBoolean(EXTRA_SHOW_SEEK)
val showVolume: Boolean = extras.getBoolean(EXTRA_SHOW_VOLUME)
val showSource: Boolean = extras.getBoolean(EXTRA_SHOW_SOURCE)
val backgroundType: WidgetBackgroundType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
extras.getSerializable(EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
} else {
@Suppress("DEPRECATION")
extras.getSerializable(EXTRA_BACKGROUND_TYPE) as? WidgetBackgroundType
} ?: WidgetBackgroundType.DAYNIGHT
val backgroundType = BundleCompat.getSerializable(extras, EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
?: WidgetBackgroundType.DAYNIGHT

if (serverId == null || entitySelection == null) {
Log.e(TAG, "Did not receive complete configuration data")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.RemoteViews
import androidx.core.content.ContextCompat
import androidx.core.graphics.toColorInt
import androidx.core.os.BundleCompat
import androidx.core.text.HtmlCompat
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -269,12 +269,8 @@ class TemplateWidget : AppWidgetProvider() {
val serverId = if (extras.containsKey(EXTRA_SERVER_ID)) extras.getInt(EXTRA_SERVER_ID) else null
val template: String? = extras.getString(EXTRA_TEMPLATE)
val textSize: Float = extras.getFloat(EXTRA_TEXT_SIZE)
val backgroundTypeSelection: WidgetBackgroundType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
extras.getSerializable(EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
} else {
@Suppress("DEPRECATION")
extras.getSerializable(EXTRA_BACKGROUND_TYPE) as? WidgetBackgroundType
} ?: WidgetBackgroundType.DAYNIGHT
val backgroundTypeSelection = BundleCompat.getSerializable(extras, EXTRA_BACKGROUND_TYPE, WidgetBackgroundType::class.java)
?: WidgetBackgroundType.DAYNIGHT
val textColorSelection: String? = extras.getString(EXTRA_TEXT_COLOR)

if (serverId == null || template == null) {
Expand Down