/** * Returns the {@link IPackageFragment} for the package registered in the manifest * * @return the {@link IPackageFragment} for the package registered in the manifest */ @Nullable public IPackageFragment getPackageFragment() { sync(); try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(mProject); if (javaProject != null) { IPackageFragmentRoot root = ManifestInfo.getSourcePackageRoot(javaProject); if (root != null) { return root.getPackageFragment(mPackage); } } } catch (CoreException e) { AdtPlugin.log(e, null); } return null; }
/** * Returns the default theme for this project, by looking at the manifest default theme * registration, target SDK, rendering target, etc. * * @param renderingTarget the rendering target use to render the theme, or null * @param screenSize the screen size to obtain a default theme for, or null if unknown * @return the theme to use for this project, never null */ @NonNull public String getDefaultTheme(IAndroidTarget renderingTarget, ScreenSize screenSize) { sync(); if (mManifestTheme != null) { return mManifestTheme; } int renderingTargetSdk = mTargetSdk; if (renderingTarget != null) { renderingTargetSdk = renderingTarget.getVersion().getApiLevel(); } int apiLevel = Math.min(mTargetSdk, renderingTargetSdk); // For now this theme works only on XLARGE screens. When it works for all sizes, // add that new apiLevel to this check. if (apiLevel >= 11 && screenSize == ScreenSize.XLARGE || apiLevel >= 14) { return PREFIX_ANDROID_STYLE + "Theme.Holo"; // $NON-NLS-1$ } else { return PREFIX_ANDROID_STYLE + "Theme"; // $NON-NLS-1$ } }
/** * Returns the activity associated with the given layout file. * * <p>This is an alternative to {@link #guessActivity(IFile, String)}. Whereas guessActivity * simply looks for references to "R.layout.foo", this method searches for all usages of * Activity#setContentView(int), and for each match it looks up the corresponding call text (such * as "setContentView(R.layout.foo)"). From this it uses a regexp to pull out "foo" from this, and * stores the association that layout "foo" is associated with the activity class that contained * the setContentView call. * * <p>This has two potential advantages: * * <ol> * <li>It can be faster. We do the reference search -once-, and we've built a map of all the * layout-to-activity mappings which we can then immediately look up other layouts for, * which is particularly useful at startup when we have to compute the layout activity * associations to populate the theme choosers. * <li>It can be more accurate. Just because an activity references an "R.layout.foo" field * doesn't mean it's setting it as a content view. * </ol> * * However, this second advantage is also its chief problem. There are some common code constructs * which means that the associated layout is not explicitly referenced in a direct setContentView * call; on a couple of sample projects I tested I found patterns like for example * "setContentView(v)" where "v" had been computed earlier. Therefore, for now we're going to * stick with the more general approach of just looking up each field when needed. We're keeping * the code around, though statically compiled out with the "if (false)" construct below in case * we revisit this. * * @param layoutFile the layout whose activity we want to look up * @return the activity name */ @SuppressWarnings("all") @Nullable public String guessActivityBySetContentView(String layoutName) { if (false) { // These should be fields final Pattern LAYOUT_FIELD_PATTERN = Pattern.compile("R\\.layout\\.([a-z0-9_]+)"); // $NON-NLS-1$ Map<String, String> mUsages = null; sync(); if (mUsages == null) { final Map<String, String> usages = new HashMap<String, String>(); mUsages = usages; SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object element = match.getElement(); if (element instanceof IMethod) { IMethod method = (IMethod) element; IType declaringType = method.getDeclaringType(); String fqcn = declaringType.getFullyQualifiedName(); IDocumentProvider provider = new TextFileDocumentProvider(); IResource resource = match.getResource(); try { provider.connect(resource); IDocument document = provider.getDocument(resource); if (document != null) { String matchText = document.get(match.getOffset(), match.getLength()); Matcher matcher = LAYOUT_FIELD_PATTERN.matcher(matchText); if (matcher.find()) { usages.put(matcher.group(1), fqcn); } } } catch (Exception e) { AdtPlugin.log(e, "Can't find range information for %1$s", resource.getName()); } finally { provider.disconnect(resource); } } } }; try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(mProject); if (javaProject == null) { return null; } // Search for which java classes call setContentView(R.layout.layoutname); String typeFqcn = "R.layout"; // $NON-NLS-1$ if (mPackage != null) { typeFqcn = mPackage + '.' + typeFqcn; } IType activityType = javaProject.findType(SdkConstants.CLASS_ACTIVITY); if (activityType != null) { IMethod method = activityType.getMethod( "setContentView", new String[] {"I"}); // $NON-NLS-1$ //$NON-NLS-2$ if (method.exists()) { SearchPattern pattern = SearchPattern.createPattern(method, REFERENCES); search(requestor, javaProject, pattern); } } } catch (CoreException e) { AdtPlugin.log(e, null); } } return mUsages.get(layoutName); } return null; }
/** * Returns the minimum SDK version name (which may not be a numeric string, e.g. it could be a * codename) * * @return the minimum SDK version */ public String getMinSdkName() { sync(); return mMinSdkName; }
/** * Returns the minimum SDK version * * @return the minimum SDK version */ public int getMinSdkVersion() { sync(); return mMinSdk; }
/** * Returns the target SDK version * * @return the target SDK version */ public int getTargetSdkVersion() { sync(); return mTargetSdk; }
/** * Returns the application label, or null * * @return the application label, or null */ @Nullable public String getApplicationLabel() { sync(); return mApplicationLabel; }
/** * Returns the application icon, or null * * @return the application icon, or null */ @Nullable public String getApplicationIcon() { sync(); return mApplicationIcon; }
/** * Returns the manifest theme registered on the application, if any * * @return a manifest theme, or null if none was registered */ @Nullable public String getManifestTheme() { sync(); return mManifestTheme; }
/** * Returns a map from activity full class names to the corresponding theme style to be used * * @return a map from activity fqcn to theme style */ @NonNull public Map<String, String> getActivityThemes() { sync(); return mActivityThemes; }
/** * Returns the default package registered in the Android manifest * * @return the default package registered in the manifest */ @NonNull public String getPackage() { sync(); return mPackage; }