Skip to content

Commit 1933183

Browse files
author
Lalit Sharma
committed
Enhance emulator time management: add adb root command for clock control, log time state before and after setting, and improve deep link handling with escaped URLs.
1 parent 655734d commit 1933183

1 file changed

Lines changed: 57 additions & 5 deletions

File tree

.github/workflows/wearos-emulator-screenshots.yml

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,13 @@ jobs:
408408
409409
adb -s "$emulator_serial" wait-for-device
410410
411+
echo "Attempting adb root for emulator clock control (best-effort)..."
412+
adb_root_output="$(adb -s "$emulator_serial" root </dev/null 2>&1 || true)"
413+
printf '%s\n' "$adb_root_output" | head -5
414+
adb -s "$emulator_serial" wait-for-device || true
415+
echo "adbd identity:"
416+
adb -s "$emulator_serial" shell id </dev/null 2>&1 | head -3 || true
417+
411418
boot_completed=""
412419
for _ in {1..180}; do
413420
boot_completed="$(adb -s "$emulator_serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
@@ -445,10 +452,27 @@ jobs:
445452
adb -s "$emulator_serial" shell input keyevent KEYCODE_WAKEUP || true
446453
sleep 1
447454
455+
log_emulator_time_state() {
456+
local label="$1"
457+
local current_epoch
458+
local current_utc
459+
local auto_time_setting
460+
local timezone_value
461+
current_epoch="$(adb -s "$emulator_serial" shell date +%s </dev/null 2>/dev/null | tr -d '\r' || true)"
462+
current_utc="$(adb -s "$emulator_serial" shell date -u </dev/null 2>/dev/null | tr -d '\r' || true)"
463+
auto_time_setting="$(adb -s "$emulator_serial" shell settings get global auto_time </dev/null 2>/dev/null | tr -d '\r' || true)"
464+
timezone_value="$(adb -s "$emulator_serial" shell getprop persist.sys.timezone </dev/null 2>/dev/null | tr -d '\r' || true)"
465+
echo "$label: epoch=$current_epoch utc='$current_utc' auto_time=$auto_time_setting timezone=$timezone_value"
466+
}
467+
468+
echo "Emulator console help snapshot:"
469+
adb -s "$emulator_serial" emu help </dev/null 2>&1 | head -40 || true
470+
448471
set_emulator_time_utc() {
449472
local iso_utc="$1"
450473
451474
echo "Setting emulator time to: $iso_utc"
475+
log_emulator_time_state "Clock state before set"
452476
453477
# Convert to Unix timestamp (seconds since epoch)
454478
local timestamp_sec
@@ -460,9 +484,11 @@ jobs:
460484
echo "Using emulator console to set time (timestamp ms: $timestamp_ms)"
461485
462486
# Prefer emulator console clock command first.
463-
if adb -s "$emulator_serial" emu avd setclock "$timestamp_ms" </dev/null >/dev/null 2>&1; then
487+
if setclock_output="$(adb -s "$emulator_serial" emu avd setclock "$timestamp_ms" </dev/null 2>&1)"; then
488+
printf '%s\n' "$setclock_output" | head -5
464489
echo "Time set via emulator console clock"
465490
else
491+
printf '%s\n' "$setclock_output" | head -5
466492
echo "::warning::Emulator console clock command failed; falling back to shell date command."
467493
fi
468494
@@ -477,15 +503,29 @@ jobs:
477503
printf '%s\n' "$shell_date_output" | head -5
478504
echo "::warning::Shell date command failed. Continuing with emulator-provided clock."
479505
fi
506+
507+
# Broadcast time-set change in case framework listeners need a nudge.
508+
adb -s "$emulator_serial" shell am broadcast -a android.intent.action.TIME_SET </dev/null >/dev/null 2>&1 || true
480509
else
481510
echo "::warning::Failed to parse timestamp from $iso_utc"
482511
fi
483512
484513
sleep 2
485514
486515
# Verify time was set
487-
current_time="$(adb -s "$emulator_serial" shell "date -u" </dev/null 2>/dev/null | tr -d '\r')"
488-
echo "Emulator time is now: $current_time"
516+
current_time="$(adb -s "$emulator_serial" shell date -u </dev/null 2>/dev/null | tr -d '\r')"
517+
current_epoch="$(adb -s "$emulator_serial" shell date +%s </dev/null 2>/dev/null | tr -d '\r')"
518+
echo "Emulator time is now: $current_time (epoch=$current_epoch, target_epoch=$timestamp_sec)"
519+
if [[ "$current_epoch" =~ ^[0-9]+$ ]] && [[ "$timestamp_sec" =~ ^[0-9]+$ ]]; then
520+
delta_sec="$((current_epoch - timestamp_sec))"
521+
if [ "$delta_sec" -lt 0 ]; then
522+
delta_sec=$(( -delta_sec ))
523+
fi
524+
if [ "$delta_sec" -gt 120 ]; then
525+
echo "::warning::Emulator clock did not move close to requested UTC time (delta=${delta_sec}s)."
526+
fi
527+
fi
528+
log_emulator_time_state "Clock state after set"
489529
}
490530
491531
set_emulator_geo_gibraltar() {
@@ -521,28 +561,40 @@ jobs:
521561
open_deep_link() {
522562
local url="$1"
523563
local tmpout="/tmp/deeplink_output_$$.txt"
564+
local escaped_url
565+
escaped_url="$(printf '%s' "$url" | sed "s/'/'\\\\''/g")"
524566
525567
adb -s "$emulator_serial" shell am force-stop "$BUNDLE_ID" </dev/null >/dev/null 2>&1 || true
526568
sleep 1
527569
528570
echo "Attempting to open deep link: $url"
529-
if adb -s "$emulator_serial" shell am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "$url" -p "$BUNDLE_ID" </dev/null >"$tmpout" 2>&1; then
571+
if adb -s "$emulator_serial" shell "am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d '$escaped_url' -p '$BUNDLE_ID'" </dev/null >"$tmpout" 2>&1; then
530572
echo "Deep link opened successfully (method 1)"
531573
rm -f "$tmpout"
532574
return 0
533575
fi
534576
535577
echo "Method 1 failed, output:"
536578
cat "$tmpout" 2>/dev/null | head -10
579+
if grep -Fq "Status: ok" "$tmpout"; then
580+
echo "::warning::Method 1 returned non-zero but am start reported Status: ok. Treating as success."
581+
rm -f "$tmpout"
582+
return 0
583+
fi
537584
538-
if adb -s "$emulator_serial" shell am start -S -W -a android.intent.action.VIEW -d "$url" -n "$BUNDLE_ID/$MAIN_ACTIVITY" </dev/null >"$tmpout" 2>&1; then
585+
if adb -s "$emulator_serial" shell "am start -S -W -a android.intent.action.VIEW -d '$escaped_url' -n '$BUNDLE_ID/$MAIN_ACTIVITY'" </dev/null >"$tmpout" 2>&1; then
539586
echo "Deep link opened successfully (method 2)"
540587
rm -f "$tmpout"
541588
return 0
542589
fi
543590
544591
echo "Method 2 failed, output:"
545592
cat "$tmpout" 2>/dev/null | head -10
593+
if grep -Fq "Status: ok" "$tmpout"; then
594+
echo "::warning::Method 2 returned non-zero but am start reported Status: ok. Treating as success."
595+
rm -f "$tmpout"
596+
return 0
597+
fi
546598
rm -f "$tmpout"
547599
548600
return 1

0 commit comments

Comments
 (0)