Skip to content

Commit daf758e

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: update test script and vibrator_haptics nil safety
1 parent 3d91e23 commit daf758e

2 files changed

Lines changed: 36 additions & 26 deletions

File tree

examples/test_all_apks.sh

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,16 @@ for i in "${!EXAMPLES[@]}"; do
8282
# Uninstall old version (ignore errors)
8383
"$ADB" uninstall "$pkg" > /dev/null 2>&1 || true
8484

85-
# Install
85+
# Install via push + pm install (more reliable than streaming install)
8686
echo " Installing..."
87-
if ! "$ADB" install -r "$dir/build/$name.apk" >> "$result_file" 2>&1; then
87+
"$ADB" wait-for-device
88+
if ! "$ADB" push "$dir/build/$name.apk" /data/local/tmp/ >> "$result_file" 2>&1; then
89+
echo " PUSH FAILED"
90+
FAIL=$((FAIL + 1))
91+
ERRORS="$ERRORS\n INSTALL_FAIL: $name"
92+
continue
93+
fi
94+
if ! "$ADB" shell pm install -r "/data/local/tmp/$name.apk" >> "$result_file" 2>&1; then
8895
echo " INSTALL FAILED"
8996
FAIL=$((FAIL + 1))
9097
ERRORS="$ERRORS\n INSTALL_FAIL: $name"
@@ -97,34 +104,45 @@ for i in "${!EXAMPLES[@]}"; do
97104
"$ADB" shell pm grant "$pkg" "$perm" 2>/dev/null || true
98105
done
99106

100-
# Clear logcat and wait for any stale crash dumps to flush
107+
# Clear logcat
101108
"$ADB" logcat -c 2>/dev/null || true
102-
sleep 2
109+
sleep 1
103110
"$ADB" logcat -c 2>/dev/null || true
104111

105112
# Launch
106113
echo " Running..."
107114
"$ADB" shell am start -n "$pkg/android.app.NativeActivity" >> "$result_file" 2>&1
108115

109-
# Get the PID of the launched app for filtering
116+
# Get the PID of the launched app for filtering (take first PID if multiple)
110117
sleep 1
111-
app_pid=$("$ADB" shell pidof "$pkg" 2>/dev/null || true)
118+
app_pid=$("$ADB" shell pidof "$pkg" 2>/dev/null | awk '{print $1}' || true)
112119

113120
# Wait for output (the app writes to logcat with tag GoJNI)
114-
# Poll for up to 30 seconds
121+
# Poll for up to 30 seconds, filtering by PID to avoid stale messages
115122
got_output=false
116123
for attempt in $(seq 1 30); do
117124
sleep 1
118-
output=$("$ADB" logcat -d -s GoJNI 2>/dev/null | grep -v "^-" || true)
125+
if [ -n "$app_pid" ]; then
126+
output=$("$ADB" logcat -d -s GoJNI --pid="$app_pid" 2>/dev/null | grep -v "^-" || true)
127+
# Fallback: some adb versions don't support --pid; use grep
128+
if [ -z "$output" ]; then
129+
output=$("$ADB" logcat -d -s GoJNI 2>/dev/null | grep -v "^-" | grep " $app_pid " || true)
130+
fi
131+
else
132+
output=$("$ADB" logcat -d -s GoJNI 2>/dev/null | grep -v "^-" || true)
133+
fi
119134
if [ -n "$output" ]; then
120135
got_output=true
121136
break
122137
fi
123138
# Check if app is still alive
124139
if [ -n "$app_pid" ]; then
125140
if ! "$ADB" shell kill -0 "$app_pid" 2>/dev/null; then
126-
# App exited, check for crash
127-
break
141+
# App exited; re-read PID in case it restarted, then check for crash
142+
app_pid=$("$ADB" shell pidof "$pkg" 2>/dev/null || true)
143+
if [ -z "$app_pid" ]; then
144+
break
145+
fi
128146
fi
129147
fi
130148
done
@@ -163,11 +181,9 @@ for i in "${!EXAMPLES[@]}"; do
163181

164182
# Force stop the app
165183
"$ADB" shell am force-stop "$pkg" 2>/dev/null || true
166-
sleep 1
167184

168185
# Uninstall to keep device clean
169186
"$ADB" uninstall "$pkg" > /dev/null 2>&1 || true
170-
sleep 1
171187
done
172188

173189
echo ""

examples/vibrator_haptics/main.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,23 @@ func run(vm *jni.VM, output *bytes.Buffer) error {
146146
fmt.Fprintln(output, "")
147147
fmt.Fprintln(output, "--- Pattern 3: Three quick pulses ---")
148148
pattern := []int64{0, 100, 100, 100, 100, 100} // ms: delay, on, off, on, off, on
149-
var patternObj *jni.Object
149+
// Create long array and call Vibrate inside the same vm.Do scope
150+
// to avoid invalid local reference after scope exit.
150151
vm.Do(func(env *jni.Env) error {
151152
longArr := env.NewLongArray(int32(len(pattern)))
152153
if longArr == nil {
153154
return fmt.Errorf("NewLongArray returned nil")
154155
}
155156
env.SetLongArrayRegion(longArr, 0, int32(len(pattern)), unsafe.Pointer(&pattern[0]))
156-
patternObj = (*jni.Object)(unsafe.Pointer(longArr))
157-
return nil
158-
})
159-
160-
if patternObj != nil {
157+
patternObj := (*jni.Object)(unsafe.Pointer(longArr))
161158
if err := vib.Vibrate2_5(patternObj, -1); err != nil {
162159
fmt.Fprintf(output, "Error: %v\n", err)
163160
} else {
164161
fmt.Fprintln(output, "Vibrating pattern: [0, 100, 100, 100, 100, 100] ms")
165162
fmt.Fprintln(output, " (delay=0, on=100, off=100, on=100, off=100, on=100)")
166163
}
167-
}
164+
return nil
165+
})
168166
time.Sleep(600 * time.Millisecond)
169167

170168
// Pattern 4: SOS pattern (... --- ...)
@@ -180,24 +178,20 @@ func run(vm *jni.VM, output *bytes.Buffer) error {
180178
300, // letter gap
181179
100, 100, 100, 100, 100, // S: dot dot dot
182180
}
183-
var sosObj *jni.Object
184181
vm.Do(func(env *jni.Env) error {
185182
longArr := env.NewLongArray(int32(len(sosPattern)))
186183
if longArr == nil {
187184
return fmt.Errorf("NewLongArray returned nil")
188185
}
189186
env.SetLongArrayRegion(longArr, 0, int32(len(sosPattern)), unsafe.Pointer(&sosPattern[0]))
190-
sosObj = (*jni.Object)(unsafe.Pointer(longArr))
191-
return nil
192-
})
193-
194-
if sosObj != nil {
187+
sosObj := (*jni.Object)(unsafe.Pointer(longArr))
195188
if err := vib.Vibrate2_5(sosObj, -1); err != nil {
196189
fmt.Fprintf(output, "Error: %v\n", err)
197190
} else {
198191
fmt.Fprintln(output, "Vibrating SOS: ... --- ...")
199192
}
200-
}
193+
return nil
194+
})
201195
time.Sleep(3 * time.Second)
202196

203197
// Cancel any ongoing vibration

0 commit comments

Comments
 (0)