use of org.jetbrains.android.dom.manifest.Manifest in project android by JetBrains.
the class AndroidRenameHandler method isPackageAttributeInManifest.
static boolean isPackageAttributeInManifest(@NotNull Project project, @Nullable PsiElement element) {
if (element == null) {
return false;
}
final PsiFile psiFile = element.getContainingFile();
if (!(psiFile instanceof XmlFile)) {
return false;
}
final AndroidFacet facet = AndroidFacet.getInstance(psiFile);
if (facet == null) {
return false;
}
final VirtualFile vFile = psiFile.getVirtualFile();
if (vFile == null || !vFile.equals(AndroidRootUtil.getPrimaryManifestFile(facet))) {
return false;
}
if (!(element instanceof XmlAttributeValue)) {
return false;
}
final PsiElement parent = element.getParent();
if (!(parent instanceof XmlAttribute)) {
return false;
}
final GenericAttributeValue attrValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) parent);
if (attrValue == null) {
return false;
}
final DomElement parentDomElement = attrValue.getParent();
return parentDomElement instanceof Manifest && attrValue.equals(((Manifest) parentDomElement).getPackage());
}
use of org.jetbrains.android.dom.manifest.Manifest in project android by JetBrains.
the class AbstractRegisterComponentAction method isAvailable.
@Override
public final boolean isAvailable(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile file) {
final PsiClass psiClass = editor == null ? null : extractClass(editor, file);
if (psiClass == null) {
return false;
}
final int elementOffset = editor.getCaretModel().getOffset();
// Checking if we're in class "header", or, more specifically, before the "{" opening the class body.
// Other class intentions, such as CreateTestAction, use the same behaviour to determine whether they
// should be active.
final PsiElement lBrace = psiClass.getLBrace();
if (lBrace == null || elementOffset >= lBrace.getTextOffset()) {
return false;
}
// Check whether the class has a qualified name and is public (thus, can be used as an activity in the manifest)
final PsiModifierList modifierList = psiClass.getModifierList();
if (psiClass.getQualifiedName() == null || modifierList == null || !modifierList.hasExplicitModifier(PsiModifier.PUBLIC)) {
return false;
}
final AndroidFacet facet = AndroidFacet.getInstance(file);
if (facet == null) {
return false;
}
final Manifest manifest = facet.getManifest();
if (manifest == null) {
return false;
}
return isAvailable(psiClass, manifest);
}
use of org.jetbrains.android.dom.manifest.Manifest in project android by JetBrains.
the class DefaultActivityLocatorTest method testEnabledActivities.
// tests that when there are multiple launcher activities, we exclude the ones with android:enabled="false"
public void testEnabledActivities() throws Exception {
myFixture.copyFileToProject("projects/runConfig/enabled/AndroidManifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
myFixture.copyFileToProject("projects/runConfig/alias/src/debug/java/com/example/unittest/Launcher.java", "src/com/example/unittest/Launcher.java");
assertEquals("LaunchActivity", DefaultActivityLocator.computeDefaultActivity(myFacet, null));
// make sure that the dom based approach to getting values works as well
final Manifest manifest = myFacet.getManifest();
assertEquals("LaunchActivity", DefaultActivityLocator.getDefaultLauncherActivityName(myFacet.getModule().getProject(), manifest));
}
use of org.jetbrains.android.dom.manifest.Manifest in project kotlin by JetBrains.
the class KotlinAndroidGotoDeclarationHandler method collectManifestElements.
private static void collectManifestElements(@NotNull String nestedClassName, @NotNull String fieldName, @NotNull AndroidFacet facet, @NotNull List<PsiElement> result) {
Manifest manifest = facet.getManifest();
if (manifest == null) {
return;
}
List<? extends ManifestElementWithRequiredName> list;
if ("permission".equals(nestedClassName)) {
list = manifest.getPermissions();
} else if ("permission_group".equals(nestedClassName)) {
list = manifest.getPermissionGroups();
} else {
return;
}
for (ManifestElementWithRequiredName domElement : list) {
AndroidAttributeValue<String> nameAttribute = domElement.getName();
String name = nameAttribute.getValue();
if (AndroidUtils.equal(name, fieldName, false)) {
XmlElement psiElement = nameAttribute.getXmlAttributeValue();
if (psiElement != null) {
result.add(psiElement);
}
}
}
}
use of org.jetbrains.android.dom.manifest.Manifest in project android by JetBrains.
the class ManifestInfo method getMergedManifest.
@NotNull
static MergingReport getMergedManifest(@NotNull AndroidFacet facet, @NotNull VirtualFile primaryManifestFile, @NotNull List<VirtualFile> flavorAndBuildTypeManifests, @NotNull List<VirtualFile> libManifests) throws ManifestMerger2.MergeFailureException {
ApplicationManager.getApplication().assertReadAccessAllowed();
final File mainManifestFile = VfsUtilCore.virtualToIoFile(primaryManifestFile);
ILogger logger = NullLogger.getLogger();
ManifestMerger2.MergeType mergeType = facet.isAppProject() ? ManifestMerger2.MergeType.APPLICATION : ManifestMerger2.MergeType.LIBRARY;
AndroidModel androidModel = facet.getAndroidModel();
AndroidModuleModel gradleModel = AndroidModuleModel.get(facet);
ManifestMerger2.Invoker manifestMergerInvoker = ManifestMerger2.newMerger(mainManifestFile, logger, mergeType);
manifestMergerInvoker.withFeatures(ManifestMerger2.Invoker.Feature.SKIP_BLAME, ManifestMerger2.Invoker.Feature.SKIP_XML_STRING);
manifestMergerInvoker.addFlavorAndBuildTypeManifests(VfsUtilCore.virtualToIoFiles(flavorAndBuildTypeManifests).toArray(new File[0]));
List<Pair<String, File>> libraryManifests = new ArrayList<>();
for (VirtualFile file : libManifests) {
libraryManifests.add(Pair.of(file.getName(), VfsUtilCore.virtualToIoFile(file)));
}
manifestMergerInvoker.addBundleManifests(libraryManifests);
if (androidModel != null) {
AndroidVersion minSdkVersion = androidModel.getMinSdkVersion();
if (minSdkVersion != null) {
manifestMergerInvoker.setOverride(ManifestSystemProperty.MIN_SDK_VERSION, minSdkVersion.getApiString());
}
AndroidVersion targetSdkVersion = androidModel.getTargetSdkVersion();
if (targetSdkVersion != null) {
manifestMergerInvoker.setOverride(ManifestSystemProperty.TARGET_SDK_VERSION, targetSdkVersion.getApiString());
}
Integer versionCode = androidModel.getVersionCode();
if (versionCode != null && versionCode > 0) {
manifestMergerInvoker.setOverride(ManifestSystemProperty.VERSION_CODE, String.valueOf(versionCode));
}
String packageOverride = androidModel.getApplicationId();
if (!Strings.isNullOrEmpty(packageOverride)) {
manifestMergerInvoker.setOverride(ManifestSystemProperty.PACKAGE, packageOverride);
}
}
if (gradleModel != null) {
BuildTypeContainer buildTypeContainer = gradleModel.findBuildType(gradleModel.getSelectedVariant().getBuildType());
assert buildTypeContainer != null;
BuildType buildType = buildTypeContainer.getBuildType();
ProductFlavor mergedProductFlavor = gradleModel.getSelectedVariant().getMergedFlavor();
// copy-paste from {@link VariantConfiguration#getManifestPlaceholders()}
Map<String, Object> placeHolders = new HashMap<>(mergedProductFlavor.getManifestPlaceholders());
placeHolders.putAll(buildType.getManifestPlaceholders());
manifestMergerInvoker.setPlaceHolderValues(placeHolders);
// @deprecated maxSdkVersion has been ignored since Android 2.1 (API level 7)
Integer maxSdkVersion = mergedProductFlavor.getMaxSdkVersion();
if (maxSdkVersion != null) {
manifestMergerInvoker.setOverride(ManifestSystemProperty.MAX_SDK_VERSION, maxSdkVersion.toString());
}
// TODO we should have version Name for non-gradle projects
// copy-paste from {@link VariantConfiguration#getVersionName()}
String versionName = mergedProductFlavor.getVersionName();
String flavorVersionNameSuffix = null;
if (gradleModel.getFeatures().isProductFlavorVersionSuffixSupported()) {
flavorVersionNameSuffix = getVersionNameSuffix(mergedProductFlavor);
}
String versionNameSuffix = Joiner.on("").skipNulls().join(flavorVersionNameSuffix, getVersionNameSuffix(buildType));
if (!Strings.isNullOrEmpty(versionName) || !Strings.isNullOrEmpty(versionNameSuffix)) {
if (Strings.isNullOrEmpty(versionName)) {
Manifest manifest = facet.getManifest();
if (manifest != null) {
versionName = manifest.getXmlTag().getAttributeValue(SdkConstants.ATTR_VERSION_NAME, ANDROID_URI);
}
}
if (!Strings.isNullOrEmpty(versionNameSuffix)) {
versionName = Strings.nullToEmpty(versionName) + versionNameSuffix;
}
manifestMergerInvoker.setOverride(ManifestSystemProperty.VERSION_NAME, versionName);
}
}
if (mergeType == ManifestMerger2.MergeType.APPLICATION) {
manifestMergerInvoker.withFeatures(ManifestMerger2.Invoker.Feature.REMOVE_TOOLS_DECLARATIONS);
}
final Module module = facet.getModule();
final Project project = module.getProject();
manifestMergerInvoker.withFileStreamProvider(new ManifestMerger2.FileStreamProvider() {
@Override
protected InputStream getInputStream(@NotNull File file) throws FileNotFoundException {
VirtualFile vFile;
if (file == mainManifestFile) {
// Some tests use VirtualFile files (e.g. temp:///src/AndroidManifest.xml) for the main manifest
vFile = primaryManifestFile;
} else {
vFile = VfsUtil.findFileByIoFile(file, false);
}
assert vFile != null : file;
// findModuleForFile does not work for other build systems (e.g. bazel)
if (!libManifests.isEmpty()) {
Module moduleContainingManifest = getAndroidModuleForManifest(vFile);
if (moduleContainingManifest != null && !module.equals(moduleContainingManifest)) {
MergedManifest manifest = MergedManifest.get(moduleContainingManifest);
Document document = manifest.getDocument();
if (document != null) {
// normally the case, but can fail on merge fail
// This is not very efficient. Consider enhancing the manifest merger API
// such that I can pass back a fully merged DOM document instead of
// an XML string since it will need to turn around and parse it anyway.
String text = XmlUtils.toXml(document);
return new ByteArrayInputStream(text.getBytes(Charsets.UTF_8));
}
}
}
try {
PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
if (psiFile != null) {
String text = psiFile.getText();
return new ByteArrayInputStream(text.getBytes(Charsets.UTF_8));
}
} catch (ProcessCanceledException ignore) {
// During startup we may receive a progress canceled exception here,
// but we don't *need* to read from PSI; we can read directly from
// disk. PSI is useful when the file has been modified, but that's not
// the case in the typical scenario where we hit process canceled.
}
return super.getInputStream(file);
}
@Nullable
private Module getAndroidModuleForManifest(@NotNull VirtualFile vFile) {
// project and look at their source providers
for (Module m : ModuleManager.getInstance(project).getModules()) {
AndroidFacet androidFacet = AndroidFacet.getInstance(m);
if (androidFacet == null) {
continue;
}
List<VirtualFile> manifestFiles = IdeaSourceProvider.getManifestFiles(androidFacet);
for (VirtualFile manifestFile : manifestFiles) {
if (vFile.equals(manifestFile)) {
return m;
}
}
}
return null;
}
});
return manifestMergerInvoker.merge();
}
Aggregations