示例#1
0
  private void setExistingProject(IProject project) {
    mValues.testedProject = project;

    // Try to update the application, package, sdk target and minSdkVersion accordingly
    if (project != null
        && (!mValues.applicationNameModifiedByUser
            || !mValues.packageNameModifiedByUser
            || !mValues.targetModifiedByUser
            || !mValues.minSdkModifiedByUser)) {
      ManifestData manifestData = AndroidManifestHelper.parseForData(project);
      if (manifestData != null) {
        String appName = String.format("%1$sTest", project.getName());
        String packageName = manifestData.getPackage();
        String minSdkVersion = manifestData.getMinSdkVersionString();
        IAndroidTarget sdkTarget = null;
        if (Sdk.getCurrent() != null) {
          sdkTarget = Sdk.getCurrent().getTarget(project);
        }

        if (packageName == null) {
          packageName = ""; // $NON-NLS-1$
        }
        mLastExistingPackageName = packageName;

        if (!mValues.projectNameModifiedByUser) {
          mValues.projectName = appName;
        }

        if (!mValues.applicationNameModifiedByUser) {
          mValues.applicationName = appName;
        }

        if (!mValues.packageNameModifiedByUser) {
          packageName += ".test"; // $NON-NLS-1$
          mValues.packageName = packageName;
        }

        if (!mValues.targetModifiedByUser && sdkTarget != null) {
          mValues.target = sdkTarget;
        }

        if (!mValues.minSdkModifiedByUser) {
          if (minSdkVersion != null || sdkTarget != null) {
            mValues.minSdk = minSdkVersion;
          }
          if (sdkTarget == null) {
            mValues.updateSdkTargetToMatchMinSdkVersion();
          }
        }
      }
    }

    updateTestTargetPackageField(mLastExistingPackageName);
  }
  @Override
  protected boolean initialize(Object element) {
    if (element instanceof IType) {
      IType type = (IType) element;
      IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
      mProject = javaProject.getProject();
      IResource manifestResource =
          mProject.findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

      if (manifestResource == null
          || !manifestResource.exists()
          || !(manifestResource instanceof IFile)) {
        RefactoringUtil.logInfo(
            "Invalid or missing the "
                + SdkConstants.FN_ANDROID_MANIFEST_XML
                + " in the "
                + mProject.getName()
                + " project.");
        return false;
      }
      mManifestFile = (IFile) manifestResource;
      ManifestData manifestData;
      manifestData = AndroidManifestHelper.parseForData(mManifestFile);
      if (manifestData == null) {
        return false;
      }
      mAppPackage = manifestData.getPackage();
      mOldFqcn = type.getFullyQualifiedName();
      Object destination = getArguments().getDestination();
      if (destination instanceof IPackageFragment) {
        IPackageFragment packageFragment = (IPackageFragment) destination;
        mNewFqcn = packageFragment.getElementName() + "." + type.getElementName();
      } else if (destination instanceof IResource) {
        try {
          IPackageFragment[] fragments = javaProject.getPackageFragments();
          for (IPackageFragment fragment : fragments) {
            IResource resource = fragment.getResource();
            if (resource.equals(destination)) {
              mNewFqcn = fragment.getElementName() + '.' + type.getElementName();
              break;
            }
          }
        } catch (JavaModelException e) {
          // pass
        }
      }
      return mOldFqcn != null && mNewFqcn != null;
    }

    return false;
  }
  /**
   * Returns the namespace for the project. The namespace contains a standard part + the application
   * package.
   *
   * @return The package namespace of the project or null in case of error.
   */
  @Override
  public String getNamespace() {
    if (mNamespace == null) {
      boolean token = RenderSecurityManager.enterSafeRegion(mCredential);
      try {
        ManifestData manifestData = AndroidManifestHelper.parseForData(mProject);
        if (manifestData != null) {
          String javaPackage = manifestData.getPackage();
          mNamespace = String.format(AdtConstants.NS_CUSTOM_RESOURCES, javaPackage);
        }
      } finally {
        RenderSecurityManager.exitSafeRegion(token);
      }
    }

    return mNamespace;
  }