use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class NlModel method inflate.
/**
* Synchronously inflates the model and updates the view hierarchy
*
* @param force forces the model to be re-inflated even if a previous version was already inflated
* @returns whether the model was inflated in this call or not
*/
private boolean inflate(boolean force) {
Configuration configuration = myConfiguration;
if (configuration == null) {
return false;
}
ResourceNotificationManager resourceNotificationManager = ResourceNotificationManager.getInstance(myFile.getProject());
// Some types of files must be saved to disk first, because layoutlib doesn't
// delegate XML parsers for non-layout files (meaning layoutlib will read the
// disk contents, so we have to push any edits to disk before rendering)
LayoutPullParserFactory.saveFileIfNecessary(myFile);
RenderResult result = null;
synchronized (RENDERING_LOCK) {
if (myRenderTask != null && !force) {
// No need to inflate
return false;
}
// Record the current version we're rendering from; we'll use that in #activate to make sure we're picking up any
// external changes
myRenderedVersion = resourceNotificationManager.getCurrentVersion(myFacet, myFile, myConfiguration);
RenderService renderService = RenderService.get(myFacet);
RenderLogger logger = renderService.createLogger();
if (myRenderTask != null) {
myRenderTask.dispose();
}
myRenderTask = renderService.createTask(myFile, configuration, logger, mySurface);
setupRenderTask(myRenderTask);
if (myRenderTask != null) {
if (!isRenderViewPort()) {
myRenderTask.useDesignMode(myFile);
}
result = myRenderTask.inflate();
if (result == null || !result.getRenderResult().isSuccess()) {
myRenderTask.dispose();
myRenderTask = null;
if (result == null) {
result = RenderResult.createBlank(myFile);
}
}
}
updateHierarchy(result);
myRenderResultLock.writeLock().lock();
try {
myRenderResult = result;
} finally {
myRenderResultLock.writeLock().unlock();
}
return myRenderTask != null;
}
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class NlModel method overrideConfigurationScreenSize.
/**
* Changes the configuration to use a custom device with screen size defined by xDimension and yDimension.
*/
public void overrideConfigurationScreenSize(@AndroidCoordinate int xDimension, @AndroidCoordinate int yDimension) {
Device original = myConfiguration.getDevice();
// doesn't copy tag id
Device.Builder deviceBuilder = new Device.Builder(original);
if (original != null) {
deviceBuilder.setTagId(original.getTagId());
}
deviceBuilder.setName("Custom");
deviceBuilder.setId(Configuration.CUSTOM_DEVICE_ID);
Device device = deviceBuilder.build();
for (State state : device.getAllStates()) {
Screen screen = state.getHardware().getScreen();
screen.setXDimension(xDimension);
screen.setYDimension(yDimension);
double dpi = screen.getPixelDensity().getDpiValue();
double width = xDimension / dpi;
double height = yDimension / dpi;
double diagonalLength = Math.sqrt(width * width + height * height);
screen.setDiagonalLength(diagonalLength);
screen.setSize(AvdScreenData.getScreenSize(diagonalLength));
screen.setRatio(AvdScreenData.getScreenRatio(xDimension, yDimension));
screen.setScreenRound(device.getDefaultHardware().getScreen().getScreenRound());
screen.setChin(device.getDefaultHardware().getScreen().getChin());
}
// If a custom device already exists, remove it before adding the latest one
List<Device> devices = myConfiguration.getConfigurationManager().getDevices();
boolean customDeviceReplaced = false;
for (int i = 0; i < devices.size(); i++) {
if ("Custom".equals(devices.get(i).getId())) {
devices.set(i, device);
customDeviceReplaced = true;
break;
}
}
if (!customDeviceReplaced) {
devices.add(device);
}
VirtualFile better;
State newState;
//Change the orientation of the device depending on the shape of the canvas
if (xDimension > yDimension) {
better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Landscape", null, null);
newState = device.getState("Landscape");
} else {
better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Portrait", null, null);
newState = device.getState("Portrait");
}
if (better != null) {
VirtualFile old = myConfiguration.getFile();
assert old != null;
Project project = mySurface.getProject();
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, better, -1);
FileEditorManager manager = FileEditorManager.getInstance(project);
FileEditor selectedEditor = manager.getSelectedEditor(old);
manager.openEditor(descriptor, true);
// Switch to the same type of editor (XML or Layout Editor) in the target file
if (selectedEditor instanceof NlEditor) {
manager.setSelectedEditor(better, NlEditorProvider.DESIGNER_ID);
} else if (selectedEditor != null) {
manager.setSelectedEditor(better, TextEditorProvider.getInstance().getEditorTypeId());
}
AndroidFacet facet = AndroidFacet.getInstance(myConfiguration.getModule());
assert facet != null;
Configuration configuration = facet.getConfigurationManager().getConfiguration(better);
configuration.setEffectiveDevice(device, newState);
} else {
myConfiguration.setEffectiveDevice(device, newState);
}
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class NlOldPalettePanel method updateConfiguration.
private void updateConfiguration() {
myConfiguration = null;
if (myDesignSurface == null) {
return;
}
Configuration designConfiguration = myDesignSurface.getConfiguration();
if (designConfiguration == null) {
return;
}
State designState = designConfiguration.getDeviceState();
Configuration configuration = designConfiguration.clone();
Device device = configuration.getDevice();
if (device == null) {
return;
}
// Override to a predefined density that closest matches the screens resolution
Density override = null;
int monitorResolution = Toolkit.getDefaultToolkit().getScreenResolution();
for (Density density : Density.values()) {
if (density.getDpiValue() > 0) {
if (override == null || Math.abs(density.getDpiValue() - monitorResolution) < Math.abs(override.getDpiValue() - monitorResolution)) {
override = density;
}
}
}
if (override != null) {
device = new Device.Builder(device).build();
for (State state : device.getAllStates()) {
Screen screen = state.getHardware().getScreen();
screen.setXDimension((int) (screen.getXDimension() * override.getDpiValue() / screen.getXdpi()));
screen.setYDimension((int) (screen.getYDimension() * override.getDpiValue() / screen.getYdpi()));
screen.setXdpi(override.getDpiValue());
screen.setYdpi(override.getDpiValue());
screen.setPixelDensity(override);
}
configuration.setDevice(device, false);
if (designState != null) {
configuration.setDeviceStateName(designState.getName());
}
myConfiguration = configuration;
}
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class NlPreviewImagePanel method getBackgroundColor.
@NotNull
private Color getBackgroundColor() {
Configuration configuration = myDesignSurface != null ? myDesignSurface.getConfiguration() : null;
ResourceResolver resolver = configuration != null ? configuration.getResourceResolver() : null;
if (resolver == null) {
return UIUtil.getPanelBackground();
}
ResourceValue windowBackground = resolver.findItemInTheme("colorBackground", true);
Color background = ResourceHelper.resolveColor(resolver, windowBackground, myDesignSurface.getProject());
return background != null ? background : UIUtil.getPanelBackground();
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class NlProperties method currentActivityIfFoundIsDerivedFromAppCompatActivity.
private static boolean currentActivityIfFoundIsDerivedFromAppCompatActivity(@NotNull List<NlComponent> components) {
assert !components.isEmpty();
NlModel model = components.get(0).getModel();
Configuration configuration = model.getConfiguration();
String activityClassName = configuration.getActivity();
if (activityClassName == null) {
// Assume we are since this is how the default activities are created.
return true;
}
if (activityClassName.startsWith(".")) {
MergedManifest manifest = MergedManifest.get(model.getModule());
String pkg = StringUtil.notNullize(manifest.getPackage());
activityClassName = pkg + activityClassName;
}
JavaPsiFacade facade = JavaPsiFacade.getInstance(model.getProject());
PsiClass activityClass = facade.findClass(activityClassName, model.getModule().getModuleScope());
while (activityClass != null && !CLASS_APP_COMPAT_ACTIVITY.equals(activityClass.getQualifiedName())) {
activityClass = activityClass.getSuperClass();
}
return activityClass != null;
}
Aggregations