2019-07-28 09:10:22 +00:00
|
|
|
package com.topjohnwu.magisk.extensions
|
2019-04-19 14:32:01 +00:00
|
|
|
|
2019-05-03 07:36:39 +00:00
|
|
|
import android.content.Context
|
2019-05-06 17:03:28 +00:00
|
|
|
import android.content.Intent
|
2019-04-19 14:32:01 +00:00
|
|
|
import android.content.pm.ApplicationInfo
|
|
|
|
import android.content.pm.ComponentInfo
|
|
|
|
import android.content.pm.PackageInfo
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.content.pm.PackageManager.*
|
2019-08-12 08:54:33 +00:00
|
|
|
import android.content.res.Configuration
|
2019-07-27 22:46:44 +00:00
|
|
|
import android.database.Cursor
|
2019-05-03 08:42:57 +00:00
|
|
|
import android.net.Uri
|
|
|
|
import android.provider.OpenableColumns
|
2019-07-28 09:10:22 +00:00
|
|
|
import com.topjohnwu.magisk.utils.FileProvider
|
2019-08-12 08:54:33 +00:00
|
|
|
import com.topjohnwu.magisk.utils.currentLocale
|
2019-05-06 17:03:28 +00:00
|
|
|
import java.io.File
|
2019-05-03 08:42:57 +00:00
|
|
|
import java.io.FileNotFoundException
|
2019-04-19 14:32:01 +00:00
|
|
|
|
2019-08-05 07:21:38 +00:00
|
|
|
val packageName: String get() = get<Context>().packageName
|
2019-05-10 14:22:03 +00:00
|
|
|
|
2019-04-19 14:32:01 +00:00
|
|
|
val PackageInfo.processes
|
|
|
|
get() = activities?.processNames.orEmpty() +
|
|
|
|
services?.processNames.orEmpty() +
|
|
|
|
receivers?.processNames.orEmpty() +
|
|
|
|
providers?.processNames.orEmpty()
|
|
|
|
|
|
|
|
val Array<out ComponentInfo>.processNames get() = mapNotNull { it.processName }
|
|
|
|
|
|
|
|
val ApplicationInfo.packageInfo: PackageInfo?
|
|
|
|
get() {
|
|
|
|
val pm: PackageManager by inject()
|
|
|
|
|
|
|
|
return try {
|
|
|
|
val request = GET_ACTIVITIES or
|
|
|
|
GET_SERVICES or
|
|
|
|
GET_RECEIVERS or
|
|
|
|
GET_PROVIDERS
|
|
|
|
pm.getPackageInfo(packageName, request)
|
|
|
|
} catch (e1: Exception) {
|
|
|
|
try {
|
|
|
|
pm.activities(packageName).apply {
|
|
|
|
services = pm.services(packageName)
|
|
|
|
receivers = pm.receivers(packageName)
|
|
|
|
providers = pm.providers(packageName)
|
|
|
|
}
|
|
|
|
} catch (e2: Exception) {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 14:33:01 +00:00
|
|
|
val Uri.fileName: String
|
|
|
|
get() {
|
|
|
|
var name: String? = null
|
2019-08-04 20:47:14 +00:00
|
|
|
get<Context>().contentResolver.query(this, null, null, null, null)?.use { c ->
|
2019-07-10 14:33:01 +00:00
|
|
|
val nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
|
|
|
if (nameIndex != -1) {
|
|
|
|
c.moveToFirst()
|
|
|
|
name = c.getString(nameIndex)
|
|
|
|
}
|
2019-05-03 08:42:57 +00:00
|
|
|
}
|
2019-07-10 14:33:01 +00:00
|
|
|
if (name == null && path != null) {
|
|
|
|
val idx = path!!.lastIndexOf('/')
|
|
|
|
name = path!!.substring(idx + 1)
|
|
|
|
}
|
|
|
|
return name.orEmpty()
|
2019-05-03 08:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:32:01 +00:00
|
|
|
fun PackageManager.activities(packageName: String) =
|
|
|
|
getPackageInfo(packageName, GET_ACTIVITIES)
|
|
|
|
|
|
|
|
fun PackageManager.services(packageName: String) =
|
|
|
|
getPackageInfo(packageName, GET_SERVICES).services
|
|
|
|
|
|
|
|
fun PackageManager.receivers(packageName: String) =
|
|
|
|
getPackageInfo(packageName, GET_RECEIVERS).receivers
|
|
|
|
|
|
|
|
fun PackageManager.providers(packageName: String) =
|
2019-05-03 07:36:39 +00:00
|
|
|
getPackageInfo(packageName, GET_PROVIDERS).providers
|
|
|
|
|
|
|
|
fun Context.rawResource(id: Int) = resources.openRawResource(id)
|
2019-05-03 08:42:57 +00:00
|
|
|
|
2019-07-10 14:33:01 +00:00
|
|
|
fun Context.readUri(uri: Uri) =
|
|
|
|
contentResolver.openInputStream(uri) ?: throw FileNotFoundException()
|
2019-05-03 08:42:57 +00:00
|
|
|
|
2019-05-06 17:03:28 +00:00
|
|
|
fun Intent.startActivity(context: Context) = context.startActivity(this)
|
|
|
|
|
2019-07-10 16:08:18 +00:00
|
|
|
fun File.provide(context: Context = get()): Uri {
|
|
|
|
return FileProvider.getUriForFile(context, context.packageName + ".provider", this)
|
2019-05-06 17:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun File.mv(destination: File) {
|
2019-07-21 04:04:06 +00:00
|
|
|
inputStream().writeTo(destination)
|
2019-05-06 17:03:28 +00:00
|
|
|
deleteRecursively()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun String.toFile() = File(this)
|
|
|
|
|
2019-07-21 04:04:06 +00:00
|
|
|
fun Intent.chooser(title: String = "Pick an app") = Intent.createChooser(this, title)
|
|
|
|
|
|
|
|
fun Context.cachedFile(name: String) = File(cacheDir, name)
|
2019-07-27 22:46:44 +00:00
|
|
|
|
|
|
|
fun <Result> Cursor.toList(transformer: (Cursor) -> Result): List<Result> {
|
|
|
|
val out = mutableListOf<Result>()
|
|
|
|
while (moveToNext()) out.add(transformer(this))
|
|
|
|
return out
|
|
|
|
}
|
2019-08-12 08:54:33 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|