Skip to content

Commit

Permalink
Cleanup more code
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Aug 4, 2019
1 parent 33b7ab5 commit 71d855e
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 306 deletions.
7 changes: 0 additions & 7 deletions app/src/main/java/com/topjohnwu/magisk/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.topjohnwu.magisk

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.res.Configuration
Expand Down Expand Up @@ -39,7 +38,6 @@ open class App : Application() {
}

deContext = base
self = this

if (Build.VERSION.SDK_INT >= 24) {
deContext = base.createDeviceProtectedStorageContext()
Expand All @@ -61,11 +59,6 @@ open class App : Application() {

companion object {

@SuppressLint("StaticFieldLeak")
@Deprecated("Use dependency injection")
@JvmStatic
lateinit var self: App

init {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
Shell.Config.setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_USE_MAGISK_BUSYBOX)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ val ApplicationInfo.packageInfo: PackageInfo?
val Uri.fileName: String
get() {
var name: String? = null
App.self.contentResolver.query(this, null, null, null, null)?.use { c ->
get<Context>().contentResolver.query(this, null, null, null, null)?.use { c ->
val nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (nameIndex != -1) {
c.moveToFirst()
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/com/topjohnwu/magisk/extensions/XJava.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.topjohnwu.magisk.extensions

import android.net.Uri
import android.os.Build
import androidx.core.net.toFile
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import kotlin.NoSuchElementException

fun ZipInputStream.forEach(callback: (ZipEntry) -> Unit) {
var entry: ZipEntry? = nextEntry
Expand Down Expand Up @@ -38,4 +41,63 @@ inline fun <T, R> List<T>.firstMap(mapper: (T) -> R?): R {
return mapper(item) ?: continue
}
throw NoSuchElementException("Collection contains no element matching the predicate.")
}

fun String.langTagToLocale(): Locale {
if (Build.VERSION.SDK_INT >= 21) {
return Locale.forLanguageTag(this)
} else {
val tok = split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (tok.isEmpty()) {
return Locale("")
}
val language = when (tok[0]) {
"und" -> "" // Undefined
"fil" -> "tl" // Filipino
else -> tok[0]
}
if (language.length != 2 && language.length != 3)
return Locale("")
if (tok.size == 1)
return Locale(language)
val country = tok[1]

return if (country.length != 2 && country.length != 3) Locale(language)
else Locale(language, country)
}
}

fun Locale.toLangTag(): String {
if (Build.VERSION.SDK_INT >= 21) {
return toLanguageTag()
} else {
var language = language
var country = country
var variant = variant
when {
language.isEmpty() || !language.matches("\\p{Alpha}{2,8}".toRegex()) ->
language = "und" // Follow the Locale#toLanguageTag() implementation
language == "iw" -> language = "he" // correct deprecated "Hebrew"
language == "in" -> language = "id" // correct deprecated "Indonesian"
language == "ji" -> language = "yi" // correct deprecated "Yiddish"
}
// ensure valid country code, if not well formed, it's omitted

// variant subtags that begin with a letter must be at least 5 characters long
// ensure valid country code, if not well formed, it's omitted
if (!country.matches("\\p{Alpha}{2}|\\p{Digit}{3}".toRegex())) {
country = ""
}

// variant subtags that begin with a letter must be at least 5 characters long
if (!variant.matches("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}".toRegex())) {
variant = ""
}
val tag = StringBuilder(language)
if (country.isNotEmpty())
tag.append('-').append(country)
if (variant.isNotEmpty())
tag.append('-').append(variant)
return tag.toString()
}
}
11 changes: 5 additions & 6 deletions app/src/main/java/com/topjohnwu/magisk/extensions/XTime.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.topjohnwu.magisk.extensions

import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.currentLocale
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
Expand All @@ -14,8 +14,7 @@ fun String.toTime(format: DateFormat) = try {
-1L
}

private val locale get() = LocaleManager.locale
val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", locale) }
val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale) }
val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM, LocaleManager.locale) }
val timeFormatTime by lazy { SimpleDateFormat("h:mm a", LocaleManager.locale) }
val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", currentLocale) }
val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", currentLocale) }
val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM, currentLocale) }
val timeFormatTime by lazy { SimpleDateFormat("h:mm a", currentLocale) }

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ open class GeneralReceiver : BroadcastReceiver() {
// Actual boot completed event
Shell.su("mm_patch_dtbo").submit { result ->
if (result.isSuccess)
Notifications.dtboPatched()
Notifications.dtboPatched(context)
}
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class UpdateCheckService : DelegateWorker() {
return runCatching {
magiskRepo.fetchUpdate().blockingGet()
if (BuildConfig.VERSION_CODE < Info.remote.app.versionCode)
Notifications.managerUpdate()
Notifications.managerUpdate(applicationContext)
else if (Info.magiskVersionCode < Info.remote.magisk.versionCode)
Notifications.magiskUpdate()
Notifications.magiskUpdate(applicationContext)
ListenableWorker.Result.success()
}.getOrElse {
ListenableWorker.Result.failure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.topjohnwu.magisk.model.navigation.Navigator
import com.topjohnwu.magisk.model.permissions.PermissionRequestBuilder
import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.currentLocale
import timber.log.Timber
import kotlin.reflect.KClass

Expand Down Expand Up @@ -68,12 +69,12 @@ abstract class MagiskActivity<ViewModel : MagiskViewModel, Binding : ViewDataBin

override fun applyOverrideConfiguration(config: Configuration?) {
// Force applying our preferred local
config?.setLocale(LocaleManager.locale)
config?.setLocale(currentLocale)
super.applyOverrideConfiguration(config)
}

override fun attachBaseContext(base: Context) {
super.attachBaseContext(LocaleManager.getLocaleContext(base, LocaleManager.locale))
super.attachBaseContext(LocaleManager.getLocaleContext(base))
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.topjohnwu.magisk.ui.home

import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.skoumal.teanity.extensions.subscribeK
import com.skoumal.teanity.viewevents.ViewEvent
import com.topjohnwu.magisk.*
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.Info
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.MagiskRepository
import com.topjohnwu.magisk.databinding.FragmentMagiskBinding
import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.extensions.inject
import com.topjohnwu.magisk.extensions.writeTo
import com.topjohnwu.magisk.model.events.*
Expand Down Expand Up @@ -114,7 +119,7 @@ class HomeFragment : MagiskFragment<HomeViewModel, FragmentMagiskBinding>(),
}

companion object {
val EXT_APK = File("${App.self.filesDir.parent}/snet", "snet.apk")
val EXT_APK by lazy { File("${get<Context>().filesDir.parent}/snet", "snet.apk") }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.database.RepoDao
import com.topjohnwu.magisk.databinding.CustomDownloadDialogBinding
import com.topjohnwu.magisk.extensions.toLangTag
import com.topjohnwu.magisk.model.download.DownloadService
import com.topjohnwu.magisk.model.entity.internal.Configuration
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.model.observer.Observer
import com.topjohnwu.magisk.ui.base.BasePreferenceFragment
import com.topjohnwu.magisk.utils.FingerprintHelper
import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.PatchAPK
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.*
import com.topjohnwu.magisk.view.dialogs.FingerprintAuthDialog
import com.topjohnwu.net.Networking
import com.topjohnwu.superuser.Shell
Expand Down Expand Up @@ -226,29 +224,26 @@ class SettingsFragment : BasePreferenceFragment() {

private fun setLocalePreference(lp: ListPreference) {
lp.isEnabled = false
LocaleManager.availableLocales
.map {
availableLocales.map {
val names = mutableListOf<String>()
val values = mutableListOf<String>()

names.add(
LocaleManager.getString(
LocaleManager.defaultLocale, R.string.system_default
)
LocaleManager.getString(defaultLocale, R.string.system_default)
)
values.add("")

it.forEach { locale ->
names.add(locale.getDisplayName(locale))
values.add(LocaleManager.toLanguageTag(locale))
values.add(locale.toLangTag())
}

Pair(names.toTypedArray(), values.toTypedArray())
}.subscribeK { (names, values) ->
lp.isEnabled = true
lp.entries = names
lp.entryValues = values
lp.summary = LocaleManager.locale.getDisplayName(LocaleManager.locale)
lp.summary = currentLocale.getDisplayName(currentLocale)
}
}

Expand Down
Loading

0 comments on commit 71d855e

Please sign in to comment.