Search in sources :

Example 1 with ValidationResult

use of com.android.tools.idea.sdk.SdkPaths.ValidationResult in project android by JetBrains.

the class SelectNdkDialog method configureNdkTextField.

private void configureNdkTextField() {
    myNdkTextFieldWithButton.setTextFieldPreferredWidth(50);
    FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false) {

        @Override
        public void validateSelectedFiles(VirtualFile[] files) throws Exception {
            for (VirtualFile virtualFile : files) {
                File file = virtualToIoFile(virtualFile);
                ValidationResult validationResult = validateAndroidNdk(file, false);
                if (!validationResult.success) {
                    String msg = validationResult.message;
                    if (isEmpty(msg)) {
                        msg = "Please choose a valid Android NDK directory.";
                    }
                    throw new IllegalArgumentException(msg);
                }
            }
        }
    };
    if (SystemInfo.isMac) {
        descriptor.withShowHiddenFiles(true);
    }
    descriptor.setTitle("Choose Android NDK Location");
    myNdkTextFieldWithButton.addActionListener(new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>("Select Android NDK Home", null, myNdkTextFieldWithButton, null, descriptor, TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) ValidationResult(com.android.tools.idea.sdk.SdkPaths.ValidationResult) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File)

Example 2 with ValidationResult

use of com.android.tools.idea.sdk.SdkPaths.ValidationResult in project android by JetBrains.

the class IdeSdksConfigurable method createTextFieldWithBrowseButton.

@NotNull
private TextFieldWithBrowseButton createTextFieldWithBrowseButton(@NotNull String title, @NotNull String errorMessage, @NotNull Function<File, ValidationResult> validation) {
    FileChooserDescriptor descriptor = createSingleFolderDescriptor(title, file -> {
        ValidationResult validationResult = validation.fun(file);
        if (!validationResult.success) {
            String msg = validationResult.message;
            if (isEmpty(msg)) {
                msg = errorMessage;
            }
            throw new IllegalArgumentException(msg);
        }
        return null;
    });
    JTextField textField = new JTextField(10);
    return new TextFieldWithBrowseButton(textField, e -> {
        VirtualFile suggestedDir = null;
        File ndkLocation = getNdkLocation();
        if (ndkLocation.isDirectory()) {
            suggestedDir = findFileByIoFile(ndkLocation, false);
        }
        VirtualFile chosen = chooseFile(descriptor, null, suggestedDir);
        if (chosen != null) {
            File f = virtualToIoFile(chosen);
            textField.setText(f.getPath());
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) ValidationResult(com.android.tools.idea.sdk.SdkPaths.ValidationResult) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) FileChooser.chooseFile(com.intellij.openapi.fileChooser.FileChooser.chooseFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ValidationResult

use of com.android.tools.idea.sdk.SdkPaths.ValidationResult in project android by JetBrains.

the class SdkSync method syncIdeAndProjectAndroidSdk.

@VisibleForTesting
void syncIdeAndProjectAndroidSdk(@NotNull LocalProperties localProperties, @NotNull FindValidSdkPathTask findSdkPathTask, @Nullable Project project) {
    if (localProperties.hasAndroidDirProperty()) {
        // if android.dir is specified, we don't sync SDKs. User is working with SDK sources.
        return;
    }
    File ideAndroidSdkPath = myIdeSdks.getAndroidSdkPath();
    File projectAndroidSdkPath = localProperties.getAndroidSdkPath();
    if (ideAndroidSdkPath != null) {
        if (projectAndroidSdkPath == null) {
            // If we have the IDE default SDK and we don't have a project SDK, update local.properties with default SDK path and exit.
            setProjectSdk(localProperties, ideAndroidSdkPath);
            return;
        }
        ValidationResult validationResult = validateAndroidSdk(projectAndroidSdkPath, true);
        if (!validationResult.success) {
            // If we have the IDE default SDK and we don't have a valid project SDK, update local.properties with default SDK path and exit.
            invokeAndWaitIfNeeded(new Runnable() {

                @Override
                public void run() {
                    if (!ApplicationManager.getApplication().isUnitTestMode()) {
                        String error = validationResult.message;
                        if (isEmpty(error)) {
                            error = String.format("The path \n'%1$s'\n" + "does not refer to a valid Android SDK.", projectAndroidSdkPath.getPath());
                        }
                        String format = "%1$s\n\nAndroid Studio will use this Android SDK instead:\n'%2$s'\nand will modify the project's local.properties file.";
                        Messages.showErrorDialog(String.format(format, error, ideAndroidSdkPath.getPath()), ERROR_DIALOG_TITLE);
                    }
                    setProjectSdk(localProperties, ideAndroidSdkPath);
                }
            });
            return;
        }
    } else {
        if (projectAndroidSdkPath == null || !myIdeSdks.isValidAndroidSdkPath(projectAndroidSdkPath)) {
            // We don't have any SDK (IDE or project.)
            File selectedPath = findSdkPathTask.selectValidSdkPath();
            if (selectedPath == null) {
                throw new ExternalSystemException("Unable to continue until an Android SDK is specified");
            }
            setIdeSdk(localProperties, selectedPath);
            return;
        }
        // If we have a valid project SDK but we don't have IDE default SDK, update IDE with project SDK path and exit.
        setIdeSdk(localProperties, projectAndroidSdkPath);
        return;
    }
    if (!filesEqual(ideAndroidSdkPath, projectAndroidSdkPath)) {
        String msg = String.format("The project and Android Studio point to different Android SDKs.\n\n" + "Android Studio's default SDK is in:\n" + "%1$s\n\n" + "The project's SDK (specified in local.properties) is in:\n" + "%2$s\n\n" + "To keep results consistent between IDE and command line builds, only one path can be used. " + "Do you want to:\n\n" + "[1] Use Android Studio's default SDK (modifies the project's local.properties file.)\n\n" + "[2] Use the project's SDK (modifies Android Studio's default.)\n\n" + "Note that switching SDKs could cause compile errors if the selected SDK doesn't have the " + "necessary Android platforms or build tools.", ideAndroidSdkPath.getPath(), projectAndroidSdkPath.getPath());
        invokeAndWaitIfNeeded(new Runnable() {

            @Override
            public void run() {
                // We need to pass the project, so on Mac, the "Mac sheet" showing this message shows inside the IDE during UI tests, otherwise
                // it will show outside and the UI testing infrastructure cannot see it. It is overall a good practice to pass the project when
                // showing a message, to ensure that the message shows in the IDE instance containing the project.
                int result = MessageDialogBuilder.yesNo("Android SDK Manager", msg).yesText("Use Android Studio's SDK").noText("Use Project's SDK").project(project).show();
                if (result == Messages.YES) {
                    // Use Android Studio's SDK
                    setProjectSdk(localProperties, ideAndroidSdkPath);
                } else {
                    // Use project's SDK
                    setIdeSdk(localProperties, projectAndroidSdkPath);
                }
                if (isGuiTestingMode() && !getGuiTestSuiteState().isSkipSdkMerge()) {
                    mergeIfNeeded(projectAndroidSdkPath, ideAndroidSdkPath);
                }
            }
        });
    }
}
Also used : ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) ValidationResult(com.android.tools.idea.sdk.SdkPaths.ValidationResult) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ValidationResult

use of com.android.tools.idea.sdk.SdkPaths.ValidationResult in project android by JetBrains.

the class SdkPathsTest method testNoPlatformsNdkDirectory.

public void testNoPlatformsNdkDirectory() throws Exception {
    if (SystemInfo.isWindows) {
        // Do not run tests on Windows (see http://b.android.com/222904)
        return;
    }
    tmpDir = createTempDirectory(SdkPathsTest.class.getSimpleName(), "testNoPlatformsNdkDirectory");
    ValidationResult result = validateAndroidNdk(tmpDir, false);
    assertFalse(result.success);
    assertEquals("NDK does not contain any platforms.", result.message);
    result = validateAndroidNdk(tmpDir, true);
    assertFalse(result.success);
    assertEquals(String.format("The NDK at\n'%1$s'\ndoes not contain any platforms.", tmpDir.getPath()), result.message);
}
Also used : ValidationResult(com.android.tools.idea.sdk.SdkPaths.ValidationResult)

Example 5 with ValidationResult

use of com.android.tools.idea.sdk.SdkPaths.ValidationResult in project android by JetBrains.

the class SdkPathsTest method testNoPlatformsSdkDirectory.

public void testNoPlatformsSdkDirectory() throws Exception {
    tmpDir = createTempDirectory(SdkPathsTest.class.getName(), "testNoPlatformsSdkDirectory");
    ValidationResult result = validateAndroidSdk(tmpDir, false);
    assertFalse(result.success);
    assertEquals("SDK does not contain any platforms.", result.message);
    result = validateAndroidSdk(tmpDir, true);
    assertFalse(result.success);
    assertEquals(String.format("The SDK at\n'%1$s'\ndoes not contain any platforms.", tmpDir.getPath()), result.message);
}
Also used : ValidationResult(com.android.tools.idea.sdk.SdkPaths.ValidationResult)

Aggregations

ValidationResult (com.android.tools.idea.sdk.SdkPaths.ValidationResult)13 File (java.io.File)9 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Nullable (org.jetbrains.annotations.Nullable)2 WizardUtils (com.android.tools.idea.npw.WizardUtils)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)1 FileChooser.chooseFile (com.intellij.openapi.fileChooser.FileChooser.chooseFile)1 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 NotNull (org.jetbrains.annotations.NotNull)1