Skip to content

Commit

Permalink
update(adb, scan): support choose network interface, more friendly ad…
Browse files Browse the repository at this point in the history
…b path config.
  • Loading branch information
dengzii committed Mar 26, 2021
1 parent 73f6cc2 commit 788d408
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 71 deletions.
6 changes: 5 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<id>com.dengzii.plugin.adb</id>
<name>Android WiFiADB</name>

<version>3.5</version>
<version>3.6</version>
<vendor email="dengzii@foxmail.com" url="https://github.com/dengzii">dengzi</vendor>
<description><![CDATA[
<h3>An AndroidStudio plugin use for connecting Android device over WiFi.</h3>
Expand All @@ -30,6 +30,10 @@
]]></description>

<change-notes><![CDATA[
<b>3.6 (2021-03-26)</b><br>
1. feat(config adb): config adb path by file browser, try search most common adb locations.<br>
2. feat(scan device): choose network interface support.<br>
<b>3.5 (2021-03-10)</b><br>
1. fix: cannot remember configured adb path.<br>
Expand Down
25 changes: 16 additions & 9 deletions src/com/dengzii/plugin/adb/ui/ConfigAdbDialog.form
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
<xy x="48" y="54" width="582" height="297"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -49,14 +49,6 @@
<properties/>
<border type="none"/>
<children>
<component id="d029e" class="javax.swing.JTextField" binding="textField1" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="a7d20" class="javax.swing.JLabel" binding="label">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
Expand All @@ -65,6 +57,21 @@
<text value="Adb command is unavailable, please input adb path "/>
</properties>
</component>
<grid id="43b77" binding="panelField" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="d029e" class="javax.swing.JTextField" binding="textField1" default-binding="true">
<constraints border-constraint="Center"/>
<properties>
<minimumSize width="49" height="10"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</children>
Expand Down
59 changes: 55 additions & 4 deletions src/com/dengzii/plugin/adb/ui/ConfigAdbDialog.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.dengzii.plugin.adb.ui;

import com.dengzii.plugin.adb.Config;
import com.dengzii.plugin.adb.utils.AdbUtils;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.actionSystem.impl.PresentationFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;

Expand All @@ -12,6 +19,8 @@ public class ConfigAdbDialog extends JDialog {
private JButton buttonOK;
private JTextField textField1;
private JLabel label;
private JPanel panelField;
private boolean lookAdb;

public ConfigAdbDialog() {
setContentPane(contentPane);
Expand All @@ -23,8 +32,10 @@ public ConfigAdbDialog() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

public static void createAndShow() {
new ConfigAdbDialog().init();
public static void createAndShow(boolean lookAdb) {
ConfigAdbDialog d = new ConfigAdbDialog();
d.lookAdb = lookAdb;
d.init();
}

private void onOK() {
Expand All @@ -41,7 +52,6 @@ private void onOK() {
}

private void init() {
label.setText("Adb command is unavailable, please input adb path");
textField1.setText(Config.INSTANCE.loadAdbPath());
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int w = 360;
Expand All @@ -52,7 +62,48 @@ private void init() {
setPreferredSize(new Dimension(w, h));

setTitle("Configure adb path");
if (!lookAdb) {
label.setText("Adb Path");
} else {
label.setText("Adb command is unavailable, please input adb path");
}
PresentationFactory factory = new PresentationFactory();
AnAction action = new AnAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
chooseAdb();
}
};
Presentation p = factory.getPresentation(action);
p.setText("Browser File");
p.setIcon(AllIcons.General.OpenDisk);
ActionButton actionButton = new ActionButton(action, p, "", new Dimension(26, 24));
panelField.add(actionButton, BorderLayout.EAST);
pack();
setVisible(true);
}

private void chooseAdb() {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setDialogTitle("Select adb executable file.");

FileFilter filter = new FileFilter() {
@Override
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isDirectory() || name.equals("adb") || name.equals("adb.exe");
}

@Override
public String getDescription() {
return "adb executable file";
}
};
fc.setFileFilter(filter);
int f = fc.showOpenDialog(panelField);
if (f == JFileChooser.APPROVE_OPTION) {
textField1.setText(fc.getSelectedFile().getAbsolutePath());
}
}
}
6 changes: 3 additions & 3 deletions src/com/dengzii/plugin/adb/ui/MainDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class MainDialog : MainDialogDesign() {
buttonRefresh.onClick {
updateDevice()
}
if (!AdbUtils.isAdbAvailable()) {
ConfigAdbDialog.createAndShow()
if (!AdbUtils.lookingForAdb()) {
ConfigAdbDialog.createAndShow(true)
}
initDeviceTable()
updateDevice()
Expand Down Expand Up @@ -246,7 +246,7 @@ class MainDialog : MainDialogDesign() {
}
menu("Settings") {
item("Configure Adb Path") {
ConfigAdbDialog.createAndShow()
ConfigAdbDialog.createAndShow(false)
}
item("Custom Column") {
ConfigDialog.createAndShow {
Expand Down
55 changes: 38 additions & 17 deletions src/com/dengzii/plugin/adb/ui/ScanDeviceDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ScanDeviceDialog(private var callback: (InetSocketAddress) -> Unit) : Scan
private val tableData = mutableListOf<MutableList<Any?>>()
private val columnInfo = mutableListOf<ColumnInfo<Any>>()
private val tableAdapter = TableAdapter(tableData, columnInfo)
private val subnetIp: List<InetAddress>
private val subnetIp = mutableListOf<InetAddress>()

companion object {
fun show(callback: (InetSocketAddress) -> Unit) {
Expand Down Expand Up @@ -57,26 +57,47 @@ class ScanDeviceDialog(private var callback: (InetSocketAddress) -> Unit) : Scan
tableResult.rowSelectionAllowed = false
tableAdapter.setup(tableResult)

val localhost = InetAddress.getLocalHost()
val bitMask = NetworkInterface.getByInetAddress(localhost)
.interfaceAddresses[0].networkPrefixLength
subnetIp = DeviceManager.getAllSubnetIp(localhost)

fieldIpStart.text = (subnetIp.first().address[3].toLong() and 0xff).toString()
fieldIpEnd.text = (subnetIp.last().address[3].toLong() and 0xff).toString()
labelIpStart.text = subnetIp.first().hostAddress.removeSuffix(fieldIpStart.text)
labelIpEnd.text = subnetIp.last().hostAddress.removeSuffix(fieldIpEnd.text)
labelIp.text = "${localhost.hostAddress}/${bitMask}"

fieldThreadNum.text = Runtime.getRuntime().availableProcessors().toString()
labelProcessor.text = "Available Processors : ${fieldThreadNum.text}"
labelProgress.text = "Tap scan to start scan available device."

val networkInterfaces = NetworkInterface.getNetworkInterfaces()
while (networkInterfaces.hasMoreElements()) {
val n = networkInterfaces.nextElement()
if (n.isUp && n.interfaceAddresses.isNotEmpty() && n.name != "lo") {
comboBoxInterface.addItem("${n.index}-${n.name} ${n.displayName}")
}
}
val localhost = InetAddress.getLocalHost()
comboBoxInterface.addItemListener {
if (comboBoxInterface.selectedIndex == 0){
initAddress(localhost)
return@addItemListener
}
val s = comboBoxInterface.selectedItem!!.toString()
val inface = NetworkInterface.getByIndex(s.split("-")[0].toInt())
initAddress(inface.interfaceAddresses[0].address)
}
initAddress(localhost)
buttonScan.onClick {
scan()
}
}

private fun initAddress(host:InetAddress) {

val bitMask = NetworkInterface.getByInetAddress(host)
.interfaceAddresses[0].networkPrefixLength
subnetIp.clear()
subnetIp.addAll(DeviceManager.getAllSubnetIp(host))

fieldIpStart.text = (subnetIp.first().address[3].toLong() and 0xff).toString()
fieldIpEnd.text = (subnetIp.last().address[3].toLong() and 0xff).toString()
labelIpStart.text = subnetIp.first().hostAddress.removeSuffix(fieldIpStart.text)
labelIpEnd.text = subnetIp.last().hostAddress.removeSuffix(fieldIpEnd.text)
labelIp.text = "${subnetIp.first().hostAddress}/${bitMask}"
}

private var scanExecutor: ExecutorService? = null
private var tableInUpdate = AtomicBoolean(false)

Expand All @@ -93,11 +114,11 @@ class ScanDeviceDialog(private var callback: (InetSocketAddress) -> Unit) : Scan
tableData.clear()
tableAdapter.fireTableDataChanged()
scanExecutor = DeviceManager.scanAvailableDevicesLan(
timeout = fieldTimeoutPing.text.toInt(),
adbTimeout = filedTimeoutAdb.text.toInt(),
threadPoolSize = fieldThreadNum.text.toInt(),
ports = (fieldPortStart.text.toInt()..fieldPortEnd.text.toInt() step 1).toList(),
scanIp = ips
timeout = fieldTimeoutPing.text.toInt(),
adbTimeout = filedTimeoutAdb.text.toInt(),
threadPoolSize = fieldThreadNum.text.toInt(),
ports = (fieldPortStart.text.toInt()..fieldPortEnd.text.toInt() step 1).toList(),
scanIp = ips
) { progress, message, ip ->
invokeLater {
labelProgress.text = "$progress% $message"
Expand Down
46 changes: 36 additions & 10 deletions src/com/dengzii/plugin/adb/ui/ScanDialogDesign.form
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,27 @@
</grid>
</children>
</grid>
<grid id="6a14e" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="8" left="6" bottom="8" right="6"/>
<grid id="6a14e" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="6" bottom="8" right="6"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line" title="Scan IP">
<border type="line" title="Scan">
<color color="-13487566"/>
</border>
<children>
<grid id="1d043" layout-manager="GridLayoutManager" row-count="1" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="1d043" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="89742" class="javax.swing.JTextField" binding="fieldIpStart">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -138,7 +138,7 @@
</component>
<component id="7d15e" class="javax.swing.JTextField" binding="fieldIpEnd">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -148,28 +148,54 @@
</component>
<component id="1a16d" class="javax.swing.JLabel" binding="labelIpStart">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="127.0.0."/>
</properties>
</component>
<component id="5b0f3" class="javax.swing.JLabel" binding="labelIpEnd">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="127.0.0."/>
</properties>
</component>
<component id="8d2a2" class="javax.swing.JLabel" binding="labelIp">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="127.0.0.1/32"/>
</properties>
</component>
<component id="dbbf4" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="IP"/>
</properties>
</component>
<component id="4c71b" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Interface"/>
</properties>
</component>
<component id="4895a" class="javax.swing.JComboBox" binding="comboBoxInterface">
<constraints>
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="default"/>
</model>
</properties>
</component>
</children>
</grid>
</children>
Expand Down
1 change: 1 addition & 0 deletions src/com/dengzii/plugin/adb/ui/ScanDialogDesign.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ScanDialogDesign extends XDialog {
public JLabel labelIpStart;
public JLabel labelIpEnd;
public JLabel labelIp;
public JComboBox<String> comboBoxInterface;

ScanDialogDesign(){
super("Scan Device [beta]");
Expand Down

0 comments on commit 788d408

Please sign in to comment.