Skip to content

Commit 423c427

Browse files
committed
Correctly handle Kodi action in long press menu
Automatically enable/disable it when changing the "Show play with Kodi" setting Include it in the default actions if the "Show play with Kodi" setting is enabled Hide the Kodi action if Kodi would not support the service.
1 parent 5575c89 commit 423c427

5 files changed

Lines changed: 73 additions & 20 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.settings;
22

3+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuSettingsKt.addOrRemoveKodiLongPressAction;
4+
35
import android.content.SharedPreferences;
46
import android.content.res.Resources;
57
import android.os.Bundle;
@@ -49,6 +51,8 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
4951
updateSeekOptions();
5052
} else if (getString(R.string.show_higher_resolutions_key).equals(key)) {
5153
updateResolutionOptions();
54+
} else if (getString(R.string.show_play_with_kodi_key).equals(key)) {
55+
addOrRemoveKodiLongPressAction(requireContext());
5256
}
5357
};
5458
}

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressAction.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ data class LongPressAction(
106106
enabled: () -> Boolean = { true },
107107
action: suspend (context: Context) -> Unit
108108
) = LongPressAction(this, action, enabled)
109-
110-
companion object {
111-
// ShowChannelDetails is not enabled by default, since navigating to channel details can
112-
// also be done by clicking on the uploader name in the long press menu header
113-
val DefaultEnabledActions: List<Type> = listOf(
114-
ShowDetails, Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere,
115-
BackgroundShuffled, Download, AddToPlaylist, Share, OpenInBrowser, MarkAsWatched,
116-
Rename, SetAsPlaylistThumbnail, UnsetPlaylistThumbnail, Delete, Unsubscribe, Remove
117-
)
118-
}
119109
}
120110

121111
companion object {
@@ -249,11 +239,17 @@ data class LongPressAction(
249239
withContext(Dispatchers.IO) {
250240
HistoryRecordManager(context).markAsWatched(item).await()
251241
}
252-
},
253-
Type.PlayWithKodi.buildAction { context ->
254-
KoreUtils.playWithKore(context, item.url.toUri())
255242
}
256-
)
243+
) +
244+
if (KoreUtils.isServiceSupportedByKore(item.serviceId)) {
245+
listOf(
246+
Type.PlayWithKodi.buildAction(
247+
enabled = { KoreUtils.isServiceSupportedByKore(item.serviceId) }
248+
) { context -> KoreUtils.playWithKore(context, item.url.toUri()) }
249+
)
250+
} else {
251+
listOf()
252+
}
257253
}
258254

259255
/**
@@ -264,7 +260,6 @@ data class LongPressAction(
264260
fun fromStreamInfoItem(
265261
item: StreamInfoItem,
266262
queueFromHere: (() -> PlayQueue)?
267-
/* TODO isKodiEnabled: Boolean, */
268263
): List<LongPressAction> {
269264
return buildPlayerActionList { context -> fetchItemInfoIfSparse(context, item) } +
270265
(queueFromHere?.let { buildPlayerFromHereActionList(queueFromHere) } ?: listOf()) +

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuEditor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fun LongPressMenuEditorPage(onBackClick: () -> Unit) {
114114
title = stringResource(R.string.long_press_menu_actions_editor),
115115
onBackClick = onBackClick,
116116
actions = {
117-
ResetToDefaultsButton(state::resetToDefaults)
117+
ResetToDefaultsButton { state.resetToDefaults(context) }
118118
}
119119
) { paddingValues ->
120120
// test scrolling on Android TV by adding `.padding(horizontal = 350.dp)` here

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuEditorState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class LongPressMenuEditorState(
8888
}.toList()
8989
}
9090

91-
fun resetToDefaults() {
91+
fun resetToDefaults(context: Context) {
9292
items.clear()
93-
items.addAll(buildItemsInList(true, LongPressAction.Type.DefaultEnabledActions))
93+
items.addAll(buildItemsInList(true, getDefaultEnabledLongPressActions(context)))
9494
}
9595

9696
private fun findItemForOffsetOrClosestInRow(offset: IntOffset): LazyGridItemInfo? {

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenuSettings.kt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ import android.util.Log
55
import androidx.core.content.edit
66
import androidx.preference.PreferenceManager
77
import org.schabi.newpipe.R
8+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.AddToPlaylist
9+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Background
10+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.BackgroundFromHere
11+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.BackgroundShuffled
12+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Delete
13+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Download
14+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Enqueue
15+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.EnqueueNext
16+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.MarkAsWatched
17+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.OpenInBrowser
18+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.PlayWithKodi
19+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Popup
20+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Remove
21+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Rename
22+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.SetAsPlaylistThumbnail
23+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Share
24+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowDetails
25+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.UnsetPlaylistThumbnail
26+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Unsubscribe
827

928
private const val TAG: String = "LongPressMenuSettings"
1029

@@ -20,12 +39,35 @@ fun storeIsHeaderEnabledToSettings(context: Context, enabled: Boolean) {
2039
}
2140
}
2241

42+
// ShowChannelDetails is not enabled by default, since navigating to channel details can
43+
// also be done by clicking on the uploader name in the long press menu header.
44+
// PlayWithKodi is only added by default if it is enabled in settings.
45+
private val DefaultEnabledActions: List<LongPressAction.Type> = listOf(
46+
ShowDetails, Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere,
47+
BackgroundShuffled, Download, AddToPlaylist, Share, OpenInBrowser, MarkAsWatched,
48+
Rename, SetAsPlaylistThumbnail, UnsetPlaylistThumbnail, Delete, Unsubscribe, Remove
49+
)
50+
51+
private fun getShowPlayWithKodi(context: Context): Boolean {
52+
return PreferenceManager.getDefaultSharedPreferences(context)
53+
.getBoolean(context.getString(R.string.show_play_with_kodi_key), false)
54+
}
55+
56+
fun getDefaultEnabledLongPressActions(context: Context): List<LongPressAction.Type> {
57+
return if (getShowPlayWithKodi(context)) {
58+
// only include Kodi in the default actions if it is enabled in settings
59+
DefaultEnabledActions + listOf(PlayWithKodi)
60+
} else {
61+
DefaultEnabledActions
62+
}
63+
}
64+
2365
fun loadLongPressActionArrangementFromSettings(context: Context): List<LongPressAction.Type> {
2466
val key = context.getString(R.string.long_press_menu_action_arrangement_key)
2567
val items = PreferenceManager.getDefaultSharedPreferences(context)
2668
.getString(key, null)
2769
if (items == null) {
28-
return LongPressAction.Type.DefaultEnabledActions
70+
return getDefaultEnabledLongPressActions(context)
2971
}
3072

3173
try {
@@ -45,7 +87,7 @@ fun loadLongPressActionArrangementFromSettings(context: Context): List<LongPress
4587
return actionsDistinct
4688
} catch (e: NoSuchElementException) {
4789
Log.e(TAG, "Invalid action in settings", e)
48-
return LongPressAction.Type.DefaultEnabledActions
90+
return getDefaultEnabledLongPressActions(context)
4991
}
5092
}
5193

@@ -56,3 +98,15 @@ fun storeLongPressActionArrangementToSettings(context: Context, actions: List<Lo
5698
putString(key, items)
5799
}
58100
}
101+
102+
fun addOrRemoveKodiLongPressAction(context: Context) {
103+
val actions = loadLongPressActionArrangementFromSettings(context).toMutableList()
104+
if (getShowPlayWithKodi(context)) {
105+
if (!actions.contains(PlayWithKodi)) {
106+
actions.add(PlayWithKodi)
107+
}
108+
} else {
109+
actions.remove(PlayWithKodi)
110+
}
111+
storeLongPressActionArrangementToSettings(context, actions)
112+
}

0 commit comments

Comments
 (0)