private IProject guessProject(IStructuredSelection selection) {
    if (selection == null) {
      return null;
    }

    for (Object element : selection.toList()) {
      if (element instanceof IAdaptable) {
        IResource res = (IResource) ((IAdaptable) element).getAdapter(IResource.class);
        IProject project = res != null ? res.getProject() : null;

        // Is this an Android project?
        try {
          if (project == null || !project.hasNature(AdtConstants.NATURE_DEFAULT)) {
            continue;
          }
        } catch (CoreException e) {
          // checking the nature failed, ignore this resource
          continue;
        }

        return project;
      } else if (element instanceof Pair<?, ?>) {
        // Pair of Project/String
        @SuppressWarnings("unchecked")
        Pair<IProject, String> pair = (Pair<IProject, String>) element;
        return pair.getFirst();
      }
    }

    // Try to figure out the project from the active editor
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window != null) {
      IWorkbenchPage page = window.getActivePage();
      if (page != null) {
        IEditorPart activeEditor = page.getActiveEditor();
        if (activeEditor instanceof AndroidXmlEditor) {
          Object input = ((AndroidXmlEditor) activeEditor).getEditorInput();
          if (input instanceof FileEditorInput) {
            FileEditorInput fileInput = (FileEditorInput) input;
            return fileInput.getFile().getProject();
          }
        }
      }
    }

    IJavaProject[] projects =
        BaseProjectHelper.getAndroidProjects(
            new IProjectFilter() {
              public boolean accept(IProject project) {
                return project.isAccessible();
              }
            });

    if (projects != null && projects.length == 1) {
      return projects[0].getProject();
    }

    return null;
  }