Search in sources :

Example 6 with AndroidSessionInfo

use of com.android.tools.idea.run.AndroidSessionInfo in project intellij by bazelbuild.

the class BlazeAndroidRunConfigurationRunner method getRunProfileState.

@Override
@Nullable
public final RunProfileState getRunProfileState(final Executor executor, ExecutionEnvironment env) throws ExecutionException {
    final AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null : "Enforced by fatal validation check in createRunner.";
    final Project project = env.getProject();
    runContext.augmentEnvironment(env);
    boolean isDebug = executor instanceof DefaultDebugExecutor;
    if (isDebug && !AndroidSdkUtils.activateDdmsIfNecessary(facet.getModule().getProject())) {
        throw new ExecutionException("Unable to obtain debug bridge. " + "Please check if there is a different tool using adb that is active.");
    }
    AndroidSessionInfo info = AndroidSessionInfo.findOldSession(project, null, runConfigId);
    BlazeAndroidDeviceSelector deviceSelector = runContext.getDeviceSelector();
    BlazeAndroidDeviceSelector.DeviceSession deviceSession = deviceSelector.getDevice(project, facet, deployTargetManager, executor, env, info, isDebug, runConfigId);
    if (deviceSession == null) {
        return null;
    }
    DeployTarget deployTarget = deviceSession.deployTarget;
    if (deployTarget != null && deployTarget.hasCustomRunProfileState(executor)) {
        DeployTargetState deployTargetState = deployTargetManager.getCurrentDeployTargetState();
        return deployTarget.getRunProfileState(executor, env, deployTargetState);
    }
    DeviceFutures deviceFutures = deviceSession.deviceFutures;
    if (deviceFutures == null) {
        // Quietly exit.
        return null;
    }
    if (deviceFutures.get().isEmpty()) {
        throw new ExecutionException(AndroidBundle.message("deployment.target.not.found"));
    }
    if (isDebug) {
        String error = canDebug(deviceFutures, facet, module.getName());
        if (error != null) {
            throw new ExecutionException(error);
        }
    }
    LaunchOptions.Builder launchOptionsBuilder = getDefaultLaunchOptions().setDebug(isDebug);
    runContext.augmentLaunchOptions(launchOptionsBuilder);
    // Store the run context on the execution environment so before-run tasks can access it.
    env.putCopyableUserData(RUN_CONTEXT_KEY, runContext);
    env.putCopyableUserData(DEVICE_SESSION_KEY, deviceSession);
    return new BlazeAndroidRunState(module, env, getName(), launchOptionsBuilder, isDebug, deviceSession, runContext);
}
Also used : AndroidSessionInfo(com.android.tools.idea.run.AndroidSessionInfo) DeviceFutures(com.android.tools.idea.run.DeviceFutures) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) LaunchOptions(com.android.tools.idea.run.LaunchOptions) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) DeployTarget(com.android.tools.idea.run.editor.DeployTarget) DeployTargetState(com.android.tools.idea.run.editor.DeployTargetState) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with AndroidSessionInfo

use of com.android.tools.idea.run.AndroidSessionInfo in project intellij by bazelbuild.

the class ConnectBlazeTestDebuggerTask method launchDebugger.

/**
 * Nearly a clone of {@link ConnectJavaDebuggerTask#launchDebugger}. There are a few changes to
 * account for null variables that could occur in our implementation.
 */
@Override
public ProcessHandler launchDebugger(@NotNull LaunchInfo currentLaunchInfo, @NotNull Client client, @NotNull ProcessHandlerLaunchStatus launchStatus, @NotNull ProcessHandlerConsolePrinter printer) {
    String debugPort = Integer.toString(client.getDebuggerListenPort());
    int pid = client.getClientData().getPid();
    Logger.getInstance(ConnectJavaDebuggerTask.class).info(String.format(Locale.US, "Attempting to connect debugger to port %1$s [client %2$d]", debugPort, pid));
    // create a new process handler
    RemoteConnection connection = new RemoteConnection(true, "localhost", debugPort, false);
    RemoteDebugProcessHandler debugProcessHandler = new RemoteDebugProcessHandler(project);
    // switch the launch status and console printers to point to the new process handler
    // this is required, esp. for AndroidTestListener which holds a
    // reference to the launch status and printers, and those should
    // be updated to point to the new process handlers,
    // otherwise test results will not be forwarded appropriately
    ProcessHandler oldProcessHandler = launchStatus.getProcessHandler();
    launchStatus.setProcessHandler(debugProcessHandler);
    printer.setProcessHandler(debugProcessHandler);
    // Detach old process handler after the launch status
    // has been updated to point to the new process handler.
    oldProcessHandler.detachProcess();
    AndroidDebugState debugState = new AndroidDebugState(project, debugProcessHandler, connection, currentLaunchInfo.consoleProvider);
    RunContentDescriptor oldDescriptor;
    AndroidSessionInfo oldSession = oldProcessHandler.getUserData(AndroidSessionInfo.KEY);
    if (oldSession != null) {
        oldDescriptor = oldSession.getDescriptor();
    } else {
        // This is the first time we are attaching the debugger; get it from the environment instead.
        oldDescriptor = currentLaunchInfo.env.getContentToReuse();
    }
    RunContentDescriptor debugDescriptor;
    try {
        // @formatter:off
        ExecutionEnvironment debugEnv = new ExecutionEnvironmentBuilder(currentLaunchInfo.env).executor(currentLaunchInfo.executor).runner(currentLaunchInfo.runner).contentToReuse(oldDescriptor).build();
        debugDescriptor = DebuggerPanelsManager.getInstance(project).attachVirtualMachine(debugEnv, debugState, connection, false);
    // @formatter:on
    } catch (ExecutionException e) {
        printer.stderr("ExecutionException: " + e.getMessage() + '.');
        return null;
    }
    // Based on the above try block we shouldn't get here unless we have assigned to debugDescriptor
    assert debugDescriptor != null;
    // re-run the collected text from the old process handler to the new
    // TODO: is there a race between messages received once the debugger has been connected,
    // and these messages that are printed out?
    final AndroidProcessText oldText = AndroidProcessText.get(oldProcessHandler);
    if (oldText != null) {
        oldText.printTo(debugProcessHandler);
    }
    RunProfile runProfile = currentLaunchInfo.env.getRunProfile();
    int uniqueId = runProfile instanceof AndroidRunConfigurationBase ? ((AndroidRunConfigurationBase) runProfile).getUniqueID() : -1;
    AndroidSessionInfo value = new AndroidSessionInfo(debugProcessHandler, debugDescriptor, uniqueId, currentLaunchInfo.executor.getId(), currentLaunchInfo.executor.getActionName(), false);
    debugProcessHandler.putUserData(AndroidSessionInfo.KEY, value);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEBUG_CLIENT, client);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEVICE_API_LEVEL, client.getDevice().getVersion());
    return debugProcessHandler;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) AndroidRunConfigurationBase(com.android.tools.idea.run.AndroidRunConfigurationBase) ConnectJavaDebuggerTask(com.android.tools.idea.run.tasks.ConnectJavaDebuggerTask) AndroidSessionInfo(com.android.tools.idea.run.AndroidSessionInfo) RunProfile(com.intellij.execution.configurations.RunProfile) RemoteDebugProcessHandler(com.intellij.debugger.engine.RemoteDebugProcessHandler) AndroidProcessText(com.android.tools.idea.run.AndroidProcessText) AndroidDebugState(com.android.tools.idea.run.AndroidDebugState) RemoteDebugProcessHandler(com.intellij.debugger.engine.RemoteDebugProcessHandler) ProcessHandler(com.intellij.execution.process.ProcessHandler) RemoteConnection(com.intellij.execution.configurations.RemoteConnection) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException)

Example 8 with AndroidSessionInfo

use of com.android.tools.idea.run.AndroidSessionInfo in project intellij by bazelbuild.

the class BlazeAndroidBinaryProgramRunner method doExecute.

@Override
protected RunContentDescriptor doExecute(final RunProfileState state, final ExecutionEnvironment env) throws ExecutionException {
    RunContentDescriptor descriptor = super.doExecute(state, env);
    if (descriptor != null) {
        ProcessHandler processHandler = descriptor.getProcessHandler();
        assert processHandler != null;
        RunProfile runProfile = env.getRunProfile();
        int uniqueId = (runProfile instanceof RunConfigurationBase) ? ((RunConfigurationBase) runProfile).getUniqueID() : -1;
        AndroidSessionInfo sessionInfo = new AndroidSessionInfo(processHandler, descriptor, uniqueId, env.getExecutor().getId(), env.getExecutor().getActionName(), InstantRunUtils.isInstantRunEnabled(env));
        processHandler.putUserData(AndroidSessionInfo.KEY, sessionInfo);
    }
    return descriptor;
}
Also used : RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) AndroidSessionInfo(com.android.tools.idea.run.AndroidSessionInfo) ProcessHandler(com.intellij.execution.process.ProcessHandler) RunProfile(com.intellij.execution.configurations.RunProfile)

Aggregations

AndroidSessionInfo (com.android.tools.idea.run.AndroidSessionInfo)8 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 RunProfile (com.intellij.execution.configurations.RunProfile)3 AndroidRunConfigurationBase (com.android.tools.idea.run.AndroidRunConfigurationBase)2 ExecutionException (com.intellij.execution.ExecutionException)2 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)2 RunConfigurationBase (com.intellij.execution.configurations.RunConfigurationBase)2 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)2 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)2 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 AndroidVersion (com.android.sdklib.AndroidVersion)1 InstantRunGradleSupport (com.android.tools.idea.fd.gradle.InstantRunGradleSupport)1 AndroidDebugState (com.android.tools.idea.run.AndroidDebugState)1 AndroidProcessText (com.android.tools.idea.run.AndroidProcessText)1 DeviceFutures (com.android.tools.idea.run.DeviceFutures)1 LaunchOptions (com.android.tools.idea.run.LaunchOptions)1 DeployTarget (com.android.tools.idea.run.editor.DeployTarget)1 DeployTargetState (com.android.tools.idea.run.editor.DeployTargetState)1