|
| 1 | +package org.schabi.newpipe.util |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.SharedPreferences |
| 5 | +import android.util.Log |
| 6 | +import androidx.preference.PreferenceManager |
| 7 | +import org.schabi.newpipe.R |
| 8 | +import org.schabi.newpipe.extractor.NewPipe |
| 9 | +import org.schabi.newpipe.extractor.downloader.Downloader |
| 10 | + |
| 11 | +object ExtractorEngineHelper { |
| 12 | + private const val TAG = "ExtractorEngineHelper" |
| 13 | + |
| 14 | + enum class Engine { |
| 15 | + NEWPIPE, |
| 16 | + PIPEPIPE |
| 17 | + } |
| 18 | + |
| 19 | + @JvmStatic |
| 20 | + fun parsePreferenceValue( |
| 21 | + value: String?, |
| 22 | + newPipeValue: String, |
| 23 | + pipePipeValue: String |
| 24 | + ): Engine = when (value) { |
| 25 | + pipePipeValue -> Engine.PIPEPIPE |
| 26 | + newPipeValue -> Engine.NEWPIPE |
| 27 | + else -> Engine.NEWPIPE |
| 28 | + } |
| 29 | + |
| 30 | + @JvmStatic |
| 31 | + fun getSelectedEngine(context: Context): Engine = getSelectedEngine( |
| 32 | + context, |
| 33 | + PreferenceManager.getDefaultSharedPreferences(context) |
| 34 | + ) |
| 35 | + |
| 36 | + @JvmStatic |
| 37 | + fun getSelectedEngine(context: Context, preferences: SharedPreferences): Engine { |
| 38 | + val newPipeValue = context.getString(R.string.extractor_engine_newpipe_value) |
| 39 | + val selectedValue = preferences.getString( |
| 40 | + context.getString(R.string.extractor_engine_key), |
| 41 | + context.getString(R.string.extractor_engine_default_value) |
| 42 | + ) |
| 43 | + return parsePreferenceValue( |
| 44 | + selectedValue, |
| 45 | + newPipeValue, |
| 46 | + context.getString(R.string.extractor_engine_pipepipe_value) |
| 47 | + ).also { engine -> |
| 48 | + if (engine == Engine.NEWPIPE && selectedValue != null && selectedValue != newPipeValue) { |
| 49 | + Log.w(TAG, "Unknown extractor engine preference '$selectedValue'; using NewPipe Extractor") |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @JvmStatic |
| 55 | + fun initSelectedEngine(context: Context, downloader: Downloader) { |
| 56 | + val localization = Localization.getPreferredLocalization(context) |
| 57 | + val contentCountry = Localization.getPreferredContentCountry(context) |
| 58 | + when (getSelectedEngine(context)) { |
| 59 | + Engine.NEWPIPE -> NewPipe.init(downloader, localization, contentCountry) |
| 60 | + |
| 61 | + Engine.PIPEPIPE -> { |
| 62 | + Log.w( |
| 63 | + TAG, |
| 64 | + "PipePipe Extractor cannot be initialized from the current classpath " + |
| 65 | + "because the verified PipePipeExtractor source uses the same " + |
| 66 | + "org.schabi.newpipe.extractor packages/classes as NewPipe Extractor; " + |
| 67 | + "falling back to NewPipe Extractor" |
| 68 | + ) |
| 69 | + try { |
| 70 | + NewPipe.init(downloader, localization, contentCountry) |
| 71 | + } catch (throwable: Throwable) { |
| 72 | + Log.e(TAG, "Fallback NewPipe Extractor initialization failed", throwable) |
| 73 | + throw throwable |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments