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

fix: MEDIA_PLAY_FROM_SEARCH bugs #407

Merged
merged 3 commits into from
May 4, 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
27 changes: 26 additions & 1 deletion android/app/src/main/java/com/noxplay/noxplayer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.noxplay.noxplayer

import android.annotation.SuppressLint
import android.app.PictureInPictureParams
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
Expand All @@ -11,6 +12,7 @@ import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.facebook.react.bridge.Arguments
import expo.modules.ReactActivityDelegateWrapper

class MainActivity : ReactActivity() {
Expand All @@ -23,8 +25,29 @@ class MainActivity : ReactActivity() {
setShowWhenLocked(true)
setTurnScreenOn(true)
}
if ("trackplayer://service-bound" in intent.data.toString()) {
moveTaskToBack(true)
}
}

@SuppressLint("VisibleForTests")
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent !== null) {
if (intent.action?.contains("android.media.action.MEDIA_PLAY_FROM_SEARCH") == true) {
this.reactInstanceManager.currentReactContext
?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
?.emit("remote-play-search", Arguments.fromBundle(intent.extras))
}
val launchOptions = Bundle()
launchOptions.putString("intentData", intent.dataString)
launchOptions.putString("intentAction", intent.action)
launchOptions.putBundle("intentBundle", intent.extras)
this.reactInstanceManager.currentReactContext
?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
?.emit("APMNewIntent", Arguments.fromBundle(launchOptions))
}
}
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
Expand All @@ -41,9 +64,11 @@ class MainActivity : ReactActivity() {
super.onCreate(savedInstanceState)
}

override fun getLaunchOptions(): Bundle? {
override fun getLaunchOptions(): Bundle {
val launchOptions = super.getLaunchOptions() ?: Bundle()
launchOptions.putString("intentData", mActivity.intent.dataString)
launchOptions.putString("intentAction", mActivity.intent.action)
launchOptions.putBundle("intentBundle", mActivity.intent.extras)
return launchOptions
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/usePlayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import noxPlayingList, { setPlayingIndex } from '@stores/playingList';
import noxCache, { noxCacheKey } from '@utils/Cache';
import useDataSaver from './useDataSaver';
import useSnack from '@stores/useSnack';
import { PlaylistTypes } from '@enums/Playlist';

const PLAYLIST_MEDIAID = 'playlist-';

Expand All @@ -38,7 +39,6 @@ const dataSaverPlaylistWrapper = (datasave = true) => {
const usePlayback = () => {
const { t } = useTranslation();
const currentPlayingList = useNoxSetting(state => state.currentPlayingList);
const playlistIds = useNoxSetting(state => state.playlistIds);
const playlists = useNoxSetting(state => state.playlists);
const currentPlayingId = useNoxSetting(state => state.currentPlayingId);
const searchPlaylist = useNoxSetting(state => state.searchPlaylist);
Expand Down Expand Up @@ -82,7 +82,8 @@ const usePlayback = () => {
if (currentPlayingId !== song.id) {
await playSongUninterrupted(song);
}
clearPlaylistUninterrupted();
// HACK: WHY?
clearPlaylistUninterrupted().then(TrackPlayer.play);
};

const playAsSearchList = async ({
Expand All @@ -101,11 +102,9 @@ const usePlayback = () => {
};

const shuffleAll = async () => {
console.log(playlistIds, playlists);
let allSongs = playlistIds.reduce(
(acc, curr) => acc.concat(playlists[curr].songList),
[] as NoxMedia.Song[]
);
let allSongs = Object.values(playlists)
.filter(playlist => playlist.type === PlaylistTypes.Typical)
.reduce((acc, curr) => acc.concat(curr.songList), [] as NoxMedia.Song[]);
if (isDataSaving) {
const cachedSongs = Array.from(noxCache.noxMediaCache.cache.keys());
allSongs = allSongs.filter(song =>
Expand Down
3 changes: 3 additions & 0 deletions src/services/PlaybackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export async function PlaybackService() {
DeviceEventEmitter.addListener('APMEnterPIP', (e: boolean) =>
setState({ pipMode: e })
);
DeviceEventEmitter.addListener('APMNewIntent', (e: NoxComponent.AppProps) =>
console.log('apm', e)
);

TrackPlayer.addEventListener(Event.RemotePause, () => {
console.log('Event.RemotePause');
Expand Down
3 changes: 3 additions & 0 deletions src/types/component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ declare global {
namespace NoxComponent {
interface AppProps {
intentData?: IntentData;
intentAction: string;
intentBundle: null | any;
rootTag: number;
}
interface NavigationProps {
navigation: DrawerNavigationProp<ParamListBase>;
Expand Down
Loading