Search in sources :

Example 61 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidSdkType method setupSdkPaths.

@Override
public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) {
    final List<String> javaSdks = Lists.newArrayList();
    final Sdk[] sdks = sdkModel.getSdks();
    for (Sdk jdk : sdks) {
        if (Jdks.getInstance().isApplicableJdk(jdk)) {
            javaSdks.add(jdk.getName());
        }
    }
    if (javaSdks.isEmpty()) {
        Messages.showErrorDialog(AndroidBundle.message("no.jdk.for.android.found.error"), "No Java SDK Found");
        return false;
    }
    MessageBuildingSdkLog log = new MessageBuildingSdkLog();
    AndroidSdkData sdkData = getSdkData(sdk);
    if (sdkData == null) {
        String errorMessage = !log.getErrorMessage().isEmpty() ? log.getErrorMessage() : AndroidBundle.message("cannot.parse.sdk.error");
        Messages.showErrorDialog(errorMessage, "SDK Parsing Error");
        return false;
    }
    IAndroidTarget[] targets = sdkData.getTargets();
    if (targets.length == 0) {
        if (Messages.showOkCancelDialog(AndroidBundle.message("no.android.targets.error"), CommonBundle.getErrorTitle(), "Open SDK Manager", Messages.CANCEL_BUTTON, Messages.getErrorIcon()) == Messages.OK) {
            ActionManager.getInstance().getAction("Android.RunAndroidSdkManager").actionPerformed(null);
        }
        return false;
    }
    String[] targetNames = new String[targets.length];
    String newestPlatform = null;
    AndroidVersion version = null;
    for (int i = 0; i < targets.length; i++) {
        IAndroidTarget target = targets[i];
        String targetName = getTargetPresentableName(target);
        targetNames[i] = targetName;
        if (target.isPlatform() && (version == null || target.getVersion().compareTo(version) > 0)) {
            newestPlatform = targetName;
            version = target.getVersion();
        }
    }
    AndroidNewSdkDialog dialog = new AndroidNewSdkDialog(null, javaSdks, javaSdks.get(0), Arrays.asList(targetNames), newestPlatform != null ? newestPlatform : targetNames[0]);
    if (!dialog.showAndGet()) {
        return false;
    }
    String name = javaSdks.get(dialog.getSelectedJavaSdkIndex());
    Sdk jdk = sdkModel.findSdk(name);
    IAndroidTarget target = targets[dialog.getSelectedTargetIndex()];
    String sdkName = AndroidSdks.getInstance().chooseNameForNewLibrary(target);
    AndroidSdks.getInstance().setUpSdk(sdk, target, sdkName, Arrays.asList(sdks), jdk, true);
    return true;
}
Also used : SdkPaths.validateAndroidSdk(com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidVersion(com.android.sdklib.AndroidVersion)

Example 62 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ConfigurationMatcher method adaptConfigSelection.

/**
   * Adapts the current device/config selection so that it's compatible with
   * the configuration.
   * <p/>
   * If the current selection is compatible, nothing is changed.
   * <p/>
   * If it's not compatible, configs from the current devices are tested.
   * <p/>
   * If none are compatible, it reverts to
   * {@link #findAndSetCompatibleConfig(boolean)}
   */
void adaptConfigSelection(boolean needBestMatch) {
    // check the device config (ie sans locale)
    // if still true, we need to find another config.
    boolean needConfigChange = true;
    boolean currentConfigIsCompatible = false;
    State selectedState = myConfiguration.getDeviceState();
    FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
    Module module = myConfiguration.getModule();
    if (selectedState != null) {
        FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, myConfiguration.getLocale(), myConfiguration.getTarget());
        if (currentConfig != null && editedConfig.isMatchFor(currentConfig)) {
            // current config is compatible
            currentConfigIsCompatible = true;
            if (!needBestMatch || isCurrentFileBestMatchFor(currentConfig)) {
                needConfigChange = false;
            }
        }
    }
    if (needConfigChange) {
        List<Locale> localeList = getPrioritizedLocales();
        // if the current state/locale isn't a correct match, then
        // look for another state/locale in the same device.
        FolderConfiguration testConfig = new FolderConfiguration();
        // first look in the current device.
        State matchState = null;
        int localeIndex = -1;
        Device device = myConfiguration.getDevice();
        IAndroidTarget target = myConfiguration.getTarget();
        if (device != null && target != null) {
            VersionQualifier versionQualifier = new VersionQualifier(target.getVersion().getFeatureLevel());
            mainloop: for (State state : device.getAllStates()) {
                testConfig.set(Configuration.getFolderConfig(module, state, myConfiguration.getLocale(), target));
                testConfig.setVersionQualifier(versionQualifier);
                // loop on the locales.
                for (int i = 0; i < localeList.size(); i++) {
                    Locale locale = localeList.get(i);
                    // update the test config with the locale qualifiers
                    testConfig.setLocaleQualifier(locale.qualifier);
                    if (editedConfig.isMatchFor(testConfig) && isCurrentFileBestMatchFor(testConfig)) {
                        matchState = state;
                        localeIndex = i;
                        break mainloop;
                    }
                }
            }
        }
        if (matchState != null) {
            myConfiguration.startBulkEditing();
            myConfiguration.setDeviceState(matchState);
            myConfiguration.setEffectiveDevice(device, matchState);
            myConfiguration.finishBulkEditing();
        } else {
            // no match in current device with any state/locale
            // attempt to find another device that can display this
            // particular state.
            findAndSetCompatibleConfig(currentConfigIsCompatible);
        }
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) State(com.android.sdklib.devices.State) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module)

Example 63 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ConfigurationMatcher method selectConfigMatch.

@NotNull
private ConfigMatch selectConfigMatch(@NotNull List<ConfigMatch> matches) {
    List<String> deviceIds = myManager.getStateManager().getProjectState().getDeviceIds();
    Map<String, Integer> idRank = Maps.newHashMapWithExpectedSize(deviceIds.size());
    int rank = 0;
    for (String id : deviceIds) {
        idRank.put(id, rank++);
    }
    // API 11-13: look for a x-large device
    Comparator<ConfigMatch> comparator = null;
    IAndroidTarget projectTarget = myManager.getProjectTarget();
    if (projectTarget != null) {
        int apiLevel = projectTarget.getVersion().getFeatureLevel();
        if (apiLevel >= 11 && apiLevel < 14) {
            // TODO: Maybe check the compatible-screen tag in the manifest to figure out
            // what kind of device should be used for display.
            comparator = new TabletConfigComparator(idRank);
        }
    }
    if (comparator == null) {
        // lets look for a high density device
        comparator = new PhoneConfigComparator(idRank);
    }
    Collections.sort(matches, comparator);
    // Look at the currently active editor to see if it's a layout editor, and if so,
    // look up its configuration and if the configuration is in our match list,
    // use it. This means we "preserve" the current configuration when you open
    // new layouts.
    // TODO: This is running too late for the layout preview; the new editor has
    // already taken over so getSelectedTextEditor() returns self. Perhaps we
    // need to fish in the open editors instead.
    // We use FileEditorManagerImpl instead of FileEditorManager to get access to the lock-free version
    // (also used by DebuggerContextUtil) since the normal method only works from the dispatch thread
    // (grabbing a read lock is not enough).
    FileEditorManager editorManager = FileEditorManager.getInstance(myManager.getProject());
    if (editorManager instanceof FileEditorManagerImpl) {
        // not the case under test fixtures apparently
        Editor activeEditor = ((FileEditorManagerImpl) editorManager).getSelectedTextEditor(true);
        if (activeEditor != null) {
            FileDocumentManager documentManager = FileDocumentManager.getInstance();
            VirtualFile file = documentManager.getFile(activeEditor.getDocument());
            if (file != null && !file.equals(myFile) && file.getFileType() == StdFileTypes.XML && ResourceHelper.getFolderType(myFile) == ResourceHelper.getFolderType(file)) {
                Configuration configuration = myManager.getConfiguration(file);
                FolderConfiguration fullConfig = configuration.getFullConfig();
                for (ConfigMatch match : matches) {
                    if (fullConfig.equals(match.testConfig)) {
                        return match;
                    }
                }
            }
        }
    }
    // the list has been sorted so that the first item is the best config
    return matches.get(0);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManagerImpl(com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IAndroidTarget(com.android.sdklib.IAndroidTarget) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 64 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ConfigurationManager method getTarget.

/** Returns the best render target to use for the given minimum API level */
@Nullable
public IAndroidTarget getTarget(int min) {
    IAndroidTarget target = getTarget();
    if (target != null && target.getVersion().getApiLevel() >= min) {
        return target;
    }
    IAndroidTarget[] targetList = getTargets();
    for (int i = targetList.length - 1; i >= 0; i--) {
        target = targetList[i];
        if (isLayoutLibTarget(target) && target.getVersion().getFeatureLevel() >= min && isLayoutLibSupported(target)) {
            return target;
        }
    }
    return null;
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 65 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class Configuration method getTarget.

/**
   * Returns the rendering target
   *
   * @return the target
   */
@Nullable
public IAndroidTarget getTarget() {
    if (myTarget == null) {
        IAndroidTarget target = myManager.getTarget();
        // If the project-wide render target isn't a match for the version qualifier in this layout
        // (for example, the render target is at API 11, and layout is in a -v14 folder) then pick
        // a target which matches.
        VersionQualifier version = myEditedConfig.getVersionQualifier();
        if (target != null && version != null && version.getVersion() > target.getVersion().getFeatureLevel()) {
            target = myManager.getTarget(version.getVersion());
        }
        return getTargetForRendering(target);
    }
    return myTarget;
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IAndroidTarget (com.android.sdklib.IAndroidTarget)105 Nullable (org.jetbrains.annotations.Nullable)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Module (com.intellij.openapi.module.Module)17 NotNull (org.jetbrains.annotations.NotNull)16 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)15 Sdk (com.intellij.openapi.projectRoots.Sdk)14 File (java.io.File)13 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)13 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)13 AndroidVersion (com.android.sdklib.AndroidVersion)11 Device (com.android.sdklib.devices.Device)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 State (com.android.sdklib.devices.State)8 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)8 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)8 Configuration (com.android.tools.idea.configurations.Configuration)7 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)7 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)7 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)6