Skip to content

Commit 152cf05

Browse files
committed
Toggle WiFi and Mobile data on target device or emulator
1 parent ecfdaf1 commit 152cf05

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
@@ -21,6 +23,10 @@ object AdbFacade {
2123
fun restartDefaultActivityWithDebugger(project: Project) = executeOnDevice(project, CommandList(KillCommand(), StartDefaultActivityCommand(true)))
2224
fun clearData(project: Project) = executeOnDevice(project, ClearDataCommand())
2325
fun clearDataAndRestart(project: Project) = executeOnDevice(project, ClearDataAndRestartCommand())
26+
fun enableWifi(project: Project) = executeOnDevice(project, ToggleSvcCommand(WIFI, true))
27+
fun disableWifi(project: Project) = executeOnDevice(project, ToggleSvcCommand(WIFI, false))
28+
fun enableMobile(project: Project) = executeOnDevice(project, ToggleSvcCommand(MOBILE, true))
29+
fun disableMobile(project: Project) = executeOnDevice(project, ToggleSvcCommand(MOBILE, false))
2430

2531
private fun executeOnDevice(project: Project, runnable: Command) {
2632
if (AdbUtil.isGradleSyncInProgress(project)) {
@@ -40,4 +46,4 @@ object AdbFacade {
4046
NotificationHelper.error("No Device found")
4147
}
4248
}
43-
}
49+
}
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("mobile", "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
@@ -202,6 +202,32 @@
202202
description="Restarts the current application and attach the debugger">
203203
</action>
204204
<add-to-group group-id="AndroidToolsGroup" anchor="first"/>
205+
<separator/>
206+
207+
<action id="com.developerphil.adbidea.action.EnableWifiAction"
208+
class="com.developerphil.adbidea.action.EnableWifiAction"
209+
text="Enable Wi-Fi"
210+
description="Enable Wi-Fi on device or emulator">
211+
</action>
212+
213+
<action id="com.developerphil.adbidea.action.DisableWifiAction"
214+
class="com.developerphil.adbidea.action.DisableWifiAction"
215+
text="Disable Wi-Fi"
216+
description="Disable Wi-Fi on device or emulator">
217+
</action>
218+
219+
<action id="com.developerphil.adbidea.action.EnableMobileAction"
220+
class="com.developerphil.adbidea.action.EnableMobileAction"
221+
text="Enable Mobile Data"
222+
description="Enable mobile data on device or emulator">
223+
</action>
224+
225+
<action id="com.developerphil.adbidea.action.DisableMobileAction"
226+
class="com.developerphil.adbidea.action.DisableMobileAction"
227+
text="Disable Mobile Data"
228+
description="Disable mobile data on device or emulator">
229+
</action>
230+
205231
</group>
206232
</actions>
207233

@@ -210,4 +236,4 @@
210236
<implementation-class>com.developerphil.adbidea.ObjectGraph</implementation-class>
211237
</component>
212238
</project-components>
213-
</idea-plugin>
239+
</idea-plugin>

0 commit comments

Comments
 (0)