Example #1
0
  private void updateSize() {
    Device device = mConfiguration.getDevice();
    if (device == null) {
      return;
    }
    Screen screen = device.getDefaultHardware().getScreen();
    if (screen == null) {
      return;
    }

    FolderConfiguration folderConfig = mConfiguration.getFullConfig();
    ScreenOrientationQualifier qualifier = folderConfig.getScreenOrientationQualifier();
    ScreenOrientation orientation =
        qualifier == null ? ScreenOrientation.PORTRAIT : qualifier.getValue();

    // compute width and height to take orientation into account.
    int x = screen.getXDimension();
    int y = screen.getYDimension();
    int screenWidth, screenHeight;

    if (x > y) {
      if (orientation == ScreenOrientation.LANDSCAPE) {
        screenWidth = x;
        screenHeight = y;
      } else {
        screenWidth = y;
        screenHeight = x;
      }
    } else {
      if (orientation == ScreenOrientation.LANDSCAPE) {
        screenWidth = y;
        screenHeight = x;
      } else {
        screenWidth = x;
        screenHeight = y;
      }
    }

    int width = RenderPreviewManager.getMaxWidth();
    int height = RenderPreviewManager.getMaxHeight();
    if (screenWidth > 0) {
      double scale = getScale(screenWidth, screenHeight);
      width = (int) (screenWidth * scale);
      height = (int) (screenHeight * scale);
    }

    if (width != mWidth || height != mHeight) {
      mWidth = width;
      mHeight = height;

      Image thumbnail = mThumbnail;
      mThumbnail = null;
      if (thumbnail != null) {
        thumbnail.dispose();
      }
      if (mHeight != 0) {
        mAspectRatio = mWidth / (double) mHeight;
      }
    }
  }
 private static String getNexusLabel(Device d) {
   String name = d.getName();
   Screen screen = d.getDefaultHardware().getScreen();
   float length = (float) screen.getDiagonalLength();
   return String.format(
       java.util.Locale.US,
       "%1$s (%3$s\", %2$s)",
       name,
       getResolutionString(d),
       Float.toString(length));
 }
  private static String getGenericLabel(Device d) {
    // * Replace "'in'" with '"' (e.g. 2.7" QVGA instead of 2.7in QVGA)
    // * Use the same precision for all devices (all but one specify decimals)
    // * Add some leading space such that the dot ends up roughly in the
    //   same space
    // * Add in screen resolution and density
    String name = d.getName();
    if (name.equals("3.7 FWVGA slider")) { // $NON-NLS-1$
      // Fix metadata: this one entry doesn't have "in" like the rest of them
      name = "3.7in FWVGA slider"; // $NON-NLS-1$
    }

    Matcher matcher = PATTERN.matcher(name);
    if (matcher.matches()) {
      String size = matcher.group(1);
      String n = matcher.group(2);
      int dot = size.indexOf('.');
      if (dot == -1) {
        size = size + ".0";
        dot = size.length() - 2;
      }
      for (int i = 0; i < 2 - dot; i++) {
        size = ' ' + size;
      }
      name = size + "\" " + n;
    }

    return String.format(java.util.Locale.US, "%1$s (%2$s)", name, getResolutionString(d));
  }
 @Nullable
 private static String getResolutionString(Device device) {
   Screen screen = device.getDefaultHardware().getScreen();
   return String.format(
       java.util.Locale.US,
       "%1$d \u00D7 %2$d: %3$s", // U+00D7: Unicode multiplication sign
       screen.getXDimension(),
       screen.getYDimension(),
       screen.getPixelDensity().getResourceValue());
 }
Example #5
0
 public Builder(Device d) {
   mName = d.getName();
   mManufacturer = d.getManufacturer();
   for (Software s : d.getAllSoftware()) {
     mSoftware.add(s.deepCopy());
   }
   for (State s : d.getAllStates()) {
     mState.add(s.deepCopy());
   }
   mSoftware.addAll(d.getAllSoftware());
   mState.addAll(d.getAllStates());
   mMeta = d.getMeta();
   mDefaultState = d.getDefaultState();
 }
  boolean computeAndSetRealScale(boolean redraw) {
    // compute average dpi of X and Y
    ConfigurationChooser chooser = mEditor.getConfigurationChooser();
    Configuration config = chooser.getConfiguration();
    Device device = config.getDevice();
    Screen screen = device.getDefaultHardware().getScreen();
    double dpi = (screen.getXdpi() + screen.getYdpi()) / 2.;

    // get the monitor dpi
    float monitor = AdtPrefs.getPrefs().getMonitorDensity();
    if (monitor == 0.f) {
      ResolutionChooserDialog dialog = new ResolutionChooserDialog(chooser.getShell());
      if (dialog.open() == Window.OK) {
        monitor = dialog.getDensity();
        AdtPrefs.getPrefs().setMonitorDensity(monitor);
      } else {
        return false;
      }
    }

    mEditor.getCanvasControl().setScale(monitor / dpi, redraw);
    return true;
  }
Example #7
0
 @Override
 public boolean equals(Object o) {
   if (o == this) {
     return true;
   }
   if (!(o instanceof Device)) {
     return false;
   }
   Device d = (Device) o;
   return mName.equals(d.getName())
       && mManufacturer.equals(d.getManufacturer())
       && mSoftware.equals(d.getAllSoftware())
       && mState.equals(d.getAllStates())
       && mMeta.equals(d.getMeta())
       && mDefaultState.equals(d.getDefaultState());
 }
  static void show(final ConfigurationChooser chooser, ToolItem combo) {
    Configuration configuration = chooser.getConfiguration();
    Device current = configuration.getDevice();
    Menu menu = new Menu(chooser.getShell(), SWT.POP_UP);

    List<Device> deviceList = chooser.getDeviceList();
    Sdk sdk = Sdk.getCurrent();
    if (sdk != null) {
      AvdManager avdManager = sdk.getAvdManager();
      if (avdManager != null) {
        boolean separatorNeeded = false;
        AvdInfo[] avds = avdManager.getValidAvds();
        for (AvdInfo avd : avds) {
          for (Device device : deviceList) {
            if (device.getManufacturer().equals(avd.getDeviceManufacturer())
                && device.getName().equals(avd.getDeviceName())) {
              separatorNeeded = true;
              MenuItem item = new MenuItem(menu, SWT.CHECK);
              item.setText(avd.getName());
              item.setSelection(current == device);

              item.addSelectionListener(new DeviceMenuListener(chooser, device));
            }
          }
        }

        if (separatorNeeded) {
          @SuppressWarnings("unused")
          MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
        }
      }
    }

    // Group the devices by manufacturer, then put them in the menu.
    // If we don't have anything but Nexus devices, group them together rather than
    // make many manufacturer submenus.
    boolean haveNexus = false;
    boolean haveNonNexus = false;
    if (!deviceList.isEmpty()) {
      Map<String, List<Device>> manufacturers = new TreeMap<String, List<Device>>();
      for (Device device : deviceList) {
        List<Device> devices;
        if (isNexus(device)) {
          haveNexus = true;
        } else if (!isGeneric(device)) {
          haveNonNexus = true;
        }
        if (manufacturers.containsKey(device.getManufacturer())) {
          devices = manufacturers.get(device.getManufacturer());
        } else {
          devices = new ArrayList<Device>();
          manufacturers.put(device.getManufacturer(), devices);
        }
        devices.add(device);
      }
      if (haveNonNexus) {
        for (List<Device> devices : manufacturers.values()) {
          Menu manufacturerMenu = menu;
          if (manufacturers.size() > 1) {
            MenuItem item = new MenuItem(menu, SWT.CASCADE);
            item.setText(devices.get(0).getManufacturer());
            manufacturerMenu = new Menu(menu);
            item.setMenu(manufacturerMenu);
          }
          for (final Device device : devices) {
            MenuItem deviceItem = new MenuItem(manufacturerMenu, SWT.CHECK);
            deviceItem.setText(getGenericLabel(device));
            deviceItem.setSelection(current == device);
            deviceItem.addSelectionListener(new DeviceMenuListener(chooser, device));
          }
        }
      } else {
        List<Device> nexus = new ArrayList<Device>();
        List<Device> generic = new ArrayList<Device>();
        if (haveNexus) {
          // Nexus
          for (List<Device> devices : manufacturers.values()) {
            for (Device device : devices) {
              if (isNexus(device)) {
                if (device.getManufacturer().equals(GENERIC)) {
                  generic.add(device);
                } else {
                  nexus.add(device);
                }
              } else {
                generic.add(device);
              }
            }
          }
        }

        if (!nexus.isEmpty()) {
          sortNexusList(nexus);
          for (final Device device : nexus) {
            MenuItem item = new MenuItem(menu, SWT.CHECK);
            item.setText(getNexusLabel(device));
            item.setSelection(current == device);
            item.addSelectionListener(new DeviceMenuListener(chooser, device));
          }

          @SuppressWarnings("unused")
          MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
        }

        // Generate the generic menu.
        Collections.reverse(generic);
        for (final Device device : generic) {
          MenuItem item = new MenuItem(menu, SWT.CHECK);
          item.setText(getGenericLabel(device));
          item.setSelection(current == device);
          item.addSelectionListener(new DeviceMenuListener(chooser, device));
        }
      }
    }

    @SuppressWarnings("unused")
    MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);

    ConfigurationMenuListener.addTogglePreviewModeAction(
        menu, "Preview All Screens", chooser, RenderPreviewMode.SCREENS);

    Rectangle bounds = combo.getBounds();
    Point location = new Point(bounds.x, bounds.y + bounds.height);
    location = combo.getParent().toDisplay(location);
    menu.setLocation(location.x, location.y);
    menu.setVisible(true);
  }
 private static boolean isNexus(Device device) {
   return device.getName().contains(NEXUS);
 }
 private static boolean isGeneric(Device device) {
   return device.getManufacturer().equals(GENERIC);
 }
Example #11
0
  /**
   * Notifies that the preview's configuration has changed.
   *
   * @param flags the change flags, a bitmask corresponding to the {@code CHANGE_} constants in
   *     {@link ConfigurationClient}
   */
  public void configurationChanged(int flags) {
    if (!mVisible) {
      mDirty |= flags;
      return;
    }

    if ((flags & MASK_RENDERING) != 0) {
      mResourceResolver.clear();
      // Handle inheritance
      mConfiguration.syncFolderConfig();
      updateForkStatus();
      updateSize();
    }

    // Sanity check to make sure things are working correctly
    if (DEBUG) {
      RenderPreviewMode mode = mManager.getMode();
      if (mode == DEFAULT) {
        assert mConfiguration instanceof VaryingConfiguration;
        VaryingConfiguration config = (VaryingConfiguration) mConfiguration;
        int alternateFlags = config.getAlternateFlags();
        switch (alternateFlags) {
          case Configuration.CFG_DEVICE_STATE:
            {
              State configState = config.getDeviceState();
              State chooserState = mManager.getChooser().getConfiguration().getDeviceState();
              assert configState != null && chooserState != null;
              assert !configState.getName().equals(chooserState.getName())
                  : configState.toString() + ':' + chooserState;

              Device configDevice = config.getDevice();
              Device chooserDevice = mManager.getChooser().getConfiguration().getDevice();
              assert configDevice != null && chooserDevice != null;
              assert configDevice == chooserDevice : configDevice.toString() + ':' + chooserDevice;

              break;
            }
          case Configuration.CFG_DEVICE:
            {
              Device configDevice = config.getDevice();
              Device chooserDevice = mManager.getChooser().getConfiguration().getDevice();
              assert configDevice != null && chooserDevice != null;
              assert configDevice != chooserDevice : configDevice.toString() + ':' + chooserDevice;

              State configState = config.getDeviceState();
              State chooserState = mManager.getChooser().getConfiguration().getDeviceState();
              assert configState != null && chooserState != null;
              assert configState.getName().equals(chooserState.getName())
                  : configState.toString() + ':' + chooserState;

              break;
            }
          case Configuration.CFG_LOCALE:
            {
              Locale configLocale = config.getLocale();
              Locale chooserLocale = mManager.getChooser().getConfiguration().getLocale();
              assert configLocale != null && chooserLocale != null;
              assert configLocale != chooserLocale : configLocale.toString() + ':' + chooserLocale;
              break;
            }
          default:
            {
              // Some other type of override I didn't anticipate
              assert false : alternateFlags;
            }
        }
      }
    }

    mDirty = 0;
    mManager.scheduleRender(this);
  }