Skip to content

Commit

Permalink
Fix webapp function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Dec 3, 2020
1 parent aa08317 commit 8afc758
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/native-10.6/nativeshell.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ window.NativeShell = {
},

getPlugins() {
return ["native/exoplayer", "native/externalplayer"];
return ["native/navigation", "native/exoplayer", "native/externalplayer"];
},

execCast(action, args, callback) {
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/assets/native-10.6/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
define(['inputManager', 'playbackManager'], function (inputManager, playbackManager) {
"use strict";

return function () {
window.NavigationHelper = this;

this.goBack = function () {
inputManager.trigger('back');
};

this.playbackManager = playbackManager;
};
});
11 changes: 11 additions & 0 deletions app/src/main/assets/native-10.7/NavigationPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class NavigationPlugin {
constructor({ playbackManager }) {
window['NavigationHelper'] = this;

this.playbackManager = playbackManager;
}

goBack() {
// TODO fix back action for 10.7
};
}
1 change: 1 addition & 0 deletions app/src/main/assets/native-10.7/nativeshell.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const features = [
];

const plugins = [
'NavigationPlugin',
'ExoPlayerPlugin',
'ExternalPlayerPlugin'
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WebViewFragment : Fragment() {
serverId = requireNotNull(args.getLong(FRAGMENT_WEB_VIEW_EXTRA_SERVER_ID)) { "Server id has not been supplied!" }
instanceUrl = requireNotNull(args.getString(FRAGMENT_WEB_VIEW_EXTRA_URL)) { "Server url has not been supplied!" }
requireActivity().onBackPressedDispatcher.addCallback(this) {
if (!connected || !webappFunctionChannel.triggerInputManagerAction(Constants.INPUT_MANAGER_COMMAND_BACK)) {
if (!connected || !webappFunctionChannel.goBack()) {
isEnabled = false
activity?.onBackPressed()
}
Expand Down Expand Up @@ -131,6 +131,7 @@ class WebViewFragment : Fragment() {
val path = url.path?.toLowerCase(Locale.ROOT) ?: return null
return when {
path.endsWith(Constants.APPLOADER_PATH) || path.endsWith(Constants.MAIN_BUNDLE_PATH) -> {
Timber.d("Loading bundle or apploader")
assetsVersion = when {
path.endsWith(Constants.APPLOADER_PATH) -> "10.6"
path.endsWith(Constants.MAIN_BUNDLE_PATH) -> "10.7"
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/org/jellyfin/mobile/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ object Constants {
const val PREF_EXTERNAL_PLAYER_APP = "pref_external_player_app"

// InputManager commands
const val INPUT_MANAGER_COMMAND_PLAY_PAUSE = "playpause"
const val INPUT_MANAGER_COMMAND_PAUSE = "pause"
const val INPUT_MANAGER_COMMAND_PREVIOUS = "previous"
const val INPUT_MANAGER_COMMAND_NEXT = "next"
const val INPUT_MANAGER_COMMAND_REWIND = "rewind"
const val INPUT_MANAGER_COMMAND_FAST_FORWARD = "fastforward"
const val INPUT_MANAGER_COMMAND_STOP = "stop"
const val INPUT_MANAGER_COMMAND_VOL_UP = "volumeup"
const val INPUT_MANAGER_COMMAND_VOL_DOWN = "volumedown"
const val INPUT_MANAGER_COMMAND_BACK = "back"
const val PLAYBACK_MANAGER_COMMAND_PLAY = "unpause"
const val PLAYBACK_MANAGER_COMMAND_PAUSE = "pause"
const val PLAYBACK_MANAGER_COMMAND_PREVIOUS = "previousTrack"
const val PLAYBACK_MANAGER_COMMAND_NEXT = "nextTrack"
const val PLAYBACK_MANAGER_COMMAND_REWIND = "rewind"
const val PLAYBACK_MANAGER_COMMAND_FAST_FORWARD = "fastForward"
const val PLAYBACK_MANAGER_COMMAND_STOP = "stop"
const val PLAYBACK_MANAGER_COMMAND_VOL_UP = "volumeUp"
const val PLAYBACK_MANAGER_COMMAND_VOL_DOWN = "volumeDown"

// Notification
const val MEDIA_NOTIFICATION_CHANNEL_ID = "org.jellyfin.mobile.media.NOW_PLAYING"
Expand Down
50 changes: 20 additions & 30 deletions app/src/main/java/org/jellyfin/mobile/webapp/RemotePlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ import org.jellyfin.mobile.utils.Constants.EXTRA_ITEM_ID
import org.jellyfin.mobile.utils.Constants.EXTRA_PLAYER_ACTION
import org.jellyfin.mobile.utils.Constants.EXTRA_POSITION
import org.jellyfin.mobile.utils.Constants.EXTRA_TITLE
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_FAST_FORWARD
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_NEXT
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_PAUSE
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_PLAY_PAUSE
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_PREVIOUS
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_REWIND
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_STOP
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_FAST_FORWARD
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_NEXT
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_PAUSE
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_PREVIOUS
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_REWIND
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_STOP
import org.jellyfin.mobile.utils.Constants.MEDIA_NOTIFICATION_CHANNEL_ID
import org.jellyfin.mobile.utils.Constants.MEDIA_PLAYER_NOTIFICATION_ID
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_PLAY
import org.jellyfin.mobile.utils.Constants.SUPPORTED_MUSIC_PLAYER_PLAYBACK_ACTIONS
import org.jellyfin.mobile.utils.applyDefaultLocalAudioAttributes
import org.jellyfin.mobile.utils.createMediaNotificationChannel
Expand Down Expand Up @@ -85,37 +85,28 @@ class RemotePlayerService : Service(), CoroutineScope {

val playbackState: PlaybackState? get() = mediaSession?.controller?.playbackState

/**
* only trip this flag if the user switches from headphones to speaker
* prevent stopping music when inserting headphones for the first time
*/
private var headphoneFlag = false
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
AudioManager.ACTION_HEADSET_PLUG -> {
val state = intent.getIntExtra("state", 2)
if (state == 0) {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PLAY_PAUSE)
headphoneFlag = true
} else if (headphoneFlag) {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PLAY_PAUSE)
}
val state = intent.getIntExtra("state", 0)
// Pause playback when unplugging headphones
if (state == 0) webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PAUSE)
}
BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED -> {
val extras = intent.extras ?: return
val state = extras.getInt(BluetoothA2dp.EXTRA_STATE)
val previousState = extras.getInt(BluetoothA2dp.EXTRA_PREVIOUS_STATE)
if ((state == BluetoothA2dp.STATE_DISCONNECTED || state == BluetoothA2dp.STATE_DISCONNECTING) && previousState == BluetoothA2dp.STATE_CONNECTED) {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PAUSE)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PAUSE)
}
}
BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED -> {
val extras = intent.extras ?: return
val state = extras.getInt(BluetoothHeadset.EXTRA_STATE)
val previousState = extras.getInt(BluetoothHeadset.EXTRA_PREVIOUS_STATE)
if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && previousState == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PAUSE)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PAUSE)
}
}
}
Expand Down Expand Up @@ -145,7 +136,7 @@ class RemotePlayerService : Service(), CoroutineScope {
createMediaNotificationChannel(notificationManager)
}

override fun onBind(intent: Intent): IBinder? {
override fun onBind(intent: Intent): IBinder {
return binder
}

Expand Down Expand Up @@ -346,31 +337,31 @@ class RemotePlayerService : Service(), CoroutineScope {
setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS or MediaSession.FLAG_HANDLES_MEDIA_BUTTONS)
setCallback(object : MediaSession.Callback() {
override fun onPlay() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PLAY_PAUSE)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PLAY)
}

override fun onPause() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PLAY_PAUSE)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PAUSE)
}

override fun onSkipToPrevious() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_PREVIOUS)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_PREVIOUS)
}

override fun onSkipToNext() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_NEXT)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_NEXT)
}

override fun onRewind() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_REWIND)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_REWIND)
}

override fun onFastForward() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_FAST_FORWARD)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_FAST_FORWARD)
}

override fun onStop() {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_STOP)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_STOP)
onStopped()
}

Expand All @@ -390,7 +381,6 @@ class RemotePlayerService : Service(), CoroutineScope {
private fun onStopped() {
notificationManager.cancel(MEDIA_PLAYER_NOTIFICATION_ID)
mediaSession?.isActive = false
headphoneFlag = false
stopWakelock()
stopSelf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package org.jellyfin.mobile.webapp

import android.media.AudioManager
import android.media.VolumeProvider
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_VOL_DOWN
import org.jellyfin.mobile.utils.Constants.INPUT_MANAGER_COMMAND_VOL_UP
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_VOL_DOWN
import org.jellyfin.mobile.utils.Constants.PLAYBACK_MANAGER_COMMAND_VOL_UP

class RemoteVolumeProvider(
private val webappFunctionChannel: WebappFunctionChannel
) : VolumeProvider(VOLUME_CONTROL_ABSOLUTE, 100, 0) {
override fun onAdjustVolume(direction: Int) {
when (direction) {
AudioManager.ADJUST_RAISE -> {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_VOL_UP)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_VOL_UP)
currentVolume += 2 // TODO: have web notify app with new volume instead
}
AudioManager.ADJUST_LOWER -> {
webappFunctionChannel.triggerInputManagerAction(INPUT_MANAGER_COMMAND_VOL_DOWN)
webappFunctionChannel.callPlaybackManager(PLAYBACK_MANAGER_COMMAND_VOL_DOWN)
currentVolume -= 2 // TODO: have web notify app with new volume instead
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class WebappFunctionChannel {
fun call(action: String) = internalChannel.offer(action)

// Web component helpers
private fun callWebComponent(component: String, cmd: String) = call("require(['$component'], function($component){$component.$cmd;});")
fun triggerInputManagerAction(action: String) = callWebComponent("inputManager", "trigger('$action')")
fun seekTo(pos: Long) = callWebComponent("inputManager", "trigger('seek', $pos)")
fun setVolume(volume: Int) = callWebComponent("playbackManager", "sendCommand({Name: 'SetVolume', Arguments: {Volume: $volume}})")
fun callPlaybackManager(action: String) = call("window.NavigationHelper.playbackManager.$action();")
fun setVolume(volume: Int) = callPlaybackManager("sendCommand({Name: 'SetVolume', Arguments: {Volume: $volume}})")
fun seekTo(pos: Long) = callPlaybackManager("seekMs($pos)")
fun goBack() = call("window.NavigationHelper.goBack();")

// ExoPlayer helpers
fun exoPlayerNotifyStopped() = call("window.ExoPlayer.notifyStopped()")
Expand Down

0 comments on commit 8afc758

Please sign in to comment.