diff options
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java')
-rw-r--r-- | eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java | 111 |
1 files changed, 102 insertions, 9 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java index c21c8a4..f0af8bb 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/actions/AddSupportJarAction.java @@ -87,7 +87,13 @@ public class AddSupportJarAction implements IObjectActionDelegate { private static final String FD_GRIDLAYOUT = "gridlayout"; //$NON-NLS-1$ private static final String FD_V7 = "v7"; //$NON-NLS-1$ private static final String FD_V4 = "v4"; //$NON-NLS-1$ + private static final String FD_V13 = "v13"; //$NON-NLS-1$ + private static final String FD_APPCOMPAT = "appcompat"; //$NON-NLS-1$ + private static final String FD_LIBS = "libs"; //$NON-NLS-1$ private static final String ANDROID_SUPPORT_V4_JAR = "android-support-v4.jar"; //$NON-NLS-1$ + private static final String ANDROID_SUPPORT_V13_JAR = "android-support-v13.jar";//$NON-NLS-1$ + private static final String APPCOMPAT_V7_JAR = "android-support-v7-appcompat.jar";//$NON-NLS-1$ + private static final String APP_COMPAT_LIB_NAME = "appcompat_v7"; //$NON-NLS-1$ private ISelection mSelection; /** @@ -166,7 +172,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { return null; } - String sdkLocation = sdk.getSdkLocation(); + String sdkLocation = sdk.getSdkOsLocation(); if (minimumRevision > 0) { File path = getSupportJarFile(); if (path != null) { @@ -223,7 +229,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { public static int getInstalledRevision() { final Sdk sdk = Sdk.getCurrent(); if (sdk != null) { - String sdkLocation = sdk.getSdkLocation(); + String sdkLocation = sdk.getSdkOsLocation(); SdkManager manager = SdkManager.createManager(sdkLocation, NullLogger.getLogger()); Map<String, Integer> versions = manager.getExtrasVersions(); Integer version = versions.get(VENDOR_ID + '/' + SUPPORT_ID); @@ -238,7 +244,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { } } - return -1; + return -1; } /** @@ -278,7 +284,73 @@ public class AddSupportJarAction implements IObjectActionDelegate { } // Create workspace copy of the project and add library dependency - IProject libraryProject = createLibraryProject(libraryPath, project, waitForFinish); + IProject libraryProject = createLibraryProject(libraryPath, project, + "gridlayout_v7", waitForFinish); //$NON-NLS-1$ + if (libraryProject != null) { + return addLibraryDependency(libraryProject, project, waitForFinish); + } + } + + return false; + } + + /** + * Similar to {@link #install}, but rather than copy a jar into the given + * project, it creates a new library project in the workspace for the + * support library, and adds a library dependency on the newly + * installed library from the given project. + * + * @param project the project to add a dependency on the library to + * @param waitForFinish If true, block until the task has finished + * @return true if the installation was successful (or if + * <code>waitForFinish</code> is false, if the installation is + * likely to be successful - e.g. the user has at least agreed to + * all installation prompts.) + */ + public static boolean installAppCompatLibrary(final IProject project, boolean waitForFinish) { + final IJavaProject javaProject = JavaCore.create(project); + if (javaProject != null) { + + // Don't add in the library if it already exists + ProjectState state = Sdk.getProjectState(project); + ProjectPropertiesWorkingCopy copy = state.getProperties().makeWorkingCopy(); + for (String property : copy.keySet()) { + if (property.startsWith(ProjectProperties.PROPERTY_LIB_REF)) { + String libraryReference = copy.getProperty(property); + if (libraryReference != null && libraryReference.contains(APP_COMPAT_LIB_NAME)) { + return true; + } + } + } + + File supportPath = getSupportPackageDir(); + if (!supportPath.isDirectory()) { + File path = installSupport(7); + if (path == null) { + return false; + } + assert path.equals(supportPath); + } + File libraryPath = new File(supportPath, FD_V7 + File.separator + FD_APPCOMPAT); + if (!libraryPath.isDirectory()) { + // Upgrade support package: it's out of date. The SDK manager will + // perform an upgrade to the latest version if the package is already installed. + File path = installSupport(-1); + if (path == null) { + return false; + } + assert path.equals(libraryPath) : path; + } + + // Check to see if there's already a version of the library available + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + IWorkspaceRoot root = workspace.getRoot(); + IProject libraryProject = root.getProject(APP_COMPAT_LIB_NAME); + if (!libraryProject.exists()) { + // Create workspace copy of the project and add library dependency + libraryProject = createLibraryProject(libraryPath, project, + APP_COMPAT_LIB_NAME, waitForFinish); + } if (libraryProject != null) { return addLibraryDependency(libraryProject, project, waitForFinish); } @@ -296,7 +368,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { private static File getSupportPackageDir() { final Sdk sdk = Sdk.getCurrent(); if (sdk != null) { - String sdkLocation = sdk.getSdkLocation(); + String sdkLocation = sdk.getSdkOsLocation(); SdkManager manager = SdkManager.createManager(sdkLocation, NullLogger.getLogger()); Map<String, Integer> versions = manager.getExtrasVersions(); Integer version = versions.get(VENDOR_ID + '/' + SUPPORT_ID); @@ -343,6 +415,25 @@ public class AddSupportJarAction implements IObjectActionDelegate { } /** + * Returns a path to the installed jar file for the support library, + * or null if it does not exist + * + * @return a path to the v13.jar or null + */ + @Nullable + public static File getSupport13JarFile() { + File supportDir = getSupportPackageDir(); + if (supportDir != null) { + File path = new File(supportDir, FD_V13 + File.separator + ANDROID_SUPPORT_V13_JAR); + if (path.exists()) { + return path; + } + } + + return null; + } + + /** * Creates a library project in the Eclipse workspace out of the grid layout project * in the SDK tree. * @@ -354,6 +445,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { private static IProject createLibraryProject( final File libraryPath, final IProject project, + final String libraryName, boolean waitForFinish) { // Install a new library into the workspace. This is a copy rather than @@ -367,7 +459,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { IWorkspaceRoot root = workspace.getRoot(); String name = AdtUtils.getUniqueProjectName( - "gridlayout_v7", "_"); //$NON-NLS-1$ //$NON-NLS-2$ + libraryName, "_"); //$NON-NLS-1$ newProject = root.getProject(name); IProjectDescription description = workspace.newProjectDescription(name); String[] natures = new String[] { AdtConstants.NATURE_DEFAULT, JavaCore.NATURE_ID }; @@ -381,13 +473,14 @@ public class AddSupportJarAction implements IObjectActionDelegate { sourceDir.copy(destDir, EFS.OVERWRITE, null); // Make sure the src folder exists - destDir.getChild("src").mkdir(0, null /*monitor*/); + destDir.getChild(SdkConstants.SRC_FOLDER).mkdir(0, null /*monitor*/); // Set the android platform to the same level as the calling project ProjectState state = Sdk.getProjectState(project); String target = state.getProperties().getProperty(ProjectProperties.PROPERTY_TARGET); if (target != null && target.length() > 0) { - ProjectProperties properties = ProjectProperties.load(libraryPath.getPath(), + ProjectProperties properties = ProjectProperties.load( + destDir.toLocalFile(EFS.NONE, new NullProgressMonitor()).getPath(), PropertyType.PROJECT); ProjectPropertiesWorkingCopy copy = properties.makeWorkingCopy(); copy.setProperty(ProjectProperties.PROPERTY_TARGET, target); @@ -478,7 +571,7 @@ public class AddSupportJarAction implements IObjectActionDelegate { return Status.OK_STATUS; } catch (Exception e) { return new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, Status.ERROR, - "Failed", e); //$NON-NLS-1$ + "Failed", e); //$NON-NLS-1$ } finally { if (monitor != null) { monitor.done(); |