Search in sources :

Example 1 with StudioSdkInstallListenerFactory

use of com.android.tools.idea.sdk.install.StudioSdkInstallListenerFactory in project android by JetBrains.

the class PatchInstallingRestarter method processPatch.

/**
   * Either restart and install the given patch or delete it (if it's already installed).
   */
private void processPatch(File androidSdkPath, StudioLoggerProgressIndicator progress, File patchDir) {
    RepoPackage pendingPackage = null;
    File installDir = null;
    try {
        RepoManager mgr = mySdkHandler.getSdkManager(progress);
        Repository repo = InstallerUtil.readPendingPackageXml(patchDir, mgr, myFileOp, progress);
        if (repo != null) {
            File patch = new File(patchDir, PatchInstallerUtil.PATCH_JAR_FN);
            pendingPackage = repo.getLocalPackage();
            boolean remote = false;
            if (pendingPackage != null) {
                // If the pending package was local, use the corresponding installed local package.
                installDir = mgr.getPackages().getLocalPackages().get(pendingPackage.getPath()).getLocation();
            } else {
                // Otherwise it's remote.
                pendingPackage = repo.getRemotePackage().get(0);
                installDir = ((RemotePackage) pendingPackage).getInstallDir(mgr, progress);
                remote = true;
            }
            File existingPackageXml = new File(installDir, LocalRepoLoaderImpl.PACKAGE_XML_FN);
            File oldPackageXml = new File(patchDir, OLD_PACKAGE_XML_FN);
            if (patch.exists() && existingPackageXml.renameTo(oldPackageXml)) {
            // This will exit the app.
            //          Main.installPatch("sdk", PatchInstallerUtil.PATCH_JAR_FN, FileUtil.getTempDirectory(), patch, installDir.getAbsolutePath());
            } else {
                // The patch is already installed, or was cancelled.
                String relativePath = FileOpUtils.makeRelative(androidSdkPath, installDir, myFileOp);
                // Use the old mechanism to get the version, since it's actually part of the package itself. Thus we can tell if the patch
                // has already been applied.
                Revision rev = AndroidCommonUtils.parsePackageRevision(androidSdkPath.getPath(), relativePath);
                if (rev != null && rev.equals(pendingPackage.getVersion())) {
                    // We need to make sure the listeners are fired, so create an installer that does nothing and invoke it.
                    InstallerFactory dummyFactory = new DummyInstallerFactory();
                    dummyFactory.setListenerFactory(new StudioSdkInstallListenerFactory(mySdkHandler));
                    if (remote) {
                        Installer installer = dummyFactory.createInstaller((RemotePackage) pendingPackage, mgr, new StudioDownloader(), myFileOp);
                        installer.complete(progress);
                    } else {
                        Uninstaller uninstaller = dummyFactory.createUninstaller((LocalPackage) pendingPackage, mgr, myFileOp);
                        uninstaller.complete(progress);
                    }
                } else {
                    // something went wrong. Move the old package.xml back into place.
                    progress.logWarning("Failed to find version information in " + new File(androidSdkPath, SdkConstants.FN_SOURCE_PROP));
                    oldPackageXml.renameTo(existingPackageXml);
                }
            }
        }
    } catch (Exception e) {
        StringBuilder message = new StringBuilder("A problem occurred while installing ");
        message.append(pendingPackage != null ? pendingPackage.getDisplayName() : "an SDK package");
        if (installDir != null) {
            message.append(" in ").append(installDir);
        }
        message.append(". Please try again.");
        Messages.showErrorDialog(message.toString(), "Error Launching SDK Component Installer");
        progress.logWarning("Failed to install SDK package", e);
    }
    // If we get here we either got an error or the patch was already installed. Delete the patch dir.
    try {
        myFileOp.deleteFileOrFolder(patchDir);
    } catch (Exception e) {
        progress.logWarning("Problem during patch cleanup", e);
    }
}
Also used : AbstractInstaller(com.android.repository.impl.installer.AbstractInstaller) StudioDownloader(com.android.tools.idea.sdk.StudioDownloader) AbstractInstallerFactory(com.android.repository.impl.installer.AbstractInstallerFactory) Revision(com.android.repository.Revision) AbstractUninstaller(com.android.repository.impl.installer.AbstractUninstaller) File(java.io.File) StudioSdkInstallListenerFactory(com.android.tools.idea.sdk.install.StudioSdkInstallListenerFactory)

Aggregations

Revision (com.android.repository.Revision)1 AbstractInstaller (com.android.repository.impl.installer.AbstractInstaller)1 AbstractInstallerFactory (com.android.repository.impl.installer.AbstractInstallerFactory)1 AbstractUninstaller (com.android.repository.impl.installer.AbstractUninstaller)1 StudioDownloader (com.android.tools.idea.sdk.StudioDownloader)1 StudioSdkInstallListenerFactory (com.android.tools.idea.sdk.install.StudioSdkInstallListenerFactory)1 File (java.io.File)1