Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 55
ADFA-3162: Add FileOpenExtension plugin delegation, archive viewer, and Install action#1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,16 +9,24 @@ import com.itsaky.androidide.plugins.extensions.MenuItem | ||
| import com.itsaky.androidide.plugins.extensions.TabItem | ||
| import com.itsaky.androidide.plugins.extensions.EditorTabItem | ||
| import com.itsaky.androidide.plugins.extensions.NavigationItem | ||
| import com.itsaky.androidide.plugins.extensions.FileOpenExtension | ||
| import com.itsaky.androidide.plugins.extensions.FileTabMenuItem | ||
| import com.itsaky.androidide.plugins.services.IdeEditorTabService | ||
| import com.example.sampleplugin.fragments.ApkAnalyzerFragment | ||
| import java.io.File | ||
| /** | ||
| * APK Viewer Plugin | ||
| * Provides APK analysis functionality via main menu toolbar and bottom sheet | ||
| */ | ||
| class ApkViewer : IPlugin, UIExtension, EditorTabExtension { | ||
| class ApkViewer : IPlugin, UIExtension, EditorTabExtension, FileOpenExtension { | ||
| private lateinit var context: PluginContext | ||
| private var pendingAnalysisFile: File? = null | ||
| companion object { | ||
| private const val TAB_ID = "apk_analyzer_main_tab" | ||
| } | ||
| override fun initialize(context: PluginContext): Boolean { | ||
| return try { | ||
| @@ -101,7 +109,7 @@ class ApkViewer : IPlugin, UIExtension, EditorTabExtension { | ||
| return listOf( | ||
| EditorTabItem( | ||
| id = "apk_analyzer_main_tab", | ||
| id = TAB_ID, | ||
| title = "APK Analyzer", | ||
| icon = android.R.drawable.ic_menu_info_details, | ||
| fragmentFactory = { | ||
| @@ -120,6 +128,11 @@ class ApkViewer : IPlugin, UIExtension, EditorTabExtension { | ||
| override fun onEditorTabSelected(tabId: String, fragment: Fragment) { | ||
| context.logger.info("Editor tab selected: $tabId") | ||
| val file = pendingAnalysisFile ?: return | ||
| pendingAnalysisFile = null | ||
| if (tabId == TAB_ID && fragment is ApkAnalyzerFragment) { | ||
| fragment.analyzeFile(file) | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| override fun onEditorTabClosed(tabId: String) { | ||
| @@ -130,6 +143,44 @@ class ApkViewer : IPlugin, UIExtension, EditorTabExtension { | ||
| return true | ||
| } | ||
| override fun canHandleFileOpen(file: File): Boolean { | ||
| return file.extension.equals("apk", ignoreCase = true) | ||
| } | ||
| override fun handleFileOpen(file: File): Boolean { | ||
| pendingAnalysisFile = file | ||
| openApkAnalyzerTab() | ||
| return true | ||
| } | ||
| override fun onFileOpened(file: File) { | ||
| if (file.extension.equals("apk", ignoreCase = true)) { | ||
| context.logger.info("APK file opened: ${file.name}") | ||
| } | ||
| } | ||
| override fun getFileTabMenuItems(file: File): List<FileTabMenuItem> { | ||
| if (!file.extension.equals("apk", ignoreCase = true)) return emptyList() | ||
| return listOf( | ||
| FileTabMenuItem( | ||
| id = "apk_viewer.analyze", | ||
| title = "Analyze APK", | ||
| order = 0, | ||
| action = { | ||
| pendingAnalysisFile = file | ||
| openApkAnalyzerTab() | ||
| } | ||
| ) | ||
| ) | ||
| } | ||
| override fun onFileClosed(file: File) { | ||
| if (file.extension.equals("apk", ignoreCase = true)) { | ||
| context.logger.info("APK file closed: ${file.name}") | ||
| } | ||
| } | ||
| private fun openApkAnalyzerTab() { | ||
| context.logger.info("Opening APK Analyzer tab") | ||
| @@ -145,7 +196,7 @@ class ApkViewer : IPlugin, UIExtension, EditorTabExtension { | ||
| } | ||
| try { | ||
| if (editorTabService.selectPluginTab("apk_analyzer_main_tab")) { | ||
| if (editorTabService.selectPluginTab(TAB_ID)) { | ||
| context.logger.info("Successfully opened APK Analyzer tab") | ||
| } else { | ||
| context.logger.warn("Failed to open APK Analyzer tab") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -33,6 +33,7 @@ class ApkAnalyzerFragment : Fragment() { | ||
| private var contextText: TextView? = null | ||
| private var btnStart: Button? = null | ||
| private var progressBar: ProgressBar? = null | ||
| private var deferredFile: java.io.File? = null | ||
| private val pickApkLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||
| if (result.resultCode == Activity.RESULT_OK) { | ||
| @@ -78,6 +79,11 @@ class ApkAnalyzerFragment : Fragment() { | ||
| updateContent() | ||
| setupClickListeners() | ||
| deferredFile?.let { file -> | ||
| deferredFile = null | ||
| analyzeFile(file) | ||
| } | ||
| } | ||
| private fun updateContent() { | ||
| @@ -98,16 +104,26 @@ class ApkAnalyzerFragment : Fragment() { | ||
| pickApkLauncher.launch(intent) | ||
| } | ||
| fun analyzeFile(file: java.io.File) { | ||
| if (!isAdded || view == null) { | ||
| deferredFile = file | ||
| return | ||
| } | ||
| runAnalysis { analyzeApkFromFile(file) } | ||
| } | ||
| private fun analyzeApkInBackground(uri: Uri) { | ||
| runAnalysis { analyzeApkFromUri(uri) } | ||
| } | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| private fun runAnalysis(block: suspend () -> String) { | ||
| progressBar?.visibility = View.VISIBLE | ||
| btnStart?.isEnabled = false | ||
| contextText?.text = "Analyzing APK..." | ||
| viewLifecycleOwner.lifecycleScope.launch { | ||
| val result = runCatching { | ||
| withContext(Dispatchers.IO) { | ||
| analyzeApkStructure(uri) | ||
| } | ||
| withContext(Dispatchers.IO) { block() } | ||
| }.getOrElse { e -> | ||
| "Failed to analyze APK: ${e.message}" | ||
| } | ||
| @@ -118,25 +134,28 @@ class ApkAnalyzerFragment : Fragment() { | ||
| } | ||
| } | ||
| private fun analyzeApkStructure(uri: Uri): String { | ||
| val result = StringBuilder() | ||
| // Create a temporary file to copy the APK content | ||
| private fun analyzeApkFromUri(uri: Uri): String { | ||
| val tempFile = java.io.File.createTempFile("apk_", ".apk", requireContext().cacheDir) | ||
| return runCatching { | ||
| // Copy the content from the URI to the temp file | ||
| return try { | ||
| requireContext().contentResolver.openInputStream(uri)?.use { input -> | ||
| tempFile.outputStream().use { output -> | ||
| input.copyTo(output) | ||
| } | ||
| tempFile.outputStream().use { output -> input.copyTo(output) } | ||
| } | ||
| analyzeApkFromFile(tempFile) | ||
| } finally { | ||
| tempFile.delete() | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| private fun analyzeApkFromFile(file: java.io.File): String { | ||
| val result = StringBuilder() | ||
| val zipFile = ZipFile(tempFile) | ||
| return runCatching { | ||
| ZipFile(file).use { zipFile -> | ||
| result.append(" APK STRUCTURE:\n") | ||
| val entries = zipFile.entries().toList().sortedBy { it.name } | ||
| val entryMap = entries.associateBy { it.name } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val explicitDirectories = mutableSetOf<String>() | ||
| val implicitDirectories = mutableSetOf<String>() | ||
| val files = mutableListOf<String>() | ||
| @@ -145,7 +164,7 @@ class ApkAnalyzerFragment : Fragment() { | ||
| var totalUncompressedSize = 0L | ||
| var totalCompressedSize = 0L | ||
| var totalEntries = 0 | ||
| val apkFileSize = tempFile.length() | ||
| val apkFileSize = file.length() | ||
| entries.forEach { entry -> | ||
| totalEntries++ | ||
| @@ -196,12 +215,11 @@ class ApkAnalyzerFragment : Fragment() { | ||
| ) | ||
| keyFiles.forEach { keyFile -> | ||
| val exists = files.any { it == keyFile } | ||
| if (exists) { | ||
| val entry = entries.find { it.name == keyFile } | ||
| val uncompressedSize = entry?.size?.let { formatFileSize(it) } ?: "?" | ||
| val compressedSize = entry?.compressedSize?.let { formatFileSize(it) } ?: "?" | ||
| val compressionRatio = if (entry != null && entry.size > 0) { | ||
| val entry = entryMap[keyFile] | ||
| if (entry != null) { | ||
| val uncompressedSize = formatFileSize(entry.size) | ||
| val compressedSize = formatFileSize(entry.compressedSize) | ||
| val compressionRatio = if (entry.size > 0) { | ||
| String.format("%.1f%%", (entry.compressedSize.toDouble() / entry.size.toDouble()) * 100) | ||
| } else "N/A" | ||
| result.append("• $keyFile: ✓ Raw: $uncompressedSize, Compressed: $compressedSize ($compressionRatio)\n") | ||
| @@ -222,7 +240,7 @@ class ApkAnalyzerFragment : Fragment() { | ||
| if (parts.size >= 3) { | ||
| val arch = parts[1] | ||
| val libName = parts.last() | ||
| val entry = entries.find { it.name == lib } | ||
| val entry = entryMap[lib] | ||
| val sizes = Pair(entry?.size ?: 0L, entry?.compressedSize ?: 0L) | ||
| archMap.getOrPut(arch) { mutableListOf() }.add(Pair(libName, sizes)) | ||
| } | ||
| @@ -255,8 +273,8 @@ class ApkAnalyzerFragment : Fragment() { | ||
| resourceDirs.forEach { dir -> | ||
| // Calculate total size for files in this directory | ||
| val dirFiles = files.filter { it.startsWith(dir) && it.count { c -> c == '/' } == dir.count { c -> c == '/' } } | ||
| val dirUncompressedSize = dirFiles.sumOf { fileName -> entries.find { it.name == fileName }?.size ?: 0L } | ||
| val dirCompressedSize = dirFiles.sumOf { fileName -> entries.find { it.name == fileName }?.compressedSize ?: 0L } | ||
| val dirUncompressedSize = dirFiles.sumOf { fileName -> entryMap[fileName]?.size ?: 0L } | ||
| val dirCompressedSize = dirFiles.sumOf { fileName -> entryMap[fileName]?.compressedSize ?: 0L } | ||
| if (dirUncompressedSize > 0) { | ||
| result.append("• $dir (${dirFiles.size} files) - Raw: ${formatFileSize(dirUncompressedSize)}, Compressed: ${formatFileSize(dirCompressedSize)}\n") | ||
| @@ -269,7 +287,7 @@ class ApkAnalyzerFragment : Fragment() { | ||
| // Large files analysis (files > 100KB) | ||
| val largeFiles = files.mapNotNull { fileName -> | ||
| entries.find { it.name == fileName }?.let { entry -> | ||
| entryMap[fileName]?.let { entry -> | ||
| if (entry.size > 100 * 1024) { | ||
| Triple(fileName, entry.size, entry.compressedSize) | ||
| } else null | ||
| @@ -312,19 +330,11 @@ class ApkAnalyzerFragment : Fragment() { | ||
| val hasProguard = files.any { it == "proguard/mappings.txt" } || files.any { it.contains("mapping.txt") } | ||
| result.append("• Code Obfuscation: ${if (hasProguard) "Detected" else "None detected"}\n") | ||
| zipFile.close() | ||
| result.toString() | ||
| }.fold( | ||
| onSuccess = { | ||
| tempFile.delete() | ||
| it | ||
| }, | ||
| onFailure = { e -> | ||
| tempFile.delete() | ||
| "Failed to analyze APK: ${e.message}" | ||
| } | ||
| ) | ||
| }.getOrElse { e -> | ||
| "Failed to analyze APK: ${e.message}" | ||
| } | ||
| } | ||
| private fun formatFileSize(bytes: Long): String { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * This file is part of AndroidIDE. | ||
| * | ||
| * AndroidIDE is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * AndroidIDE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.itsaky.androidide.actions.file | ||
| import android.content.Context | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.itsaky.androidide.actions.ActionData | ||
| import com.itsaky.androidide.actions.markInvisible | ||
| import com.itsaky.androidide.activities.editor.EditorHandlerActivity | ||
| import com.itsaky.androidide.repositories.PluginRepository | ||
| import com.itsaky.androidide.resources.R | ||
| import com.itsaky.androidide.utils.DialogUtils | ||
| import com.itsaky.androidide.utils.flashError | ||
| import com.itsaky.androidide.utils.flashSuccess | ||
| import kotlinx.coroutines.launch | ||
| import org.koin.core.context.GlobalContext | ||
| class InstallFileAction(context: Context, override val order: Int) : FileTabAction() { | ||
| override val id: String = "ide.editor.fileTab.install" | ||
| init { | ||
| label = context.getString(R.string.action_install) | ||
| } | ||
| override fun prepare(data: ActionData) { | ||
| super.prepare(data) | ||
| if (!visible) return | ||
| val activity = data.getActivity() ?: run { markInvisible(); return } | ||
| val currentFile = activity.editorViewModel.getCurrentFile() | ||
| visible = currentFile?.extension?.lowercase() in setOf("apk", "cgp") | ||
| enabled = visible | ||
| } | ||
| override fun EditorHandlerActivity.doAction(data: ActionData): Boolean { | ||
| val file = editorViewModel.getCurrentFile() ?: return false | ||
| when (file.extension.lowercase()) { | ||
| "apk" -> apkInstallationViewModel.installApk( | ||
| context = this, apk = file, launchInDebugMode = false | ||
| ) | ||
| "cgp" -> lifecycleScope.launch { | ||
| val repo = GlobalContext.get().get<PluginRepository>() | ||
| repo.installPluginFromFile(file) | ||
| .onSuccess { | ||
| flashSuccess(getString(R.string.msg_plugin_installed_restart)) | ||
| DialogUtils.showRestartPrompt(this@doAction) | ||
| } | ||
| .onFailure { e -> | ||
| flashError(getString(R.string.msg_plugin_install_failed, e.message)) | ||
| } | ||
| } | ||
| } | ||
| return true | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.