From 5fa452aa7429f87533e0d5f427e95aba2d4c539b Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 12 Aug 2019 01:54:33 -0700 Subject: [PATCH] Multiple minor changes --- app/src/main/java/com/topjohnwu/magisk/App.kt | 45 +++++++------------ .../magisk/data/database/PolicyDao.kt | 14 ++---- .../data/repository/MagiskRepository.kt | 4 +- .../topjohnwu/magisk/di/ApplicationModule.kt | 11 +++-- .../topjohnwu/magisk/extensions/XAndroid.kt | 20 +++++++-- .../magisk/model/entity/MagiskPolicy.kt | 3 +- .../magisk/model/receiver/GeneralReceiver.kt | 14 +++--- .../com/topjohnwu/magisk/ui/SplashActivity.kt | 2 +- .../magisk/ui/base/BasePreferenceFragment.kt | 2 - .../magisk/ui/settings/SettingsFragment.kt | 18 ++++---- .../java/com/topjohnwu/magisk/utils/Utils.kt | 35 ++------------- 11 files changed, 68 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index 85e9fb2b7..1224d400f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -3,7 +3,6 @@ package com.topjohnwu.magisk import android.app.Application import android.content.Context import android.content.res.Configuration -import android.os.Build import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex import androidx.room.Room @@ -14,9 +13,9 @@ import com.topjohnwu.magisk.data.database.RepoDatabase_Impl import com.topjohnwu.magisk.di.ActivityTracker import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.extensions.get +import com.topjohnwu.magisk.net.Networking import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.RootUtils -import com.topjohnwu.magisk.net.Networking import com.topjohnwu.superuser.Shell import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin @@ -24,7 +23,20 @@ import timber.log.Timber open class App : Application() { - lateinit var deContext: Context + init { + AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) + Shell.Config.setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_USE_MAGISK_BUSYBOX) + Shell.Config.verboseLogging(BuildConfig.DEBUG) + Shell.Config.addInitializers(RootUtils::class.java) + Shell.Config.setTimeout(2) + Room.setFactory { + when (it) { + WorkDatabase::class.java -> WorkDatabase_Impl() + RepoDatabase::class.java -> RepoDatabase_Impl() + else -> null + } + } + } override fun attachBaseContext(base: Context) { super.attachBaseContext(base) @@ -37,13 +49,6 @@ open class App : Application() { modules(koinModules) } - deContext = base - - if (Build.VERSION.SDK_INT >= 24) { - deContext = base.createDeviceProtectedStorageContext() - deContext.moveSharedPreferencesFrom(base, defaultPrefsName) - } - registerActivityLifecycleCallbacks(get()) Networking.init(base) @@ -54,24 +59,4 @@ open class App : Application() { super.onConfigurationChanged(newConfig) LocaleManager.setLocale(this) } - - private val Context.defaultPrefsName get() = "${packageName}_preferences" - - companion object { - - init { - AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) - Shell.Config.setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_USE_MAGISK_BUSYBOX) - Shell.Config.verboseLogging(BuildConfig.DEBUG) - Shell.Config.addInitializers(RootUtils::class.java) - Shell.Config.setTimeout(2) - Room.setFactory { - when (it) { - WorkDatabase::class.java -> WorkDatabase_Impl() - RepoDatabase::class.java -> RepoDatabase_Impl() - else -> null - } - } - } - } } diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt index d3a51aba0..3e4d0464f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt @@ -62,20 +62,14 @@ class PolicyDao( private fun Map.toPolicySafe(): MagiskPolicy? { - val taskResult = runCatching { toPolicy(context.packageManager) } - val result = taskResult.getOrNull() - val exception = taskResult.exceptionOrNull() - - Timber.e(exception) - - when (exception) { - is PackageManager.NameNotFoundException -> { + return runCatching { toPolicy(context.packageManager) }.getOrElse { + Timber.e(it) + if (it is PackageManager.NameNotFoundException) { val uid = getOrElse("uid") { null } ?: return null delete(uid).subscribe() } + null } - - return result } } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt index bc69296c2..0d73fc6c1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt @@ -5,11 +5,11 @@ import com.topjohnwu.magisk.Config import com.topjohnwu.magisk.Info import com.topjohnwu.magisk.data.database.base.su import com.topjohnwu.magisk.data.network.GithubRawServices +import com.topjohnwu.magisk.extensions.getLabel import com.topjohnwu.magisk.extensions.packageName import com.topjohnwu.magisk.extensions.toSingle import com.topjohnwu.magisk.model.entity.HideAppInfo import com.topjohnwu.magisk.model.entity.HideTarget -import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.superuser.Shell import io.reactivex.Single @@ -43,7 +43,7 @@ class MagiskRepository( .flattenAsFlowable { it } .filter { it.enabled && !blacklist.contains(it.packageName) } .map { - val label = Utils.getAppLabel(it, packageManager) + val label = it.getLabel(packageManager) val icon = it.loadIcon(packageManager) HideAppInfo(it, label, icon) } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt index e05af66fa..d03cec643 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt @@ -4,10 +4,10 @@ import android.annotation.SuppressLint import android.app.Activity import android.app.Application import android.content.Context +import android.os.Build import android.os.Bundle import androidx.preference.PreferenceManager import com.skoumal.teanity.rxbus.RxBus -import com.topjohnwu.magisk.App import org.koin.core.qualifier.named import org.koin.dsl.module @@ -17,15 +17,20 @@ val Protected = named("protected") val applicationModule = module { single { RxBus() } factory { get().resources } - factory { get() as App } factory { get().packageManager } - factory(Protected) { get().deContext } + factory(Protected) { createDEContext(get()) } single(SUTimeout) { get(Protected).getSharedPreferences("su_timeout", 0) } single { PreferenceManager.getDefaultSharedPreferences(get(Protected)) } single { ActivityTracker() } factory { get().foreground ?: NullActivity } } +private fun createDEContext(context: Context): Context { + return if (Build.VERSION.SDK_INT >= 24) + context.createDeviceProtectedStorageContext() + else context +} + class ActivityTracker : Application.ActivityLifecycleCallbacks { @Volatile diff --git a/app/src/main/java/com/topjohnwu/magisk/extensions/XAndroid.kt b/app/src/main/java/com/topjohnwu/magisk/extensions/XAndroid.kt index 2432c5fc2..abf5e81c9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/extensions/XAndroid.kt +++ b/app/src/main/java/com/topjohnwu/magisk/extensions/XAndroid.kt @@ -7,10 +7,12 @@ import android.content.pm.ComponentInfo import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.pm.PackageManager.* +import android.content.res.Configuration import android.database.Cursor import android.net.Uri import android.provider.OpenableColumns import com.topjohnwu.magisk.utils.FileProvider +import com.topjohnwu.magisk.utils.currentLocale import java.io.File import java.io.FileNotFoundException @@ -81,10 +83,6 @@ fun Context.rawResource(id: Int) = resources.openRawResource(id) fun Context.readUri(uri: Uri) = contentResolver.openInputStream(uri) ?: throw FileNotFoundException() -fun ApplicationInfo.findAppLabel(pm: PackageManager): String { - return pm.getApplicationLabel(this).toString().orEmpty() -} - fun Intent.startActivity(context: Context) = context.startActivity(this) fun File.provide(context: Context = get()): Uri { @@ -107,3 +105,17 @@ fun Cursor.toList(transformer: (Cursor) -> Result): List { while (moveToNext()) out.add(transformer(this)) return out } + +fun ApplicationInfo.getLabel(pm: PackageManager): String { + runCatching { + if (labelRes > 0) { + val res = pm.getResourcesForApplication(this) + val config = Configuration() + config.setLocale(currentLocale) + res.updateConfiguration(config, res.displayMetrics) + return res.getString(labelRes) + } + } + + return loadLabel(pm).toString() +} diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskPolicy.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskPolicy.kt index 11b9151ca..2a843b9b3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskPolicy.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskPolicy.kt @@ -2,6 +2,7 @@ package com.topjohnwu.magisk.model.entity import android.content.pm.ApplicationInfo import android.content.pm.PackageManager +import com.topjohnwu.magisk.extensions.getLabel import com.topjohnwu.magisk.model.entity.MagiskPolicy.Companion.INTERACTIVE @@ -50,7 +51,7 @@ fun Map.toPolicy(pm: PackageManager): MagiskPolicy { logging = get("logging")?.toIntOrNull() != 0, notification = get("notification")?.toIntOrNull() != 0, applicationInfo = info, - appName = info.loadLabel(pm).toString() + appName = info.getLabel(pm) ) } diff --git a/app/src/main/java/com/topjohnwu/magisk/model/receiver/GeneralReceiver.kt b/app/src/main/java/com/topjohnwu/magisk/model/receiver/GeneralReceiver.kt index b8b2bdae7..93e277780 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/receiver/GeneralReceiver.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/receiver/GeneralReceiver.kt @@ -33,20 +33,18 @@ open class GeneralReceiver : BroadcastReceiver() { } private fun getPkg(intent: Intent): String { - return intent.data?.encodedSchemeSpecificPart ?: "" + return intent.data?.encodedSchemeSpecificPart.orEmpty() } override fun onReceive(context: Context, intent: Intent?) { - if (intent == null) - return - var action: String? = intent.action ?: return - when (action) { + intent ?: return + when (intent.action ?: return) { Intent.ACTION_REBOOT, Intent.ACTION_BOOT_COMPLETED -> { - action = intent.getStringExtra("action") + val action = intent.getStringExtra("action") if (action == null) { // Actual boot completed event - Shell.su("mm_patch_dtbo").submit { result -> - if (result.isSuccess) + Shell.su("mm_patch_dtbo").submit { + if (it.isSuccess) Notifications.dtboPatched(context) } return diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt index b9e2d02fb..82ab980fd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt @@ -53,7 +53,7 @@ open class SplashActivity : AppCompatActivity() { Notifications.setup(this) // Schedule periodic update checks - Utils.scheduleUpdateCheck() + Utils.scheduleUpdateCheck(this) // Setup shortcuts Shortcuts.setup(this) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/base/BasePreferenceFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/base/BasePreferenceFragment.kt index b41bc6c91..885a91c14 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/base/BasePreferenceFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/base/BasePreferenceFragment.kt @@ -11,7 +11,6 @@ import androidx.core.view.children import androidx.core.view.isVisible import androidx.preference.* import androidx.recyclerview.widget.RecyclerView -import com.topjohnwu.magisk.App import com.topjohnwu.magisk.R import org.koin.android.ext.android.inject @@ -19,7 +18,6 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { protected val prefs: SharedPreferences by inject() - protected val app: App by inject() protected val activity get() = requireActivity() as MagiskActivity<*, *> override fun onCreateView( diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsFragment.kt index 0c7b2d1e4..d7b1a706f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsFragment.kt @@ -26,10 +26,10 @@ 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.net.Networking import com.topjohnwu.magisk.ui.base.BasePreferenceFragment import com.topjohnwu.magisk.utils.* import com.topjohnwu.magisk.view.dialogs.FingerprintAuthDialog -import com.topjohnwu.magisk.net.Networking import com.topjohnwu.superuser.Shell import io.reactivex.Completable import org.koin.android.ext.android.inject @@ -155,7 +155,7 @@ class SettingsFragment : BasePreferenceFragment() { } if (Shell.rootAccess() && Const.USER_ID == 0) { - if (app.packageName == BuildConfig.APPLICATION_ID) { + if (activity.packageName == BuildConfig.APPLICATION_ID) { generalCatagory.removePreference(restoreManager) } else { if (!Networking.checkNetworkStatus(requireContext())) { @@ -179,10 +179,12 @@ class SettingsFragment : BasePreferenceFragment() { } override fun onSharedPreferenceChanged(prefs: SharedPreferences, key: String) { + fun getStrInt() = prefs.getString(key, null)?.toInt() ?: 0 + when (key) { - Config.Key.ROOT_ACCESS -> Config.rootMode = Utils.getPrefsInt(prefs, key) - Config.Key.SU_MULTIUSER_MODE -> Config.suMultiuserMode = Utils.getPrefsInt(prefs, key) - Config.Key.SU_MNT_NS -> Config.suMntNamespaceMode = Utils.getPrefsInt(prefs, key) + Config.Key.ROOT_ACCESS -> Config.rootMode = getStrInt() + Config.Key.SU_MULTIUSER_MODE -> Config.suMultiuserMode = getStrInt() + Config.Key.SU_MNT_NS -> Config.suMntNamespaceMode = getStrInt() Config.Key.DARK_THEME -> requireActivity().recreate() Config.Key.COREONLY -> { if (prefs.getBoolean(key, false)) { @@ -200,10 +202,10 @@ class SettingsFragment : BasePreferenceFragment() { Shell.su("magiskhide --disable").submit() } Config.Key.LOCALE -> { - LocaleManager.setLocale(app) - requireActivity().recreate() + LocaleManager.setLocale(activity.application) + activity.recreate() } - Config.Key.CHECK_UPDATES -> Utils.scheduleUpdateCheck() + Config.Key.CHECK_UPDATES -> Utils.scheduleUpdateCheck(activity) } setSummary(key) } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt index e27c176f5..bf3758fe6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt @@ -2,10 +2,6 @@ package com.topjohnwu.magisk.utils import android.content.Context import android.content.Intent -import android.content.SharedPreferences -import android.content.pm.ApplicationInfo -import android.content.pm.PackageManager -import android.content.res.Configuration import android.content.res.Resources import android.net.Uri import android.os.Environment @@ -18,7 +14,6 @@ import com.topjohnwu.magisk.model.update.UpdateCheckService import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.internal.UiThreadHandler import java.io.File -import java.util.* import java.util.concurrent.TimeUnit object Utils { @@ -33,40 +28,17 @@ object Utils { UiThreadHandler.run { Toast.makeText(get(), resId, duration).show() } } - fun getPrefsInt(prefs: SharedPreferences, key: String, def: Int = 0): Int { - return prefs.getString(key, def.toString())!!.toInt() - } - fun dpInPx(dp: Int): Int { val scale = get().displayMetrics.density return (dp * scale + 0.5).toInt() } - fun fmt(fmt: String, vararg args: Any): String { - return String.format(Locale.US, fmt, *args) - } - - fun getAppLabel(info: ApplicationInfo, pm: PackageManager): String { - try { - if (info.labelRes > 0) { - val res = pm.getResourcesForApplication(info) - val config = Configuration() - config.setLocale(currentLocale) - res.updateConfiguration(config, res.displayMetrics) - return res.getString(info.labelRes) - } - } catch (ignored: Exception) { - } - - return info.loadLabel(pm).toString() - } - fun showSuperUser(): Boolean { return Shell.rootAccess() && (Const.USER_ID == 0 || Config.suMultiuserMode != Config.Value.MULTIUSER_MODE_OWNER_MANAGED) } - fun scheduleUpdateCheck() { + fun scheduleUpdateCheck(context: Context) { if (Config.checkUpdate) { val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) @@ -76,11 +48,12 @@ object Utils { .Builder(ClassMap[UpdateCheckService::class.java], 12, TimeUnit.HOURS) .setConstraints(constraints) .build() - WorkManager.getInstance().enqueueUniquePeriodicWork( + WorkManager.getInstance(context).enqueueUniquePeriodicWork( Const.ID.CHECK_MAGISK_UPDATE_WORKER_ID, ExistingPeriodicWorkPolicy.REPLACE, request) } else { - WorkManager.getInstance().cancelUniqueWork(Const.ID.CHECK_MAGISK_UPDATE_WORKER_ID) + WorkManager.getInstance(context) + .cancelUniqueWork(Const.ID.CHECK_MAGISK_UPDATE_WORKER_ID) } }