Skip to content

Commit 4dda52b

Browse files
author
Yevhen Babiichuk (DustDFG)
committed
Convert newpipe/util/ServiceHelper to kotlin
1 parent da912b5 commit 4dda52b

2 files changed

Lines changed: 184 additions & 213 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java

Lines changed: 0 additions & 213 deletions
This file was deleted.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2018-2026 NewPipe contributors <https://newpipe.net>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package org.schabi.newpipe.util
7+
8+
import android.content.Context
9+
import androidx.annotation.DrawableRes
10+
import androidx.annotation.StringRes
11+
import androidx.core.content.edit
12+
import androidx.preference.PreferenceManager
13+
import com.grack.nanojson.JsonObject
14+
import com.grack.nanojson.JsonParser
15+
import com.grack.nanojson.JsonParserException
16+
import org.schabi.newpipe.R
17+
import org.schabi.newpipe.extractor.NewPipe
18+
import org.schabi.newpipe.extractor.ServiceList
19+
import org.schabi.newpipe.extractor.StreamingService
20+
import org.schabi.newpipe.extractor.exceptions.ExtractionException
21+
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance
22+
import org.schabi.newpipe.ktx.getStringSafe
23+
import java.util.concurrent.TimeUnit
24+
25+
object ServiceHelper {
26+
private val DEFAULT_FALLBACK_SERVICE: StreamingService = ServiceList.YouTube
27+
28+
@JvmStatic
29+
@DrawableRes
30+
fun getIcon(serviceId: Int): Int {
31+
return when (serviceId) {
32+
0 -> R.drawable.ic_smart_display
33+
1 -> R.drawable.ic_cloud
34+
2 -> R.drawable.ic_placeholder_media_ccc
35+
3 -> R.drawable.ic_placeholder_peertube
36+
4 -> R.drawable.ic_placeholder_bandcamp
37+
else -> R.drawable.ic_circle
38+
}
39+
}
40+
41+
@JvmStatic
42+
fun getTranslatedFilterString(filter: String, context: Context): String {
43+
return when (filter) {
44+
"all" -> context.getString(R.string.all)
45+
"videos", "sepia_videos", "music_videos" -> context.getString(R.string.videos_string)
46+
"channels" -> context.getString(R.string.channels)
47+
"playlists", "music_playlists" -> context.getString(R.string.playlists)
48+
"tracks" -> context.getString(R.string.tracks)
49+
"users" -> context.getString(R.string.users)
50+
"conferences" -> context.getString(R.string.conferences)
51+
"events" -> context.getString(R.string.events)
52+
"music_songs" -> context.getString(R.string.songs)
53+
"music_albums" -> context.getString(R.string.albums)
54+
"music_artists" -> context.getString(R.string.artists)
55+
else -> filter
56+
}
57+
}
58+
59+
/**
60+
* Get a resource string with instructions for importing subscriptions for each service.
61+
*
62+
* @param serviceId service to get the instructions for
63+
* @return the string resource containing the instructions or -1 if the service don't support it
64+
*/
65+
@JvmStatic
66+
@StringRes
67+
fun getImportInstructions(serviceId: Int): Int {
68+
return when (serviceId) {
69+
0 -> R.string.import_youtube_instructions
70+
1 -> R.string.import_soundcloud_instructions
71+
else -> -1
72+
}
73+
}
74+
75+
/**
76+
* For services that support importing from a channel url, return a hint that will
77+
* be used in the EditText that the user will type in his channel url.
78+
*
79+
* @param serviceId service to get the hint for
80+
* @return the hint's string resource or -1 if the service don't support it
81+
*/
82+
@JvmStatic
83+
@StringRes
84+
fun getImportInstructionsHint(serviceId: Int): Int {
85+
return when (serviceId) {
86+
1 -> R.string.import_soundcloud_instructions_hint
87+
else -> -1
88+
}
89+
}
90+
91+
@JvmStatic
92+
fun getSelectedServiceId(context: Context): Int {
93+
return (getSelectedService(context) ?: DEFAULT_FALLBACK_SERVICE).serviceId
94+
}
95+
96+
@JvmStatic
97+
fun getSelectedService(context: Context): StreamingService? {
98+
val serviceName: String = PreferenceManager.getDefaultSharedPreferences(context)
99+
.getStringSafe(
100+
context.getString(R.string.current_service_key),
101+
context.getString(R.string.default_service_value)
102+
)
103+
104+
try {
105+
return NewPipe.getService(serviceName)
106+
} catch (_: ExtractionException) {
107+
return null
108+
}
109+
}
110+
111+
@JvmStatic
112+
fun getNameOfServiceById(serviceId: Int): String {
113+
return ServiceList.all().stream()
114+
.filter { it.serviceId == serviceId }
115+
.findFirst()
116+
.map(StreamingService::getServiceInfo)
117+
.map(StreamingService.ServiceInfo::getName)
118+
.orElse("<unknown>")
119+
}
120+
121+
/**
122+
* @param serviceId the id of the service
123+
* @return the service corresponding to the provided id
124+
* @throws java.util.NoSuchElementException if there is no service with the provided id
125+
*/
126+
@JvmStatic
127+
fun getServiceById(serviceId: Int): StreamingService {
128+
return ServiceList.all().firstNotNullOf { it.takeIf { it.serviceId == serviceId } }
129+
}
130+
131+
@JvmStatic
132+
fun setSelectedServiceId(context: Context, serviceId: Int) {
133+
var serviceName: String?
134+
try {
135+
serviceName = NewPipe.getService(serviceId).serviceInfo.name
136+
} catch (_: ExtractionException) {
137+
serviceName = DEFAULT_FALLBACK_SERVICE.serviceInfo.name
138+
}
139+
140+
setSelectedServicePreferences(context, serviceName)
141+
}
142+
143+
private fun setSelectedServicePreferences(context: Context, serviceName: String?) {
144+
val sp = PreferenceManager.getDefaultSharedPreferences(context)
145+
sp.edit { putString(context.getString(R.string.current_service_key), serviceName) }
146+
}
147+
148+
@JvmStatic
149+
fun getCacheExpirationMillis(serviceId: Int): Long {
150+
return if (serviceId == ServiceList.SoundCloud.serviceId) {
151+
TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES)
152+
} else {
153+
TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)
154+
}
155+
}
156+
157+
fun initService(context: Context, serviceId: Int) {
158+
if (serviceId == ServiceList.PeerTube.serviceId) {
159+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
160+
val json = sharedPreferences.getString(
161+
context.getString(R.string.peertube_selected_instance_key),
162+
null
163+
)
164+
if (null == json) {
165+
return
166+
}
167+
168+
val jsonObject: JsonObject
169+
try {
170+
jsonObject = JsonParser.`object`().from(json)
171+
} catch (_: JsonParserException) {
172+
return
173+
}
174+
val name = jsonObject.getString("name")
175+
val url = jsonObject.getString("url")
176+
ServiceList.PeerTube.instance = PeertubeInstance(url, name)
177+
}
178+
}
179+
180+
@JvmStatic
181+
fun initServices(context: Context) {
182+
ServiceList.all().forEach { initService(context, it.serviceId) }
183+
}
184+
}

0 commit comments

Comments
 (0)