From dea9b6fe558fd5b7df6a49b3ef2be3ebe55bea77 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 23 Jul 2022 13:38:42 -0700 Subject: [PATCH] Some warning and deprecation cleanup. --- .../streaming/StreamingPlaybackService.kt | 7 ++- .../service/system/MediaButtonReceiver.kt | 3 +- .../remote/service/system/SystemService.kt | 5 +- .../ui/settings/activity/SettingsActivity.kt | 61 ++++++++----------- .../remote/ui/shared/fragment/BaseFragment.kt | 4 +- .../casey/musikcube/remote/util/Extensions.kt | 26 ++++++++ 6 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt index 6a8b7a74f..658f61c86 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt @@ -496,7 +496,7 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { state = PlaybackState.Playing prefetchNextTrackAudio() cancelScheduledPausedSleep() - precacheTrackMetadata(playContext.currentIndex, PRECACHE_METADATA_SIZE) + precacheTrackMetadataPage(playContext.currentIndex) } PlayerWrapper.State.Buffering -> state = PlaybackState.Buffering @@ -673,6 +673,7 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { } } + @SuppressLint("CheckResult") private fun prefetchNextTrackMetadata() { if (playContext.nextMetadata == null) { val originalParams = queryContext @@ -770,9 +771,9 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { } @SuppressLint("CheckResult") - private fun precacheTrackMetadata(start: Int, count: Int) { + private fun precacheTrackMetadataPage(start: Int) { val originalParams = queryContext - val query = playlistQueryFactory.page(start, count) + val query = playlistQueryFactory.page(start, PRECACHE_METADATA_SIZE) if (query != null) { @Suppress("unused") diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/MediaButtonReceiver.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/MediaButtonReceiver.kt index fec0f1818..38f61c452 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/MediaButtonReceiver.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/MediaButtonReceiver.kt @@ -5,12 +5,13 @@ import android.content.Context import android.content.Intent import android.view.KeyEvent import androidx.core.content.ContextCompat +import io.casey.musikcube.remote.util.getParcelableExtraCompat class MediaButtonReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (Intent.ACTION_MEDIA_BUTTON == action) { - val event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) + val event = intent.getParcelableExtraCompat(Intent.EXTRA_KEY_EVENT) if (event != null && event.action == KeyEvent.ACTION_DOWN) { when (event.keyCode) { diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt index a0abdb09a..d6052d739 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt @@ -35,6 +35,7 @@ import io.casey.musikcube.remote.ui.shared.extension.fallback import io.casey.musikcube.remote.ui.shared.util.AlbumArtLookup import io.casey.musikcube.remote.ui.shared.util.Size import io.casey.musikcube.remote.util.Debouncer +import io.casey.musikcube.remote.util.getParcelableExtraCompat import androidx.core.app.NotificationCompat.Action as NotifAction const val ENABLE_LOGGING = false @@ -511,7 +512,9 @@ class SystemService : Service() { private val mediaSessionCallback = object : MediaSessionCompat.Callback() { override fun onMediaButtonEvent(mediaButtonEvent: Intent?): Boolean { if (Intent.ACTION_MEDIA_BUTTON == mediaButtonEvent?.action) { - val event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) ?: return super.onMediaButtonEvent(mediaButtonEvent) + val event = + mediaButtonEvent.getParcelableExtraCompat(Intent.EXTRA_KEY_EVENT) ?: + return super.onMediaButtonEvent(mediaButtonEvent) val keycode = event.keyCode val action = event.action diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt index 47c89b4b7..64bf5e1dd 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt @@ -10,6 +10,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.* +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.fragment.app.DialogFragment import com.uacf.taskrunner.Task @@ -25,6 +26,8 @@ import io.casey.musikcube.remote.ui.shared.activity.BaseActivity import io.casey.musikcube.remote.ui.shared.extension.* import io.casey.musikcube.remote.ui.shared.mixin.MetadataProxyMixin import io.casey.musikcube.remote.ui.shared.mixin.PlaybackMixin +import io.casey.musikcube.remote.util.getParcelableCompat +import io.casey.musikcube.remote.util.getParcelableExtraCompat import java.util.* import javax.inject.Inject import io.casey.musikcube.remote.ui.settings.constants.Prefs.Default as Defaults @@ -83,26 +86,6 @@ class SettingsActivity : BaseActivity() { return super.onOptionsItemSelected(item) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (requestCode == CONNECTIONS_REQUEST_CODE && resultCode == RESULT_OK) { - if (data != null) { - val connection = data.getParcelableExtra( - ConnectionsActivity.EXTRA_SELECTED_CONNECTION) - - if (connection != null) { - addressText.setText(connection.hostname) - passwordText.setText(connection.password) - portText.setText(connection.wssPort.toString()) - httpPortText.setText(connection.httpPort.toString()) - sslCheckbox.setCheckWithoutEvent(connection.ssl, sslCheckChanged) - certCheckbox.setCheckWithoutEvent(connection.noValidate, certValidationChanged) - } - } - } - - super.onActivityResult(requestCode, resultCode, data) - } - override val transitionType: Transition get() = Transition.Vertical @@ -219,14 +202,12 @@ class SettingsActivity : BaseActivity() { } private fun bindListeners() { - findViewById(R.id.button_save_as).setOnClickListener{ + findViewById(R.id.button_save_as).setOnClickListener { showSaveAsDialog() } - findViewById(R.id.button_load).setOnClickListener{ - startActivityForResult( - ConnectionsActivity.getStartIntent(this), - CONNECTIONS_REQUEST_CODE) + findViewById(R.id.button_load).setOnClickListener { + connectionsActivityLauncher.launch(ConnectionsActivity.getStartIntent(this)) } findViewById(R.id.button_diagnostics).setOnClickListener { @@ -234,6 +215,23 @@ class SettingsActivity : BaseActivity() { } } + private val connectionsActivityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult -> + if (activityResult.resultCode == RESULT_OK && activityResult.data != null) { + activityResult.data?.let { data -> + val connection = data.getParcelableExtraCompat(ConnectionsActivity.EXTRA_SELECTED_CONNECTION) + if (connection != null) { + addressText.setText(connection.hostname) + passwordText.setText(connection.password) + portText.setText(connection.wssPort.toString()) + httpPortText.setText(connection.httpPort.toString()) + sslCheckbox.setCheckWithoutEvent(connection.ssl, sslCheckChanged) + certCheckbox.setCheckWithoutEvent(connection.noValidate, certValidationChanged) + } + + } + } + } + private fun showSaveAsDialog() { if (!dialogVisible(SaveAsDialog.TAG)) { showDialog(SaveAsDialog.newInstance(), SaveAsDialog.TAG) @@ -413,13 +411,10 @@ class SettingsActivity : BaseActivity() { .setMessage(R.string.settings_confirm_overwrite_message) .setNegativeButton(R.string.button_no, null) .setPositiveButton(R.string.button_yes) { _, _ -> - when (val connection = arguments?.getParcelable(EXTRA_CONNECTION)) { - null -> throw IllegalArgumentException("invalid connection") - else -> { - val db = (activity as SettingsActivity).connectionsDb - val saveAs = SaveAsTask(db, connection, true) - (activity as SettingsActivity).runner.run(SaveAsTask.nameFor(connection), saveAs) - } + arguments?.getParcelableCompat(EXTRA_CONNECTION)?.let { connection -> + val db = (activity as SettingsActivity).connectionsDb + val saveAs = SaveAsTask(db, connection, true) + (activity as SettingsActivity).runner.run(SaveAsTask.nameFor(connection), saveAs) } } .create() @@ -484,8 +479,6 @@ class SettingsActivity : BaseActivity() { } companion object { - const val CONNECTIONS_REQUEST_CODE = 1000 - fun getStartIntent(context: Context): Intent { return Intent(context, SettingsActivity::class.java) } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt index cb6f640e8..6eb17c791 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt @@ -173,7 +173,7 @@ open class BaseFragment: Fragment(), ViewModel.Provider, IBackHandler { toolbar?.collapseActionViewIfExpanded() ?: false override fun > createViewModel(): T? = null - @Suppress protected fun > getViewModel(): T = mixin(ViewModelMixin::class.java)?.get() as T + @Suppress("unused") protected fun > getViewModel(): T = mixin(ViewModelMixin::class.java)?.get() as T protected fun mixin(mixin: T): T = mixins.add(mixin) protected fun mixin(cls: Class): T? = mixins.get(cls) @@ -188,7 +188,7 @@ open class BaseFragment: Fragment(), ViewModel.Provider, IBackHandler { if (arguments == null) { arguments = Bundle() } - return arguments!! + return requireArguments() } val appCompatActivity: AppCompatActivity diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt new file mode 100644 index 000000000..bc72080c0 --- /dev/null +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt @@ -0,0 +1,26 @@ +package io.casey.musikcube.remote.util + +import android.content.Intent +import android.os.Bundle +import android.os.Parcelable +import androidx.annotation.RequiresApi + +@RequiresApi(33) +@Suppress("deprecation") +inline fun Intent.getParcelableExtraCompat(name: String): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + this.getParcelableExtra(name) + } + else { + this.getParcelableExtra(name, T::class.java) + } + +@RequiresApi(33) +@Suppress("deprecation") +inline fun Bundle.getParcelableCompat(name: String): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + this.getParcelable(name) + } + else { + this.getParcelable(name, T::class.java) + } \ No newline at end of file