Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
added a language selector
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Sep 26, 2020
1 parent 7cfc16d commit 8206046
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 10 deletions.
51 changes: 50 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.regex.Matcher
import java.util.regex.Pattern

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
Expand All @@ -7,6 +10,8 @@ apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'androidx.navigation.safeargs.kotlin'

ext.langs = getLanguages()

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
Expand All @@ -19,6 +24,9 @@ android {
versionName "1.3.0b (Arcturus)"

vectorDrawables.useSupportLibrary = true

buildConfigField "String[]", "MANAGER_LANGUAGES", "{" + surroundWithQuotes(langs) + "}"
buildConfigField "String[]", "MANAGER_LANGUAGE_NAMES", "{" + surroundWithQuotes(getLanguageNames()) + "}"
}

lintOptions {
Expand Down Expand Up @@ -57,14 +65,55 @@ android {

}

def getLanguages() {
List<String> langs = new ArrayList<String>()
langs.add("en")
Pattern pattern = Pattern.compile("-(\\w+)-")
new File("${projectDir}/src/main/res").eachDir { dir ->
if (dir.name.startsWith("values-") && !dir.name.contains("v23")) {
Matcher matcher = pattern.matcher(dir.name)
if (matcher.find()) {
if (langs.contains(matcher.group(1)))
langs.add(matcher.group(1) + "_${dir.name.substring(dir.name.length() - 2)}")
else
langs.add(matcher.group(1))
}
}
}
return langs.toArray()
}

def getLanguageNames() {
List<String> langnames = new ArrayList<String>()
for (int i = 0; i < langs.size(); i++) {
if (langs[i].length() > 2) {
Locale loc = new Locale(langs[i].substring(0, langs[i].length() - 3), langs[i].substring(langs[i].length() - 2))
project.logger.lifecycle(loc.getDisplayLanguage(loc).capitalize() + " (" + loc.getDisplayCountry(loc).capitalize() + ")")
langnames.add(loc.getDisplayLanguage(loc).capitalize() + " (" + loc.getDisplayCountry(loc).capitalize() + ")")
} else {
Locale loc = new Locale(langs[i])
project.logger.lifecycle(loc.getDisplayLanguage(loc).capitalize())
langnames.add(loc.getDisplayLanguage(loc).capitalize())
}
}
return langnames.toArray()
}

def surroundWithQuotes(Object[] arr) {
String[] dummyArr = arr
for (int i = 0; i < arr.length; i++) {
dummyArr[i] = "\"" + arr[i] + "\""
}
return dummyArr.join(", ")
}

dependencies {

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

// AndroidX
implementation 'androidx.activity:activity:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.browser:browser:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.vanced.manager">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ object VancedDownloader {
private var variant: String? = null
private var theme: String? = null
private var lang: Array<String>? = null
//private var newInstaller: Boolean? = null

private lateinit var themePath: String

Expand Down Expand Up @@ -85,7 +84,7 @@ object VancedDownloader {
when (type) {
"theme" -> "$themePath/$theme.apk"
"arch" -> "$installUrl/apks/v$vancedVersion/$variant/Arch/split_config.$arch.apk"
"stock" -> "$themePath/stock.apk"
"stock" -> "$themePath/stock.apk"
"dpi" -> "$themePath/dpi.apk"
"lang" -> "$installUrl/apks/v$vancedVersion/$variant/Language/split_config.${lang?.get(count)}.apk"
else -> throw NotImplementedError("This type of APK is NOT valid. What the hell did you even do?")
Expand Down Expand Up @@ -146,7 +145,7 @@ object VancedDownloader {
} else {
mutableInstall.value = false
vancedProgress.get()?.showDownloadBar?.set(false)
Toast.makeText(context, context.getString(R.string.error_downloading, "Vanced"), Toast.LENGTH_SHORT).show()
Toast.makeText(context, context.getString(R.string.error_downloading, getFileNameFromUrl(url)), Toast.LENGTH_SHORT).show()
}
}
})
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/com/vanced/manager/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.vanced.manager.ui.fragments.SettingsFragmentDirections
import com.vanced.manager.ui.fragments.UpdateCheckFragment
import com.vanced.manager.utils.AppUtils.installing
import com.vanced.manager.utils.InternetTools
import com.vanced.manager.utils.LanguageContextWrapper
import com.vanced.manager.utils.PackageHelper
import com.vanced.manager.utils.ThemeHelper.setFinalTheme
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -85,11 +86,6 @@ class MainActivity : AppCompatActivity() {
Crowdin.registerDataLoadingObserver(loadingObserver)
}

override fun recreate() {
startActivity(Intent(this, MainActivity::class.java))
finish()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (installing.value!!)
return false
Expand Down Expand Up @@ -118,7 +114,7 @@ class MainActivity : AppCompatActivity() {
}

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(Crowdin.wrapContext(newBase))
super.attachBaseContext(LanguageContextWrapper.wrap(newBase))
}

private fun initDialogs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.perf.FirebasePerformance
import com.vanced.manager.BuildConfig.MANAGER_LANGUAGES
import com.vanced.manager.BuildConfig.MANAGER_LANGUAGE_NAMES
import com.vanced.manager.R
import com.vanced.manager.utils.LanguageHelper.getLanguageFormat
import java.io.File

class SettingsFragment : PreferenceFragmentCompat() {
Expand Down Expand Up @@ -92,6 +95,21 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

val langPref = preferenceScreen.sharedPreferences.getString("manager_lang", "System Default")
preferenceScreen.findPreference<ListPreference>("manager_lang")?.apply {
summary = langPref?.let { getLanguageFormat(requireActivity(), it) }
entries = arrayOf(getString(R.string.system_default)) + MANAGER_LANGUAGE_NAMES
entryValues = arrayOf("System Default") + MANAGER_LANGUAGES

setOnPreferenceChangeListener { _, newValue ->
if (langPref != newValue) {
requireActivity().recreate()
return@setOnPreferenceChangeListener true
}
false
}
}

findPreference<Preference>("vanced_chosen_modes")?.setOnPreferenceClickListener {
ChosenPreferenceDialogFragment().show(childFragmentManager.beginTransaction(), "Chosen Preferences")
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.vanced.manager.utils

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import androidx.preference.PreferenceManager
import com.crowdin.platform.Crowdin
import java.util.*

class LanguageContextWrapper(base: Context?) : ContextWrapper(base) {

companion object {

fun wrap(context: Context): ContextWrapper {
val config: Configuration = context.resources.configuration
val pref = PreferenceManager.getDefaultSharedPreferences(context).getString("manager_lang", "System Default")
val locale =
when {
pref == "System Default" -> Locale(config.locale.displayLanguage)
pref?.length!! > 2 -> Locale(pref.substring(0, pref.length - 3), pref.substring(pref.length - 2))
else -> Locale(pref)
}
Locale.setDefault(locale)
config.setLocale(locale)
context.createConfigurationContext(config)
Crowdin.wrapContext(context)
return LanguageContextWrapper(context)
}

}

}
27 changes: 27 additions & 0 deletions app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.vanced.manager.utils

import android.content.Context
import com.vanced.manager.R
import java.util.*

object LanguageHelper {

fun getLanguageFormat(context: Context, language: String): String {
return when {
language == "System Default" -> context.getString(R.string.system_default)
language.length > 2 -> {
val loc = Locale(
language.substring(0, language.length - 3),
language.substring(language.length - 2)
)
loc.getDisplayName(loc)
}
else -> {
val loc = Locale(language)
loc.getDisplayName(loc)
}
}

}

}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_language_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorPrimaryVariant">
<path
android:fillColor="@android:color/white"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
<string name="cleared_files">Successfully cleared files</string>
<string name="firebase_title">Firebase Analytics</string>
<string name="firebase_summary">This lets us collect information about app performance and crash logs</string>
<string name="language_title">Language</string>
<string name="link_title">Use Chrome Custom Tabs</string>
<string name="link_custom_tabs">Links will open in Chrome Custom Tabs</string>
<string name="link_external_browser">Links will open in an External Browser</string>
<string name="new_installer_title">Use the new root installer (Experimental)</string>
<string name="new_installer_summary">This installer does not depend on Signature Verification status. WARNING: Vanced may disappear after reboot if you install it using this</string>
<string name="system_default">System Default</string>
<string name="theme">Theme</string>
<string name="theme_follow">Follow System</string>
<string name="theme_dark">Dark Theme</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
android:key="accent_color"
android:title="@string/accent_color" />

<ListPreference
android:defaultValue="System Default"
android:key="manager_lang"
android:title="@string/language_title"
android:icon="@drawable/ic_baseline_language_24"/>

</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit 8206046

Please sign in to comment.