Skip to content

Commit caad25f

Browse files
authored
Merge pull request pbreault#120 from tiwiz/toggle_wifi_mobile
Toggle WiFi and Mobile data on target device or emulator
2 parents 72e1acb + 43342d3 commit caad25f

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.developerphil.adbidea.action
2+
3+
import com.developerphil.adbidea.adb.AdbFacade
4+
import com.intellij.openapi.actionSystem.AnActionEvent
5+
import com.intellij.openapi.project.Project
6+
7+
class DisableMobileAction : AdbAction() {
8+
override fun actionPerformed(e: AnActionEvent, project: Project) = AdbFacade.disableMobile(project)
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.developerphil.adbidea.action
2+
3+
import com.developerphil.adbidea.adb.AdbFacade
4+
import com.intellij.openapi.actionSystem.AnActionEvent
5+
import com.intellij.openapi.project.Project
6+
7+
class DisableWifiAction : AdbAction() {
8+
override fun actionPerformed(e: AnActionEvent, project: Project) = AdbFacade.disableWifi(project)
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.developerphil.adbidea.action
2+
3+
import com.developerphil.adbidea.adb.AdbFacade
4+
import com.intellij.openapi.actionSystem.AnActionEvent
5+
import com.intellij.openapi.project.Project
6+
7+
class EnableMobileAction : AdbAction() {
8+
override fun actionPerformed(e: AnActionEvent, project: Project) = AdbFacade.enableMobile(project)
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.developerphil.adbidea.action
2+
3+
import com.developerphil.adbidea.adb.AdbFacade
4+
import com.intellij.openapi.actionSystem.AnActionEvent
5+
import com.intellij.openapi.project.Project
6+
7+
class EnableWifiAction : AdbAction() {
8+
override fun actionPerformed(e: AnActionEvent, project: Project) = AdbFacade.enableWifi(project)
9+
}

src/main/kotlin/com/developerphil/adbidea/adb/AdbFacade.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.developerphil.adbidea.adb
22

33
import com.developerphil.adbidea.ObjectGraph
44
import com.developerphil.adbidea.adb.command.*
5+
import com.developerphil.adbidea.adb.command.SvcCommand.MOBILE
6+
import com.developerphil.adbidea.adb.command.SvcCommand.WIFI
57
import com.developerphil.adbidea.ui.NotificationHelper
68
import com.google.common.util.concurrent.ThreadFactoryBuilder
79
import com.intellij.openapi.project.Project
@@ -22,6 +24,10 @@ object AdbFacade {
2224
fun clearData(project: Project) = executeOnDevice(project, ClearDataCommand())
2325
fun clearDataAndRestart(project: Project) = executeOnDevice(project, ClearDataAndRestartCommand())
2426
fun clearDataAndRestartWithDebugger(project: Project) = executeOnDevice(project, ClearDataAndRestartWithDebuggerCommand())
27+
fun enableWifi(project: Project) = executeOnDevice(project, ToggleSvcCommand(WIFI, true))
28+
fun disableWifi(project: Project) = executeOnDevice(project, ToggleSvcCommand(WIFI, false))
29+
fun enableMobile(project: Project) = executeOnDevice(project, ToggleSvcCommand(MOBILE, true))
30+
fun disableMobile(project: Project) = executeOnDevice(project, ToggleSvcCommand(MOBILE, false))
2531

2632
private fun executeOnDevice(project: Project, runnable: Command) {
2733
if (AdbUtil.isGradleSyncInProgress(project)) {
@@ -41,4 +47,4 @@ object AdbFacade {
4147
NotificationHelper.error("No Device found")
4248
}
4349
}
44-
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.developerphil.adbidea.adb.command
2+
3+
import com.android.ddmlib.IDevice
4+
import com.developerphil.adbidea.adb.command.receiver.GenericReceiver
5+
import com.developerphil.adbidea.ui.NotificationHelper.error
6+
import com.developerphil.adbidea.ui.NotificationHelper.info
7+
import com.intellij.openapi.project.Project
8+
import org.jetbrains.android.facet.AndroidFacet
9+
import java.util.concurrent.TimeUnit
10+
11+
enum class SvcCommand(val parameter: String, val description: String) {
12+
WIFI("wifi", "Wi-Fi"),
13+
MOBILE("data", "Mobile data")
14+
}
15+
16+
class ToggleSvcCommand(
17+
private val command: SvcCommand,
18+
private val enable: Boolean) : Command {
19+
20+
private val shellCommand = "svc ${command.parameter} ${enable.toState()}"
21+
22+
override fun run(project: Project, device: IDevice, facet: AndroidFacet, packageName: String): Boolean {
23+
try {
24+
device.executeShellCommand(shellCommand, GenericReceiver(), 30L, TimeUnit.SECONDS)
25+
info(String.format("<b>%s</b> %s%s on %s", command.description, enable.toState(), "d", device.name))
26+
return true
27+
} catch (e: Exception) {
28+
error("Failure while attempting to ${enable.toState()} ${command.description} on ${device.name}: " + e.message)
29+
}
30+
return false
31+
}
32+
33+
private fun Boolean.toState() = if (this) "enable" else "disable"
34+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@
208208
description="Clears the private storage of the app and restarts it, and attach the debugger">
209209
</action>
210210
<add-to-group group-id="AndroidToolsGroup" anchor="first"/>
211+
<separator/>
212+
213+
<action id="com.developerphil.adbidea.action.EnableWifiAction"
214+
class="com.developerphil.adbidea.action.EnableWifiAction"
215+
text="ADB Enable Wi-Fi"
216+
description="Enable Wi-Fi on device or emulator">
217+
</action>
218+
219+
<action id="com.developerphil.adbidea.action.DisableWifiAction"
220+
class="com.developerphil.adbidea.action.DisableWifiAction"
221+
text="ADB Disable Wi-Fi"
222+
description="Disable Wi-Fi on device or emulator">
223+
</action>
224+
225+
<action id="com.developerphil.adbidea.action.EnableMobileAction"
226+
class="com.developerphil.adbidea.action.EnableMobileAction"
227+
text="ADB Enable Mobile Data"
228+
description="Enable mobile data on device or emulator">
229+
</action>
230+
231+
<action id="com.developerphil.adbidea.action.DisableMobileAction"
232+
class="com.developerphil.adbidea.action.DisableMobileAction"
233+
text="ADB Disable Mobile Data"
234+
description="Disable mobile data on device or emulator">
235+
</action>
236+
211237
</group>
212238
</actions>
213239

@@ -216,4 +242,4 @@
216242
<implementation-class>com.developerphil.adbidea.ObjectGraph</implementation-class>
217243
</component>
218244
</project-components>
219-
</idea-plugin>
245+
</idea-plugin>

0 commit comments

Comments
 (0)