@@ -3,11 +3,13 @@ package org.schabi.newpipe.local.subscription.workers
33import android.content.Context
44import android.content.pm.ServiceInfo
55import android.os.Build
6+ import android.os.Parcelable
67import android.webkit.MimeTypeMap
78import android.widget.Toast
89import androidx.core.app.NotificationCompat
910import androidx.core.net.toUri
1011import androidx.work.CoroutineWorker
12+ import androidx.work.Data
1113import androidx.work.ForegroundInfo
1214import androidx.work.WorkManager
1315import androidx.work.WorkerParameters
@@ -18,12 +20,11 @@ import kotlinx.coroutines.rx3.await
1820import kotlinx.coroutines.sync.Mutex
1921import kotlinx.coroutines.sync.withLock
2022import kotlinx.coroutines.withContext
23+ import kotlinx.parcelize.Parcelize
2124import org.schabi.newpipe.R
2225import org.schabi.newpipe.extractor.NewPipe
2326import org.schabi.newpipe.local.subscription.SubscriptionManager
2427import org.schabi.newpipe.util.ExtractorHelper
25- import org.schabi.newpipe.util.KEY_SERVICE_ID
26- import org.schabi.newpipe.util.NO_SERVICE_ID
2728
2829class SubscriptionImportWorker (
2930 appContext : Context ,
@@ -35,27 +36,29 @@ class SubscriptionImportWorker(
3536 }
3637
3738 override suspend fun doWork (): Result {
38- val mode = inputData.getInt(KEY_MODE , CHANNEL_URL_MODE )
39- val serviceId = inputData.getInt(KEY_SERVICE_ID , NO_SERVICE_ID )
40- val value = inputData.getString(KEY_VALUE )!!
39+ val input = SubscriptionImportInput .fromData(inputData)
4140
4241 val subscriptions = withContext(Dispatchers .IO ) {
43- if (mode == CHANNEL_URL_MODE ) {
44- NewPipe .getService(serviceId).subscriptionExtractor
45- .fromChannelUrl(value)
46- .map { SubscriptionItem (it.serviceId, it.url, it.name) }
47- } else {
48- applicationContext.contentResolver.openInputStream(value.toUri())?.use {
49- if (mode == INPUT_STREAM_MODE ) {
50- val contentType = MimeTypeMap .getFileExtensionFromUrl(value).ifEmpty { DEFAULT_MIME }
51- NewPipe .getService(serviceId).subscriptionExtractor
42+ when (input) {
43+ is SubscriptionImportInput .ChannelUrlMode ->
44+ NewPipe .getService(input.serviceId).subscriptionExtractor
45+ .fromChannelUrl(input.url)
46+ .map { SubscriptionItem (it.serviceId, it.url, it.name) }
47+
48+ is SubscriptionImportInput .InputStreamMode ->
49+ applicationContext.contentResolver.openInputStream(input.url.toUri())?.use {
50+ val contentType =
51+ MimeTypeMap .getFileExtensionFromUrl(input.url).ifEmpty { DEFAULT_MIME }
52+ NewPipe .getService(input.serviceId).subscriptionExtractor
5253 .fromInputStream(it, contentType)
5354 .map { SubscriptionItem (it.serviceId, it.url, it.name) }
54- } else {
55+ }
56+
57+ is SubscriptionImportInput .PreviousExportMode ->
58+ applicationContext.contentResolver.openInputStream(input.url.toUri())?.use {
5559 ImportExportJsonHelper .readFrom(it)
5660 }
57- } ? : emptyList()
58- }
61+ } ? : emptyList()
5962 }
6063
6164 val mutex = Mutex ()
@@ -146,10 +149,69 @@ class SubscriptionImportWorker(
146149 private const val BUFFER_COUNT_BEFORE_INSERT = 50
147150
148151 const val WORK_NAME = " SubscriptionImportWorker"
149- const val CHANNEL_URL_MODE = 0
150- const val INPUT_STREAM_MODE = 1
151- const val PREVIOUS_EXPORT_MODE = 2
152- const val KEY_MODE = " key_mode"
153- const val KEY_VALUE = " key_value"
152+ }
153+ }
154+
155+ sealed class SubscriptionImportInput : Parcelable {
156+ @Parcelize
157+ data class ChannelUrlMode (val serviceId : Int , val url : String ) : SubscriptionImportInput()
158+ @Parcelize
159+ data class InputStreamMode (val serviceId : Int , val url : String ) : SubscriptionImportInput()
160+ @Parcelize
161+ data class PreviousExportMode (val url : String ) : SubscriptionImportInput()
162+
163+ fun toData (): Data {
164+ return when (this ) {
165+ is ChannelUrlMode -> Data .Builder ()
166+ .putInt(" mode" , CHANNEL_URL_MODE )
167+ .putInt(" service_id" , serviceId)
168+ .putString(" url" , url)
169+ .build()
170+ is InputStreamMode ->
171+ Data .Builder ()
172+ .putInt(" mode" , INPUT_STREAM_MODE )
173+ .putInt(" service_id" , serviceId)
174+ .putString(" url" , url)
175+ .build()
176+ is PreviousExportMode ->
177+ Data .Builder ()
178+ .putInt(" mode" , PREVIOUS_EXPORT_MODE )
179+ .putString(" url" , url)
180+ .build()
181+ }
182+ }
183+
184+ companion object {
185+
186+ private const val CHANNEL_URL_MODE = 0
187+ private const val INPUT_STREAM_MODE = 1
188+ private const val PREVIOUS_EXPORT_MODE = 2
189+
190+ fun fromData (data : Data ): SubscriptionImportInput {
191+ val mode = data.getInt(" mode" , PREVIOUS_EXPORT_MODE )
192+ when (mode) {
193+ CHANNEL_URL_MODE -> {
194+ val serviceId = data.getInt(" service_id" , - 1 )
195+ if (serviceId == - 1 ) {
196+ throw IllegalArgumentException (" No service id provided" )
197+ }
198+ val url = data.getString(" url" )!!
199+ return ChannelUrlMode (serviceId, url)
200+ }
201+ INPUT_STREAM_MODE -> {
202+ val serviceId = data.getInt(" service_id" , - 1 )
203+ if (serviceId == - 1 ) {
204+ throw IllegalArgumentException (" No service id provided" )
205+ }
206+ val url = data.getString(" url" )!!
207+ return InputStreamMode (serviceId, url)
208+ }
209+ PREVIOUS_EXPORT_MODE -> {
210+ val url = data.getString(" url" )!!
211+ return PreviousExportMode (url)
212+ }
213+ else -> throw IllegalArgumentException (" Unknown mode: $mode " )
214+ }
215+ }
154216 }
155217}
0 commit comments