Java tutorial
/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.tools.idea.apk.viewer; import com.android.builder.model.AndroidArtifact; import com.android.builder.model.AndroidArtifactOutput; import com.android.tools.idea.gradle.project.model.AndroidModuleModel; import com.intellij.ide.util.PropertiesComponent; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.Nullable; import java.io.File; import static com.android.builder.model.AndroidProject.PROJECT_TYPE_APP; public class AnalyzeApkAction extends DumbAwareAction { private static final java.lang.String LAST_APK_PATH = "AnalyzeApkAction.lastApkPath"; public AnalyzeApkAction() { super("Analyze APK..."); } @Override public void actionPerformed(AnActionEvent e) { Project project = e.getProject(); if (project == null) { Logger.getInstance(AnalyzeApkAction.class).warn("Unable to obtain project from event"); return; } // find the apk to open VirtualFile vf = promptUserForApk(project); if (vf == null) { return; } OpenFileDescriptor fd = new OpenFileDescriptor(project, vf); FileEditorManager.getInstance(project).openEditor(fd, true); } @Nullable private static VirtualFile promptUserForApk(Project project) { FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileDescriptor() .withDescription("Select APK to analyze") .withFileFilter(file -> ApkFileSystem.EXTENSIONS.contains(file.getExtension())); VirtualFile apk = FileChooser.chooseFile(descriptor, project, getLastSelectedApk(project)); if (apk != null) { saveLastSelectedApk(project, apk); } return apk; } @Nullable private static VirtualFile getLastSelectedApk(Project project) { String lastApkPath = PropertiesComponent.getInstance(project).getValue(LAST_APK_PATH); if (lastApkPath != null) { File f = new File(lastApkPath); if (f.exists()) { return VfsUtil.findFileByIoFile(f, true); } } // see if we can find an apk generated by gradle if this is the first time for (Module module : ModuleManager.getInstance(project).getModules()) { AndroidModuleModel model = AndroidModuleModel.get(module); if (model == null) { continue; } if (model.getProjectType() != PROJECT_TYPE_APP) { continue; } AndroidArtifact mainArtifact = model.getSelectedVariant().getMainArtifact(); for (AndroidArtifactOutput output : mainArtifact.getOutputs()) { File outputFile = output.getMainOutputFile().getOutputFile(); if (outputFile.exists()) { return VfsUtil.findFileByIoFile(outputFile, true); } } } return null; } private static void saveLastSelectedApk(Project project, VirtualFile apk) { PropertiesComponent.getInstance(project).setValue(LAST_APK_PATH, apk.getPath()); } }