Skip to content

Commit

Permalink
[skip ci] Added option to select the preferred language for subs (Res…
Browse files Browse the repository at this point in the history
…olves #239) (#393)
  • Loading branch information
JohnVictoryz authored Nov 18, 2024
1 parent 3d4f5aa commit 9fa326c
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
26 changes: 25 additions & 1 deletion app/src/main/java/ani/dantotsu/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import java.io.FileOutputStream
import java.io.OutputStream
import java.lang.reflect.Field
import java.util.Calendar
import java.util.Locale
import java.util.TimeZone
import java.util.Timer
import java.util.TimerTask
Expand Down Expand Up @@ -1538,4 +1539,27 @@ fun getYoutubeId(url: String): String {
val regex = """(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|(?:youtu\.be|youtube\.com)/)([^"&?/\s]{11})|youtube\.com/""".toRegex()
val matchResult = regex.find(url)
return matchResult?.groupValues?.getOrNull(1) ?: ""
}
}

fun getLanguageCode(language: String): CharSequence {
val locales = Locale.getAvailableLocales()
for (locale in locales) {
if (locale.displayLanguage.equals(language, ignoreCase = true)) {
val lang: CharSequence = locale.language
return lang

}
}
val out: CharSequence = "null"
return out
}

fun getLanguageName(language: String): String? {
val locales = Locale.getAvailableLocales()
for (locale in locales) {
if (locale.language.equals(language, ignoreCase = true)) {
return locale.displayLanguage
}
}
return null
}
47 changes: 46 additions & 1 deletion app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import ani.dantotsu.download.DownloadsManager.Companion.getSubDirectory
import ani.dantotsu.download.video.Helper
import ani.dantotsu.dp
import ani.dantotsu.getCurrentBrightnessValue
import ani.dantotsu.getLanguageCode
import ani.dantotsu.hideSystemBars
import ani.dantotsu.hideSystemBarsExtendView
import ani.dantotsu.isOnline
Expand Down Expand Up @@ -1396,13 +1397,57 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
val ext = episode.extractors?.find { it.server.name == episode.selectedExtractor } ?: return
extractor = ext
video = ext.videos.getOrNull(episode.selectedVideo) ?: return
val subLanguages = arrayOf(
"Albanian",
"Arabic",
"Bosnian",
"Bulgarian",
"Chinese",
"Croatian",
"Czech",
"Danish",
"Dutch",
"English",
"Estonian",
"Finnish",
"French",
"Georgian",
"German",
"Greek",
"Hebrew",
"Hindi",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Korean",
"Lithuanian",
"Luxembourgish",
"Macedonian",
"Mongolian",
"Norwegian",
"Polish",
"Portuguese",
"Punjabi",
"Romanian",
"Russian",
"Serbian",
"Slovak",
"Slovenian",
"Spanish",
"Turkish",
"Ukrainian",
"Urdu",
"Vietnamese",
)
val lang = subLanguages[PrefManager.getVal(PrefName.SubLanguage)]
subtitle = intent.getSerialized("subtitle")
?: when (val subLang: String? =
PrefManager.getNullableCustomVal("subLang_${media.id}", null, String::class.java)) {
null -> {
when (episode.selectedSubtitle) {
null -> null
-1 -> ext.subtitles.find { it.language.trim() == "English" || it.language == "en-US" }
-1 -> ext.subtitles.find { it.language.contains( lang, ignoreCase = true ) || it.language.contains( getLanguageCode(lang), ignoreCase = true ) }
else -> ext.subtitles.getOrNull(episode.selectedSubtitle!!)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ani.dantotsu.settings

import android.app.AlertDialog
import android.app.Dialog
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
Expand Down Expand Up @@ -32,7 +33,6 @@ import ani.dantotsu.util.customAlertDialog
import com.google.android.material.slider.Slider.OnChangeListener
import kotlin.math.roundToInt


class PlayerSettingsActivity : AppCompatActivity() {
lateinit var binding: ActivityPlayerSettingsBinding
private val player = "player_settings"
Expand Down Expand Up @@ -318,6 +318,61 @@ class PlayerSettingsActivity : AppCompatActivity() {
toggleSubOptions(isChecked)
}
toggleSubOptions(binding.subSwitch.isChecked)
val subLanguages = arrayOf(
"Albanian",
"Arabic",
"Bosnian",
"Bulgarian",
"Chinese",
"Croatian",
"Czech",
"Danish",
"Dutch",
"English",
"Estonian",
"Finnish",
"French",
"Georgian",
"German",
"Greek",
"Hebrew",
"Hindi",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Korean",
"Lithuanian",
"Luxembourgish",
"Macedonian",
"Mongolian",
"Norwegian",
"Polish",
"Portuguese",
"Punjabi",
"Romanian",
"Russian",
"Serbian",
"Slovak",
"Slovenian",
"Spanish",
"Turkish",
"Ukrainian",
"Urdu",
"Vietnamese",
)
val subLanguageDialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle(getString(R.string.subtitle_langauge))
binding.videoSubLanguage.setOnClickListener {
val dialog = subLanguageDialog.setSingleChoiceItems(
subLanguages,
PrefManager.getVal(PrefName.SubLanguage)
) { dialog, count ->
PrefManager.setVal(PrefName.SubLanguage, count)
dialog.dismiss()
}.show()
dialog.window?.setDimAmount(0.8f)
}
val colorsPrimary =
arrayOf(
"Black",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
CursedSpeeds(Pref(Location.Player, Boolean::class, false)),
Resize(Pref(Location.Player, Int::class, 0)),
Subtitles(Pref(Location.Player, Boolean::class, true)),
SubLanguage(Pref(Location.Player, Int::class, 9)),
PrimaryColor(Pref(Location.Player, Int::class, 4)),
SecondaryColor(Pref(Location.Player, Int::class, 0)),
Outline(Pref(Location.Player, Int::class, 0)),
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_player_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@
app:showText="false"
app:thumbTint="@color/button_switch_track" />

<Button
android:id="@+id/videoSubLanguage"
style="@style/Widget.Material3.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_marginBottom="8dp"
android:fontFamily="@font/poppins_bold"
android:insetTop="0dp"
android:insetBottom="0dp"
android:paddingHorizontal="32dp"
android:text="@string/subtitle_langauge"
android:textAlignment="viewStart"
android:textAllCaps="false"
android:textColor="@color/bg_opp"
app:cornerRadius="0dp"
app:icon="@drawable/ic_round_subtitles_24"
app:iconPadding="16dp"
app:iconSize="24dp" />

<Button
android:id="@+id/videoSubColorPrimary"
style="@style/Widget.Material3.Button.TextButton"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@

<string name="question_18">I can\'t login to Anilist.</string>
<string name="answer_18">The reason this happens is probably because you\'re not using the default browser.\n\n>You have to set Chrome as your default browser to login to Anilist.\n\n>It takes a few seconds for the login button to display changes.\n\nIf it doesn\'t work then you could possibly be IP banned from Anilist or your ISP banned Anilist themselves. We believe that this is highly unlikely so open [Anilist](https://anilist.co) in your browser to see if that\`s the case.</string>

<string name="question_19">What is torrent? How do I use it?</string>
<string name="answer_19">Torrent or formally known as BitTorrent is an internet communication protocol for peer\-to\-peer file sharing. A torrent network doesn\'t use a centralised server to host files. It shares files in a decentralised manner. A torrent network has two types of peers. Seeders &amp; Leachers.\n\n• Seeders : These are peers who completed downloading and has the full file. And now they are sharing this file to other peers which is called seeding. A torrent cannot work without at least one seeder. The more seeder a torrent has, the better.\n\n• Leachers : These are peers who are currently downloading the file. If they currently have 40&#37; downloaded then they will share the 40&#37; to any other peers who requests it.\n\nUnlike a centralised server, torrents have no bandwidth limit. You can get your files from hundreds of seeders at the highest possible speed your internet can offer. But many ISP throttle torrent to slow them down because it\'s demanding on their infrastructure. Use a VPN to circumvent this.\n\n• How to use torrents :\n\n1. Install the Torrent Add-on from \`Dantotsu &gt; Settings &gt; Add-ons &gt; Torrent Add-on.\` \n2. Get a source that provides torrents.\n3. USE A VPN. Using VPN while torrenting only has upsides. Without a VPN your IP address will be exposed, your ISP will throttle your network and you could possibly be fined if you live in a country first world country. DO NOT USE IT WITHOUT A VPN IF YOU DON\'T KNOW WHAT YOU\'RE DOING.\n4. Now use that source to start torrenting.</string>

Expand Down Expand Up @@ -1073,5 +1073,6 @@ Non quae tempore quo provident laudantium qui illo dolor vel quia dolor et exerc
<string name="private_mode">Private</string>
<string name="omega_cursed">you have been Ǫ̴̺̙͎̤̫͓̮̰̿͝M̴͇̤͗́̾̈́̑̍̿̈͌͝Ȅ̴̡̨̛͉̣̙̩̲̣̤̟̪̣̎͗̎̆̒̉͆̆̕ͅͅǴ̸̯̬̗̠̙͛͐̀̈͋̀̈̽́̎̿͘͘͝ͅĀ̶̧̲̀ͅ ̴̢̟͕̜̓̾̓C̶̬̜̰̘̝̱̫͓͙̭̈́͐͋̓̏̈̍̓̀̌̾̚Ư̸̛̤̱̈́͆̽͊͛̐̓́̑͘̕̕͝R̸̨̨͈̬̱̺͕̪̪̘͕͎̂͛́̅̆̓̀͝ͅS̴̨̨̛̩̭̗̹̰̭̥͉̮̝̠̓̔͆̂͊͆̀̈́̅̕͘̚͝È̴̢̛̝͈̳͉͈͒͒̒̄̏̈̈́D̸̢̡̨̜̞̩̼̫̹̗̮͛̀̈̋̾̇̕̕͜ͅ</string>
<string name="omega_freed">you have been freed</string>
<string name="subtitle_langauge">Subtitle Langauge</string>

</resources>

0 comments on commit 9fa326c

Please sign in to comment.