Skip to content

Commit

Permalink
Fix network adb multi-device issue (#534)
Browse files Browse the repository at this point in the history
* Fix network adb multi-device issue

* Update PreInstallListener.java

* update

* Fix client app crash

* update

---------

Co-authored-by: Le Zhou <[email protected]>
Co-authored-by: Ran Tao <[email protected]>
  • Loading branch information
3 people authored Jul 14, 2023
1 parent 0a4cd05 commit cac5736
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions android_client/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down Expand Up @@ -69,6 +68,7 @@

<service
android:name=".ScreenRecorderService"
android:foregroundServiceType="mediaProjection"
android:exported="true" />
<service
android:name=".KeepAliveService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// Licensed under the MIT License.
package com.microsoft.hydralab.android.client;

import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.os.Build.VERSION_CODES.M;
import static com.microsoft.hydralab.android.client.ScreenRecorderService.AUDIO_AAC;
import static com.microsoft.hydralab.android.client.ScreenRecorderService.VIDEO_AVC;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
Expand Down Expand Up @@ -47,12 +53,6 @@
import java.util.List;
import java.util.Locale;

import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.os.Build.VERSION_CODES.M;
import static com.microsoft.hydralab.android.client.ScreenRecorderService.AUDIO_AAC;
import static com.microsoft.hydralab.android.client.ScreenRecorderService.VIDEO_AVC;

public class MainActivity extends Activity {
private static final String TAG = "ScreenRecorder";
private static final String DEFAULT_ACV_PROFILE = "Default";
Expand Down Expand Up @@ -283,7 +283,12 @@ private void startCapturing(int resultCode, Intent data) {
intent.putExtra("dstPath", file.getAbsolutePath());
intent.putExtra("resultCode", resultCode);
intent.putExtra("data", data);
startService(intent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}

handler.postDelayed(updateButtonState, 1000);
//moveTaskToBack(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static android.media.MediaFormat.MIMETYPE_VIDEO_AVC;

import android.app.Activity;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
Expand Down Expand Up @@ -182,6 +183,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
dstPath = new File(getSavingDir(), fileName).getAbsolutePath();
}

startForeground(Notifications.id, mNotifications.createRecordingNotification());

if (mMediaProjection == null) {
Intent data = intent.getParcelableExtra("data");
int resultCode = intent.getIntExtra("resultCode", 0);
Expand All @@ -205,8 +208,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
mAudioEncoder = audio == null ? null : new MicRecorder(audio);

start();

startForeground(Notifications.id, mNotifications.createRecordingNotification());
}
return START_STICKY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public void updateWithDefaultValues() {
if (StringUtils.isBlank(testSuiteClass)) {
testSuiteClass = pkgName;
}
if (enableNetworkMonitor && StringUtils.isBlank(networkMonitorRule)) {
networkMonitorRule = pkgName;
}
}

private void determineScopeOfTestCase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void onDeviceConnected(DeviceInfo deviceInfo) {
try {
FlowUtil.retryAndSleepWhenFalse(3, 10, () -> deviceDriverManager.installApp(deviceInfo, appFile.getAbsolutePath(), classLogger));
classLogger.info("Pre-Install {} successfully", appFile.getAbsolutePath());
break;
} catch (Exception e) {
String errorMessage = String.format("Pre-Install %s failed", appFile.getAbsolutePath());
classLogger.error(errorMessage, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ public void start() {
logger.info("Start VPN service");

// launch vpn
String command_launch = "adb shell am start";
String command_launch = "am start";
command_launch += " -a com.microsoft.hydralab.android.client.vpn.START";
command_launch += " -n com.microsoft.hydralab.android.client/.VpnActivity";
ShellUtils.execLocalCommandWithResult(command_launch, logger);
try {
adbOperateUtil.execOnDevice(deviceInfo, command_launch, new MultiLineNoCancelLoggingReceiver(logger), logger);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
while (true) {
ThreadUtils.safeSleep(1000);
boolean clicked = grantPermissionClick();
Expand All @@ -61,12 +65,16 @@ public void start() {
}

// start vpn
String command_start = "adb shell am start";
String command_start = "am start";
command_start += " -a com.microsoft.hydralab.android.client.vpn.START";
command_start += " -n com.microsoft.hydralab.android.client/.VpnActivity";
command_start += String.format(" --es \"apps\" \"%s\"", rule);
command_start += " --es \"output\" \"" + AndroidDumpPath + "\"";
ShellUtils.execLocalCommandWithResult(command_start, logger);
try {
adbOperateUtil.execOnDevice(deviceInfo, command_start, new MultiLineNoCancelLoggingReceiver(logger), logger);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
while (true) {
ThreadUtils.safeSleep(1000);
boolean clicked = grantPermissionClick();
Expand All @@ -82,10 +90,14 @@ public void stop() {

try {
// stop vpn
String command_stop = "adb shell am start";
String command_stop = "am start";
command_stop += " -a com.microsoft.hydralab.android.client.vpn.STOP";
command_stop += " -n com.microsoft.hydralab.android.client/.VpnActivity";
ShellUtils.execLocalCommandWithResult(command_stop, logger);
try {
adbOperateUtil.execOnDevice(deviceInfo, command_stop, new MultiLineNoCancelLoggingReceiver(logger), logger);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
ThreadUtils.safeSleep(2000);

// pull result
Expand Down Expand Up @@ -113,7 +125,7 @@ public void stop() {
}

private boolean grantPermissionClick() {
String[] possibleTexts = { "Start now", "Allow", "允许" };
String[] possibleTexts = { "Start now", "Allow", "OK", "允许" };
String dump = dumpView(deviceInfo, logger);
// classLogger.info("Dump on {}: {}", adbDeviceInfo.getSerialNum(), dump);
if (StringUtils.isBlank(dump)) {
Expand Down

0 comments on commit cac5736

Please sign in to comment.