From e1f4ee15d536de71ae2dcc1df357793d781f4e1f Mon Sep 17 00:00:00 2001 From: tzugen Date: Tue, 5 Apr 2022 22:24:06 +0200 Subject: [PATCH] Fix Widget --- ultrasonic/src/main/AndroidManifest.xml | 6 +- .../receiver/MediaButtonIntentReceiver.java | 83 ------------------- .../receiver/MediaButtonIntentReceiver.kt | 52 ++++++++++++ .../service/MediaPlayerController.kt | 7 +- 4 files changed, 60 insertions(+), 88 deletions(-) delete mode 100644 ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java create mode 100644 ultrasonic/src/main/kotlin/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.kt diff --git a/ultrasonic/src/main/AndroidManifest.xml b/ultrasonic/src/main/AndroidManifest.xml index 7e4b433e..27946252 100644 --- a/ultrasonic/src/main/AndroidManifest.xml +++ b/ultrasonic/src/main/AndroidManifest.xml @@ -127,7 +127,11 @@ android:name="android.appwidget.provider" android:resource="@xml/appwidget_info_4x4"/> - + + + + + diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java b/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java deleted file mode 100644 index 229da68d..00000000 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see . - - Copyright 2010 (C) Sindre Mehus - */ -package org.moire.ultrasonic.receiver; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.os.Parcelable; -import timber.log.Timber; - -import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport; -import org.moire.ultrasonic.util.Constants; -import org.moire.ultrasonic.util.Settings; -import org.moire.ultrasonic.util.Util; - -import kotlin.Lazy; - -import static org.koin.java.KoinJavaComponent.inject; - -/** - * @author Sindre Mehus - */ -public class MediaButtonIntentReceiver extends BroadcastReceiver -{ - private Lazy lifecycleSupport = inject(MediaPlayerLifecycleSupport.class); - - @Override - public void onReceive(Context context, Intent intent) - { - String intentAction = intent.getAction(); - - // If media button are turned off and we received a media button, exit - if (!Settings.getMediaButtonsEnabled() && Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) - return; - - // Only process media buttons and CMD_PROCESS_KEYCODE, which is received from the widgets - if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction) && - !Constants.CMD_PROCESS_KEYCODE.equals(intentAction)) return; - - Bundle extras = intent.getExtras(); - - if (extras == null) - { - return; - } - - Parcelable event = (Parcelable) extras.get(Intent.EXTRA_KEY_EVENT); - Timber.i("Got MEDIA_BUTTON key event: %s", event); - - try - { - Intent serviceIntent = new Intent(Constants.CMD_PROCESS_KEYCODE); - serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event); - lifecycleSupport.getValue().receiveIntent(serviceIntent); - - if (isOrderedBroadcast()) - { - abortBroadcast(); - } - } - catch (Exception x) - { - // Ignored. - } - } -} diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.kt new file mode 100644 index 00000000..48f6a135 --- /dev/null +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.kt @@ -0,0 +1,52 @@ +/* + * MediaButtonIntentReceiver.kt + * Copyright (C) 2009-2022 Ultrasonic developers + * + * Distributed under terms of the GNU GPLv3 license. + */ + +package org.moire.ultrasonic.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Parcelable +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject +import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport +import org.moire.ultrasonic.util.Constants +import org.moire.ultrasonic.util.Settings +import timber.log.Timber +import java.lang.Exception + +/** + * This class is used to receive commands from the widget + */ +class MediaButtonIntentReceiver : BroadcastReceiver(), KoinComponent { + private val lifecycleSupport: MediaPlayerLifecycleSupport by inject() + + override fun onReceive(context: Context, intent: Intent) { + val intentAction = intent.action + + // If media button are turned off and we received a media button, exit + if (!Settings.mediaButtonsEnabled && Intent.ACTION_MEDIA_BUTTON == intentAction) return + + // Only process media buttons and CMD_PROCESS_KEYCODE, which is received from the widgets + if (Intent.ACTION_MEDIA_BUTTON != intentAction && + Constants.CMD_PROCESS_KEYCODE != intentAction + ) return + val extras = intent.extras ?: return + val event = extras[Intent.EXTRA_KEY_EVENT] as Parcelable? + Timber.i("Got MEDIA_BUTTON key event: %s", event) + try { + val serviceIntent = Intent(Constants.CMD_PROCESS_KEYCODE) + serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event) + lifecycleSupport.receiveIntent(serviceIntent) + if (isOrderedBroadcast) { + abortBroadcast() + } + } catch (x: Exception) { + // Ignored. + } + } +} \ No newline at end of file diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt index ec866abb..d591d630 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -146,10 +146,9 @@ class MediaPlayerController( when { playerState === PlayerState.PAUSED -> { - // TODO: Save playlist -// playbackStateSerializer.serialize( -// downloader.getPlaylist(), downloader.currentPlayingIndex, playerPosition -// ) + playbackStateSerializer.serialize( + playList, currentMediaItemIndex, playerPosition + ) } playerState === PlayerState.STARTED -> { scrobbler.scrobble(currentPlaying, false)