Beispiel #1
0
  /** Moves an AVD. */
  private void moveAvd() {
    try {
      String avdName = mSdkCommandLine.getParamName();
      AvdManager avdManager = new AvdManager(mSdkManager, mSdkLog);
      AvdInfo info = avdManager.getAvd(avdName, true /*validAvdOnly*/);

      if (info == null) {
        errorAndExit("There is no valid Android Virtual Device named '%s'.", avdName);
        return;
      }

      // This is a rename if there's a new name for the AVD
      String newName = mSdkCommandLine.getParamMoveNewName();
      if (newName != null && newName.equals(info.getName())) {
        // same name, not actually a rename operation
        newName = null;
      }

      // This is a move (of the data files) if there's a new location path
      String paramFolderPath = mSdkCommandLine.getParamLocationPath();
      if (paramFolderPath != null) {
        // check if paths are the same. Use File methods to account for OS idiosyncrasies.
        try {
          File f1 = new File(paramFolderPath).getCanonicalFile();
          File f2 = new File(info.getPath()).getCanonicalFile();
          if (f1.equals(f2)) {
            // same canonical path, so not actually a move
            paramFolderPath = null;
          }
        } catch (IOException e) {
          // Fail to resolve canonical path. Fail now since a move operation might fail
          // later and be harder to recover from.
          errorAndExit(e.getMessage());
          return;
        }
      }

      if (newName == null && paramFolderPath == null) {
        mSdkLog.warning("Move operation aborted: same AVD name, same canonical data path");
        return;
      }

      // If a rename was requested and no data move was requested, check if the original
      // data path is our default constructed from the AVD name. In this case we still want
      // to rename that folder too.
      if (newName != null && paramFolderPath == null) {
        // Compute the original data path
        File originalFolder =
            new File(
                AndroidLocation.getFolder() + AndroidLocation.FOLDER_AVD,
                info.getName() + AvdManager.AVD_FOLDER_EXTENSION);
        if (originalFolder.equals(info.getPath())) {
          try {
            // The AVD is using the default data folder path based on the AVD name.
            // That folder needs to be adjusted to use the new name.
            File f =
                new File(
                    AndroidLocation.getFolder() + AndroidLocation.FOLDER_AVD,
                    newName + AvdManager.AVD_FOLDER_EXTENSION);
            paramFolderPath = f.getCanonicalPath();
          } catch (IOException e) {
            // Fail to resolve canonical path. Fail now rather than later.
            errorAndExit(e.getMessage());
          }
        }
      }

      // Check for conflicts
      if (newName != null) {
        if (avdManager.getAvd(newName, false /*validAvdOnly*/) != null) {
          errorAndExit("There is already an AVD named '%s'.", newName);
          return;
        }

        File ini = info.getIniFile();
        if (ini.equals(AvdInfo.getIniFile(newName))) {
          errorAndExit("The AVD file '%s' is in the way.", ini.getCanonicalPath());
          return;
        }
      }

      if (paramFolderPath != null && new File(paramFolderPath).exists()) {
        errorAndExit(
            "There is already a file or directory at '%s'.\nUse --path to specify a different data folder.",
            paramFolderPath);
      }

      avdManager.moveAvd(info, newName, paramFolderPath, mSdkLog);
    } catch (AndroidLocationException e) {
      errorAndExit(e.getMessage());
    } catch (IOException e) {
      errorAndExit(e.getMessage());
    }
  }