|
1 | 1 | package com.dergoogler.mmrl.wx.ui.activity.webui.interfaces |
2 | 2 |
|
| 3 | +import android.content.pm.ApplicationInfo |
| 4 | +import android.content.pm.PackageInfo |
3 | 5 | import android.text.TextUtils |
4 | 6 | import android.view.Window |
5 | 7 | import android.webkit.JavascriptInterface |
6 | 8 | import android.widget.Toast |
7 | 9 | import androidx.compose.runtime.getValue |
8 | 10 | import androidx.compose.runtime.mutableStateOf |
9 | 11 | import androidx.compose.runtime.setValue |
| 12 | +import androidx.core.content.pm.PackageInfoCompat |
10 | 13 | import androidx.core.view.WindowInsetsCompat |
11 | 14 | import androidx.core.view.WindowInsetsControllerCompat |
| 15 | +import com.dergoogler.mmrl.platform.PlatformManager |
| 16 | +import com.dergoogler.mmrl.platform.model.ModId.Companion.moduleDir |
12 | 17 | import com.dergoogler.mmrl.webui.interfaces.WXInterface |
13 | 18 | import com.dergoogler.mmrl.webui.interfaces.WXOptions |
14 | 19 | import com.topjohnwu.superuser.CallbackList |
@@ -78,17 +83,13 @@ class KernelSUInterface( |
78 | 83 | } |
79 | 84 |
|
80 | 85 | @JavascriptInterface |
81 | | - fun moduleInfo(): String { |
82 | | - console.warn("$name.moduleInfo() have been removed due to security reasons.") |
83 | | - val currentModuleInfo = JSONObject() |
84 | | - currentModuleInfo.put("moduleDir", null) |
85 | | - currentModuleInfo.put("id", null) |
86 | | - return currentModuleInfo.toString() |
| 86 | + fun exec(cmd: String): String { |
| 87 | + return withNewRootShell { ShellUtils.fastCmd(this, cmd) } |
87 | 88 | } |
88 | 89 |
|
89 | 90 | @JavascriptInterface |
90 | | - fun exec(cmd: String): String { |
91 | | - return withNewRootShell { ShellUtils.fastCmd(this, cmd) } |
| 91 | + fun execBool(cmd: String): Boolean { |
| 92 | + return withNewRootShell { ShellUtils.fastCmdResult(this, cmd) } |
92 | 93 | } |
93 | 94 |
|
94 | 95 | @JavascriptInterface |
@@ -229,6 +230,82 @@ class KernelSUInterface( |
229 | 230 | } |
230 | 231 | } |
231 | 232 |
|
| 233 | + private val um = PlatformManager.userManager |
| 234 | + private val pm = PlatformManager.packageManager |
| 235 | + private val packages get(): List<PackageInfo> = pm.getInstalledPackagesAll(um, 0) |
| 236 | + |
| 237 | + @JavascriptInterface |
| 238 | + fun moduleInfo(): String { |
| 239 | + val moduleInfos = JSONArray(PlatformManager.moduleManager.modules) |
| 240 | + val currentModuleInfo = JSONObject() |
| 241 | + currentModuleInfo.put("moduleDir", modId.moduleDir) |
| 242 | + for (i in 0 until moduleInfos.length()) { |
| 243 | + val currentInfo = moduleInfos.getJSONObject(i) |
| 244 | + |
| 245 | + if (currentInfo.getString("id") != modId.toString()) { |
| 246 | + continue |
| 247 | + } |
| 248 | + |
| 249 | + val keys = currentInfo.keys() |
| 250 | + for (key in keys) { |
| 251 | + currentModuleInfo.put(key, currentInfo[key]) |
| 252 | + } |
| 253 | + break |
| 254 | + } |
| 255 | + return currentModuleInfo.toString() |
| 256 | + } |
| 257 | + |
| 258 | + @JavascriptInterface |
| 259 | + fun listPackages(type: String): String { |
| 260 | + val packageNames = packages.filter { appInfo -> |
| 261 | + val flags = appInfo.applicationInfo?.flags ?: 0 |
| 262 | + when (type.lowercase()) { |
| 263 | + "system" -> (flags and ApplicationInfo.FLAG_SYSTEM) != 0 |
| 264 | + "user" -> (flags and ApplicationInfo.FLAG_SYSTEM) == 0 |
| 265 | + else -> true |
| 266 | + } |
| 267 | + } |
| 268 | + .map { it.packageName } |
| 269 | + .sorted() |
| 270 | + |
| 271 | + val jsonArray = JSONArray() |
| 272 | + for (pkgName in packageNames) { |
| 273 | + jsonArray.put(pkgName) |
| 274 | + } |
| 275 | + return jsonArray.toString() |
| 276 | + } |
| 277 | + |
| 278 | + @JavascriptInterface |
| 279 | + fun getPackagesInfo(packageNamesJson: String): String { |
| 280 | + val packageNames = JSONArray(packageNamesJson) |
| 281 | + val jsonArray = JSONArray() |
| 282 | + val appMap = packages.associateBy { it.packageName } |
| 283 | + for (i in 0 until packageNames.length()) { |
| 284 | + val pkgName = packageNames.getString(i) |
| 285 | + val appInfo = appMap[pkgName] |
| 286 | + if (appInfo != null) { |
| 287 | + val app = appInfo.applicationInfo |
| 288 | + val obj = JSONObject() |
| 289 | + obj.put("packageName", appInfo.packageName) |
| 290 | + obj.put("versionName", appInfo.versionName ?: "") |
| 291 | + obj.put("versionCode", PackageInfoCompat.getLongVersionCode(appInfo)) |
| 292 | + obj.put("appLabel", app?.loadLabel(context.packageManager)) |
| 293 | + obj.put( |
| 294 | + "isSystem", |
| 295 | + if (app != null) ((app.flags and ApplicationInfo.FLAG_SYSTEM) != 0) else JSONObject.NULL |
| 296 | + ) |
| 297 | + obj.put("uid", app?.uid ?: JSONObject.NULL) |
| 298 | + jsonArray.put(obj) |
| 299 | + } else { |
| 300 | + val obj = JSONObject() |
| 301 | + obj.put("packageName", pkgName) |
| 302 | + obj.put("error", "Package not found or inaccessible") |
| 303 | + jsonArray.put(obj) |
| 304 | + } |
| 305 | + } |
| 306 | + return jsonArray.toString() |
| 307 | + } |
| 308 | + |
232 | 309 | override fun onActivityStop() { |
233 | 310 | super.onActivityStop() |
234 | 311 |
|
|
0 commit comments