org.jetbrains.android.sdk.AndroidSdkUtils Java Examples

The following examples show how to use org.jetbrains.android.sdk.AndroidSdkUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyDeviceChooser.java    From ADB-Duang with MIT License 6 votes vote down vote up
@NotNull
public IDevice[] getSelectedDevices() {
  int[] rows = mySelectedRows != null ? mySelectedRows : myDeviceTable.getSelectedRows();
  List<IDevice> result = new ArrayList<IDevice>();
  for (int row : rows) {
    if (row >= 0) {
      Object serial = myDeviceTable.getValueAt(row, SERIAL_COLUMN_INDEX);
      final AndroidDebugBridge bridge = AndroidSdkUtils.getDebugBridge(myFacet.getModule().getProject());
      if (bridge == null) {
        return EMPTY_DEVICE_ARRAY;
      }
      IDevice[] devices = getFilteredDevices(bridge);
      for (IDevice device : devices) {
        if (device.getSerialNumber().equals(serial.toString())) {
          result.add(device);
          break;
        }
      }
    }
  }
  return result.toArray(new IDevice[result.size()]);
}
 
Example #2
Source File: ToolWindowFactory.java    From adc with Apache License 2.0 6 votes vote down vote up
public void createToolWindowContent(@NotNull final Project project, @NotNull final ToolWindow toolWindow) {
    ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
    JPanel framePanel = createPanel(project);
    disableAll();

    AndroidDebugBridge adb = AndroidSdkUtils.getDebugBridge(project);
    if (adb == null) {
        return;
    }

    if(adb.isConnected()){
        ToolWindowFactory.this.adBridge = adb;
        Logger.getInstance(ToolWindowFactory.class).info("Successfully obtained debug bridge");
        AndroidDebugBridge.addDeviceChangeListener(deviceChangeListener);
        updateDeviceComboBox();
    } else {
        Logger.getInstance(ToolWindowFactory.class).info("Unable to obtain debug bridge");
        String msg = MessageFormat.format(resourceBundle.getString("error.message.adb"), "");
        Messages.showErrorDialog(msg, resourceBundle.getString("error.title.adb"));
    }

    Content content = contentFactory.createContent(framePanel, "", false);
    toolWindow.getContentManager().addContent(content);
}
 
Example #3
Source File: MyDeviceChooser.java    From ADBWIFI with Apache License 2.0 6 votes vote down vote up
@NotNull
public IDevice[] getSelectedDevices() {
  int[] rows = mySelectedRows != null ? mySelectedRows : myDeviceTable.getSelectedRows();
  List<IDevice> result = new ArrayList<IDevice>();
  for (int row : rows) {
    if (row >= 0) {
      Object serial = myDeviceTable.getValueAt(row, SERIAL_COLUMN_INDEX);
      final AndroidDebugBridge bridge = AndroidSdkUtils.getDebugBridge(myFacet.getModule().getProject());
      if (bridge == null) {
        return EMPTY_DEVICE_ARRAY;
      }
      IDevice[] devices = getFilteredDevices(bridge);
      for (IDevice device : devices) {
        if (device.getSerialNumber().equals(serial.toString())) {
          result.add(device);
          break;
        }
      }
    }
  }
  return result.toArray(new IDevice[result.size()]);
}
 
Example #4
Source File: WindowTool.java    From ADB-Duang with MIT License 5 votes vote down vote up
private boolean initDevices(Project project) {
    boolean update = true;
    if (bridge == null) {
        bridge = AndroidSdkUtils.getDebugBridge(project);
    }
    deviceList.clear();
    if (bridge != null) {
        IDevice[] devices = bridge.getDevices();
        if (devices != null) {
            for (IDevice device : devices) {
                if (selectDevice != null) {
                    if (device.getName().equals(selectDevice.getName()) && device.getSerialNumber().equals(selectDevice.getSerialNumber())) {
                        selectDevice = device;
                        update = false;
                    }
                }
                deviceList.add(device);
            }
        }
        if (deviceList.size() > 0 && selectDevice == null) {
            selectDevice = deviceList.get(0);
            update = true;
        }
    } else {
        update = false;
    }
    return update;
}
 
Example #5
Source File: AndroidModuleLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAndroidLibraryDependencies(@NotNull Project androidProject,
                                           @NotNull Module androidModule,
                                           @NotNull Module flutterModule) {
  AndroidSdkUtils.setupAndroidPlatformIfNecessary(androidModule, true);
  Sdk currentSdk = ModuleRootManager.getInstance(androidModule).getSdk();
  if (currentSdk != null) {
    // TODO(messick) Add sdk dependency on currentSdk if not already set
  }
  LibraryTable androidProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(androidProject);
  Library[] androidProjectLibraries = androidProjectLibraryTable.getLibraries();
  LibraryTable flutterProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(getProject());
  Library[] flutterProjectLibraries = flutterProjectLibraryTable.getLibraries();
  Set<String> knownLibraryNames = new HashSet<>(flutterProjectLibraries.length);
  for (Library lib : flutterProjectLibraries) {
    if (lib.getName() != null) {
      knownLibraryNames.add(lib.getName());
    }
  }
  for (Library library : androidProjectLibraries) {
    if (library.getName() != null && !knownLibraryNames.contains(library.getName())) {

      List<String> roots = Arrays.asList(library.getRootProvider().getUrls(OrderRootType.CLASSES));
      Set<String> filteredRoots = roots.stream().filter(s -> shouldIncludeRoot(s)).collect(Collectors.toSet());
      if (filteredRoots.isEmpty()) continue;

      HashSet<String> sources = new HashSet<>(Arrays.asList(library.getRootProvider().getUrls(OrderRootType.SOURCES)));

      updateLibraryContent(library.getName(), filteredRoots, sources);
      updateAndroidModuleLibraryDependencies(flutterModule);
    }
  }
}
 
Example #6
Source File: AndroidModuleLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAndroidLibraryDependencies(@NotNull Project androidProject,
                                           @NotNull Module androidModule,
                                           @NotNull Module flutterModule) {
  AndroidSdkUtils.setupAndroidPlatformIfNecessary(androidModule, true);
  Sdk currentSdk = ModuleRootManager.getInstance(androidModule).getSdk();
  if (currentSdk != null) {
    // TODO(messick) Add sdk dependency on currentSdk if not already set
  }
  LibraryTable androidProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(androidProject);
  Library[] androidProjectLibraries = androidProjectLibraryTable.getLibraries();
  LibraryTable flutterProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(getProject());
  Library[] flutterProjectLibraries = flutterProjectLibraryTable.getLibraries();
  Set<String> knownLibraryNames = new HashSet<>(flutterProjectLibraries.length);
  for (Library lib : flutterProjectLibraries) {
    if (lib.getName() != null) {
      knownLibraryNames.add(lib.getName());
    }
  }
  for (Library library : androidProjectLibraries) {
    if (library.getName() != null && !knownLibraryNames.contains(library.getName())) {

      List<String> roots = Arrays.asList(library.getRootProvider().getUrls(OrderRootType.CLASSES));
      Set<String> filteredRoots = roots.stream().filter(s -> shouldIncludeRoot(s)).collect(Collectors.toSet());
      if (filteredRoots.isEmpty()) continue;

      HashSet<String> sources = new HashSet<>(Arrays.asList(library.getRootProvider().getUrls(OrderRootType.SOURCES)));

      updateLibraryContent(library.getName(), filteredRoots, sources);
      updateAndroidModuleLibraryDependencies(flutterModule);
    }
  }
}
 
Example #7
Source File: Utils.java    From WIFIADB with Apache License 2.0 5 votes vote down vote up
public static String getAdbPath() {
    String adbPath = Properties.instance().pjLevel().getValue(Config.ADB_PATH);

    if (Utils.isBlank(adbPath)) {
        final File adbFile = AndroidSdkUtils.getAdb(Global.instance().project());

        if (adbFile != null) {
            adbPath = adbFile.getAbsolutePath();
            Properties.instance().pjLevel().setValue(Config.ADB_PATH, adbPath);
        }
    }

    return adbPath;
}
 
Example #8
Source File: MobileInstallAdbLocationProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Returns location of the adb binary suitable for the given project. */
static Optional<String> getAdbLocationForMobileInstall(Project project) {
  for (MobileInstallAdbLocationProvider provider : EP_NAME.getExtensions()) {
    return provider.getAdbLocation(project);
  }
  File adb = AndroidSdkUtils.getAdb(project);
  return adb == null ? Optional.empty() : Optional.of(adb.getAbsolutePath());
}
 
Example #9
Source File: AdbWifiConnect.java    From ADBWIFI with Apache License 2.0 5 votes vote down vote up
private static DeviceResult getDevice(Project project) {
    List<AndroidFacet> facets = getApplicationFacets(project);
    if (!facets.isEmpty()) {
        AndroidFacet facet = facets.get(0);
        String packageName = AdbUtil.computePackageName(facet);
        AndroidDebugBridge bridge = AndroidSdkUtils.getDebugBridge(project);
        if (bridge == null) {
            error("No platform configured");
            return null;
        }
        int count = 0;
        while (!bridge.isConnected() || !bridge.hasInitialDeviceList()) {
            try {
                Thread.sleep(100);
                count++;
            } catch (InterruptedException e) {
                // pass
            }

            // let's not wait > 10 sec.
            if (count > 100) {
                error("Timeout getting device list!");
                return null;
            }
        }

        IDevice[] devices = bridge.getDevices();
        if (devices.length == 1) {
            return new DeviceResult(devices, facet, packageName);
        } else if (devices.length > 1) {
            return askUserForDevice(facet, packageName);
        } else {
            return null;
        }

    }
    error("No devices found");
    return null;
}
 
Example #10
Source File: AdbUSBRestart.java    From ADBWIFI with Apache License 2.0 5 votes vote down vote up
public static void restart(final Project project){

        EXECUTOR.submit(new Runnable() {
            @Override
            public void run() {
                String androidSdkPath;
                if (AndroidSdkUtils.getAndroidSdkPathsFromExistingPlatforms().size() > 0) {
                    androidSdkPath = Iterables.get(AndroidSdkUtils.getAndroidSdkPathsFromExistingPlatforms(), 0);
                    androidSdkPath = androidSdkPath + "/platform-tools/";
                } else {
                    error("Android SDK path not found");
                    return;
                }
                try {
                    WindowManager.getInstance().getStatusBar(project).setInfo("adb kill-server...");
                    Runtime.getRuntime().exec(androidSdkPath + "adb kill-server");
                    WindowManager.getInstance().getStatusBar(project).setInfo("adb start-server...");
                    Runtime.getRuntime().exec(androidSdkPath + "adb start-server");

                    info("restart successfully");

                } catch (IOException e) {
                    e.printStackTrace();
                    error(e.getMessage());
                }
            }
        });

    }
 
Example #11
Source File: ADB.java    From AndroidWiFiADB with Apache License 2.0 5 votes vote down vote up
private String getAdbPath() {
  String adbPath = "";
  File adbFile = AndroidSdkUtils.getAdb(project);
  if (adbFile != null) {
    adbPath = adbFile.getAbsolutePath();
  }
  return adbPath;
}
 
Example #12
Source File: LogviewFactory.java    From logviewer with Apache License 2.0 4 votes vote down vote up
@Override
public void createToolWindowContent(@NotNull final Project project, @NotNull final ToolWindow toolWindow) {

    final File adb = AndroidSdkUtils.getAdb(project);
    ExecutionManager.getInstance(project).getContentManager();

    RunnerLayoutUi layoutUi = RunnerLayoutUi.Factory.getInstance(project).create("LogViewer", TOOL_WINDOW_ID, "Logview Tools", project);

    toolWindow.setIcon(LogviewerPluginIcons.TOOL_ICON);
    toolWindow.setAvailable(true, null);
    toolWindow.setToHideOnEmptyContent(true);
    toolWindow.setTitle(TOOL_WINDOW_ID);


    DeviceContext deviceContext = new DeviceContext();

    Content logcatContent = createLogcatContent(layoutUi, project, deviceContext);
    final LogView logcatView = logcatContent.getUserData(LOG_VIEW_KEY);
    layoutUi.addContent(logcatContent, 0, PlaceInGrid.center, false);

    final JBLoadingPanel loadingPanel = new JBLoadingPanel(new BorderLayout(), project);
    loadingPanel.add(layoutUi.getComponent(), BorderLayout.CENTER);

    final ContentManager contentManager = toolWindow.getContentManager();
    Content c = contentManager.getFactory().createContent(loadingPanel, "", true);
    c.putUserData(LOG_VIEW_KEY, logcatView);
    contentManager.addContent(c);
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
            logcatView.activate();
        }
    }, project.getDisposed());


    if (adb != null) {
        loadingPanel.setLoadingText("Initializing ADB");
        loadingPanel.startLoading();

        //ListenableFuture<AndroidDebugBridge> future = AdbService.getInstance().getDebugBridge(adb);
        ListenableFuture<AndroidDebugBridge> future = AdbBridgeFactory.getAdb(adb);
        Futures.addCallback(future, new FutureCallback<AndroidDebugBridge>() {
            @Override
            public void onSuccess(@Nullable AndroidDebugBridge bridge) {
                Logger.getInstance(LogviewFactory.class).info("Successfully obtained debug bridge");
                loadingPanel.stopLoading();
            }

            @Override
            public void onFailure(@NotNull Throwable t) {
                loadingPanel.stopLoading();
                Logger.getInstance(LogviewFactory.class).info("Unable to obtain debug bridge", t);
                String msg;
                if (t.getMessage() != null) {
                    msg = t.getMessage();
                } else {
                    msg = String.format("Unable to establish a connection to adb",
                            ApplicationNamesInfo.getInstance().getProductName(), adb.getAbsolutePath());
                }
                Messages.showErrorDialog(msg, "ADB Connection Error");
            }
        }, EdtExecutor.INSTANCE);
    } else {
        logcatView.showHint("No adb connection!.\n\nDrag and drop log files to view them.");
    }
}
 
Example #13
Source File: BaseAction.java    From ADB-Duang with MIT License 4 votes vote down vote up
private DeviceResult getDevice(AnActionEvent anActionEvent) {
    List<AndroidFacet> facets = getApplicationFacets(anActionEvent.getProject());
    if (!facets.isEmpty()) {

        AndroidFacet facet = null;

        String androidFacetName = getAndroidFacetName(anActionEvent);

        if (androidFacetName != null) {
            for (AndroidFacet androidFacet : facets) {
                if (androidFacet.getModule().getName().equals(androidFacetName)) {
                    facet = androidFacet;
                }
            }
            if (facet == null) {
                return null;
            }

        } else {

            if (facets.size() > 1) {
                facet = ModuleChooserDialogHelper.showDialogForFacets(anActionEvent.getProject(), facets);
                if (facet == null) {
                    return null;
                }
            } else {
                facet = facets.get(0);
            }
        }
        String packageName =facet.getAndroidModuleInfo().getPackage();
        AndroidDebugBridge bridge = AndroidSdkUtils.getDebugBridge(anActionEvent.getProject());
        if (bridge == null) {
            error("No platform configured");
            return null;
        }

        if (bridge.isConnected() && bridge.hasInitialDeviceList()) {

            IDevice[] devices = bridge.getDevices();
            if (devices.length == 1) {
                return new DeviceResult(anActionEvent, devices[0], facet, packageName);
            } else if (devices.length > 1) {
                return askUserForDevice(anActionEvent, facet, packageName);
            } else {
                return new DeviceResult(anActionEvent, null, facet, null);
            }
        }
    }
    return new DeviceResult(anActionEvent, null, null, null);
}
 
Example #14
Source File: ADB.java    From AndroidWiFiADB with Apache License 2.0 4 votes vote down vote up
public boolean isInstalled() {
  return AndroidSdkUtils.isAndroidSdkAvailable();
}