VancedManager/app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt

78 lines
2.4 KiB
Kotlin
Raw Normal View History

2020-09-26 17:09:01 +00:00
package com.vanced.manager.utils
2020-10-31 19:45:39 +00:00
import android.app.Activity
2020-09-26 17:09:01 +00:00
import android.content.Context
import android.content.Intent
2020-10-31 19:45:39 +00:00
import android.content.res.Resources
import android.net.Uri
2020-10-31 19:45:39 +00:00
import android.os.Build
import android.os.LocaleList
import android.provider.Settings
2020-10-31 19:45:39 +00:00
import androidx.annotation.RequiresApi
import com.crowdin.platform.Crowdin
2020-09-26 17:09:01 +00:00
import com.vanced.manager.R
import java.util.*
2021-01-16 17:00:38 +00:00
fun getLanguageFormat(context: Context, language: String): String {
return when {
language == "System Default" -> context.getString(R.string.system_default)
2021-01-16 22:09:57 +00:00
language.contains("_") -> {
2021-01-16 17:00:38 +00:00
val loc = Locale(
2021-01-16 22:09:57 +00:00
language.substringBefore("_"),
language.substringAfter("_")
2021-01-16 17:00:38 +00:00
)
loc.getDisplayName(loc).capitalize(Locale.ENGLISH)
}
else -> {
val loc = Locale(language)
loc.getDisplayName(loc).capitalize(Locale.ENGLISH)
2020-09-26 17:09:01 +00:00
}
}
2021-01-16 17:00:38 +00:00
}
2020-10-31 19:45:39 +00:00
2021-01-16 17:00:38 +00:00
@Suppress("DEPRECATION")
fun getDefaultVancedLanguages(): String {
val serverLangs = vanced.value?.array("langs") ?: mutableListOf("")
val sysLocales = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) Resources.getSystem().configuration.locales.toLangTags() else arrayOf(Resources.getSystem().configuration.locale.language)
val finalLangs = mutableListOf<String>()
sysLocales.forEach { sysLocale ->
when {
sysLocale == "en" -> finalLangs.add(sysLocale)
serverLangs.contains(sysLocale) -> finalLangs.add(sysLocale)
}
2020-10-31 19:45:39 +00:00
}
2021-01-16 17:00:38 +00:00
return finalLangs.distinct().sorted().joinToString(", ")
}
@RequiresApi(Build.VERSION_CODES.N)
fun LocaleList.toLangTags(): Array<String> {
val langTags: Array<String> = this.toLanguageTags().split(",").toTypedArray()
for (i in 0 until this.size()) {
langTags[i] = langTags[i].substring(0, 2)
2020-10-31 19:45:39 +00:00
}
2021-01-16 17:00:38 +00:00
return langTags
}
2020-10-31 19:45:39 +00:00
2021-01-16 17:00:38 +00:00
fun Activity.authCrowdin() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName")
)
startActivityForResult(intent, 69)
return
}
2021-01-16 17:00:38 +00:00
Crowdin.authorize(this)
}
2021-01-16 17:00:38 +00:00
}
2021-01-16 17:00:38 +00:00
fun Activity.onActivityResult(requestCode: Int) {
if (requestCode == 69 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
Crowdin.authorize(this)
}
}
2020-09-26 17:09:01 +00:00
}